migrationutils.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from misago.core.cache import cache as default_cache
  2. from .dbsettings import CACHE_KEY
  3. from .hydrators import dehydrate_value
  4. from .utils import get_setting_value, has_custom_value
  5. def migrate_settings_group(apps, group_fixture, old_group_key=None):
  6. SettingsGroup = apps.get_model('misago_conf', 'SettingsGroup')
  7. Setting = apps.get_model('misago_conf', 'Setting')
  8. group_key = group_fixture['key']
  9. # Fetch settings group
  10. if old_group_key:
  11. group = get_group(SettingsGroup, old_group_key)
  12. custom_settings_values = get_custom_settings_values(group)
  13. else:
  14. group = get_group(SettingsGroup, group_key)
  15. if group.pk:
  16. custom_settings_values = get_custom_settings_values(group)
  17. else:
  18. custom_settings_values = {}
  19. # Update group's attributes
  20. group.key = group_fixture['key']
  21. group.name = group_fixture['name']
  22. if group_fixture.get('description'):
  23. group.description = group_fixture.get('description')
  24. group.save()
  25. # Delete groups settings and make new ones
  26. # Its easier to create news ones and then assign them old values
  27. group.setting_set.all().delete()
  28. for order, setting_fixture in enumerate(group_fixture['settings']):
  29. old_value = custom_settings_values.pop(setting_fixture['setting'], None)
  30. migrate_setting(Setting, group, setting_fixture, order, old_value)
  31. def get_group(SettingsGroup, group_key):
  32. try:
  33. return SettingsGroup.objects.get(key=group_key)
  34. except SettingsGroup.DoesNotExist:
  35. return SettingsGroup()
  36. def get_custom_settings_values(group):
  37. custom_settings_values = {}
  38. for setting in group.setting_set.iterator():
  39. if has_custom_value(setting):
  40. custom_settings_values[setting.setting] = get_setting_value(setting)
  41. return custom_settings_values
  42. def migrate_setting(Setting, group, setting_fixture, order, old_value):
  43. setting_fixture['group'] = group
  44. setting_fixture['order'] = order
  45. setting_fixture['name'] = setting_fixture['name']
  46. if setting_fixture.get('description'):
  47. setting_fixture['description'] = setting_fixture.get('description')
  48. if (setting_fixture.get('field_extra') and
  49. setting_fixture.get('field_extra').get('choices')):
  50. untranslated_choices = setting_fixture['field_extra']['choices']
  51. translated_choices = []
  52. for val, name in untranslated_choices:
  53. translated_choices.append((val, name))
  54. setting_fixture['field_extra']['choices'] = tuple(
  55. translated_choices)
  56. if old_value is None:
  57. value = setting_fixture.pop('value', None)
  58. else:
  59. value = old_value
  60. setting_fixture.pop('value', None)
  61. field_extra = setting_fixture.pop('field_extra', None)
  62. setting = Setting(**setting_fixture)
  63. setting.dry_value = dehydrate_value(setting.python_type, value)
  64. if setting_fixture.get("default_value"):
  65. setting.default_value = dehydrate_value(
  66. setting.python_type, setting_fixture.get("default_value"))
  67. setting.field_extra = field_extra or {}
  68. setting.save()
  69. def delete_settings_cache():
  70. default_cache.delete(CACHE_KEY)