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 2022-08-29 11:18:18 -07:00 committed by James Valleroy
parent 013caa28bc
commit f9fd1b142a
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 19 additions and 43 deletions

View File

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

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)

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,