From 91eaf3301d268dbbb26d895f32ca0b751e4c3cf3 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 4 Sep 2015 19:53:46 +0530 Subject: [PATCH] datetime: Move time zone to date time module - It is more appropriate to have the time zone setting in date time module. --- actions/timezone-change | 2 +- plinth/modules/config/config.py | 65 +------------------------------- plinth/modules/datetime/forms.py | 42 +++++++++++++++++++++ plinth/modules/datetime/views.py | 27 ++++++++++--- 4 files changed, 67 insertions(+), 69 deletions(-) diff --git a/actions/timezone-change b/actions/timezone-change index 75cb3532f..e972561cf 100755 --- a/actions/timezone-change +++ b/actions/timezone-change @@ -25,6 +25,6 @@ if [ -e "$tzpath" ] ; then echo "$zonename" > /etc/timezone exit 0 else - echo "timezone not valid" 1>&2 + echo "Time zone not valid" 1>&2 exit 1 fi diff --git a/plinth/modules/config/config.py b/plinth/modules/config/config.py index f277fe588..eb1e5a7ff 100644 --- a/plinth/modules/config/config.py +++ b/plinth/modules/config/config.py @@ -16,7 +16,7 @@ # """ -Plinth module for configuring timezone, hostname etc. +Plinth module for configuring hostname and domainname. """ from django import forms @@ -24,9 +24,7 @@ from django.contrib import messages from django.core import validators from django.template.response import TemplateResponse from gettext import gettext as _ -import glob import logging -import re import socket from plinth import actions @@ -61,11 +59,6 @@ class TrimmedCharField(forms.CharField): class ConfigurationForm(forms.Form): """Main system configuration form""" - time_zone = forms.ChoiceField( - label=_('Time Zone'), - help_text=_('Set your time zone to get accurate timestamps. \ -This will set the systemwide time zone.')) - # We're more conservative than RFC 952 and RFC 1123 hostname = TrimmedCharField( label=_('Hostname'), @@ -86,41 +79,6 @@ separated by dots.'), validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$', _('Invalid domain name'))]) - def __init__(self, *args, **kwargs): - # pylint: disable-msg=E1101, W0233 - forms.Form.__init__(self, *args, **kwargs) - - timezone_options = [(zone, zone) - for zone in self.get_time_zones()] - # Show not-set option only when time zone is not set - if self.initial.get('time_zone') == 'none': - timezone_options.insert(0, ('none', _('-- no time zone set --'))) - - self.fields['time_zone'].choices = timezone_options - - @staticmethod - def get_time_zones(): - """Return list of available time zones""" - time_zones = [] - for line in open('/usr/share/zoneinfo/zone.tab'): - if re.match(r'^(#|\s*$)', line): - continue - - try: - time_zones.append(line.split()[2]) - except IndexError: - pass - - time_zones.sort() - - additional_time_zones = [ - path[len('/usr/share/zoneinfo/'):] - for path in glob.glob('/usr/share/zoneinfo/Etc/*')] - - # Add additional time zones at the top to make them more - # noticeable because people won't look for them - return additional_time_zones + time_zones - def init(): """Initialize the module""" @@ -154,14 +112,7 @@ def index(request): def get_status(): """Return the current status""" return {'hostname': get_hostname(), - 'domainname': get_domainname(), - 'time_zone': get_current_timezone()} - - -def get_current_timezone(): - """Get current timezone""" - timezone = open('/etc/timezone').read().rstrip() - return timezone or 'none' + 'domainname': get_domainname()} def _apply_changes(request, old_status, new_status): @@ -188,18 +139,6 @@ def _apply_changes(request, old_status, new_status): else: messages.info(request, _('Domain name is unchanged')) - 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']]) - except Exception as exception: - messages.error(request, _('Error setting time zone: %s') % - exception) - else: - messages.success(request, _('Time zone set')) - else: - messages.info(request, _('Time zone is unchanged')) - def set_hostname(hostname): """Sets machine hostname to hostname""" diff --git a/plinth/modules/datetime/forms.py b/plinth/modules/datetime/forms.py index 6fbbe93d7..a3b9dfe0c 100644 --- a/plinth/modules/datetime/forms.py +++ b/plinth/modules/datetime/forms.py @@ -21,6 +21,8 @@ Forms for configuring date and time from django import forms from gettext import gettext as _ +import glob +import re class DateTimeForm(forms.Form): @@ -28,3 +30,43 @@ class DateTimeForm(forms.Form): enabled = forms.BooleanField( label=_('Enable network time'), required=False) + + time_zone = forms.ChoiceField( + label=_('Time Zone'), + help_text=_('Set your time zone to get accurate timestamps. \ +This will set the systemwide time zone.')) + + def __init__(self, *args, **kwargs): + """Initialize the date/time form.""" + forms.Form.__init__(self, *args, **kwargs) + + time_zone_options = [(zone, zone) + for zone in self.get_time_zones()] + # Show not-set option only when time zone is not set + if self.initial.get('time_zone') == 'none': + time_zone_options.insert(0, ('none', _('-- no time zone set --'))) + + self.fields['time_zone'].choices = time_zone_options + + @staticmethod + def get_time_zones(): + """Return list of available time zones""" + time_zones = [] + for line in open('/usr/share/zoneinfo/zone.tab'): + if re.match(r'^(#|\s*$)', line): + continue + + try: + time_zones.append(line.split()[2]) + except IndexError: + pass + + time_zones.sort() + + additional_time_zones = [ + path[len('/usr/share/zoneinfo/'):] + for path in glob.glob('/usr/share/zoneinfo/Etc/*')] + + # Add additional time zones at the top to make them more + # noticeable because people won't look for them + return additional_time_zones + time_zones diff --git a/plinth/modules/datetime/views.py b/plinth/modules/datetime/views.py index 1f3727a14..de0b5c2bf 100644 --- a/plinth/modules/datetime/views.py +++ b/plinth/modules/datetime/views.py @@ -63,7 +63,14 @@ def index(request): def get_status(): """Get the current settings from server.""" return {'enabled': datetime.is_enabled(), - 'is_running': datetime.is_running()} + 'is_running': datetime.is_running(), + 'time_zone': get_current_time_zone()} + + +def get_current_time_zone(): + """Get current time zone.""" + time_zone = open('/etc/timezone').read().rstrip() + return time_zone or 'none' def _apply_changes(request, old_status, new_status): @@ -72,11 +79,21 @@ def _apply_changes(request, old_status, new_status): if old_status['enabled'] != new_status['enabled']: sub_command = 'enable' if new_status['enabled'] else 'disable' + modified = True actions.superuser_run('datetime', [sub_command]) datetime.service.notify_enabled(None, new_status['enabled']) - modified = True - - if modified: messages.success(request, _('Configuration updated')) - else: + + if old_status['time_zone'] != new_status['time_zone'] and \ + new_status['time_zone'] != 'none': + modified = True + try: + actions.superuser_run('timezone-change', [new_status['time_zone']]) + except Exception as exception: + messages.error(request, _('Error setting time zone: %s') % + exception) + else: + messages.success(request, _('Time zone set')) + + if not modified: messages.info(request, _('Setting unchanged'))