From 2f408ce94685292cf59b54a9c0e20bf2a2b5b5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 6 Sep 2017 15:00:23 +0200 Subject: [PATCH] manager: add popup to set theme options (global title) (#18493) --- hobo/emails/views.py | 45 ++++------------------------ hobo/environment/forms.py | 38 +++++++++++++++++++++++ hobo/environment/utils.py | 8 +++++ hobo/settings.py | 2 +- hobo/theme/forms.py | 22 ++++++++++++++ hobo/theme/templates/hobo/theme_home.html | 1 + hobo/theme/templates/hobo/theme_options.html | 24 +++++++++++++++ hobo/theme/urls.py | 1 + hobo/theme/views.py | 10 +++++++ 9 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 hobo/theme/forms.py create mode 100644 hobo/theme/templates/hobo/theme_options.html diff --git a/hobo/emails/views.py b/hobo/emails/views.py index 0902b95..bfd7ca8 100644 --- a/hobo/emails/views.py +++ b/hobo/emails/views.py @@ -15,53 +15,18 @@ # along with this program. If not, see . from django.conf import settings -from django.contrib import messages -from django.http import HttpResponseRedirect from django.utils.translation import ugettext as _ from django.views.generic import TemplateView -from hobo.environment.models import Variable - +from hobo.environment.forms import VariablesFormMixin from .forms import EmailsForm -def get_emails_setting(name): - variable, created = Variable.objects.get_or_create(name=name, - defaults={'auto': True, 'value': settings.EMAIL_DEFAULTS.get(name) or ''}) - return variable - - -class HomeView(TemplateView): +class HomeView(VariablesFormMixin, TemplateView): template_name = 'hobo/emails_home.html' - variables = ['default_from_email', 'email_signature'] - - def get_context_data(self, **kwargs): - context = super(HomeView, self).get_context_data(**kwargs) - if self.request.POST: - form_data = self.request.POST - else: - form_data = None - initial_data = {} - for variable_name in self.variables: - initial_data[variable_name] = get_emails_setting(variable_name).value - context['form'] = EmailsForm(form_data, initial=initial_data) - return context - - def post(self, request, *args, **kwargs): - form = EmailsForm(request.POST) - if not form.is_valid(): - return self.get(request, *args, **kwargs) - changed = False - for variable_name in self.variables: - variable = get_emails_setting(variable_name) - if variable.value != form.cleaned_data[variable_name]: - variable.value = form.cleaned_data[variable_name] - variable.save() - changed = True - if changed: - messages.info(self.request, _('Emails settings have been updated. ' - 'It will take a few seconds to be effective.')) - return HttpResponseRedirect('.') + form_class = EmailsForm + success_message = _('Emails settings have been updated. ' + 'It will take a few seconds to be effective.') home = HomeView.as_view() diff --git a/hobo/environment/forms.py b/hobo/environment/forms.py index 9c251c7..237b946 100644 --- a/hobo/environment/forms.py +++ b/hobo/environment/forms.py @@ -1,11 +1,15 @@ from django import forms from django.conf import settings +from django.contrib import messages +from django.http import HttpResponseRedirect from django.template.defaultfilters import slugify from django.utils.translation import ugettext_lazy as _ from django.core.validators import validate_email + from .models import (Authentic, Wcs, Passerelle, Variable, Combo, Fargo, Welco, MandayeJS, Chrono, Piwik, Corbo, BiJoe, Hobo) +from .utils import get_setting_variable EXCLUDED_FIELDS = ('last_operational_check_timestamp', 'last_operational_success_timestamp', 'secret_key', 'secondary') @@ -162,3 +166,37 @@ class VariableForm(forms.ModelForm): if self.service: self.instance.service = self.service return super(VariableForm, self).save(commit=commit) + + +class VariablesFormMixin(object): + form_class = None + success_message = None + variables = [] + + def get_context_data(self, **kwargs): + context = super(VariablesFormMixin, self).get_context_data(**kwargs) + if self.request.POST: + form_data = self.request.POST + else: + form_data = None + initial_data = {} + for variable_name in self.variables: + initial_data[variable_name] = get_setting_variable(variable_name).value + context['form'] = self.form_class(form_data, initial=initial_data) + return context + + def post(self, request, *args, **kwargs): + form = self.form_class(request.POST) + if not form.is_valid(): + return self.get(request, *args, **kwargs) + changed = False + for variable_name in self.variables: + variable = get_setting_variable(variable_name) + if variable.value != form.cleaned_data[variable_name]: + variable.value = form.cleaned_data[variable_name] + variable.save() + changed = True + if changed and self.success_message: + messages.info(self.request, self.success_message) + + return HttpResponseRedirect('.') diff --git a/hobo/environment/utils.py b/hobo/environment/utils.py index 7b736fc..d215015 100644 --- a/hobo/environment/utils.py +++ b/hobo/environment/utils.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.core.urlresolvers import reverse from django.db import connection @@ -50,3 +51,10 @@ class Zone: self.title = title self.zone_icon_id = zone_icon_id self.href = href + + +def get_setting_variable(name): + from .models import Variable + variable, created = Variable.objects.get_or_create(name=name, + defaults={'auto': True, 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''}) + return variable diff --git a/hobo/settings.py b/hobo/settings.py index 5aa6459..be3dbdc 100644 --- a/hobo/settings.py +++ b/hobo/settings.py @@ -177,7 +177,7 @@ MELLON_SUPERUSER_MAPPING = { THEMES_DIRECTORY = '/usr/share/publik/themes/' -EMAIL_DEFAULTS = {} +VARIABLE_SETTINGS_DEFAULTS = {} MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}' diff --git a/hobo/theme/forms.py b/hobo/theme/forms.py new file mode 100644 index 0000000..b539573 --- /dev/null +++ b/hobo/theme/forms.py @@ -0,0 +1,22 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django import forms +from django.utils.translation import ugettext_lazy as _ + + +class ThemeOptionsForm(forms.Form): + global_title = forms.CharField(label=_('Global Title')) diff --git a/hobo/theme/templates/hobo/theme_home.html b/hobo/theme/templates/hobo/theme_home.html index 0250f06..aac8bcd 100644 --- a/hobo/theme/templates/hobo/theme_home.html +++ b/hobo/theme/templates/hobo/theme_home.html @@ -8,6 +8,7 @@ {% block appbar %}

{% trans 'Theme' %}

+ {% trans 'Options' %} {% endblock %} {% block content %} diff --git a/hobo/theme/templates/hobo/theme_options.html b/hobo/theme/templates/hobo/theme_options.html new file mode 100644 index 0000000..4e0d999 --- /dev/null +++ b/hobo/theme/templates/hobo/theme_options.html @@ -0,0 +1,24 @@ +{% extends "hobo/theme_home.html" %} +{% load i18n %} + +{% block breadcrumb %} +{{ block.super }} +{% trans 'Theme' %} +{% endblock %} + +{% block appbar %} +

{% trans 'Theme Options' %}

+{% endblock %} + +{% block content %} + +
+{% csrf_token %} +{{ form.as_p }} +
+ +{% trans "Cancel" %} +
+
+ +{% endblock %} diff --git a/hobo/theme/urls.py b/hobo/theme/urls.py index 5656851..88f5e59 100644 --- a/hobo/theme/urls.py +++ b/hobo/theme/urls.py @@ -21,4 +21,5 @@ from . import views urlpatterns = patterns('', url(r'^$', views.home, name='theme-home'), url(r'^select$', views.select, name='theme-select'), + url(r'^options$', views.options, name='theme-options'), ) diff --git a/hobo/theme/views.py b/hobo/theme/views.py index 70e52f4..9ba4778 100644 --- a/hobo/theme/views.py +++ b/hobo/theme/views.py @@ -23,6 +23,8 @@ from django.shortcuts import redirect from django.utils.translation import ugettext as _ from django.views.generic import RedirectView, TemplateView +from hobo.environment.forms import VariablesFormMixin +from .forms import ThemeOptionsForm from .utils import (get_themes, get_selected_theme, set_theme) @@ -60,3 +62,11 @@ class SelectView(RedirectView): return reverse('theme-home') select = SelectView.as_view() + + +class OptionsView(VariablesFormMixin, TemplateView): + template_name = 'hobo/theme_options.html' + variables = ['global_title'] + form_class = ThemeOptionsForm + +options = OptionsView.as_view() -- 2.14.1