datetime: Use privileged decorator for actions

Tests:

- Setting timezone shows:
  - In the interface and
  - timedatectl

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-10-08 18:51:45 -04:00
1 parent 013caa28bc
commit f9fd1b142a
3 files changed
+19 -43

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Set time zone with timedatectl."""
import subprocess
from plinth.actions import privileged
@privileged
def set_timezone(timezone: str):
"""Set time zone with timedatectl."""
command = ['timedatectl', 'set-timezone', timezone]
subprocess.run(command, stdout=subprocess.DEVNULL, check=True)
+6 -6
View File
@@ -1,7 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app for configuring date and time.
"""
"""FreedomBox app for configuring date and time."""
import logging
import pathlib
@@ -9,9 +7,9 @@ import pathlib
from django.contrib import messages
from django.utils.translation import gettext as _
from plinth import actions
from plinth.views import AppView
from . import privileged
from .forms import DateTimeForm
logger = logging.getLogger(__name__)
@@ -19,10 +17,12 @@ logger = logging.getLogger(__name__)
class DateTimeAppView(AppView):
"""Serve configuration page."""
form_class = DateTimeForm
app_id = 'datetime'
def get_initial(self):
"""Return the values to fill in the form."""
status = super().get_initial()
status['time_zone'] = self.get_current_time_zone()
return status
@@ -35,14 +35,14 @@ class DateTimeAppView(AppView):
return time_zone or 'none'
def form_valid(self, form):
"""Change the timezone."""
old_status = form.initial
new_status = form.cleaned_data
if old_status['time_zone'] != new_status['time_zone'] and \
new_status['time_zone'] != 'none':
try:
actions.superuser_run('timezone-change',
[new_status['time_zone']])
privileged.set_timezone(new_status['time_zone'])
except Exception as exception:
messages.error(
self.request,