mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
Convert packages page to Django forms
This commit is contained in:
parent
e5d43a87a2
commit
2500e37e89
@ -1,83 +1,132 @@
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from gettext import gettext as _
|
||||
from auth import require
|
||||
from plugin_mount import PagePlugin, FormPlugin
|
||||
from forms import Form
|
||||
from plugin_mount import PagePlugin
|
||||
import actions
|
||||
import cfg
|
||||
import util
|
||||
|
||||
class Packages(PagePlugin, FormPlugin):
|
||||
|
||||
def get_modules_available():
|
||||
"""Return list of all modules"""
|
||||
output, error = actions.run('module-manager', ['list-available'])
|
||||
if error:
|
||||
raise Exception('Error getting modules: %s' % error)
|
||||
|
||||
return output.split()
|
||||
|
||||
|
||||
def get_modules_enabled():
|
||||
"""Return list of all modules"""
|
||||
output, error = actions.run('module-manager', ['list-enabled'])
|
||||
if error:
|
||||
raise Exception('Error getting enabled modules - %s' % error)
|
||||
|
||||
return output.split()
|
||||
|
||||
|
||||
class PackagesForm(forms.Form):
|
||||
"""Packages form"""
|
||||
# XXX: Only present due to issue with submitting empty form
|
||||
dummy = forms.CharField(label='Dummy', initial='dummy',
|
||||
widget=forms.HiddenInput())
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# pylint: disable-msg=E1002, E1101
|
||||
super(forms.Form, self).__init__(*args, **kwargs)
|
||||
|
||||
modules_available = get_modules_available()
|
||||
|
||||
for module in modules_available:
|
||||
label = _('Enable {module}').format(module=module)
|
||||
self.fields[module + '_enabled'] = forms.BooleanField(label=label)
|
||||
|
||||
|
||||
class Packages(PagePlugin):
|
||||
"""Package page"""
|
||||
order = 20
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
PagePlugin.__init__(self, *args, **kwargs)
|
||||
self.register_page("sys.packages")
|
||||
cfg.html_root.sys.menu.add_item("Package Manager", "icon-gift", "/sys/packages", 20)
|
||||
self.register_page('sys.packages')
|
||||
|
||||
cfg.html_root.sys.menu.add_item('Package Manager', 'icon-gift',
|
||||
'/sys/packages', 20)
|
||||
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, *args, **kwargs):
|
||||
output, error = actions.run("module-manager", ["list-available"])
|
||||
if error:
|
||||
raise Exception("something is wrong: " + error)
|
||||
modules_available = output.split()
|
||||
|
||||
output, error = actions.run("module-manager", \
|
||||
["list-enabled", cfg.python_root])
|
||||
if error:
|
||||
raise Exception("something is wrong: " + error)
|
||||
modules_enabled = output.split()
|
||||
"""Serve the form"""
|
||||
del args # Unused
|
||||
|
||||
if 'submitted' in kwargs:
|
||||
del kwargs['submitted']
|
||||
modules_selected = map(lambda x: x.split("_")[0], kwargs.keys())
|
||||
for module in modules_available:
|
||||
if module in modules_enabled:
|
||||
if module not in modules_selected:
|
||||
output, error = actions.superuser_run(\
|
||||
"module-manager",\
|
||||
["disable", cfg.python_root, module])
|
||||
# TODO: need a smoother way for plinth
|
||||
# to unload the module
|
||||
status = self.get_status()
|
||||
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
if kwargs:
|
||||
form = PackagesForm(kwargs, prefix='packages')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = PackagesForm(initial=status, prefix='packages')
|
||||
else:
|
||||
form = PackagesForm(initial=status, prefix='packages')
|
||||
|
||||
return util.render_template(template='packages',
|
||||
title=_('Add/Remove Plugins'),
|
||||
form=form, messages=messages)
|
||||
|
||||
@staticmethod
|
||||
def get_status():
|
||||
"""Return the current status"""
|
||||
modules_available = get_modules_available()
|
||||
modules_enabled = get_modules_enabled()
|
||||
|
||||
return {module + '_enabled': module in modules_enabled
|
||||
for module in modules_available}
|
||||
|
||||
@staticmethod
|
||||
def _apply_changes(old_status, new_status, messages):
|
||||
"""Apply form changes"""
|
||||
for field, enabled in new_status.items():
|
||||
if not field.endswith('_enabled'):
|
||||
continue
|
||||
|
||||
if old_status[field] == new_status[field]:
|
||||
continue
|
||||
|
||||
module = field.split('_enabled')[0]
|
||||
if enabled:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['enable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
# TODO: need to get plinth to load the module we just
|
||||
# enabled
|
||||
if error:
|
||||
messages.append(
|
||||
('error', _('Error enabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
if module in modules_selected:
|
||||
output, error = actions.superuser_run(\
|
||||
"module-manager",\
|
||||
["enable", cfg.python_root, module])
|
||||
# TODO: need to get plinth to load
|
||||
# the module we just enabled
|
||||
messages.append(
|
||||
('success', _('Module enabled - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
output, error = actions.superuser_run(
|
||||
'module-manager', ['disable', cfg.python_root, module])
|
||||
del output # Unused
|
||||
|
||||
main = _("""
|
||||
<p>aptitude purge modules</p>
|
||||
<p>aptitude install modules</p>
|
||||
<p>The modules should depend on the appropriate Debian packages.</p>""")
|
||||
main += self.form(self, util.Message(output), args, kwargs)
|
||||
sidebar_right = _("""
|
||||
<strong>Help</strong>
|
||||
<p>On this page, you can add or remove %s plugins to your %s.</p>
|
||||
<p>Plugins are just Debian packages, so Debian's usual package management features should make this job fairly easy.</p>""" % (cfg.product_name, cfg.box_name))
|
||||
return util.render_template(title=_("Add/Remove Plugins"), main=main, sidebar_right=sidebar_right)
|
||||
|
||||
def form(self, message=None, *args, **kwargs):
|
||||
output, error = actions.run("module-manager", ["list-available"])
|
||||
if error:
|
||||
raise Exception("something is wrong: " + error)
|
||||
modules_available = output.split()
|
||||
|
||||
output, error = actions.run("module-manager",\
|
||||
["list-enabled", cfg.python_root])
|
||||
if error:
|
||||
raise Exception("something is wrong: " + error)
|
||||
modules_enabled = output.split()
|
||||
|
||||
form = Form(title="Manage Plugins",
|
||||
action=cfg.server_dir + "/sys/packages/index",
|
||||
name="manage_modules",
|
||||
message=message)
|
||||
for module in modules_available:
|
||||
form.checkbox(_("Enable %s" % module), name="%s_enable" % module, id="%s_enable" % module, checked = True if module in modules_enabled else False)
|
||||
form.hidden(name="submitted", value="True")
|
||||
form.html(_("""<p>Enabling a plugin will cause a corresponding page to appear in Plinth.</p>"""))
|
||||
form.submit(_("Update setup"))
|
||||
return form.render()
|
||||
# TODO: need a smoother way for plinth to unload the
|
||||
# module
|
||||
if error:
|
||||
messages.append(
|
||||
('error',
|
||||
_('Error disabling module - {module}').format(
|
||||
module=module)))
|
||||
else:
|
||||
messages.append(
|
||||
('success', _('Module disabled - {module}').format(
|
||||
module=module)))
|
||||
|
||||
65
modules/installed/system/templates/packages.html
Normal file
65
modules/installed/system/templates/packages.html
Normal file
@ -0,0 +1,65 @@
|
||||
{% extends "login_nav.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load bootstrap %}
|
||||
|
||||
{% block main_block %}
|
||||
|
||||
{% for severity, message in messages %}
|
||||
<div class='alert alert-{{ severity }}'>{{ message }}</div>
|
||||
{% endfor %}
|
||||
|
||||
<p>aptitude purge modules</p>
|
||||
|
||||
<p>aptitude install modules</p>
|
||||
|
||||
<p>The modules should depend on the appropriate Debian packages.</p>
|
||||
|
||||
<h2>Manage Plugins</h2>
|
||||
|
||||
<form class="form" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{{ form|bootstrap }}
|
||||
|
||||
<p>Enabling a plugin will cause a corresponding page to appear in
|
||||
Plinth.</p>
|
||||
|
||||
<input type="submit" class="btn-primary" value="Update setup"/>
|
||||
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block sidebar_right_block %}
|
||||
|
||||
<div class="well sidebar-nav">
|
||||
|
||||
<h3>Help</h3>
|
||||
|
||||
<p>On this page, you can add or remove {{ cfg.product_name }}
|
||||
plugins to your {{ cfg.box_name }}.</p>
|
||||
|
||||
<p>Plugins are just Debian packages, so Debian's usual package
|
||||
management features should make this job fairly easy.</p>
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
Loading…
x
Reference in New Issue
Block a user