migrationutils.py 3.8 KB

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