south.hacks.django_1_0: 48 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/south/hacks/django_1_0.py

Stats: 0 executed, 42 missed, 6 excluded, 59 ignored

  1. """
  2. Hacks for the Django 1.0/1.0.2 releases.
  3. """
  4. from django.conf import settings
  5. from django.db.backends.creation import BaseDatabaseCreation
  6. from django.db.models.loading import cache
  7. from django.core import management
  8. from django.core.management.commands.flush import Command as FlushCommand
  9. from django.utils.datastructures import SortedDict
  10. class SkipFlushCommand(FlushCommand):
  11. def handle_noargs(self, **options):
  12. # no-op to avoid calling flush
  13. return
  14. class Hacks:
  15. def set_installed_apps(self, apps):
  16. """
  17. Sets Django's INSTALLED_APPS setting to be effectively the list passed in.
  18. """
  19. # Make sure it's a list.
  20. apps = list(apps)
  21. # Make sure it contains strings
  22. if apps:
  23. assert isinstance(apps[0], basestring), "The argument to set_installed_apps must be a list of strings."
  24. # Monkeypatch in!
  25. settings.INSTALLED_APPS, settings.OLD_INSTALLED_APPS = (
  26. apps,
  27. settings.INSTALLED_APPS,
  28. )
  29. self._redo_app_cache()
  30. def reset_installed_apps(self):
  31. """
  32. Undoes the effect of set_installed_apps.
  33. """
  34. settings.INSTALLED_APPS = settings.OLD_INSTALLED_APPS
  35. self._redo_app_cache()
  36. def _redo_app_cache(self):
  37. """
  38. Used to repopulate AppCache after fiddling with INSTALLED_APPS.
  39. """
  40. cache.loaded = False
  41. cache.handled = {}
  42. cache.postponed = []
  43. cache.app_store = SortedDict()
  44. cache.app_models = SortedDict()
  45. cache.app_errors = {}
  46. cache._populate()
  47. def clear_app_cache(self):
  48. """
  49. Clears the contents of AppCache to a blank state, so new models
  50. from the ORM can be added.
  51. """
  52. self.old_app_models, cache.app_models = cache.app_models, {}
  53. def unclear_app_cache(self):
  54. """
  55. Reversed the effects of clear_app_cache.
  56. """
  57. cache.app_models = self.old_app_models
  58. cache._get_models_cache = {}
  59. def repopulate_app_cache(self):
  60. """
  61. Rebuilds AppCache with the real model definitions.
  62. """
  63. cache._populate()
  64. def store_app_cache_state(self):
  65. self.stored_app_cache_state = dict(**cache.__dict__)
  66. def restore_app_cache_state(self):
  67. cache.__dict__ = self.stored_app_cache_state
  68. def patch_flush_during_test_db_creation(self):
  69. """
  70. Patches BaseDatabaseCreation.create_test_db to not flush database
  71. """
  72. def patch(f):
  73. def wrapper(*args, **kwargs):
  74. # hold onto the original and replace flush command with a no-op
  75. original_flush_command = management._commands['flush']
  76. try:
  77. management._commands['flush'] = SkipFlushCommand()
  78. # run create_test_db
  79. f(*args, **kwargs)
  80. finally:
  81. # unpatch flush back to the original
  82. management._commands['flush'] = original_flush_command
  83. return wrapper
  84. BaseDatabaseCreation.create_test_db = patch(BaseDatabaseCreation.create_test_db)