From c63a061cf3ec069003b8e038493098b3ce1db794 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 27 Jul 2021 08:31:43 +1000 Subject: [PATCH] Refactor "theme" selection --- InvenTree/InvenTree/urls.py | 2 +- InvenTree/InvenTree/views.py | 54 ++-------------- .../part/templatetags/inventree_extras.py | 11 +++- .../InvenTree/settings/appearance.html | 64 ------------------- .../templates/InvenTree/settings/user.html | 24 +++++-- 5 files changed, 31 insertions(+), 124 deletions(-) delete mode 100644 InvenTree/templates/InvenTree/settings/appearance.html diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 74b4c41b23..41ef306a50 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -82,9 +82,9 @@ settings_urls = [ url(r'^i18n/?', include('django.conf.urls.i18n')), url(r'^appearance/?', AppearanceSelectView.as_view(), name='settings-appearance'), + url(r'^currencies-refresh/', CurrencyRefreshView.as_view(), name='settings-currencies-refresh'), url(r'^category/', SettingCategorySelectView.as_view(), name='settings-category'), - url(r'^currencies-refresh/', CurrencyRefreshView.as_view(), name='settings-currencies-refresh'), url(r'^(?P\d+)/edit/user', UserSettingEdit.as_view(), name='user-setting-edit'), url(r'^(?P\d+)/edit/', SettingEdit.as_view(), name='setting-edit'), diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index be09969671..fbb83aeb79 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -821,13 +821,9 @@ class CurrencyRefreshView(RedirectView): return redirect(reverse_lazy('settings')) -class AppearanceSelectView(FormView): +class AppearanceSelectView(RedirectView): """ View for selecting a color theme """ - form_class = ColorThemeSelectForm - success_url = reverse_lazy('settings-appearance') - template_name = "InvenTree/settings/appearance.html" - def get_user_theme(self): """ Get current user color theme """ try: @@ -837,40 +833,10 @@ class AppearanceSelectView(FormView): return user_theme - def get_initial(self): - """ Select current user color theme as initial choice """ - - initial = super(AppearanceSelectView, self).get_initial() - - user_theme = self.get_user_theme() - if user_theme: - initial['name'] = user_theme.name - return initial - - def get(self, request, *args, **kwargs): - """ Check if current color theme exists, else display alert box """ - - context = {} - - form = self.get_form() - context['form'] = form - - user_theme = self.get_user_theme() - if user_theme: - # Check color theme is a valid choice - if not ColorTheme.is_valid_choice(user_theme): - user_color_theme_name = user_theme.name - if not user_color_theme_name: - user_color_theme_name = 'default' - - context['invalid_color_theme'] = user_color_theme_name - - return self.render_to_response(context) - def post(self, request, *args, **kwargs): """ Save user color theme selection """ - form = self.get_form() + theme = request.POST.get('theme', None) # Get current user theme user_theme = self.get_user_theme() @@ -880,20 +846,10 @@ class AppearanceSelectView(FormView): user_theme = ColorTheme() user_theme.user = request.user - if form.is_valid(): - theme_selected = form.cleaned_data['name'] + user_theme.name = theme + user_theme.save() - # Set color theme to form selection - user_theme.name = theme_selected - user_theme.save() - - return self.form_valid(form) - else: - # Set color theme to default - user_theme.name = ColorTheme.default_color_theme[0] - user_theme.save() - - return self.form_invalid(form) + return redirect(reverse_lazy('settings')) class SettingCategorySelectView(FormView): diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py index 1d4c690cf9..f930fa4467 100644 --- a/InvenTree/part/templatetags/inventree_extras.py +++ b/InvenTree/part/templatetags/inventree_extras.py @@ -232,10 +232,15 @@ def get_available_themes(*args, **kwargs): Return the available theme choices """ - print("available:") - print(ColorTheme.get_color_themes_choices()) + themes = [] - return ColorTheme.get_color_themes_choices() + for key, name in ColorTheme.get_color_themes_choices(): + themes.append({ + 'key': key, + 'name': name + }) + + return themes @register.filter diff --git a/InvenTree/templates/InvenTree/settings/appearance.html b/InvenTree/templates/InvenTree/settings/appearance.html deleted file mode 100644 index 7c4d3a067e..0000000000 --- a/InvenTree/templates/InvenTree/settings/appearance.html +++ /dev/null @@ -1,64 +0,0 @@ -{% extends "panel.html" %} - -{% load i18n %} - -{% block label %}appearance{% endblock %} - -{% block heading %} -{% trans "Appearance" %} -{% endblock %} - -{% block content %} - -
-
-

{% trans "Color Themes" %}

-
-
- -
- {% csrf_token %} - {% load crispy_forms_tags %} - {% crispy form %} -
- -{% if invalid_color_theme %} - -{% endif %} - -
-
-

{% trans "Language" %}

-
-
- -
-

{% trans "Language Settings" %}

-
- -
-
{% csrf_token %} - -
- -
-
- -
-
-
-{% endblock %} diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index e4a08635fa..140d2f6b86 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -44,14 +44,24 @@
-
+ {% csrf_token %} - +
+
+
+ +
+
+
+
+ +
@@ -64,7 +74,7 @@
{% csrf_token %} -
+