From 5461694b10c7f9706501290728e5213fbd38bbed Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 11 May 2014 19:43:55 +0530 Subject: [PATCH] Convert router pages to Django forms --- modules/installed/router/router.py | 211 +++++++++--------- .../router/templates/router_setup.html | 114 ++++++++++ 2 files changed, 218 insertions(+), 107 deletions(-) create mode 100644 modules/installed/router/templates/router_setup.html diff --git a/modules/installed/router/router.py b/modules/installed/router/router.py index d17496ae3..2cc75d980 100644 --- a/modules/installed/router/router.py +++ b/modules/installed/router/router.py @@ -1,153 +1,150 @@ import cherrypy -from plugin_mount import PagePlugin, FormPlugin +from django import forms +from gettext import gettext as _ +from plugin_mount import PagePlugin from modules.auth import require -from forms import Form import cfg import util -class router(PagePlugin): - order = 9 # order of running init in PagePlugins - def __init__(self, *args, **kwargs): - self.register_page("router") - self.menu = cfg.main_menu.add_item("Router", "icon-retweet", "/router", 10) - self.menu.add_item("Wireless", "icon-signal", "/router/wireless", 12) - self.menu.add_item("Firewall", "icon-fire", "/router/firewall", 18) - self.menu.add_item("Hotspot and Mesh", "icon-map-marker", "/router/hotspot") - self.menu.add_item("Info", "icon-list-alt", "/router/info", 100) +class Router(PagePlugin): + """Router page""" + order = 9 # order of running init in PagePlugins + + def __init__(self, *args, **kwargs): + PagePlugin.__init__(self, args, kwargs) + + self.register_page('router') + + self.menu = cfg.main_menu.add_item('Router', 'icon-retweet', '/router', + 10) + self.menu.add_item('Wireless', 'icon-signal', '/router/wireless', 12) + self.menu.add_item('Firewall', 'icon-fire', '/router/firewall', 18) + self.menu.add_item('Hotspot and Mesh', 'icon-map-marker', + '/router/hotspot') + self.menu.add_item('Info', 'icon-list-alt', '/router/info', 100) + + @staticmethod @cherrypy.expose - def index(self): + def index(): """This isn't an internal redirect, because we need the url to reflect that we've moved down into the submenu hierarchy. Otherwise, it's hard to know which menu portion to make active or expand or contract.""" raise cherrypy.HTTPRedirect(cfg.server_dir + '/router/setup') + @staticmethod @cherrypy.expose @require() - def wireless(self): + def wireless(): + """Serve the wireless page""" return util.render_template(title="Wireless", main="

wireless setup: essid, etc.

") + @staticmethod @cherrypy.expose @require() - def firewall(self): + def firewall(): + """Serve the firewall page""" return util.render_template(title="Firewall", main="

Iptables twiddling.

") + @staticmethod @cherrypy.expose @require() - def hotspot(self): + def hotspot(): + """Serve the hotspot page""" return util.render_template(title="Hotspot and Mesh", main="

connection sharing setup.

") -class setup(PagePlugin): +class WANForm(forms.Form): # pylint: disable-msg=W0232 + """WAN setup form""" + connection_type = forms.ChoiceField( + label=_('Connection Type'), + choices=[('dhcp', _('DHCP')), ('static_ip', _('Static IP'))]) + + wan_ip = forms.IPAddressField(label=_('WAN IP Address'), required=False) + + subnet_mask = forms.IPAddressField(label=_('Subnet Mask'), required=False) + + dns_1 = forms.IPAddressField(label=_('Static DNS 1'), required=False) + + dns_2 = forms.IPAddressField(label=_('Static DNS 2'), required=False) + + dns_3 = forms.IPAddressField(label=_('Static DNS 3'), required=False) + + +class Setup(PagePlugin): + """Router setup page""" def __init__(self, *args, **kwargs): - self.register_page("router.setup") - self.menu = cfg.html_root.router.menu.add_item("General Setup", "icon-cog", "/router/setup", 10) - self.menu.add_item("Dynamic DNS", "icon-flag", "/router/setup/ddns", 20) - self.menu.add_item("MAC Address Clone", "icon-barcode", "/router/setup/mac_address", 30) + PagePlugin.__init__(self, args, kwargs) + + self.register_page('router.setup') + + self.menu = cfg.html_root.router.menu.add_item( + 'General Setup', 'icon-cog', '/router/setup', 10) + self.menu.add_item('Dynamic DNS', 'icon-flag', '/router/setup/ddns', + 20) + self.menu.add_item('MAC Address Clone', 'icon-barcode', + '/router/setup/mac_address', 30) @cherrypy.expose @require() - def index(self): - parts = self.forms('/router/setup') - parts['title'] = "General Router Setup" - parts['sidebar_right']="""Introduction

