Projet

Général

Profil

0001-general-start-an-emails-settings-panel-9858.patch

Frédéric Péters, 12 juin 2016 10:55

Télécharger (9,69 ko)

Voir les différences:

Subject: [PATCH 1/2] general: start an "emails" settings panel (#9858)

 hobo/emails/__init__.py                     |  0
 hobo/emails/forms.py                        | 28 ++++++++++++
 hobo/emails/templates/hobo/emails_home.html | 24 +++++++++++
 hobo/emails/urls.py                         | 23 ++++++++++
 hobo/emails/views.py                        | 67 +++++++++++++++++++++++++++++
 hobo/multitenant/settings_loaders.py        |  4 ++
 hobo/settings.py                            |  3 ++
 hobo/templates/hobo/home.html               |  1 +
 hobo/urls.py                                |  2 +
 9 files changed, 152 insertions(+)
 create mode 100644 hobo/emails/__init__.py
 create mode 100644 hobo/emails/forms.py
 create mode 100644 hobo/emails/templates/hobo/emails_home.html
 create mode 100644 hobo/emails/urls.py
 create mode 100644 hobo/emails/views.py
hobo/emails/forms.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2016 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 EmailsForm(forms.Form):
22
    default_from_email = forms.EmailField(label=_('Default From'))
23
    email_signature = forms.CharField(label=_('Signature'), required=False,
24
            widget=forms.Textarea)
25

  
26
    def __init__(self, *args, **kwargs):
27
        super(EmailsForm, self).__init__(*args, **kwargs)
28
        self.fields['email_signature'].widget.attrs = {'rows': 4, 'cols': 80}
hobo/emails/templates/hobo/emails_home.html
1
{% extends "hobo/base.html" %}
2
{% load i18n %}
3

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

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

  
13
{% block content %}
14

  
15
<form action="." method="post">
16
{% csrf_token %}
17
{{ form.as_p }}
18
<div class="buttons">
19
<button>{% trans "Submit" %}</button>
20
</div>
21
</form>
22

  
23
{% endblock %}
24

  
hobo/emails/urls.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2016 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.conf.urls import patterns, url
18

  
19
from . import views
20

  
21
urlpatterns = patterns('',
22
    url(r'^$', views.home, name='emails-home'),
23
)
hobo/emails/views.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2016 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.conf import settings
18
from django.contrib import messages
19
from django.http import HttpResponseRedirect
20
from django.utils.translation import ugettext as _
21
from django.views.generic import TemplateView
22

  
23
from hobo.environment.models import Variable
24

  
25
from .forms import EmailsForm
26

  
27

  
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):
35
    template_name = 'hobo/emails_home.html'
36

  
37
    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('.')
66

  
67
home = HomeView.as_view()
hobo/multitenant/settings_loaders.py
121 121
            tenant_settings.TEMPLATE_VARS = {}
122 122
        tenant_settings.TEMPLATE_VARS.update(variables)
123 123

  
124
        if variables.get('default_from_email'):
125
            tenant_settings.DEFAULT_FROM_EMAIL = variables['default_from_email']
126

  
127

  
124 128
class CORSSettings(FileBaseSettingsLoader):
125 129
    FILENAME = 'hobo.json'
126 130

  
hobo/settings.py
43 43
    'hobo.environment',
44 44
    'hobo.profile',
45 45
    'hobo.theme',
46
    'hobo.emails',
46 47
    'hobo.deploy',
47 48
)
48 49

  
......
176 177

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

  
180
EMAIL_DEFAULTS = {}
181

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

  
181 184
local_settings_file = os.environ.get('HOBO_SETTINGS_FILE',
hobo/templates/hobo/home.html
11 11
<ul class="apps">
12 12
  <li class="icon-users"><a href="{% url 'profile-home' %}">{% trans 'User Profile' %}</a></li>
13 13
  <li class="icon-theme"><a href="{% url 'theme-home' %}">{% trans 'Theme' %}</a></li>
14
  <li class="icon-mail"><a href="{% url 'emails-home' %}">{% trans 'Emails' %}</a></li>
14 15
  <li class="icon-portal"><a href="{% url 'environment-home' %}">{% trans 'Sites' %}</a></li>
15 16
  <li class="icon-settings"><a href="{% url 'environment-variables' %}">{% trans 'Variables' %}</a></li>
16 17
  {% for passerelle in passerelles %}
hobo/urls.py
9 9
from .environment.urls import urlpatterns as environment_urls
10 10
from .profile.urls import urlpatterns as profile_urls
11 11
from .theme.urls import urlpatterns as theme_urls
12
from .emails.urls import urlpatterns as emails_urls
12 13

  
13 14
urlpatterns = patterns('',
14 15
    url(r'^$', 'hobo.views.home', name='home'),
......
18 19
                                             include(profile_urls))),
19 20
    url(r'^theme/', decorated_includes(admin_required,
20 21
                                             include(theme_urls))),
22
    url(r'^emails/', decorated_includes(admin_required, include(emails_urls))),
21 23
    url(r'^menu.json$', 'hobo.views.menu_json', name='menu_json'),
22 24
    url(r'^hobos.json$', 'hobo.views.hobo'),
23 25
    url(r'^admin/', include(admin.site.urls)),
24
-