migrationutils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, 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 value, name in untranslated_choices:
  44. translated_choices.append((value, original_message(name)))
  45. setting_fixture['field_extra']['choices'] = tuple(
  46. translated_choices)
  47. if not value:
  48. value = setting_fixture.pop('value', None)
  49. setting_fixture.pop('value', None)
  50. field_extra = setting_fixture.pop('field_extra', None)
  51. setting = orm['conf.Setting'](**setting_fixture)
  52. setting.dry_value = dehydrate_value(setting.python_type, value)
  53. if setting_fixture.get("default_value"):
  54. setting.default_value = dehydrate_value(
  55. setting.python_type, setting_fixture.get("default_value"))
  56. if field_extra:
  57. pickled_extra = pickle.dumps(field_extra, pickle.HIGHEST_PROTOCOL)
  58. setting.pickled_field_extra = base64.encodestring(pickled_extra)
  59. setting.save()
  60. def migrate_settings_group(orm, group_fixture, old_group_key=None):
  61. group_key = group_fixture['key']
  62. # Fetch settings group
  63. if old_group_key:
  64. group = get_group(orm, old_group_key)
  65. custom_settings_values = get_custom_settings_values(orm, group)
  66. else:
  67. group = get_group(orm, group_key)
  68. if group.pk:
  69. custom_settings_values = get_custom_settings_values(orm, group)
  70. else:
  71. custom_settings_values = {}
  72. # Update group's attributes
  73. group.key = group_fixture['key']
  74. group.name = original_message(group_fixture['name'])
  75. if group_fixture.get('description'):
  76. group.description = original_message(group_fixture.get('description'))
  77. group.save()
  78. # Delete groups settings and make new ones
  79. # Its easier to create news ones and then assign them old values
  80. group.setting_set.all().delete()
  81. for order, setting_fixture in enumerate(group_fixture['settings']):
  82. migrate_setting(orm, group, setting_fixture, order,
  83. custom_settings_values.pop(setting_fixture['name']))
  84. def delete_settings_cache():
  85. default_cache.delete(CACHE_KEY)