Added admin theme caching to remove duplicated queries. Fixed #19
parent
b4d0b4c985
commit
352701ae7d
|
|
@ -0,0 +1,20 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.cache import cache, caches
|
||||||
|
|
||||||
|
|
||||||
|
def app_cache():
|
||||||
|
return caches['admin_interface'] if 'admin_interface' in settings.CACHES else cache
|
||||||
|
|
||||||
|
|
||||||
|
def del_cached_active_theme():
|
||||||
|
app_cache().delete('admin_interface_theme')
|
||||||
|
|
||||||
|
|
||||||
|
def get_cached_active_theme():
|
||||||
|
return app_cache().get('admin_interface_theme', None)
|
||||||
|
|
||||||
|
|
||||||
|
def set_cached_active_theme(theme):
|
||||||
|
app_cache().set('admin_interface_theme', theme)
|
||||||
|
|
@ -9,20 +9,25 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from colorfield.fields import ColorField
|
from colorfield.fields import ColorField
|
||||||
|
|
||||||
|
from admin_interface.cache import del_cached_active_theme
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Theme(models.Model):
|
class Theme(models.Model):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_migrate_handler(**kwargs):
|
def post_migrate_handler(**kwargs):
|
||||||
|
del_cached_active_theme()
|
||||||
Theme.get_active_theme()
|
Theme.get_active_theme()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_delete_handler(**kwargs):
|
def post_delete_handler(**kwargs):
|
||||||
|
del_cached_active_theme()
|
||||||
Theme.get_active_theme()
|
Theme.get_active_theme()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_save_handler(instance, **kwargs):
|
def post_save_handler(instance, **kwargs):
|
||||||
|
del_cached_active_theme()
|
||||||
if instance.active:
|
if instance.active:
|
||||||
Theme.objects.exclude(pk=instance.pk).update(active=False)
|
Theme.objects.exclude(pk=instance.pk).update(active=False)
|
||||||
Theme.get_active_theme()
|
Theme.get_active_theme()
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from django import template, VERSION
|
from django import template, VERSION
|
||||||
|
|
||||||
|
from admin_interface.cache import get_cached_active_theme, set_cached_active_theme
|
||||||
from admin_interface.models import Theme
|
from admin_interface.models import Theme
|
||||||
from admin_interface.version import __version__
|
from admin_interface.version import __version__
|
||||||
|
|
||||||
|
|
@ -15,18 +16,10 @@ else:
|
||||||
|
|
||||||
@simple_tag(takes_context=True)
|
@simple_tag(takes_context=True)
|
||||||
def get_admin_interface_theme(context):
|
def get_admin_interface_theme(context):
|
||||||
theme = None
|
theme = get_cached_active_theme()
|
||||||
request = context.get('request', None)
|
|
||||||
|
|
||||||
if request:
|
|
||||||
theme = getattr(request, 'admin_interface_theme', None)
|
|
||||||
|
|
||||||
if not theme:
|
if not theme:
|
||||||
theme = Theme.get_active_theme()
|
theme = Theme.get_active_theme()
|
||||||
|
set_cached_active_theme(theme)
|
||||||
if request:
|
|
||||||
request.admin_interface_theme = theme
|
|
||||||
|
|
||||||
return theme
|
return theme
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue