From f9fd1b142a2ce9bb1fe8fde39bd349061918d077 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 29 Aug 2022 11:18:18 -0700 Subject: [PATCH] datetime: Use privileged decorator for actions Tests: - Setting timezone shows: - In the interface and - timedatectl Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- actions/timezone-change | 37 --------------------------- plinth/modules/datetime/privileged.py | 13 ++++++++++ plinth/modules/datetime/views.py | 12 ++++----- 3 files changed, 19 insertions(+), 43 deletions(-) delete mode 100755 actions/timezone-change create mode 100644 plinth/modules/datetime/privileged.py diff --git a/actions/timezone-change b/actions/timezone-change deleted file mode 100755 index 8ba152975..000000000 --- a/actions/timezone-change +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/python3 -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Set time zones with timedatectl (requires root permission). -""" - -import argparse -import subprocess -import sys - - -def parse_arguments(): - """Return parsed command line arguments as dictionary.""" - parser = argparse.ArgumentParser() - parser.add_argument( - 'timezone', help='Time zone to set; see "timedatectl list-timezones".') - return parser.parse_args() - - -def _set_timezone(arguments): - """Set time zone with timedatectl.""" - try: - command = ['timedatectl', 'set-timezone', arguments.timezone] - subprocess.run(command, stdout=subprocess.DEVNULL, check=True) - except subprocess.CalledProcessError as exception: - print('Error setting timezone:', exception, file=sys.stderr) - sys.exit(1) - - -def main(): - """Parse arguments and perform all duties.""" - arguments = parse_arguments() - _set_timezone(arguments) - - -if __name__ == '__main__': - main() diff --git a/plinth/modules/datetime/privileged.py b/plinth/modules/datetime/privileged.py new file mode 100644 index 000000000..45790405e --- /dev/null +++ b/plinth/modules/datetime/privileged.py @@ -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) diff --git a/plinth/modules/datetime/views.py b/plinth/modules/datetime/views.py index d92848245..68c228e9e 100644 --- a/plinth/modules/datetime/views.py +++ b/plinth/modules/datetime/views.py @@ -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,