privacy: Disable DNS fallback option if resolved is not installed

Tests:

- Ensure that systemd-resolved is not installed.

- Notice that form field for Fallback DNS servers is disabled. Form value is
unchecked (default value when initial value is not provided).

- Submitting the form works with and without changes.

- Ensure that systemd-resolved is installed.

- Notice that form field for Fallback DNS servers is not disabled. Form value
reflects the current state of fallback dns as shown in names app.

- Submitting the form works with and without changes.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2024-10-01 18:42:39 -07:00 committed by Veiko Aasa
parent dd16034e3c
commit 71ed5f16c2
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 16 additions and 5 deletions

View File

@ -5,6 +5,7 @@ from django import forms
from django.utils.translation import gettext_lazy as _
from plinth import cfg
from plinth.modules import names
from plinth.utils import format_lazy
@ -30,3 +31,9 @@ class PrivacyForm(forms.Form):
'unusual circumstances where no DNS servers are known but '
'internet connectivity is available. Can be disabled in most '
'cases if network connectivity is stable and reliable.'))
def __init__(self, *args, **kwargs):
"""Disable DNS fallback field if necessary."""
super().__init__(*args, **kwargs)
self.fields['dns_fallback'].disabled = (
not names.is_resolved_installed())

View File

@ -5,6 +5,7 @@ from django.contrib import messages
from django.utils.translation import gettext as _
import plinth.modules.names.privileged as names_privileged
from plinth.modules import names
from plinth.modules.privacy.forms import PrivacyForm
from plinth.views import AppView
@ -21,7 +22,9 @@ class PrivacyAppView(AppView):
"""Return the values to fill in the form."""
initial = super().get_initial()
initial.update(privileged.get_configuration())
initial.update(names_privileged.get_resolved_configuration())
if names.is_resolved_installed():
initial.update(names_privileged.get_resolved_configuration())
return initial
def form_valid(self, form):
@ -34,10 +37,11 @@ class PrivacyAppView(AppView):
if old_config['enable_popcon'] != new_config['enable_popcon']:
changes['enable_popcon'] = new_config['enable_popcon']
if old_config['dns_fallback'] != new_config['dns_fallback']:
names_privileged.set_resolved_configuration(
dns_fallback=new_config['dns_fallback'])
is_changed = True
if 'dns_fallback' in old_config:
if old_config['dns_fallback'] != new_config['dns_fallback']:
names_privileged.set_resolved_configuration(
dns_fallback=new_config['dns_fallback'])
is_changed = True
if changes:
privileged.set_configuration(**changes)