Your %s is a replacement for your -wireless router. By default, it should do everything your usual -router does. With the addition of some extra modules, its abilities -can rival those of high-end routers costing hundreds of dollars.

-""" % cfg.box_name + parts['sidebar_right'] - if not cfg.users.expert(): - parts['main'] += """

In basic mode, you don't need to do any - router setup before you can go online. Just plug your - %(product)s in to your cable or DSL modem and the router - will try to get you on the internet using DHCP.

+ def index(self, **kwargs): + """Return the setup page""" + status = self.get_status() -

If that fails, you might need to resort to the expert - options. Enable expert mode in the "%(product)s System / - Configure" menu.

""" % {'product':cfg.box_name} + form = None + messages = [] + + if kwargs: + form = WANForm(kwargs, prefix='router') + # pylint: disable-msg=E1101 + if form.is_valid(): + self._apply_changes(status, form.cleaned_data, messages) + status = self.get_status() + form = WANForm(initial=status, prefix='router') else: - parts['main'] += "

router name, domain name, router IP, dhcp

" - return util.render_template(**parts) + form = WANForm(initial=status, prefix='router') + return util.render_template(template='router_setup', + title=_('General Router Setup'), + form=form, messages=messages) + + @staticmethod @cherrypy.expose @require() - def ddns(self): - return util.render_template(title="Dynamic DNS", main="

Masquerade setup

") + def ddns(): + """Return the DDNS page""" + return util.render_template(title="Dynamic DNS", + main="

Masquerade setup

") + @staticmethod @cherrypy.expose @require() - def mac_address(self): - return util.render_template(title="MAC Address Cloning", - main="

Your router can pretend to have a different MAC address on any interface.

") + def mac_address(): + """Return the MAC address page""" + return util.render_template( + title="MAC Address Cloning", + main="

Your router can pretend to have a different MAC address \ +on any interface.

") + @staticmethod + def get_status(): + """Return the current status""" + store = util.filedict_con(cfg.store_file, 'router') + return {'connection_type': store.get('connection_type', 'dhcp')} -class wan(FormPlugin, PagePlugin): - url = ["/router/setup"] - order = 10 - - js = """ -""" - - def sidebar_right(self, *args, **kwargs): - side='' - if cfg.users.expert(): - side += """WAN Connection Type -

DHCP

DHCP allows your router to automatically - connect with the upstream network. If you are unsure what - option to choose, stick with DHCP. It is usually - correct. - -

Static IP

If you want to setup your connection - manually, you can enter static IP information. This option is - for those who know what they're doing. As such, it is only - available in expert mode.

