Skip to content

Commit

Permalink
user preference fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed May 2, 2020
1 parent 349b962 commit 4620c78
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cookbook/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class UserPreferenceAdmin(admin.ModelAdmin):
list_display = ('name', 'theme', 'nav_color')
list_display = ('name', 'theme', 'nav_color', 'default_page', 'search_style')

@staticmethod
def name(obj):
Expand Down
22 changes: 22 additions & 0 deletions cookbook/migrations/0040_auto_20200502_1433.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
5 changes: 3 additions & 2 deletions cookbook/models.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -54,15 +55,15 @@ 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')
default_page = models.CharField(choices=PAGES, max_length=64, default=SEARCH)
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):
Expand Down
51 changes: 24 additions & 27 deletions cookbook/templatetags/theming_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,38 @@

@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
5 changes: 1 addition & 4 deletions cookbook/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ djangorestframework
django-autocomplete-light
django-emoji-picker
django-cleanup
django-annoying
bleach
bleach-whitelist
six
Expand Down

0 comments on commit 4620c78

Please sign in to comment.