From 6bb3abd5f66014e8baf1e06a8b5d0fca6efb9b80 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Tue, 3 Jan 2023 11:05:39 +0100
Subject: [PATCH] manage Publik's SMS default-country setting through dedicated
 form (#72760)

---
 hobo/environment/models.py           |  1 +
 hobo/multitenant/settings_loaders.py |  1 +
 hobo/settings.py                     | 11 +++++++++++
 hobo/sms/forms.py                    | 12 ++++++++++++
 hobo/sms/views.py                    |  2 +-
 tests/test_settings_loaders.py       |  7 ++++++-
 6 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/hobo/environment/models.py b/hobo/environment/models.py
index 31bc5de3..1c674fb3 100644
--- a/hobo/environment/models.py
+++ b/hobo/environment/models.py
@@ -52,6 +52,7 @@ AUTO_VARIABLES = (
     'meta_keywords',
     'sms_url',
     'sms_sender',
+    'default_country_code',
 )
 
 
diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py
index 45da4c38..18bb7815 100644
--- a/hobo/multitenant/settings_loaders.py
+++ b/hobo/multitenant/settings_loaders.py
@@ -203,6 +203,7 @@ class TemplateVars(FileBaseSettingsLoader):
         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', '')
+        tenant_settings.DEFAULT_COUNTRY_CODE = variables.get('default_country_code', '')
 
 
 class SettingsVars(SettingsDictUpdateMixin, FileBaseSettingsLoader):
diff --git a/hobo/settings.py b/hobo/settings.py
index 2335ad29..96e6d1c0 100644
--- a/hobo/settings.py
+++ b/hobo/settings.py
@@ -238,6 +238,17 @@ HOBO_SERVICES_DISABLED = [
 # List of service to show in the create service menu, it overrides HOBO_SERVICES_DISABLED.
 HOBO_SERVICES_ENABLED = []
 
+# Phone prefixes by country for phone number as authentication identifier
+PHONE_COUNTRY_CODES = {
+    '32': {'lang': 'BE', 'area': _('Belgium')},
+    '33': {'lang': 'FR', 'area': _('Metropolitan France')},
+    '262': {'lang': 'FR', 'area': _('Réunion')},
+    '508': {'lang': 'FR', 'area': _('Saint Pierre and Miquelon')},
+    '590': {'lang': 'FR', 'area': _('Guadeloupe')},
+    '594': {'lang': 'FR', 'area': _('French Guiana')},
+    '596': {'lang': 'FR', 'area': _('Martinique')},
+}
+
 local_settings_file = os.environ.get(
     'HOBO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')
 )
diff --git a/hobo/sms/forms.py b/hobo/sms/forms.py
index 234e89da..bae9b87c 100644
--- a/hobo/sms/forms.py
+++ b/hobo/sms/forms.py
@@ -14,6 +14,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 from django import forms
+from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.validators import RegexValidator
 from django.utils.translation import ugettext_lazy as _
@@ -33,6 +34,17 @@ class SMSForm(forms.Form):
             'Sender name or phone number. It must neither exceed 11 characters nor contain special characters.'
         ),
     )
+    default_country_code = forms.ChoiceField(
+        label=_('Default country code'),
+        help_text=_('Default country code used as international prefix while sending SMS messages.'),
+    )
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        country_code_choices = [
+            (key, f"+{key} ({value['area']})") for key, value in settings.PHONE_COUNTRY_CODES.items()
+        ]
+        self.fields['default_country_code'].choices = country_code_choices
 
     def clean_sms_url(self):
         value = self.cleaned_data['sms_url']
diff --git a/hobo/sms/views.py b/hobo/sms/views.py
index bcbf5339..f55e74a4 100644
--- a/hobo/sms/views.py
+++ b/hobo/sms/views.py
@@ -25,7 +25,7 @@ from .forms import SMSForm
 
 class HomeView(VariablesFormMixin, TemplateView):
     template_name = 'hobo/sms_home.html'
-    variables = ['sms_url', 'sms_sender']
+    variables = ['sms_url', 'sms_sender', 'default_country_code']
     form_class = SMSForm
     success_message = _('SMS settings have been updated. It will take a few seconds to be effective.')
 
diff --git a/tests/test_settings_loaders.py b/tests/test_settings_loaders.py
index 1793b428..4948a67c 100644
--- a/tests/test_settings_loaders.py
+++ b/tests/test_settings_loaders.py
@@ -162,10 +162,15 @@ def test_sms_update_settings_from_path(tmpdir):
     tenant_settings = UserSettingsHolder({})
     loader = TemplateVars()
 
-    variables = {'sms_url': 'https://example.com/send/', 'sms_sender': 'Sender'}
+    variables = {
+        'sms_url': 'https://example.com/send/',
+        'sms_sender': 'Sender',
+        'default_country_code': '262',
+    }
     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'
+    assert tenant_settings.DEFAULT_COUNTRY_CODE == '262'
-- 
2.39.0

