mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-28 08:03:36 +00:00
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:
parent
013caa28bc
commit
f9fd1b142a
@ -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()
|
||||
13
plinth/modules/datetime/privileged.py
Normal file
13
plinth/modules/datetime/privileged.py
Normal 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)
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user