From 7ce1ca68e1a13a8c08fbdcb8ee39d6f174d6126e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 11 Jun 2016 20:26:11 +0200 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 diff --git a/hobo/emails/__init__.py b/hobo/emails/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hobo/emails/forms.py b/hobo/emails/forms.py new file mode 100644 index 0000000..c80bbce --- /dev/null +++ b/hobo/emails/forms.py @@ -0,0 +1,28 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2016 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 EmailsForm(forms.Form): + default_from_email = forms.EmailField(label=_('Default From')) + email_signature = forms.CharField(label=_('Signature'), required=False, + widget=forms.Textarea) + + def __init__(self, *args, **kwargs): + super(EmailsForm, self).__init__(*args, **kwargs) + self.fields['email_signature'].widget.attrs = {'rows': 4, 'cols': 80} diff --git a/hobo/emails/templates/hobo/emails_home.html b/hobo/emails/templates/hobo/emails_home.html new file mode 100644 index 0000000..37fd2db --- /dev/null +++ b/hobo/emails/templates/hobo/emails_home.html @@ -0,0 +1,24 @@ +{% extends "hobo/base.html" %} +{% load i18n %} + +{% block breadcrumb %} +{{ block.super }} +{% trans 'Emails' %} +{% endblock %} + +{% block appbar %} +

{% trans 'Emails' %}

+{% endblock %} + +{% block content %} + +
+{% csrf_token %} +{{ form.as_p }} +
+ +
+
+ +{% endblock %} + diff --git a/hobo/emails/urls.py b/hobo/emails/urls.py new file mode 100644 index 0000000..90e4087 --- /dev/null +++ b/hobo/emails/urls.py @@ -0,0 +1,23 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2016 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.conf.urls import patterns, url + +from . import views + +urlpatterns = patterns('', + url(r'^$', views.home, name='emails-home'), +) diff --git a/hobo/emails/views.py b/hobo/emails/views.py new file mode 100644 index 0000000..0902b95 --- /dev/null +++ b/hobo/emails/views.py @@ -0,0 +1,67 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2016 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.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 .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): + 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('.') + +home = HomeView.as_view() diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py index 20cb378..98c1e67 100644 --- a/hobo/multitenant/settings_loaders.py +++ b/hobo/multitenant/settings_loaders.py @@ -121,6 +121,10 @@ class TemplateVars(FileBaseSettingsLoader): tenant_settings.TEMPLATE_VARS = {} tenant_settings.TEMPLATE_VARS.update(variables) + if variables.get('default_from_email'): + tenant_settings.DEFAULT_FROM_EMAIL = variables['default_from_email'] + + class CORSSettings(FileBaseSettingsLoader): FILENAME = 'hobo.json' diff --git a/hobo/settings.py b/hobo/settings.py index 5126d79..5aa6459 100644 --- a/hobo/settings.py +++ b/hobo/settings.py @@ -43,6 +43,7 @@ INSTALLED_APPS = ( 'hobo.environment', 'hobo.profile', 'hobo.theme', + 'hobo.emails', 'hobo.deploy', ) @@ -176,6 +177,8 @@ MELLON_SUPERUSER_MAPPING = { THEMES_DIRECTORY = '/usr/share/publik/themes/' +EMAIL_DEFAULTS = {} + MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}' local_settings_file = os.environ.get('HOBO_SETTINGS_FILE', diff --git a/hobo/templates/hobo/home.html b/hobo/templates/hobo/home.html index 6fdb384..20d18bb 100644 --- a/hobo/templates/hobo/home.html +++ b/hobo/templates/hobo/home.html @@ -11,6 +11,7 @@
  • {% trans 'User Profile' %}
  • {% trans 'Theme' %}
  • +
  • {% trans 'Emails' %}
  • {% trans 'Sites' %}
  • {% trans 'Variables' %}
  • {% for passerelle in passerelles %} diff --git a/hobo/urls.py b/hobo/urls.py index 90b575e..e7dbc24 100644 --- a/hobo/urls.py +++ b/hobo/urls.py @@ -9,6 +9,7 @@ from .urls_utils import decorated_includes from .environment.urls import urlpatterns as environment_urls from .profile.urls import urlpatterns as profile_urls from .theme.urls import urlpatterns as theme_urls +from .emails.urls import urlpatterns as emails_urls urlpatterns = patterns('', url(r'^$', 'hobo.views.home', name='home'), @@ -18,6 +19,7 @@ urlpatterns = patterns('', include(profile_urls))), url(r'^theme/', decorated_includes(admin_required, include(theme_urls))), + url(r'^emails/', decorated_includes(admin_required, include(emails_urls))), url(r'^menu.json$', 'hobo.views.menu_json', name='menu_json'), url(r'^hobos.json$', 'hobo.views.hobo'), url(r'^admin/', include(admin.site.urls)), -- 2.8.1