mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-05-01 04:04:58 +00:00
Split utils module.
This commit is contained in:
parent
2c814889ed
commit
ca032e70cf
3 changed files with 39 additions and 45 deletions
0
dbtemplates/utils/__init__.py
Normal file
0
dbtemplates/utils/__init__.py
Normal file
39
dbtemplates/utils/cache.py
Normal file
39
dbtemplates/utils/cache.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
from django.core.cache import get_cache
|
||||
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
from dbtemplates.conf import settings
|
||||
|
||||
|
||||
def get_cache_backend():
|
||||
return get_cache(settings.CACHE_BACKEND)
|
||||
|
||||
cache = get_cache_backend()
|
||||
|
||||
|
||||
def get_cache_key(name):
|
||||
current_site = Site.objects.get_current()
|
||||
return 'dbtemplates::%s::%s' % (name, current_site.pk)
|
||||
|
||||
|
||||
def set_and_return(content):
|
||||
# Save in cache backend explicitly if manually deleted or invalidated
|
||||
if cache:
|
||||
cache.set(cache_key, content)
|
||||
return (content, display_name)
|
||||
|
||||
def add_template_to_cache(instance, **kwargs):
|
||||
"""
|
||||
Called via Django's signals to cache the templates, if the template
|
||||
in the database was added or changed.
|
||||
"""
|
||||
remove_cached_template(instance)
|
||||
cache.set(get_cache_key(instance.name), instance.content)
|
||||
|
||||
|
||||
def remove_cached_template(instance, **kwargs):
|
||||
"""
|
||||
Called via Django's signals to remove cached templates, if the template
|
||||
in the database was changed or deleted.
|
||||
"""
|
||||
cache.delete(get_cache_key(instance.name))
|
||||
|
|
@ -1,52 +1,7 @@
|
|||
from django import VERSION
|
||||
from django.core.cache import get_cache
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
from dbtemplates import settings
|
||||
|
||||
|
||||
def get_cache_backend():
|
||||
return get_cache(settings.CACHE_BACKEND)
|
||||
|
||||
cache = get_cache_backend()
|
||||
|
||||
|
||||
def add_default_site(instance, **kwargs):
|
||||
"""
|
||||
Called via Django's signals to cache the templates, if the template
|
||||
in the database was added or changed, only if DBTEMPLATES_ADD_DEFAULT_SITE
|
||||
setting is set.
|
||||
"""
|
||||
if settings.ADD_DEFAULT_SITE:
|
||||
current_site = Site.objects.get_current()
|
||||
if current_site not in instance.sites.all():
|
||||
instance.sites.add(current_site)
|
||||
|
||||
|
||||
def get_cache_key(name):
|
||||
current_site = Site.objects.get_current()
|
||||
return 'dbtemplates::%s::%s' % (name, current_site.pk)
|
||||
|
||||
|
||||
def add_template_to_cache(instance, **kwargs):
|
||||
"""
|
||||
Called via Django's signals to cache the templates, if the template
|
||||
in the database was added or changed.
|
||||
"""
|
||||
remove_cached_template(instance)
|
||||
cache.set(get_cache_key(instance.name), instance.content)
|
||||
|
||||
|
||||
def remove_cached_template(instance, **kwargs):
|
||||
"""
|
||||
Called via Django's signals to remove cached templates, if the template
|
||||
in the database was changed or deleted.
|
||||
"""
|
||||
cache.delete(get_cache_key(instance.name))
|
||||
|
||||
|
||||
def get_loaders():
|
||||
from django.template.loader import template_source_loaders
|
||||
Loading…
Reference in a new issue