mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-29 10:10:19 +00:00
datetime: Use common view for configuration
This commit is contained in:
parent
7385bebb89
commit
d31de69d36
@ -59,6 +59,19 @@ def setup(helper, old_version=None):
|
|||||||
helper.call('post', service.notify_enabled, None, True)
|
helper.call('post', service.notify_enabled, None, True)
|
||||||
|
|
||||||
|
|
||||||
|
def get_status():
|
||||||
|
"""Get the current settings from server."""
|
||||||
|
return {'enabled': is_enabled(),
|
||||||
|
'is_running': 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 is_enabled():
|
def is_enabled():
|
||||||
"""Return whether the module is enabled."""
|
"""Return whether the module is enabled."""
|
||||||
return action_utils.service_is_enabled('ntp')
|
return action_utils.service_is_enabled('ntp')
|
||||||
|
|||||||
@ -21,8 +21,10 @@ URLs for the date and time module
|
|||||||
|
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from . import views
|
from .views import ConfigurationView
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^sys/datetime/$', views.index, name='index'),
|
url(r'^sys/datetime/$', ConfigurationView.as_view(module_name='datetime'),
|
||||||
|
name='index'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -20,74 +20,43 @@ Plinth module for configuring date and time
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.template.response import TemplateResponse
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .forms import DateTimeForm
|
from .forms import DateTimeForm
|
||||||
from plinth import actions
|
from plinth import actions
|
||||||
|
from plinth import views
|
||||||
from plinth.modules import datetime
|
from plinth.modules import datetime
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
class ConfigurationView(views.ConfigurationView):
|
||||||
"""Serve configuration page."""
|
"""Serve configuration page."""
|
||||||
status = get_status()
|
form_class = DateTimeForm
|
||||||
|
|
||||||
form = None
|
def apply_changes(self, old_status, new_status):
|
||||||
|
"""Apply the changes."""
|
||||||
|
modified = False
|
||||||
|
|
||||||
if request.method == 'POST':
|
if old_status['enabled'] != new_status['enabled']:
|
||||||
form = DateTimeForm(request.POST, prefix='datetime')
|
sub_command = 'enable' if new_status['enabled'] else 'disable'
|
||||||
# pylint: disable=E1101
|
modified = True
|
||||||
if form.is_valid():
|
actions.superuser_run('datetime', [sub_command])
|
||||||
_apply_changes(request, status, form.cleaned_data)
|
datetime.service.notify_enabled(None, new_status['enabled'])
|
||||||
status = get_status()
|
messages.success(self.request, _('Configuration updated'))
|
||||||
form = DateTimeForm(initial=status, prefix='datetime')
|
|
||||||
else:
|
|
||||||
form = DateTimeForm(initial=status, prefix='datetime')
|
|
||||||
|
|
||||||
return TemplateResponse(request, 'datetime.html',
|
if old_status['time_zone'] != new_status['time_zone'] and \
|
||||||
{'title': datetime.title,
|
new_status['time_zone'] != 'none':
|
||||||
'description': datetime.description,
|
modified = True
|
||||||
'status': status,
|
try:
|
||||||
'form': form})
|
actions.superuser_run(
|
||||||
|
'timezone-change', [new_status['time_zone']])
|
||||||
|
except Exception as exception:
|
||||||
|
messages.error(
|
||||||
|
self.request, _('Error setting time zone: {exception}')
|
||||||
|
.format(exception=exception))
|
||||||
|
else:
|
||||||
|
messages.success(self.request, _('Time zone set'))
|
||||||
|
|
||||||
|
return modified
|
||||||
def get_status():
|
|
||||||
"""Get the current settings from server."""
|
|
||||||
return {'enabled': datetime.is_enabled(),
|
|
||||||
'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):
|
|
||||||
"""Apply the changes."""
|
|
||||||
modified = False
|
|
||||||
|
|
||||||
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'])
|
|
||||||
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: {exception}')
|
|
||||||
.format(exception=exception))
|
|
||||||
else:
|
|
||||||
messages.success(request, _('Time zone set'))
|
|
||||||
|
|
||||||
if not modified:
|
|
||||||
messages.info(request, _('Setting unchanged'))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user