manage Publik's phone-related settings through dedicated form (#72760)
gitea/hobo/pipeline/head This commit looks good Details

This commit is contained in:
Paul Marillonnet 2023-01-03 11:05:39 +01:00
parent 5e7f1599f5
commit 99eb6e3f35
6 changed files with 71 additions and 3 deletions

View File

@ -52,6 +52,7 @@ AUTO_VARIABLES = (
'meta_keywords', 'meta_keywords',
'sms_url', 'sms_url',
'sms_sender', 'sms_sender',
'local_country_code',
) )

View File

@ -203,6 +203,11 @@ class TemplateVars(FileBaseSettingsLoader):
tenant_settings.USER_PROFILE_CONFIG = hobo_json.get('profile') tenant_settings.USER_PROFILE_CONFIG = hobo_json.get('profile')
tenant_settings.SMS_URL = variables.get('sms_url', '') tenant_settings.SMS_URL = variables.get('sms_url', '')
tenant_settings.SMS_SENDER = variables.get('sms_sender', '') tenant_settings.SMS_SENDER = variables.get('sms_sender', '')
# propagate phone country codes list
tenant_settings.PHONE_COUNTRY_CODES = variables.get(
'phone_country_codes', getattr(settings, 'PHONE_COUNTRY_CODES', {})
)
tenant_settings.LOCAL_COUNTRY_CODE = variables.get('local_country_code', '')
class SettingsVars(SettingsDictUpdateMixin, FileBaseSettingsLoader): class SettingsVars(SettingsDictUpdateMixin, FileBaseSettingsLoader):

View File

@ -244,6 +244,17 @@ HOBO_SERVICES_DISABLED = [
# List of service to show in the create service menu, it overrides HOBO_SERVICES_DISABLED. # List of service to show in the create service menu, it overrides HOBO_SERVICES_DISABLED.
HOBO_SERVICES_ENABLED = [] HOBO_SERVICES_ENABLED = []
# Phone prefixes by country for phone number as authentication identifier
PHONE_COUNTRY_CODES = {
'32': {'region': 'BE', 'region_desc': _('Belgium')},
'33': {'region': 'FR', 'region_desc': _('Metropolitan France')},
'262': {'region': 'RE', 'region_desc': _('Réunion')},
'508': {'region': 'PM', 'region_desc': _('Saint Pierre and Miquelon')},
'590': {'region': 'GP', 'region_desc': _('Guadeloupe')},
'594': {'region': 'GF', 'region_desc': _('French Guiana')},
'596': {'region': 'MQ', 'region_desc': _('Martinique')},
}
local_settings_file = os.environ.get( local_settings_file = os.environ.get(
'HOBO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py') 'HOBO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')
) )

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -22,7 +23,11 @@ from hobo.utils import TemplateError, get_templated_url
class SMSForm(forms.Form): class SMSForm(forms.Form):
sms_url = forms.CharField(label=_('SMS URL'), help_text=_('URL that can receive POST data to send SMS.')) sms_url = forms.CharField(
label=_('SMS URL'),
help_text=_('URL that can receive POST data to send SMS.'),
required=False,
)
sms_sender = forms.CharField( sms_sender = forms.CharField(
label=_('Sender'), label=_('Sender'),
max_length=11, max_length=11,
@ -32,7 +37,23 @@ class SMSForm(forms.Form):
help_text=_( help_text=_(
'Sender name or phone number. It must neither exceed 11 characters nor contain special characters.' 'Sender name or phone number. It must neither exceed 11 characters nor contain special characters.'
), ),
required=False,
) )
local_country_code = forms.ChoiceField(
label=_('Local country code'),
help_text=_(
'Local country code, used to identify local mobile phone numbers and '
'as international prefix while sending SMS messages.'
),
required=False,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
country_code_choices = [
(key, f"+{key} ({value['region_desc']})") for key, value in settings.PHONE_COUNTRY_CODES.items()
]
self.fields['local_country_code'].choices = [('', '')] + country_code_choices
def clean_sms_url(self): def clean_sms_url(self):
value = self.cleaned_data['sms_url'] value = self.cleaned_data['sms_url']

View File

@ -25,7 +25,7 @@ from .forms import SMSForm
class HomeView(VariablesFormMixin, TemplateView): class HomeView(VariablesFormMixin, TemplateView):
template_name = 'hobo/sms_home.html' template_name = 'hobo/sms_home.html'
variables = ['sms_url', 'sms_sender'] variables = ['sms_url', 'sms_sender', 'local_country_code']
form_class = SMSForm form_class = SMSForm
success_message = _('SMS settings have been updated. It will take a few seconds to be effective.') success_message = _('SMS settings have been updated. It will take a few seconds to be effective.')

View File

@ -4,6 +4,7 @@ import urllib.parse
import pytest import pytest
from django.conf import UserSettingsHolder from django.conf import UserSettingsHolder
from django.test import override_settings
from hobo.deploy.utils import get_hobo_json from hobo.deploy.utils import get_hobo_json
from hobo.environment.models import Authentic, Combo, Variable from hobo.environment.models import Authentic, Combo, Variable
@ -162,10 +163,39 @@ def test_sms_update_settings_from_path(tmpdir):
tenant_settings = UserSettingsHolder({}) tenant_settings = UserSettingsHolder({})
loader = TemplateVars() loader = TemplateVars()
variables = {'sms_url': 'https://example.com/send/', 'sms_sender': 'Sender'} variables = {
'sms_url': 'https://example.com/send/',
'sms_sender': 'Sender',
'local_country_code': '262',
'phone_country_codes': {
'262': {'region': 'RE', 'region_desc': 'Réunion'},
'666': {'region': 'WLD', 'region_desc': 'Wonderland'},
},
}
env = {'services': [], 'variables': variables} env = {'services': [], 'variables': variables}
path = os.path.join(str(tmpdir), 'hobo.json') path = os.path.join(str(tmpdir), 'hobo.json')
json.dump(env, open(path, 'w')) json.dump(env, open(path, 'w'))
loader.update_settings_from_path(tenant_settings, path) loader.update_settings_from_path(tenant_settings, path)
assert tenant_settings.SMS_URL == 'https://example.com/send/' assert tenant_settings.SMS_URL == 'https://example.com/send/'
assert tenant_settings.SMS_SENDER == 'Sender' assert tenant_settings.SMS_SENDER == 'Sender'
assert tenant_settings.LOCAL_COUNTRY_CODE == '262'
assert tenant_settings.PHONE_COUNTRY_CODES == {
'262': {'region': 'RE', 'region_desc': 'Réunion'},
'666': {'region': 'WLD', 'region_desc': 'Wonderland'},
}
variables.pop('phone_country_codes')
PHONE_COUNTRY_CODES = {
'32': {'region': 'BE', 'region_desc': 'Belgium'},
'777': {'region': 'WLD', 'region_desc': 'Wonderland'},
}
with override_settings(PHONE_COUNTRY_CODES=PHONE_COUNTRY_CODES):
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.PHONE_COUNTRY_CODES == {
'32': {'region': 'BE', 'region_desc': 'Belgium'},
'777': {'region': 'WLD', 'region_desc': 'Wonderland'},
}