datetime: Move time zone to date time module

- It is more appropriate to have the time zone setting in date time
  module.
This commit is contained in:
Sunil Mohan Adapa committed 2015-09-04 19:53:46 +05:30
1 parent da3e92312c
commit 91eaf3301d
4 files changed
+67 -69

No files matched your search

+42
View File
@@ -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
+22 -5
View File
@@ -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'))