Skip to content
Snippets Groups Projects
Commit 437e01c3 authored by Bryan BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

BUGFIX when asking for Anonymous user preferences

parent 12010c45
No related branches found
No related tags found
No related merge requests found
Pipeline #11964 passed
...@@ -2,12 +2,14 @@ import os ...@@ -2,12 +2,14 @@ import os
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.test import TestCase, RequestFactory, override_settings from django.test import TestCase, RequestFactory, override_settings
from django.urls import reverse from django.urls import reverse
from basetheme_bootstrap import user_preferences from basetheme_bootstrap import user_preferences
from basetheme_bootstrap.user_preferences import get_user_preferences_for_user
class AboutPageTests(TestCase): class AboutPageTests(TestCase):
...@@ -337,6 +339,9 @@ class UserPreferencesTests(TestCase): ...@@ -337,6 +339,9 @@ class UserPreferencesTests(TestCase):
pref.user = None pref.user = None
self.assertRaises(ValidationError, pref.save) self.assertRaises(ValidationError, pref.save)
def test_AnonymousUser_do_not_crash(self):
get_user_preferences_for_user(AnonymousUser())
def test_caching_disabled_status_when_it_is_the_case(self): def test_caching_disabled_status_when_it_is_the_case(self):
user_preferences.get_user_preference_class() user_preferences.get_user_preference_class()
self.assertTrue( self.assertTrue(
......
...@@ -55,6 +55,8 @@ class UserPreferencesAbstractModel(models.Model): ...@@ -55,6 +55,8 @@ class UserPreferencesAbstractModel(models.Model):
def get_for_user(cls, user): def get_for_user(cls, user):
try: try:
pref = cls.objects.get(user=user) pref = cls.objects.get(user=user)
except TypeError:
return cls.get_for_user(None)
except cls.DoesNotExist: except cls.DoesNotExist:
pref, _ = cls.objects.get_or_create(user=None) pref, _ = cls.objects.get_or_create(user=None)
if user is not None: if user is not None:
......
...@@ -7,7 +7,7 @@ readme = open('README.rst').read() ...@@ -7,7 +7,7 @@ readme = open('README.rst').read()
setup( setup(
name='django-basetheme-bootstrap', name='django-basetheme-bootstrap',
version='0.1.1', version='0.1.2',
description='Django Basetheme Bootstrap', description='Django Basetheme Bootstrap',
long_description=readme, long_description=readme,
author='Bryan Brancotte', author='Bryan Brancotte',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment