diff --git a/README.rst b/README.rst index fd0792f..c3e2cef 100644 --- a/README.rst +++ b/README.rst @@ -147,13 +147,31 @@ in your project settings: DJANGO_SETTINGS_TIMEOUT = 60 * 60 * 10 # 10 hours - Timeout let's you define cache timeout (in sec.) for each of the settings. After the given time values gets expired and each of them will be recalculated (at the moment you ask for the given setting). Introduced due to django's defaults cache timeout (5 min): https://docs.djangoproject.com/en/dev/topics/cache/#cache-arguments +If you want to override the cache key maker tell it in the settings: + +.. code-block:: python + + DJANGO_SETTINGS_CACHE_KEYMAKER = 'myapp.django_settings_keymaker.TenantKeyMaker' + +And inherit from the default keymaker. (Example is for django-tenant-schemas based application) + +.. code-block:: python + + from django.db import connection + from django_settings.keymaker import KeyMaker + + class TenantKeyMaker(KeyMaker): + def make(self, method_name, args, kwargs): + key = super().make(method_name, args, kwargs) + key = connection.get_schema()+":"+key + return key + Settings types -------------- @@ -166,7 +184,6 @@ Admin You can manipulate setting via your admin interface. - Changelog --------- diff --git a/django_settings/cache.py b/django_settings/cache.py index 4a3ebb2..5cf7023 100644 --- a/django_settings/cache.py +++ b/django_settings/cache.py @@ -9,49 +9,25 @@ XXX: the whole mechanism should be fixed as now it's too complicated to explain """ from .lazyimport import lazyimport +import importlib django = lazyimport({ 'settings': 'django.conf', }) config = lazyimport({ 'DJANGO_SETTINGS_TIMEOUT': 'django_settings.conf', + 'DJANGO_SETTINGS_CACHE_KEYMAKER': 'django_settings.conf' }) - -class KeyMaker(object): - def __init__(self, prefix): - self.prefix = prefix - - def convert(self, arg): - if isinstance(arg, unicode): - return arg.encode(django.settings.DEFAULT_CHARSET) - else: - return str(arg) - - def args_to_key(self, args): - return ":".join(map(self.convert, args)) - - def kwargs_to_key(self, kwargs): - return ":".join([ - "%s:%s" % (self.convert(k), self.convert(v)) - for k, v in kwargs.items() - ]) - - def make(self, method_name, args, kwargs): - key = ":".join(( - self.prefix, - method_name, - self.args_to_key(args), - self.kwargs_to_key(kwargs), - )) - return key - - class MethodProxy(object): def __init__(self, instance, method): self.instance = instance self.method = method # accually it's NOT bounded s it's a function! - self._keymaker = KeyMaker(prefix='django_settings') + keymakerklass = config.DJANGO_SETTINGS_CACHE_KEYMAKER + if isinstance(keymakerklass, str): + module_name, class_name = keymakerklass.rsplit(".", 1) + keymakerklass = getattr(importlib.import_module(module_name), class_name) + self._keymaker = keymakerklass(prefix='django_settings') # NOTE: it's proxy, so let's add at least some basic func properties self.func_name = self.method.__name__ diff --git a/django_settings/conf.py b/django_settings/conf.py index dc7f1b0..fa5b481 100644 --- a/django_settings/conf.py +++ b/django_settings/conf.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from django.conf import settings +from .keymaker import KeyMaker DJANGO_SETTINGS_UNIQUE_NAMES = getattr(settings, 'DJANGO_SETTINGS_UNIQUE_NAMES', True) DJANGO_SETTINGS_TIMEOUT = getattr(settings, 'DJANGO_SETTINGS_TIMEOUT', 60 * 60 * 24 * 1) # one day +DJANGO_SETTINGS_CACHE_KEYMAKER = getattr(settings, 'DJANGO_SETTINGS_CACHE_KEYMAKER', KeyMaker) DJANGO_SETTINGS = getattr(settings, 'DJANGO_SETTINGS', None) or {} diff --git a/django_settings/keymaker.py b/django_settings/keymaker.py new file mode 100644 index 0000000..cd94798 --- /dev/null +++ b/django_settings/keymaker.py @@ -0,0 +1,30 @@ +import sys + +class KeyMaker(object): + def __init__(self, prefix): + self.prefix = prefix + + def convert(self, arg): + if sys.version_info < (3,) and isinstance(arg, unicode): + return arg.encode(django.settings.DEFAULT_CHARSET) + return str(arg) + + def args_to_key(self, args): + return ":".join(map(self.convert, args)) + + def kwargs_to_key(self, kwargs): + return ":".join([ + "%s:%s" % (self.convert(k), self.convert(v)) + for k, v in kwargs.items() + ]) + + def make(self, method_name, args, kwargs): + key = ":".join(( + self.prefix, + method_name, + self.args_to_key(args), + self.kwargs_to_key(kwargs), + )) + return key + +