mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
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:
parent
da3e92312c
commit
91eaf3301d
@ -25,6 +25,6 @@ if [ -e "$tzpath" ] ; then
|
|||||||
echo "$zonename" > /etc/timezone
|
echo "$zonename" > /etc/timezone
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
echo "timezone not valid" 1>&2
|
echo "Time zone not valid" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Plinth module for configuring timezone, hostname etc.
|
Plinth module for configuring hostname and domainname.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
@ -24,9 +24,7 @@ from django.contrib import messages
|
|||||||
from django.core import validators
|
from django.core import validators
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
import glob
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from plinth import actions
|
from plinth import actions
|
||||||
@ -61,11 +59,6 @@ class TrimmedCharField(forms.CharField):
|
|||||||
|
|
||||||
class ConfigurationForm(forms.Form):
|
class ConfigurationForm(forms.Form):
|
||||||
"""Main system configuration 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
|
# We're more conservative than RFC 952 and RFC 1123
|
||||||
hostname = TrimmedCharField(
|
hostname = TrimmedCharField(
|
||||||
label=_('Hostname'),
|
label=_('Hostname'),
|
||||||
@ -86,41 +79,6 @@ separated by dots.'),
|
|||||||
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$',
|
validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$',
|
||||||
_('Invalid domain name'))])
|
_('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():
|
def init():
|
||||||
"""Initialize the module"""
|
"""Initialize the module"""
|
||||||
@ -154,14 +112,7 @@ def index(request):
|
|||||||
def get_status():
|
def get_status():
|
||||||
"""Return the current status"""
|
"""Return the current status"""
|
||||||
return {'hostname': get_hostname(),
|
return {'hostname': get_hostname(),
|
||||||
'domainname': get_domainname(),
|
'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'
|
|
||||||
|
|
||||||
|
|
||||||
def _apply_changes(request, old_status, new_status):
|
def _apply_changes(request, old_status, new_status):
|
||||||
@ -188,18 +139,6 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
else:
|
else:
|
||||||
messages.info(request, _('Domain name is unchanged'))
|
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):
|
def set_hostname(hostname):
|
||||||
"""Sets machine hostname to hostname"""
|
"""Sets machine hostname to hostname"""
|
||||||
|
|||||||
@ -21,6 +21,8 @@ Forms for configuring date and time
|
|||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
import glob
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
class DateTimeForm(forms.Form):
|
class DateTimeForm(forms.Form):
|
||||||
@ -28,3 +30,43 @@ class DateTimeForm(forms.Form):
|
|||||||
enabled = forms.BooleanField(
|
enabled = forms.BooleanField(
|
||||||
label=_('Enable network time'),
|
label=_('Enable network time'),
|
||||||
required=False)
|
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
|
||||||
|
|||||||
@ -63,7 +63,14 @@ def index(request):
|
|||||||
def get_status():
|
def get_status():
|
||||||
"""Get the current settings from server."""
|
"""Get the current settings from server."""
|
||||||
return {'enabled': datetime.is_enabled(),
|
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):
|
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']:
|
if old_status['enabled'] != new_status['enabled']:
|
||||||
sub_command = 'enable' if new_status['enabled'] else 'disable'
|
sub_command = 'enable' if new_status['enabled'] else 'disable'
|
||||||
|
modified = True
|
||||||
actions.superuser_run('datetime', [sub_command])
|
actions.superuser_run('datetime', [sub_command])
|
||||||
datetime.service.notify_enabled(None, new_status['enabled'])
|
datetime.service.notify_enabled(None, new_status['enabled'])
|
||||||
modified = True
|
|
||||||
|
|
||||||
if modified:
|
|
||||||
messages.success(request, _('Configuration updated'))
|
messages.success(request, _('Configuration updated'))
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
|
messages.success(request, _('Time zone set'))
|
||||||
|
|
||||||
|
if not modified:
|
||||||
messages.info(request, _('Setting unchanged'))
|
messages.info(request, _('Setting unchanged'))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user