""" - return side - - def main(self, wan_ip0=0, wan_ip1=0, wan_ip2=0, wan_ip3=0, - subnet0=0, subnet1=0, subnet2=0, subnet3=0, - gateway0=0, gateway1=0, gateway2=0, gateway3=0, - dns10=0, dns11=0, dns12=0, dns13=0, - dns20=0, dns21=0, dns22=0, dns23=0, - dns30=0, dns31=0, dns32=0, dns33=0, - message=None, **kwargs): - if not cfg.users.expert(): - return '' + @staticmethod + def _apply_changes(old_status, new_status, messages): + """Apply the changes""" + print 'Apply changes - %s, %s', old_status, new_status + if old_status['connection_type'] == new_status['connection_type']: + return store = util.filedict_con(cfg.store_file, 'router') - defaults = {'connect_type': 'DHCP'} - for key, value in defaults.items(): - if not key in kwargs: - try: - kwargs[key] = store[key] - except KeyError: - store[key] = kwargs[key] = value + store['connection_type'] = new_status['connection_type'] - form = Form(title="WAN Connection", - action=cfg.server_dir + "/router/setup/wan/index", - name="wan_connection_form", - message=message) - form.dropdown('Connection Type', vals=["DHCP", "Static IP"], id="connect_type") - form.html('
') - form.dotted_quad("WAN IP Address", name="wan_ip", quad=[wan_ip0, wan_ip1, wan_ip2, wan_ip3]) - form.dotted_quad("Subnet Mask", name="subnet", quad=[subnet0, subnet1, subnet2, subnet3]) - form.dotted_quad("Gateway", name="gateway", quad=[gateway0, gateway1, gateway2, gateway3]) - form.dotted_quad("Static DNS 1", name="dns1", quad=[dns10, dns11, dns12, dns13]) - form.dotted_quad("Static DNS 2", name="dns2", quad=[dns20, dns21, dns22, dns23]) - form.dotted_quad("Static DNS 3", name="dns3", quad=[dns30, dns31, dns32, dns33]) - form.html('
') - form.submit("Set Wan") - return form.render() + messages.append(('success', _('Connection type set'))) + messages.append(('info', _('IP address settings unimplemented'))) diff --git a/modules/installed/router/templates/router_setup.html b/modules/installed/router/templates/router_setup.html new file mode 100644 index 000000000..5036ad6a6 --- /dev/null +++ b/modules/installed/router/templates/router_setup.html @@ -0,0 +1,114 @@ +{% 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 . +# +{% endcomment %} + +{% block main_block %} + +{% if cfg.users.expert %} + +

WAN Connection

+ + {% for severity, message in messages %} +
{{ message }}
+ {% endfor %} + +
+ {% csrf_token %} + + {% include 'bootstrapform/field.html' with field=form.connection_type %} + +
+ {% include 'bootstrapform/field.html' with field=form.wan_ip %} + {% include 'bootstrapform/field.html' with field=form.subnet_mask %} + {% include 'bootstrapform/field.html' with field=form.dns_1 %} + {% include 'bootstrapform/field.html' with field=form.dns_2 %} + {% include 'bootstrapform/field.html' with field=form.dns_3 %} +
+ + + +
+ +{% else %} + +

In basic mode, you don't need to do any router setup before you + can go online. Just plug your {{ cfg.product_name }} in to your + cable or DSL modem and the router will try to get you on the + internet using DHCP.

+ +

If that fails, you might need to resort to the expert options. + Enable expert mode in the "{{ cfg.product_name }} / System / + Configure" menu.

+ +{% endif %} + +{% endblock %} + +{% block sidebar_right_block %} + +

Introduction

+ +

Your {{ cfg.box_name }} is a replacement for your wireless + router. By default, it should do everything your usual router + does. With the addition of some extra modules, its abilities can + rival those of high-end routers costing hundreds of dollars.

+ + {% if cfg.users.expert %} + +

WAN Connection Type

+ +

DHCP

+ +

DHCP allows your router to automatically connect with the + upstream network. If you are unsure what option to choose, + stick with DHCP. It is usually correct.

+ +

Static IP

+ +

If you want to setup your connection manually, you can enter + static IP information. This option is for those who know what + they're doing. As such, it is only available in expert + mode.

+ + {% endif %} + +{% endblock %} + +{% block js_block %} + {{ js|safe }} + + + +{% endblock %}