-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nitely
committed
Jan 9, 2015
1 parent
c294af1
commit 29f3284
Showing
6 changed files
with
102 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#-*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from django.core.cache import get_cache | ||
from django.conf import settings as django_settings | ||
|
||
from djconfig.forms import ConfigForm | ||
from djconfig.models import Config as ConfigModel | ||
from djconfig.settings import BACKEND | ||
from djconfig.utils import prefixer | ||
|
||
_registered_forms = set() | ||
|
||
|
||
def register(form_class): | ||
""" | ||
Register config forms | ||
:param form_class: The form to be registered. | ||
:type form_class: ConfigForm. | ||
""" | ||
global _registered_forms | ||
|
||
assert issubclass(form_class, ConfigForm), \ | ||
"The form does not inherit from ConfigForm" | ||
|
||
_registered_forms.add(form_class) | ||
_check_backend() | ||
|
||
|
||
def load(): | ||
""" | ||
Loads every registered form into the cache. | ||
If a field name is found in the db, it will load it from there. | ||
Otherwise, the initial value from the field form is used. | ||
""" | ||
global _registered_forms | ||
|
||
cache_values = {} | ||
data = dict(ConfigModel.objects.all().values_list('key', 'value')) | ||
|
||
for form_class in _registered_forms: | ||
form = form_class(data=data) | ||
form.is_valid() | ||
|
||
initial = {prefixer(field_name): field.initial | ||
for field_name, field in form.fields.items()} | ||
cache_values.update(initial) | ||
|
||
cleaned_data = {prefixer(field_name): value | ||
for field_name, value in form.cleaned_data.items() | ||
if field_name in data} | ||
cache_values.update(cleaned_data) | ||
|
||
cache_values[prefixer('_updated_at')] = data.get('_updated_at') | ||
cache = get_cache(BACKEND) | ||
cache.set_many(cache_values) | ||
|
||
|
||
def _check_backend(): | ||
if django_settings.CACHES[BACKEND]['BACKEND'].endswith(".LocMemCache") and \ | ||
"djconfig.middleware.DjConfigLocMemMiddleware" not in django_settings.MIDDLEWARE_CLASSES: | ||
raise ValueError("LocMemCache requires DjConfigLocMemMiddleware " | ||
"but it was not found in MIDDLEWARE_CLASSES") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,4 +90,4 @@ | |
'default': { | ||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', | ||
} | ||
} | ||
} |