mumble: Use common view for configuration

This commit is contained in:
Sunil Mohan Adapa 2016-02-28 15:06:11 +05:30
parent 07df62854b
commit 75d35b3d60
No known key found for this signature in database
GPG Key ID: 36C361440C9BC971
4 changed files with 17 additions and 108 deletions

View File

@ -21,6 +21,7 @@ Plinth module to configure Mumble server
from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import action_utils
from plinth import cfg
from plinth import service as service_module
@ -60,6 +61,12 @@ def setup(helper, old_version=None):
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()}
def is_enabled():
"""Return whether the module is enabled."""
return action_utils.service_is_enabled('mumble-server')
@ -70,6 +77,13 @@ def is_running():
return action_utils.service_is_running('mumble-server')
def enable(should_enable):
"""Enable/disable the module."""
sub_command = 'enable' if should_enable else 'disable'
actions.superuser_run('mumble', [sub_command])
service.notify_enabled(None, should_enable)
def diagnose():
"""Run diagnostics and return the results."""
results = []

View File

@ -1,30 +0,0 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Forms for configuring Mumble
"""
from django import forms
from django.utils.translation import ugettext_lazy as _
class MumbleForm(forms.Form):
"""Mumble configuration form."""
enabled = forms.BooleanField(
label=_('Enable Mumble daemon'),
required=False)

View File

@ -21,9 +21,10 @@ URLs for the Mumble module
from django.conf.urls import url
from . import views
from plinth.views import ConfigurationView
urlpatterns = [
url(r'^apps/mumble/$', views.index, name='index'),
url(r'^apps/mumble/$', ConfigurationView.as_view(module_name='mumble'),
name='index'),
]

View File

@ -1,76 +0,0 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Plinth module for configuring Mumble Server
"""
from django.contrib import messages
from django.template.response import TemplateResponse
from django.utils.translation import ugettext as _
import logging
from .forms import MumbleForm
from plinth import actions
from plinth.modules import mumble
logger = logging.getLogger(__name__)
def index(request):
"""Serve configuration page."""
status = get_status()
form = None
if request.method == 'POST':
form = MumbleForm(request.POST, prefix='mumble')
# pylint: disable=E1101
if form.is_valid():
_apply_changes(request, status, form.cleaned_data)
status = get_status()
form = MumbleForm(initial=status, prefix='mumble')
else:
form = MumbleForm(initial=status, prefix='mumble')
return TemplateResponse(request, 'mumble.html',
{'title': mumble.title,
'description': mumble.description,
'status': status,
'form': form})
def get_status():
"""Get the current settings from server."""
return {'enabled': mumble.is_enabled(),
'is_running': mumble.is_running()}
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'
actions.superuser_run('mumble', [sub_command])
mumble.service.notify_enabled(None, new_status['enabled'])
modified = True
if modified:
messages.success(request, _('Configuration updated'))
else:
messages.info(request, _('Setting unchanged'))