diff --git a/admin_interface/models.py b/admin_interface/models.py index f5fcbb7..40fbe11 100644 --- a/admin_interface/models.py +++ b/admin_interface/models.py @@ -21,6 +21,9 @@ class Theme(models.Model): @staticmethod def post_save_handler(instance, created, **kwargs): + theme = instance + if theme.active: + Theme.objects.exclude( pk = theme.pk ).update( active = False ) Theme.get_active_theme() @staticmethod @@ -99,11 +102,8 @@ class Theme(models.Model): def set_active(self): - Theme.objects.exclude( pk = self.pk ).update( active = False ) - - if not self.active: - self.active = True - self.save() + self.active = True + self.save() def set_default_logo(self): diff --git a/tests/tests.py b/tests/tests.py index 572b437..4cb0ebe 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -5,6 +5,7 @@ from django.test import TestCase from django.template import Context, Template import os +import random import shutil from admin_interface.models import Theme @@ -93,6 +94,22 @@ class AdminInterfaceTestCase(TestCase): self.assertEqual( Theme.get_active_theme().pk, theme_3.pk ) self.__test_active_theme() + def test_last_theme_activated_on_multiple_themes_activated(self): + + Theme.objects.all().delete() + theme_1 = Theme.objects.create( name = 'Custom 1', active = True ) + theme_2 = Theme.objects.create( name = 'Custom 2', active = True ) + theme_3 = Theme.objects.create( name = 'Custom 3', active = True ) + theme_4 = Theme.objects.create( name = 'Custom 4', active = True ) + theme_5 = Theme.objects.create( name = 'Custom 5', active = True ) + themes = [ theme_1, theme_2, theme_3, theme_4, theme_5 ] + for i in range(5): + random.shuffle(themes) + for theme in themes: + theme.set_active() + self.assertEqual( Theme.get_active_theme().pk, theme.pk ) + self.__test_active_theme() + def test_templatetags(self): Theme.objects.all().delete()