Corretto il get_active_theme nel context_processor e nel modello

testato-con-bdlaser
Davide Borgonovo 2022-12-27 10:43:38 +01:00
parent 7e85ebac9b
commit 1b0d20f494
2 changed files with 6 additions and 4 deletions

View File

@ -1,7 +1,8 @@
from .models import Theme from .models import Theme, UserTheme
def get_active_theme(request): def get_active_theme(request):
objs_manager = Theme.objects objs_manager = Theme.objects
user_theme_manager = UserTheme.objects
objs_active_qs = objs_manager.filter(active=True) objs_active_qs = objs_manager.filter(active=True)
objs_active_ls = list(objs_active_qs) objs_active_ls = list(objs_active_qs)
objs_active_count = len(objs_active_ls) objs_active_count = len(objs_active_ls)
@ -26,7 +27,7 @@ def get_active_theme(request):
# request = None # request = None
user = request.user user = request.user
try: try:
obj = objs_active_qs.filter(user=user).first() obj = user_theme_manager.filter(user=user, theme__active=True).first().theme
except: except:
obj = objs_active_ls[-1] obj = objs_active_ls[-1]
obj.set_active() obj.set_active()

View File

@ -57,6 +57,7 @@ class Theme(models.Model):
@staticmethod @staticmethod
def get_active_theme(): def get_active_theme():
objs_manager = Theme.objects objs_manager = Theme.objects
user_theme_manager = UserTheme.objects
objs_active_qs = objs_manager.filter(active=True) objs_active_qs = objs_manager.filter(active=True)
objs_active_ls = list(objs_active_qs) objs_active_ls = list(objs_active_qs)
objs_active_count = len(objs_active_ls) objs_active_count = len(objs_active_ls)
@ -81,8 +82,8 @@ class Theme(models.Model):
request = None request = None
try: try:
return objs_active_qs.filter(user=user).first() return user_theme_manager.filter(user=user, theme__active=True).first().theme
except: except AttributeError as e:
obj = objs_active_ls[-1] obj = objs_active_ls[-1]
obj.set_active() obj.set_active()