mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
Convert packages page to Django forms
This commit is contained in:
parent
e5d43a87a2
commit
2500e37e89
@ -1,83 +1,132 @@
|
|||||||
import cherrypy
|
import cherrypy
|
||||||
|
from django import forms
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
from auth import require
|
from auth import require
|
||||||
from plugin_mount import PagePlugin, FormPlugin
|
from plugin_mount import PagePlugin
|
||||||
from forms import Form
|
|
||||||
import actions
|
import actions
|
||||||
import cfg
|
import cfg
|
||||||
import util
|
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
|
order = 20
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
PagePlugin.__init__(self, *args, **kwargs)
|
PagePlugin.__init__(self, *args, **kwargs)
|
||||||
self.register_page("sys.packages")
|
self.register_page('sys.packages')
|
||||||
cfg.html_root.sys.menu.add_item("Package Manager", "icon-gift", "/sys/packages", 20)
|
|
||||||
|
cfg.html_root.sys.menu.add_item('Package Manager', 'icon-gift',
|
||||||
|
'/sys/packages', 20)
|
||||||
|
|
||||||
@cherrypy.expose
|
@cherrypy.expose
|
||||||
@require()
|
@require()
|
||||||
def index(self, *args, **kwargs):
|
def index(self, *args, **kwargs):
|
||||||
output, error = actions.run("module-manager", ["list-available"])
|
"""Serve the form"""
|
||||||
if error:
|
del args # Unused
|
||||||
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()
|
|
||||||
|
|
||||||
if 'submitted' in kwargs:
|
status = self.get_status()
|
||||||
del kwargs['submitted']
|
|
||||||
modules_selected = map(lambda x: x.split("_")[0], kwargs.keys())
|
form = None
|
||||||
for module in modules_available:
|
messages = []
|
||||||
if module in modules_enabled:
|
|
||||||
if module not in modules_selected:
|
if kwargs:
|
||||||
output, error = actions.superuser_run(\
|
form = PackagesForm(kwargs, prefix='packages')
|
||||||
"module-manager",\
|
# pylint: disable-msg=E1101
|
||||||
["disable", cfg.python_root, module])
|
if form.is_valid():
|
||||||
# TODO: need a smoother way for plinth
|
self._apply_changes(status, form.cleaned_data, messages)
|
||||||
# to unload the module
|
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:
|
else:
|
||||||
if module in modules_selected:
|
messages.append(
|
||||||
output, error = actions.superuser_run(\
|
('success', _('Module enabled - {module}').format(
|
||||||
"module-manager",\
|
module=module)))
|
||||||
["enable", cfg.python_root, module])
|
else:
|
||||||
# TODO: need to get plinth to load
|
output, error = actions.superuser_run(
|
||||||
# the module we just enabled
|
'module-manager', ['disable', cfg.python_root, module])
|
||||||
|
del output # Unused
|
||||||
|
|
||||||
main = _("""
|
# TODO: need a smoother way for plinth to unload the
|
||||||
<p>aptitude purge modules</p>
|
# module
|
||||||
<p>aptitude install modules</p>
|
if error:
|
||||||
<p>The modules should depend on the appropriate Debian packages.</p>""")
|
messages.append(
|
||||||
main += self.form(self, util.Message(output), args, kwargs)
|
('error',
|
||||||
sidebar_right = _("""
|
_('Error disabling module - {module}').format(
|
||||||
<strong>Help</strong>
|
module=module)))
|
||||||
<p>On this page, you can add or remove %s plugins to your %s.</p>
|
else:
|
||||||
<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))
|
messages.append(
|
||||||
return util.render_template(title=_("Add/Remove Plugins"), main=main, sidebar_right=sidebar_right)
|
('success', _('Module disabled - {module}').format(
|
||||||
|
module=module)))
|
||||||
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()
|
|
||||||
|
|||||||
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