|
1 | 1 | from django.conf import settings |
2 | 2 | from django.core.cache import caches |
3 | 3 | from django.http import HttpRequest |
| 4 | +from django.utils.functional import SimpleLazyObject |
4 | 5 | from modeldict import ModelDict |
5 | 6 |
|
6 | | -from gargoyle.models import DISABLED, EXCLUDE, GLOBAL, INCLUDE, INHERIT, SELECTIVE, Switch |
7 | 7 | from gargoyle.proxy import SwitchProxy |
8 | 8 |
|
| 9 | +from .constants import DISABLED, EXCLUDE, GLOBAL, INCLUDE, INHERIT, SELECTIVE |
| 10 | + |
9 | 11 |
|
10 | 12 | class SwitchManager(ModelDict): |
11 | 13 | DISABLED = DISABLED |
@@ -144,10 +146,19 @@ def as_request(self, user=None, ip_address=None): |
144 | 146 | return MockRequest(user, ip_address) |
145 | 147 |
|
146 | 148 |
|
147 | | -if hasattr(settings, 'GARGOYLE_CACHE_NAME'): |
148 | | - gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, |
149 | | - auto_create=getattr(settings, 'GARGOYLE_AUTO_CREATE', True), |
150 | | - cache=caches[settings.GARGOYLE_CACHE_NAME]) |
151 | | -else: |
152 | | - gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, |
153 | | - auto_create=getattr(settings, 'GARGOYLE_AUTO_CREATE', True)) |
| 149 | +def make_gargoyle(): |
| 150 | + from gargoyle.models import Switch |
| 151 | + |
| 152 | + kwargs = { |
| 153 | + 'key': 'key', |
| 154 | + 'value': 'value', |
| 155 | + 'instances': True, |
| 156 | + 'auto_create': getattr(settings, 'GARGOYLE_AUTO_CREATE', True), |
| 157 | + } |
| 158 | + |
| 159 | + if hasattr(settings, 'GARGOYLE_CACHE_NAME'): |
| 160 | + kwargs['cache'] = caches[settings.GARGOYLE_CACHE_NAME] |
| 161 | + |
| 162 | + return SwitchManager(Switch, **kwargs) |
| 163 | + |
| 164 | +gargoyle = SimpleLazyObject(make_gargoyle) |
0 commit comments