mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-11 09:04:54 +00:00
transmission: Use common view for configuration
This commit is contained in:
parent
73c7d8e89e
commit
7385bebb89
@ -21,6 +21,7 @@ Plinth module to configure Transmission server
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import json
|
||||
import socket
|
||||
|
||||
from plinth import actions
|
||||
from plinth import action_utils
|
||||
@ -42,6 +43,8 @@ description = [
|
||||
|
||||
service = None
|
||||
|
||||
TRANSMISSION_CONFIG = '/etc/transmission-daemon/settings.json'
|
||||
|
||||
|
||||
def init():
|
||||
"""Intialize the Transmission module."""
|
||||
@ -67,6 +70,19 @@ def setup(helper, old_version=None):
|
||||
helper.call('post', service.notify_enabled, None, True)
|
||||
|
||||
|
||||
def get_status():
|
||||
"""Get the current settings from Transmission server."""
|
||||
configuration = open(TRANSMISSION_CONFIG, 'r').read()
|
||||
status = json.loads(configuration)
|
||||
status = {key.translate(str.maketrans({'-': '_'})): value
|
||||
for key, value in status.items()}
|
||||
status['enabled'] = is_enabled()
|
||||
status['is_running'] = is_running()
|
||||
status['hostname'] = socket.gethostname()
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def is_enabled():
|
||||
"""Return whether the module is enabled."""
|
||||
return (action_utils.service_is_enabled('transmission-daemon') and
|
||||
@ -78,6 +94,13 @@ def is_running():
|
||||
return action_utils.service_is_running('transmission-daemon')
|
||||
|
||||
|
||||
def enable(should_enable):
|
||||
"""Enable/disable the module."""
|
||||
sub_command = 'enable' if should_enable else 'disable'
|
||||
actions.superuser_run('transmission', [sub_command])
|
||||
service.notify_enabled(None, should_enable)
|
||||
|
||||
|
||||
def diagnose():
|
||||
"""Run diagnostics and return the results."""
|
||||
results = []
|
||||
|
||||
@ -22,13 +22,11 @@ Plinth module for configuring Transmission.
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from plinth.forms import ConfigurationForm
|
||||
|
||||
class TransmissionForm(forms.Form): # pylint: disable=W0232
|
||||
|
||||
class TransmissionForm(ConfigurationForm): # pylint: disable=W0232
|
||||
"""Transmission configuration form"""
|
||||
enabled = forms.BooleanField(
|
||||
label=_('Enable Transmission daemon'),
|
||||
required=False)
|
||||
|
||||
download_dir = forms.CharField(
|
||||
label=_('Download directory'),
|
||||
help_text=_('Directory where downloads are saved. If you change the \
|
||||
|
||||
@ -21,9 +21,10 @@ URLs for the Transmission module.
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import views
|
||||
from .views import ConfigurationView
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^apps/transmission/$', views.index, name='index'),
|
||||
url(r'^apps/transmission/$',
|
||||
ConfigurationView.as_view(module_name='transmission'), name='index'),
|
||||
]
|
||||
|
||||
@ -20,81 +20,37 @@ Plinth module for configuring Transmission Server
|
||||
"""
|
||||
|
||||
from django.contrib import messages
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.translation import ugettext as _
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
|
||||
from .forms import TransmissionForm
|
||||
from plinth import actions
|
||||
from plinth.modules import transmission
|
||||
from plinth import views
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TRANSMISSION_CONFIG = '/etc/transmission-daemon/settings.json'
|
||||
|
||||
|
||||
def index(request):
|
||||
class ConfigurationView(views.ConfigurationView):
|
||||
"""Serve configuration page."""
|
||||
status = get_status()
|
||||
form_class = TransmissionForm
|
||||
|
||||
form = None
|
||||
def apply_changes(self, old_status, new_status):
|
||||
"""Apply the changes submitted in the form."""
|
||||
modified = super().apply_changes(old_status, new_status)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = TransmissionForm(request.POST, prefix='transmission')
|
||||
# pylint: disable=E1101
|
||||
if form.is_valid():
|
||||
_apply_changes(request, status, form.cleaned_data)
|
||||
status = get_status()
|
||||
form = TransmissionForm(initial=status, prefix='transmission')
|
||||
else:
|
||||
form = TransmissionForm(initial=status, prefix='transmission')
|
||||
if old_status['download_dir'] != new_status['download_dir'] or \
|
||||
old_status['rpc_username'] != new_status['rpc_username'] or \
|
||||
old_status['rpc_password'] != new_status['rpc_password']:
|
||||
new_configuration = {
|
||||
'download-dir': new_status['download_dir'],
|
||||
'rpc-username': new_status['rpc_username'],
|
||||
'rpc-password': new_status['rpc_password'],
|
||||
}
|
||||
|
||||
return TemplateResponse(request, 'transmission.html',
|
||||
{'title': transmission.title,
|
||||
'description': transmission.description,
|
||||
'status': status,
|
||||
'form': form})
|
||||
actions.superuser_run('transmission', ['merge-configuration'],
|
||||
input=json.dumps(new_configuration).encode())
|
||||
messages.success(self.request, _('Configuration updated'))
|
||||
return True
|
||||
|
||||
|
||||
def get_status():
|
||||
"""Get the current settings from Transmission server."""
|
||||
configuration = open(TRANSMISSION_CONFIG, 'r').read()
|
||||
status = json.loads(configuration)
|
||||
status = {key.translate(str.maketrans({'-': '_'})): value
|
||||
for key, value in status.items()}
|
||||
status['enabled'] = transmission.is_enabled()
|
||||
status['is_running'] = transmission.is_running()
|
||||
status['hostname'] = socket.gethostname()
|
||||
|
||||
return status
|
||||
|
||||
|
||||
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('transmission', [sub_command])
|
||||
transmission.service.notify_enabled(None, new_status['enabled'])
|
||||
modified = True
|
||||
|
||||
if old_status['download_dir'] != new_status['download_dir'] or \
|
||||
old_status['rpc_username'] != new_status['rpc_username'] or \
|
||||
old_status['rpc_password'] != new_status['rpc_password']:
|
||||
new_configuration = {
|
||||
'download-dir': new_status['download_dir'],
|
||||
'rpc-username': new_status['rpc_username'],
|
||||
'rpc-password': new_status['rpc_password'],
|
||||
}
|
||||
|
||||
actions.superuser_run('transmission', ['merge-configuration'],
|
||||
input=json.dumps(new_configuration).encode())
|
||||
modified = True
|
||||
|
||||
if modified:
|
||||
messages.success(request, _('Configuration updated'))
|
||||
else:
|
||||
messages.info(request, _('Setting unchanged'))
|
||||
return modified
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user