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 <contact@nbenedek.me>
[sunil: Add missing docstring]
[sunil: Update form label for consistency]
[sunil: Reduce number of success form messages]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Benedek Nagy 2023-10-16 22:59:38 +02:00 committed by Sunil Mohan Adapa
parent 8ef680f450
commit b21af70510
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
4 changed files with 47 additions and 2 deletions

View File

@ -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)

View File

@ -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 <strong>10 characters'
'</strong>. 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)

View File

@ -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',

View File

@ -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.'))