Projet

Général

Profil

0001-manager-add-popup-to-set-theme-options-global-title-.patch

Frédéric Péters, 06 septembre 2017 15:07

Télécharger (9,88 ko)

Voir les différences:

Subject: [PATCH] manager: add popup to set theme options (global title)
 (#18493)

 hobo/emails/views.py                         | 45 ++++------------------------
 hobo/environment/forms.py                    | 37 +++++++++++++++++++++++
 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, 109 insertions(+), 41 deletions(-)
 create mode 100644 hobo/theme/forms.py
 create mode 100644 hobo/theme/templates/hobo/theme_options.html
hobo/emails/views.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
from django.conf import settings
18
from django.contrib import messages
19
from django.http import HttpResponseRedirect
20 18
from django.utils.translation import ugettext as _
21 19
from django.views.generic import TemplateView
22 20

  
23
from hobo.environment.models import Variable
24

  
21
from hobo.environment.forms import VariablesFormMixin
25 22
from .forms import EmailsForm
26 23

  
27 24

  
28
def get_emails_setting(name):
29
    variable, created = Variable.objects.get_or_create(name=name,
30
            defaults={'auto': True, 'value': settings.EMAIL_DEFAULTS.get(name) or ''})
31
    return variable
32

  
33

  
34
class HomeView(TemplateView):
25
class HomeView(VariablesFormMixin, TemplateView):
35 26
    template_name = 'hobo/emails_home.html'
36

  
37 27
    variables = ['default_from_email', 'email_signature']
38

  
39
    def get_context_data(self, **kwargs):
40
        context = super(HomeView, self).get_context_data(**kwargs)
41
        if self.request.POST:
42
            form_data = self.request.POST
43
        else:
44
            form_data = None
45
        initial_data = {}
46
        for variable_name in self.variables:
47
            initial_data[variable_name] = get_emails_setting(variable_name).value
48
        context['form'] = EmailsForm(form_data, initial=initial_data)
49
        return context
50

  
51
    def post(self, request, *args, **kwargs):
52
        form = EmailsForm(request.POST)
53
        if not form.is_valid():
54
            return self.get(request, *args, **kwargs)
55
        changed = False
56
        for variable_name in self.variables:
57
            variable = get_emails_setting(variable_name)
58
            if variable.value != form.cleaned_data[variable_name]:
59
                variable.value = form.cleaned_data[variable_name]
60
                variable.save()
61
                changed = True
62
        if changed:
63
            messages.info(self.request, _('Emails settings have been updated. '
64
                                          'It will take a few seconds to be effective.'))
65
        return HttpResponseRedirect('.')
28
    form_class = EmailsForm
29
    success_message = _('Emails settings have been updated. '
30
                        'It will take a few seconds to be effective.')
66 31

  
67 32
home = HomeView.as_view()
hobo/environment/forms.py
1 1
from django import forms
2 2
from django.conf import settings
3
from django.contrib import messages
4
from django.http import HttpResponseRedirect
3 5
from django.template.defaultfilters import slugify
4 6
from django.utils.translation import ugettext_lazy as _
5 7
from django.core.validators import validate_email
6 8

  
9

  
7 10
from .models import (Authentic, Wcs, Passerelle, Variable, Combo, Fargo, Welco,
8 11
                     MandayeJS, Chrono, Piwik, Corbo, BiJoe, Hobo)
12
from .utils import get_setting_variable
9 13

  
10 14
EXCLUDED_FIELDS = ('last_operational_check_timestamp',
11 15
        'last_operational_success_timestamp', 'secret_key', 'secondary')
......
162 166
        if self.service:
163 167
            self.instance.service = self.service
164 168
        return super(VariableForm, self).save(commit=commit)
169

  
170

  
171
class VariablesFormMixin(object):
172
    form_class = None
173
    variables = []
174

  
175
    def get_context_data(self, **kwargs):
176
        context = super(VariablesFormMixin, self).get_context_data(**kwargs)
177
        if self.request.POST:
178
            form_data = self.request.POST
179
        else:
180
            form_data = None
181
        initial_data = {}
182
        for variable_name in self.variables:
183
            initial_data[variable_name] = get_setting_variable(variable_name).value
184
        context['form'] = self.form_class(form_data, initial=initial_data)
185
        return context
186

  
187
    def post(self, request, *args, **kwargs):
188
        form = self.form_class(request.POST)
189
        if not form.is_valid():
190
            return self.get(request, *args, **kwargs)
191
        changed = False
192
        for variable_name in self.variables:
193
            variable = get_setting_variable(variable_name)
194
            if variable.value != form.cleaned_data[variable_name]:
195
                variable.value = form.cleaned_data[variable_name]
196
                variable.save()
197
                changed = True
198
        if changed and self.success_message:
199
            messages.info(self.request, self.success_message)
200

  
201
        return HttpResponseRedirect('.')
hobo/environment/utils.py
1
from django.conf import settings
1 2
from django.core.urlresolvers import reverse
2 3
from django.db import connection
3 4

  
......
50 51
        self.title = title
51 52
        self.zone_icon_id = zone_icon_id
52 53
        self.href = href
54

  
55

  
56
def get_setting_variable(name):
57
    from .models import Variable
58
    variable, created = Variable.objects.get_or_create(name=name,
59
            defaults={'auto': True, 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''})
60
    return variable
hobo/settings.py
177 177

  
178 178
THEMES_DIRECTORY = '/usr/share/publik/themes/'
179 179

  
180
EMAIL_DEFAULTS = {}
180
VARIABLE_SETTINGS_DEFAULTS = {}
181 181

  
182 182
MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'
183 183

  
hobo/theme/forms.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2017 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django import forms
18
from django.utils.translation import ugettext_lazy as _
19

  
20

  
21
class ThemeOptionsForm(forms.Form):
22
    global_title = forms.CharField(label=_('Global Title'))
hobo/theme/templates/hobo/theme_home.html
8 8

  
9 9
{% block appbar %}
10 10
  <h2>{% trans 'Theme' %}</h2>
11
  <a rel="popup" href="{% url 'theme-options' %}">{% trans 'Options' %}</a>
11 12
{% endblock %}
12 13

  
13 14
{% block content %}
hobo/theme/templates/hobo/theme_options.html
1
{% extends "hobo/theme_home.html" %}
2
{% load i18n %}
3

  
4
{% block breadcrumb %}
5
{{ block.super }}
6
<a href="{% url 'theme-home' %}">{% trans 'Theme' %}</a>
7
{% endblock %}
8

  
9
{% block appbar %}
10
  <h2>{% trans 'Theme Options' %}</h2>
11
{% endblock %}
12

  
13
{% block content %}
14

  
15
<form method="post">
16
{% csrf_token %}
17
{{ form.as_p }}
18
<div class="buttons">
19
<button class="submit-button">{% trans "Submit" %}</button>
20
<a class="cancel" href=".">{% trans "Cancel" %}</a>
21
</div>
22
</form>
23

  
24
{% endblock %}
hobo/theme/urls.py
21 21
urlpatterns = patterns('',
22 22
    url(r'^$', views.home, name='theme-home'),
23 23
    url(r'^select$', views.select, name='theme-select'),
24
    url(r'^options$', views.options, name='theme-options'),
24 25
)
hobo/theme/views.py
23 23
from django.utils.translation import ugettext as _
24 24
from django.views.generic import RedirectView, TemplateView
25 25

  
26
from hobo.environment.forms import VariablesFormMixin
27
from .forms import ThemeOptionsForm
26 28
from .utils import (get_themes, get_selected_theme, set_theme)
27 29

  
28 30

  
......
60 62
        return reverse('theme-home')
61 63

  
62 64
select = SelectView.as_view()
65

  
66

  
67
class OptionsView(VariablesFormMixin, TemplateView):
68
    template_name = 'hobo/theme_options.html'
69
    variables = ['global_title']
70
    form_class = ThemeOptionsForm
71

  
72
options = OptionsView.as_view()
63
-