<form method="{{ form.method }}" action="{{ form.action }}">
{% raw xsrf_form_html() %}
+ {#
+ Add the form ID so that we can find which form has been submitted
+ #}
+ {% if form.id %}
+ <input type="hidden" name="form" value="{{ form.id }}">
+ {% end %}
+
{% module FormElements(form.elements) %}
<button type="submit" class="primary button">{{ form.submit_text or _("Submit") }}</button>
def get(self):
# Render the default view
self.render("default.html")
+
+ def post(self):
+ """
+ This is the default handler which will find the correct form
+ and execute it.
+ """
+ form_id = self.get_argument("form")
+
+ # Find the form
+ form = self.tabs.get_form(form_id)
+ if not form:
+ raise tornado.web.HTTPError(400, "Could not find form with ID '%s'" % form_id)
+
+ # Execute the form action
+ form.execute()
# #
###############################################################################
+import logging
+
from . import base
+log = logging.getLogger(__name__)
+
class Form(object):
- def __init__(self, handler, method="POST", action="",
+ def __init__(self, handler, id, method="POST", action="",
form=None, label=None, help=None, disabled=False):
self.handler = handler
+ self.id = id
self.method = method
self.action = action
self.form = form
return ret
+ def execute(self):
+ """
+ Executes the form action
+ """
+ log.debug("Executing form %s" % self)
+
+ # Export all form data
+ data = self.export()
+
+ # Log everything
+ for key in data:
+ log.debug(" %-24s : %s" % (key, data[key]))
+
+ # TODO handle callback
+
# Sub-Forms
def _add_subform(self, cls, *args, **kwargs):
- form = cls(self.handler, *args, form=self, **kwargs)
+ form = cls(self.handler, self.id, *args, form=self, **kwargs)
self.elements.append(form)
return form
# Return the new tab
return tab
+ def get_form(self, id):
+ """
+ Returns the form with a matching ID
+ """
+ for tab in self:
+ for item in tab.items:
+ if isinstance(item, forms.Form):
+ if item.id == id:
+ return item
+
class Tab(object):
def __init__(self, handler, id, title):