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