From 4620c78f5aaf68f4987b841e5001be89aa68c054 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 2 May 2020 14:41:54 +0200 Subject: [PATCH] user preference fixes and improvements --- cookbook/admin.py | 2 +- .../migrations/0040_auto_20200502_1433.py | 22 ++++++++ cookbook/models.py | 5 +- cookbook/templatetags/theming_tags.py | 51 +++++++++---------- cookbook/views/views.py | 5 +- requirements.txt | 1 + 6 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 cookbook/migrations/0040_auto_20200502_1433.py diff --git a/cookbook/admin.py b/cookbook/admin.py index d91d6ad5..ed8cad2a 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -3,7 +3,7 @@ from .models import * class UserPreferenceAdmin(admin.ModelAdmin): - list_display = ('name', 'theme', 'nav_color') + list_display = ('name', 'theme', 'nav_color', 'default_page', 'search_style') @staticmethod def name(obj): diff --git a/cookbook/migrations/0040_auto_20200502_1433.py b/cookbook/migrations/0040_auto_20200502_1433.py new file mode 100644 index 00000000..38c74689 --- /dev/null +++ b/cookbook/migrations/0040_auto_20200502_1433.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.5 on 2020-05-02 12:33 + +import annoying.fields +from django.conf import settings +from django.db import migrations +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('cookbook', '0039_recipebook_shared'), + ] + + operations = [ + migrations.AlterField( + model_name='userpreference', + name='user', + field=annoying.fields.AutoOneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 1b48f46e..f0611a1f 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -1,5 +1,6 @@ import re +from annoying.fields import AutoOneToOneField from django.contrib import auth from django.contrib.auth.models import User from django.utils.translation import gettext as _ @@ -54,7 +55,7 @@ class UserPreference(models.Model): SEARCH_STYLE = ((SMALL, _('Small')), (LARGE, _('Large')),) - user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) + user = AutoOneToOneField(User, on_delete=models.CASCADE, primary_key=True) theme = models.CharField(choices=THEMES, max_length=128, default=FLATLY) nav_color = models.CharField(choices=COLORS, max_length=128, default=PRIMARY) default_unit = models.CharField(max_length=32, default='g') @@ -62,7 +63,7 @@ class UserPreference(models.Model): search_style = models.CharField(choices=SEARCH_STYLE, max_length=64, default=LARGE) def __str__(self): - return self.user + return str(self.user) class Storage(models.Model): diff --git a/cookbook/templatetags/theming_tags.py b/cookbook/templatetags/theming_tags.py index ea024b3e..0b6609d5 100644 --- a/cookbook/templatetags/theming_tags.py +++ b/cookbook/templatetags/theming_tags.py @@ -8,41 +8,38 @@ register = template.Library() @register.simple_tag def theme_url(request): - try: - themes = { - UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css', - UserPreference.FLATLY: 'themes/flatly.min.css', - UserPreference.DARKLY: 'themes/darkly.min.css', - UserPreference.SUPERHERO: 'themes/superhero.min.css', - } - if request.user.userpreference.theme in themes: - return static(themes[request.user.userpreference.theme]) - else: - raise AttributeError - except AttributeError: + if not request.user.is_authenticated: return static('themes/flatly.min.css') + themes = { + UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css', + UserPreference.FLATLY: 'themes/flatly.min.css', + UserPreference.DARKLY: 'themes/darkly.min.css', + UserPreference.SUPERHERO: 'themes/superhero.min.css', + } + if request.user.userpreference.theme in themes: + return static(themes[request.user.userpreference.theme]) + else: + raise AttributeError @register.simple_tag def nav_color(request): - try: - return request.user.userpreference.nav_color - except AttributeError: + if not request.user.is_authenticated: return 'primary' + return request.user.userpreference.nav_color @register.simple_tag def tabulator_theme_url(request): - try: - themes = { - UserPreference.BOOTSTRAP: 'tabulator/tabulator_bootstrap4.min.css', - UserPreference.FLATLY: 'tabulator/tabulator_bootstrap4.min.css', - UserPreference.DARKLY: 'tabulator/tabulator_site.min.css', - UserPreference.SUPERHERO: 'tabulator/tabulator_site.min.css', - } - if request.user.userpreference.theme in themes: - return static(themes[request.user.userpreference.theme]) - else: - raise AttributeError - except AttributeError: + if not request.user.is_authenticated: return static('tabulator/tabulator_bootstrap4.min.css') + themes = { + UserPreference.BOOTSTRAP: 'tabulator/tabulator_bootstrap4.min.css', + UserPreference.FLATLY: 'tabulator/tabulator_bootstrap4.min.css', + UserPreference.DARKLY: 'tabulator/tabulator_site.min.css', + UserPreference.SUPERHERO: 'tabulator/tabulator_site.min.css', + } + if request.user.userpreference.theme in themes: + return static(themes[request.user.userpreference.theme]) + else: + raise AttributeError diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 36d01251..6ceb10a2 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -179,10 +179,7 @@ def shopping_list(request): @group_required('guest') def settings(request): - try: - up = request.user.userpreference - except UserPreference.DoesNotExist: - up = None + up = request.user.userpreference user_name_form = UserNameForm(instance=request.user) password_form = PasswordChangeForm(request.user) diff --git a/requirements.txt b/requirements.txt index d87ec433..18b1b999 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ djangorestframework django-autocomplete-light django-emoji-picker django-cleanup +django-annoying bleach bleach-whitelist six