Added admin theme caching to remove duplicated queries. Fixed #19

pull/62/head
Fabio Caccamo 2019-04-29 17:35:24 +02:00
parent b4d0b4c985
commit 352701ae7d
3 changed files with 28 additions and 10 deletions

View File

@ -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)

View File

@ -9,20 +9,25 @@ from django.utils.translation import ugettext_lazy as _
from colorfield.fields import ColorField
from admin_interface.cache import del_cached_active_theme
@python_2_unicode_compatible
class Theme(models.Model):
@staticmethod
def post_migrate_handler(**kwargs):
del_cached_active_theme()
Theme.get_active_theme()
@staticmethod
def post_delete_handler(**kwargs):
del_cached_active_theme()
Theme.get_active_theme()
@staticmethod
def post_save_handler(instance, **kwargs):
del_cached_active_theme()
if instance.active:
Theme.objects.exclude(pk=instance.pk).update(active=False)
Theme.get_active_theme()

View File

@ -2,6 +2,7 @@
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.version import __version__
@ -15,18 +16,10 @@ else:
@simple_tag(takes_context=True)
def get_admin_interface_theme(context):
theme = None
request = context.get('request', None)
if request:
theme = getattr(request, 'admin_interface_theme', None)
theme = get_cached_active_theme()
if not theme:
theme = Theme.get_active_theme()
if request:
request.admin_interface_theme = theme
set_cached_active_theme(theme)
return theme