From b21af70510a76e76a8c467e6d6dbdff968eeffbe Mon Sep 17 00:00:00 2001 From: Benedek Nagy Date: Mon, 16 Oct 2023 22:59:38 +0200 Subject: [PATCH] nextcloud: Add option to configure the default phone region This setting is necessarry to pass the setup warning under /nextcloud/settings/admin/overview Note: during testing the field only becomes visible once the freedombox-develop command is restarted https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html#default-phone-region Signed-off-by: Benedek Nagy [sunil: Add missing docstring] [sunil: Update form label for consistency] [sunil: Reduce number of success form messages] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/modules/nextcloud/__init__.py | 3 ++- plinth/modules/nextcloud/forms.py | 17 +++++++++++++++++ plinth/modules/nextcloud/privileged.py | 18 ++++++++++++++++++ plinth/modules/nextcloud/views.py | 11 ++++++++++- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/plinth/modules/nextcloud/__init__.py b/plinth/modules/nextcloud/__init__.py index 0db1b8250..043d343e3 100644 --- a/plinth/modules/nextcloud/__init__.py +++ b/plinth/modules/nextcloud/__init__.py @@ -64,7 +64,8 @@ class NextcloudApp(app_module.App): self.add(shortcut) packages = Packages('packages-nextcloud', - ['podman', 'default-mysql-server'], + ['podman', 'default-mysql-server', + 'python3-iso3166'], conflicts=['libpam-tmpdir'], conflicts_action=Packages.ConflictsAction.REMOVE) self.add(packages) diff --git a/plinth/modules/nextcloud/forms.py b/plinth/modules/nextcloud/forms.py index d2c0dcb64..1d32ccaf3 100644 --- a/plinth/modules/nextcloud/forms.py +++ b/plinth/modules/nextcloud/forms.py @@ -5,6 +5,17 @@ from django import forms from django.utils.translation import gettext_lazy as _ +def _get_phone_regions(): + """Return choice field choices for phone regions.""" + try: + from iso3166 import countries # type: ignore + phone_regions = [(country.alpha2, country.name) + for country in countries] + return sorted(phone_regions) + except ImportError: + return [('US', 'United States of America')] + + class NextcloudForm(forms.Form): """Nextcloud configuration form.""" @@ -19,3 +30,9 @@ class NextcloudForm(forms.Form): 'and the minimum required length is 10 characters' '. Leave this field blank to keep the current password.'), required=False, widget=forms.PasswordInput, min_length=10) + + default_phone_region = forms.ChoiceField( + label=_('Default phone region'), required=False, + help_text=_('The default phone region is required to validate phone ' + 'numbers in the profile settings without a country code.'), + choices=_get_phone_regions) diff --git a/plinth/modules/nextcloud/privileged.py b/plinth/modules/nextcloud/privileged.py index e3188cfac..d0fe5f80e 100644 --- a/plinth/modules/nextcloud/privileged.py +++ b/plinth/modules/nextcloud/privileged.py @@ -126,6 +126,24 @@ def set_admin_password(password: str): ], check=True) +@privileged +def get_default_phone_region(): + """"Get the value of default_phone_region.""" + try: + default_phone_region = _run_occ('config:system:get', + 'default_phone_region', + capture_output=True) + return default_phone_region.stdout.decode().strip() + except subprocess.CalledProcessError: + return None + + +@privileged +def set_default_phone_region(region: str): + """"Set the value of default_phone_region.""" + _run_occ('config:system:set', 'default_phone_region', '--value', region) + + def _configure_firewall(action, interface_name): subprocess.run([ 'firewall-cmd', '--permanent', '--zone=trusted', diff --git a/plinth/modules/nextcloud/views.py b/plinth/modules/nextcloud/views.py index aed885bce..3eb01240d 100644 --- a/plinth/modules/nextcloud/views.py +++ b/plinth/modules/nextcloud/views.py @@ -23,7 +23,10 @@ class NextcloudAppView(AppView): def get_initial(self): """Return the values to fill in the form.""" initial = super().get_initial() - initial.update({'domain': privileged.get_domain()}) + initial.update({ + 'domain': privileged.get_domain(), + 'default_phone_region': privileged.get_default_phone_region() + }) return initial def form_valid(self, form): @@ -49,6 +52,12 @@ class NextcloudAppView(AppView): self.request, _('Password update failed. Please choose a stronger ' 'password.')) + + if _value_changed('default_phone_region'): + privileged.set_default_phone_region( + new_config['default_phone_region']) + is_changed = True + if is_changed: messages.success(self.request, _('Configuration updated.'))