migrationutils.py 3.5 KB

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