Commit 0c9ce0fe authored by Jan Čermák's avatar Jan Čermák

skeleton of configuration interface (based on wizard code) - refs #2813

parent de775471
from bottle import Bottle, request, template
import bottle
import logging
from utils import login_required
import wizard
logger = logging.getLogger("admin")
app = Bottle()
handler_map = {
'password': wizard.WizardStep1,
'wan': wizard.WizardStep2,
'lan': wizard.WizardStep5,
'wifi': wizard.WizardStep6,
}
def get_handler(handler_name):
Handler = handler_map.get(handler_name)
if Handler is None:
raise bottle.HTTPError(404, "Unknown configuration page.")
return Handler
def render_form(form, **kwargs):
# same premise as in wizard form - we are handling single-section ForisForm
first_section = form.sections[0]
title = first_section.title
description = first_section.description
return template("config/main", form=form, title=title, description=description, **kwargs)
@app.route("/<handler_name:re:.+>/")
@login_required
def config_get(handler_name):
Handler = get_handler(handler_name)
handler = Handler()
form = handler.form
return render_form(form)
@app.route("/<handler_name:re:.+>/", method="POST")
@login_required
def config_post(handler_name):
Handler = get_handler(handler_name)
handler = Handler(request.POST)
if request.is_xhr:
# only update is allowed
logger.debug("ajax request")
request.POST.pop("update", None)
return dict(html=render_form(handler.form, is_xhr=True))
try:
if handler.save():
logger.info("saved")
except TypeError:
# raised by Validator - could happen when the form is posted with wrong fields
pass
return render_form(handler.form)
@app.route("/<handler_name:re:.+>/ajax")
@login_required
def config_ajax(handler_name):
action = request.GET.get("action")
if not action:
raise bottle.HTTPError(404, "AJAX action not specified.")
Handler = get_handler(handler_name)
handler = Handler()
try:
result = handler.call_action(action)
return result
except ValueError:
raise bottle.HTTPError(404, "Unknown action.")
\ No newline at end of file
......@@ -4,6 +4,7 @@ import bottle
from bottle_i18n import I18NMiddleware, I18NPlugin, i18n_defaults
import gettext
import logging
import config
from nuci import client, filters
import os
import sys
......@@ -88,6 +89,7 @@ if __name__ == "__main__":
bottle.TEMPLATE_PATH.append(template_dir)
logging.basicConfig(level=logging.DEBUG if args.debug else logging.WARNING)
app = bottle.app()
app.mount("/config", config.app)
app.mount("/wizard", wizard.app)
if args.debug:
# "about:config" is available only in debug mode
......
%if not defined('is_xhr'):
%rebase _layout **locals()
<div id="admin-content">
%end
%include
%if not defined('is_xhr'):
</div>
%end
\ No newline at end of file
%rebase config/base **locals()
<form id="wizard-main-form" class="wizard-form" action="" method="post" autocomplete="off">
<h1>{{ title }}</h1>
<p class="wizard-description">{{ description }}</p>
%for field in form.active_fields:
<div>
{{! field.label_tag }}
{{! field.render() }}
%if field.hint:
<img class="field-hint" src="/static/img/icon-help.png" title="{{ field.hint }}">
%end
</div>
%end
<button type="submit" name="send" class="button">{{ _("Save") }}</button>
</form>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment