migrationutils.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from importlib import import_module
  2. from django.utils import translation
  3. from misago.core.cache import cache as default_cache
  4. from misago.core.cachebuster import CACHE_KEY
  5. def ugettext_lazy(string):
  6. """
  7. Custom wrapper that preserves untranslated message on lazy translation
  8. string object, useful for db entries that should be found by makemessages
  9. and stored untranslated
  10. """
  11. t = translation.ugettext_lazy(string)
  12. t.message = string
  13. return t
  14. def original_message(string):
  15. try:
  16. return unicode(string.message)
  17. except AttributeError:
  18. return unicode(string)
  19. def with_core_models(migration, this_migration=None):
  20. module_name = 'misago.core.migrations.%s' % migration
  21. migration_module = import_module(module_name)
  22. core_models = migration_module.Migration.models
  23. if this_migration:
  24. core_models.update(this_migration)
  25. return core_models
  26. def cachebuster_register_cache(orm, cache):
  27. orm['core.CacheVersion'].objects.create(cache=cache)
  28. def cachebuster_unregister_cache(orm, cache):
  29. try:
  30. cache = orm['core.CacheVersion'].objects.get(cache=cache)
  31. cache.delete()
  32. except orm['core.CacheVersion'].DoesNotExist:
  33. raise ValueError('Cache "%s" is not registered' % cache)
  34. def delete_cachebuster_cache():
  35. default_cache.delete(CACHE_KEY)