From 538963b8ba4b12f259b3fa45260981da0493cfa2 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 16 Sep 2020 16:48:49 +0200 Subject: [PATCH] misc: add sms configuration (#46444) --- MANIFEST.in | 1 + hobo/environment/models.py | 2 ++ hobo/multitenant/settings_loaders.py | 2 ++ hobo/settings.py | 1 + hobo/sms/__init__.py | 0 hobo/sms/forms.py | 28 ++++++++++++++++++++++++ hobo/sms/templates/hobo/sms_home.html | 24 +++++++++++++++++++++ hobo/sms/urls.py | 23 ++++++++++++++++++++ hobo/sms/views.py | 31 +++++++++++++++++++++++++++ hobo/templates/hobo/home.html | 1 + hobo/urls.py | 2 ++ tests/test_settings_loaders.py | 13 +++++++++++ tests/test_sms.py | 19 ++++++++++++++++ 13 files changed, 147 insertions(+) create mode 100644 hobo/sms/__init__.py create mode 100644 hobo/sms/forms.py create mode 100644 hobo/sms/templates/hobo/sms_home.html create mode 100644 hobo/sms/urls.py create mode 100644 hobo/sms/views.py create mode 100644 tests/test_sms.py diff --git a/MANIFEST.in b/MANIFEST.in index cd7885b..3b98a56 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -7,6 +7,7 @@ recursive-include hobo/seo/templates *.html *.txt recursive-include hobo/theme/templates *.html *.txt recursive-include hobo/environment/templates *.html *.txt recursive-include hobo/emails/templates *.html *.txt +recursive-include hobo/sms/templates *.html *.txt recursive-include hobo/matomo/templates *.html recursive-include hobo/locale *.po *.mo recursive-include hobo/environment/locale *.po *.mo diff --git a/hobo/environment/models.py b/hobo/environment/models.py index 34a69ff..3f6cad8 100644 --- a/hobo/environment/models.py +++ b/hobo/environment/models.py @@ -52,6 +52,8 @@ AUTO_VARIABLES = ( 'robots_txt', 'meta_description', 'meta_keywords', + 'sms_url', + 'sms_sender', ) class Variable(models.Model): diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py index f65ee03..2536ba3 100644 --- a/hobo/multitenant/settings_loaders.py +++ b/hobo/multitenant/settings_loaders.py @@ -191,6 +191,8 @@ class TemplateVars(FileBaseSettingsLoader): tenant_settings.DEFAULT_FROM_EMAIL = variables['default_from_email'] tenant_settings.USER_PROFILE_CONFIG = hobo_json.get('profile') + tenant_settings.SMS_URL = variables.get('sms_url', '') + tenant_settings.SMS_SENDER = variables.get('sms_sender', '') class SettingsVars(SettingsDictUpdateMixin, FileBaseSettingsLoader): diff --git a/hobo/settings.py b/hobo/settings.py index f0363b8..64fa290 100644 --- a/hobo/settings.py +++ b/hobo/settings.py @@ -50,6 +50,7 @@ INSTALLED_APPS = ( 'hobo.theme', 'hobo.emails', 'hobo.deploy', + 'hobo.sms', ) MIDDLEWARE_CLASSES = ( diff --git a/hobo/sms/__init__.py b/hobo/sms/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hobo/sms/forms.py b/hobo/sms/forms.py new file mode 100644 index 0000000..9485f79 --- /dev/null +++ b/hobo/sms/forms.py @@ -0,0 +1,28 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2020 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.core.validators import RegexValidator +from django.utils.translation import ugettext_lazy as _ + + +class SMSForm(forms.Form): + sms_url = forms.URLField(label=_('SMS URL'), help_text=_('URL that can receive POST data to send SMS.')) + sms_sender = forms.CharField( + label=_('Sender'), + max_length=11, + validators=[RegexValidator('^[A-Za-z0-9 ]{0,11}$', _('Only alphanumeric characters and spaces are allowed.'))], + help_text=_('Sender name or phone number. It must neither exceed 11 characters nor contain special characters.') + ) diff --git a/hobo/sms/templates/hobo/sms_home.html b/hobo/sms/templates/hobo/sms_home.html new file mode 100644 index 0000000..f6c19d8 --- /dev/null +++ b/hobo/sms/templates/hobo/sms_home.html @@ -0,0 +1,24 @@ +{% extends "hobo/base.html" %} +{% load i18n %} + +{% block breadcrumb %} +{{ block.super }} +{% trans 'SMS' %} +{% endblock %} + +{% block appbar %} +

{% trans 'SMS' %}

+{% endblock %} + +{% block content %} + +
+{% csrf_token %} +{{ form.as_p }} +
+ +
+
+ +{% endblock %} + diff --git a/hobo/sms/urls.py b/hobo/sms/urls.py new file mode 100644 index 0000000..84e680c --- /dev/null +++ b/hobo/sms/urls.py @@ -0,0 +1,23 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2020 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 url + +from . import views + +urlpatterns = [ + url(r'^$', views.home, name='sms-home'), +] diff --git a/hobo/sms/views.py b/hobo/sms/views.py new file mode 100644 index 0000000..234aa4a --- /dev/null +++ b/hobo/sms/views.py @@ -0,0 +1,31 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2020 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.utils.translation import ugettext as _ +from django.views.generic import TemplateView + +from hobo.environment.forms import VariablesFormMixin +from .forms import SMSForm + + +class HomeView(VariablesFormMixin, TemplateView): + template_name = 'hobo/sms_home.html' + variables = ['sms_url', 'sms_sender'] + form_class = SMSForm + success_message = _('SMS settings have been updated. It will take a few seconds to be effective.') + +home = HomeView.as_view() diff --git a/hobo/templates/hobo/home.html b/hobo/templates/hobo/home.html index fd29d39..9a53eb4 100644 --- a/hobo/templates/hobo/home.html +++ b/hobo/templates/hobo/home.html @@ -11,6 +11,7 @@ {% endif %}
  • {% trans 'Theme' %}
  • {% trans 'Emails' %}
  • +
  • {% trans 'SMS' %}
  • {% if has_authentic %}
  • FranceConnect
  • {% endif %} diff --git a/hobo/urls.py b/hobo/urls.py index 7993d50..6bb1df6 100644 --- a/hobo/urls.py +++ b/hobo/urls.py @@ -29,6 +29,7 @@ from .theme.urls import urlpatterns as theme_urls from .emails.urls import urlpatterns as emails_urls from .debug.urls import urlpatterns as debug_urls from .seo.urls import urlpatterns as seo_urls +from .sms.urls import urlpatterns as sms_urls admin.autodiscover() @@ -41,6 +42,7 @@ urlpatterns = [ url(r'^theme/', decorated_includes(admin_required, include(theme_urls))), url(r'^emails/', decorated_includes(admin_required, include(emails_urls))), url(r'^seo/', decorated_includes(admin_required, include(seo_urls))), + url(r'^sms/', decorated_includes(admin_required, include(sms_urls))), url(r'^debug/', decorated_includes(admin_required, include(debug_urls))), url(r'^api/health/$', health_json, name='health-json'), url(r'^menu.json$', menu_json, name='menu_json'), diff --git a/tests/test_settings_loaders.py b/tests/test_settings_loaders.py index 5d2b46e..0d49dc4 100644 --- a/tests/test_settings_loaders.py +++ b/tests/test_settings_loaders.py @@ -130,3 +130,16 @@ def test_email_update_settings_from_path(tmpdir): tenant_settings.DEFAULT_FROM_EMAIL = '' update_settings({'services': [], 'variables': {'global_title': 'Publik'}}) assert tenant_settings.DEFAULT_FROM_EMAIL == '' + + +def test_sms_update_settings_from_path(tmpdir): + tenant_settings = UserSettingsHolder({}) + loader = TemplateVars() + + variables = {'sms_url': 'https://example.com/send/', 'sms_sender': 'Sender'} + env = {'services': [], 'variables': variables} + path = os.path.join(str(tmpdir), 'hobo.json') + json.dump(env, open(path, 'w')) + loader.update_settings_from_path(tenant_settings, path) + assert tenant_settings.SMS_URL == 'https://example.com/send/' + assert tenant_settings.SMS_SENDER == 'Sender' diff --git a/tests/test_sms.py b/tests/test_sms.py new file mode 100644 index 0000000..ae022fd --- /dev/null +++ b/tests/test_sms.py @@ -0,0 +1,19 @@ +from hobo.environment.models import Variable + +from test_manager import login + + +def test_sms_view(app, admin_user): + app = login(app) + resp = app.get('/') + resp = resp.click('SMS') + resp.form['sms_url'] = 'https://example.com/send/' + resp.form['sms_sender'] = 'Sender Name' + resp = resp.form.submit().follow() + + assert Variable.objects.filter(name='sms_url')[0].value == 'https://example.com/send/' + assert Variable.objects.filter(name='sms_sender')[0].value == 'Sender Name' + + resp.form['sms_sender'] = 'Entr\'ouvert' + resp = resp.form.submit() + assert 'Only alphanumeric' in resp.text