From f33e1d2f840aa7a9b7f2505ba00c24adfdd94b10 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 22 Jul 2016 18:42:47 +0200 Subject: [PATCH] Create a simple engine for forms This allows to create forms without replicating HTML code and allows accessing them very easily on the backend. Signed-off-by: Michael Tremer --- Makefile.am | 31 ++- po/POTFILES.in | 2 + src/templates/demo.html | 5 + src/templates/modules/forms/element.html | 13 ++ src/templates/modules/forms/fieldset.html | 25 +++ src/templates/modules/forms/group.html | 11 ++ src/templates/modules/forms/index.html | 21 +++ src/templates/modules/forms/inputs/base.html | 11 ++ .../modules/forms/inputs/checkbox.html | 6 + src/templates/modules/forms/inputs/text.html | 18 ++ .../modules/forms/inputs/textarea.html | 3 + src/westferry/backend/__init__.py | 2 + src/westferry/backend/forms/__init__.py | 26 +++ src/westferry/backend/forms/base.py | 64 +++++++ src/westferry/backend/forms/choice.py | 29 +++ src/westferry/backend/forms/main.py | 178 ++++++++++++++++++ src/westferry/backend/forms/text.py | 53 ++++++ src/westferry/backend/forms/yesno.py | 37 ++++ src/westferry/handlers/__init__.py | 1 + src/westferry/handlers/demo.py | 104 ++++++++++ src/westferry/ui/__init__.py | 1 + src/westferry/ui/forms.py | 75 ++++++++ 22 files changed, 715 insertions(+), 1 deletion(-) create mode 100644 src/templates/demo.html create mode 100644 src/templates/modules/forms/element.html create mode 100644 src/templates/modules/forms/fieldset.html create mode 100644 src/templates/modules/forms/group.html create mode 100644 src/templates/modules/forms/index.html create mode 100644 src/templates/modules/forms/inputs/base.html create mode 100644 src/templates/modules/forms/inputs/checkbox.html create mode 100644 src/templates/modules/forms/inputs/text.html create mode 100644 src/templates/modules/forms/inputs/textarea.html create mode 100644 src/westferry/backend/forms/__init__.py create mode 100644 src/westferry/backend/forms/base.py create mode 100644 src/westferry/backend/forms/choice.py create mode 100644 src/westferry/backend/forms/main.py create mode 100644 src/westferry/backend/forms/text.py create mode 100644 src/westferry/backend/forms/yesno.py create mode 100644 src/westferry/handlers/demo.py create mode 100644 src/westferry/ui/forms.py diff --git a/Makefile.am b/Makefile.am index 70993a1..6eeef3b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -106,10 +106,21 @@ westferry_backend_PYTHON = \ westferry_backenddir = $(pythondir)/westferry/backend +westferry_backend_forms_PYTHON = \ + src/westferry/backend/forms/__init__.py \ + src/westferry/backend/forms/base.py \ + src/westferry/backend/forms/choice.py \ + src/westferry/backend/forms/main.py \ + src/westferry/backend/forms/text.py \ + src/westferry/backend/forms/yesno.py + +westferry_backend_formsdir = $(westferry_backenddir)/forms + westferry_handlers_PYTHON = \ src/westferry/handlers/__init__.py \ src/westferry/handlers/analytics.py \ src/westferry/handlers/base.py \ + src/westferry/handlers/demo.py \ src/westferry/handlers/index.py westferry_handlersdir = $(pythondir)/westferry/handlers @@ -117,6 +128,7 @@ westferry_handlersdir = $(pythondir)/westferry/handlers westferry_ui_PYTHON = \ src/westferry/ui/__init__.py \ src/westferry/ui/base.py \ + src/westferry/ui/forms.py \ src/westferry/ui/graphs.py \ src/westferry/ui/menu.py \ src/westferry/ui/utils.py @@ -128,12 +140,29 @@ westferry_uidir = $(pythondir)/westferry/ui templatesdir = $(datadir)/westferry/templates dist_templates_DATA = \ - src/templates/base.html + src/templates/base.html \ + src/templates/demo.html templates_modulesdir = $(templatesdir)/modules dist_templates_modules_DATA = +templates_modules_formsdir = $(templates_modulesdir)/forms + +dist_templates_modules_forms_DATA = \ + src/templates/modules/forms/element.html \ + src/templates/modules/forms/fieldset.html \ + src/templates/modules/forms/group.html \ + src/templates/modules/forms/index.html + +templates_modules_forms_inputsdir = $(templates_modules_formsdir)/inputs + +dist_templates_modules_forms_inputs_DATA = \ + src/templates/modules/forms/inputs/base.html \ + src/templates/modules/forms/inputs/checkbox.html \ + src/templates/modules/forms/inputs/text.html \ + src/templates/modules/forms/inputs/textarea.html + templates_modules_graphsdir = $(templates_modulesdir)/graphs dist_templates_modules_graphs_DATA = \ diff --git a/po/POTFILES.in b/po/POTFILES.in index 292abac..82a7d6a 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,4 +1,6 @@ src/westferry/backend/__version__.py src/westferry/handlers/analytics.py src/westferry/handlers/base.py +src/westferry/handlers/demo.py +src/westferry/handlers/index.py src/westferry/__init__.py diff --git a/src/templates/demo.html b/src/templates/demo.html new file mode 100644 index 0000000..9b3bda1 --- /dev/null +++ b/src/templates/demo.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block main %} + {% module Form(form) %} +{% end block %} diff --git a/src/templates/modules/forms/element.html b/src/templates/modules/forms/element.html new file mode 100644 index 0000000..34b1471 --- /dev/null +++ b/src/templates/modules/forms/element.html @@ -0,0 +1,13 @@ +
+ {% if e.label %} + + {% end %} + +
+ {% module FormInput(e) %} + + {% if e.help %} + {{ e.help }} + {% end %} +
+
diff --git a/src/templates/modules/forms/fieldset.html b/src/templates/modules/forms/fieldset.html new file mode 100644 index 0000000..18c76cb --- /dev/null +++ b/src/templates/modules/forms/fieldset.html @@ -0,0 +1,25 @@ +{% import westferry.backend.forms %} + +
+ {% if fs.label %} + {{ fs.label }} + {% else %} +
+ {% end %} + + {% if fs.help %} +
+
+ {{ fs.help }} +
+
+ {% end %} + + {% for e in fs %} + {% if isinstance(e, westferry.backend.forms.Group) %} + {% module FormGroup(e) %} + {% else %} + {% module FormElement(e) %} + {% end %} + {% end %} +
diff --git a/src/templates/modules/forms/group.html b/src/templates/modules/forms/group.html new file mode 100644 index 0000000..caacfb6 --- /dev/null +++ b/src/templates/modules/forms/group.html @@ -0,0 +1,11 @@ +
+ {% for e in group %} +
+ {% if e.label %} + + {% end %} + + {% module FormInput(e) %} +
+ {% end %} +
diff --git a/src/templates/modules/forms/index.html b/src/templates/modules/forms/index.html new file mode 100644 index 0000000..291721d --- /dev/null +++ b/src/templates/modules/forms/index.html @@ -0,0 +1,21 @@ +{% import westferry.backend.forms %} + +
+ {% raw xsrf_form_html() %} + + {% for e in f %} + {% if isinstance(e, westferry.backend.forms.Fieldset) %} + {% module FormFieldset(e) %} + {% elif isinstance(e, westferry.backend.forms.Group) %} + {% module FormGroup(e) %} + {% else %} + {% module FormElement(e) %} + {% end %} + {% end %} + +
+
+ +
+
+
diff --git a/src/templates/modules/forms/inputs/base.html b/src/templates/modules/forms/inputs/base.html new file mode 100644 index 0000000..ed6b341 --- /dev/null +++ b/src/templates/modules/forms/inputs/base.html @@ -0,0 +1,11 @@ +{% import westferry.backend.forms %} + +{% if isinstance(input, westferry.backend.forms.MultilineTextInput) %} + {% module MultilineTextInput(input) %} +{% elif isinstance(input, westferry.backend.forms.PasswordInput) %} + {% module PasswordInput(input) %} +{% elif isinstance(input, westferry.backend.forms.TextInput) %} + {% module TextInput(input) %} +{% elif isinstance(input, westferry.backend.forms.YesNoInput) %} + {% module YesNoInput(input) %} +{% end %} diff --git a/src/templates/modules/forms/inputs/checkbox.html b/src/templates/modules/forms/inputs/checkbox.html new file mode 100644 index 0000000..a4e56ab --- /dev/null +++ b/src/templates/modules/forms/inputs/checkbox.html @@ -0,0 +1,6 @@ +
+ +
diff --git a/src/templates/modules/forms/inputs/text.html b/src/templates/modules/forms/inputs/text.html new file mode 100644 index 0000000..6f3308c --- /dev/null +++ b/src/templates/modules/forms/inputs/text.html @@ -0,0 +1,18 @@ +{% if input.prefix or input.suffix %} +
+ {% if input.prefix %} +
{{ input.prefix }}
+ {% end%} +{% end %} + + + +{% if input.prefix or input.suffix %} + {% if input.suffix %} +
{{ input.suffix }}
+ {% end %} +
+{% end %} diff --git a/src/templates/modules/forms/inputs/textarea.html b/src/templates/modules/forms/inputs/textarea.html new file mode 100644 index 0000000..6e88343 --- /dev/null +++ b/src/templates/modules/forms/inputs/textarea.html @@ -0,0 +1,3 @@ + diff --git a/src/westferry/backend/__init__.py b/src/westferry/backend/__init__.py index cad0eb3..1cc3a3f 100644 --- a/src/westferry/backend/__init__.py +++ b/src/westferry/backend/__init__.py @@ -20,3 +20,5 @@ ############################################################################### from .main import Backend + +from . import forms diff --git a/src/westferry/backend/forms/__init__.py b/src/westferry/backend/forms/__init__.py new file mode 100644 index 0000000..c9683c6 --- /dev/null +++ b/src/westferry/backend/forms/__init__.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from .base import Element +from .main import Form, Fieldset, Group + +from .text import TextInput, MultilineTextInput, PasswordInput +from .yesno import YesNoInput diff --git a/src/westferry/backend/forms/base.py b/src/westferry/backend/forms/base.py new file mode 100644 index 0000000..e2e8633 --- /dev/null +++ b/src/westferry/backend/forms/base.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +class Element(object): + # The element type + type = None + + def __init__(self, form, name=None, label=None, default=None, **kwargs): + assert self.type + + self.form = form + self.name = name + self.label = label + self.default = default + + # Is this element editable? + self.disabled = False + self.readonly = False + + # Help text + self.help = None + + # Set defaults for inheriting classes + self._set_defaults() + + # Set args + for k, v in kwargs.items(): + setattr(self, k, v) + + self.init(**kwargs) + + def _set_defaults(self): + pass + + def init(self, *args, **kwargs): + pass + + @property + def value(self): + """ + Returns the value that the user entered for this element. + """ + return self.get_value() + + def get_value(self): + return self.form._get_value(self.name, self.default) diff --git a/src/westferry/backend/forms/choice.py b/src/westferry/backend/forms/choice.py new file mode 100644 index 0000000..73e2421 --- /dev/null +++ b/src/westferry/backend/forms/choice.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base + +class SingleChoiceInput(base.Element): + type = "single-choice" + + +class MultipleChoiceInput(base.Element): + type = "multiple-choice" diff --git a/src/westferry/backend/forms/main.py b/src/westferry/backend/forms/main.py new file mode 100644 index 0000000..ab49ce0 --- /dev/null +++ b/src/westferry/backend/forms/main.py @@ -0,0 +1,178 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base +from . import choice +from . import text +from . import yesno + +class _Form(object): + def __init__(self, form): + self.form = form + self._elements = [] + + # Is this form disabled? + self.disabled = False + + def __iter__(self): + """ + Iterating over a form will iterate over its elements. + """ + return iter(self._elements) + + @property + def parent_form(self): + if self.form: + return self.form.parent_form + + return self + + @property + def elements(self): + for e in self._elements: + if isinstance(e, _Form): + for i in e._elements: + yield i + + else: + yield e + + def _add_element(self, element): + """ + Adds a new form element to this form. + """ + assert isinstance(element, base.Element) + assert not element in self.elements + + self._elements.append(element) + + def _add_element_class(self, cls, *args, **kwargs): + e = cls(self, *args, **kwargs) + self._add_element(e) + + return e + + def get_element_by_name(self, name): + """ + Returns the element with the given name (if any). + """ + for e in self.elements: + if e.name == name: + return e + + # Have a shortcut to find elements by their name quickly. + get = get_element_by_name + + def add_text_input(self, *args, **kwargs): + """ + Creates a new text input. + """ + return self._add_element_class(text.TextInput, *args, **kwargs) + + def add_multiline_text_input(self, *args, **kwargs): + """ + Creates a new multiline text input. + """ + return self._add_element_class(text.MultilineTextInput, *args, **kwargs) + + def add_password_input(self, *args, **kwargs): + """ + Creates a new password input. + """ + return self._add_element_class(text.PasswordInput, *args, **kwargs) + + def add_yesno_input(self, *args, **kwargs): + """ + Creates a new yes/no input. + """ + return self._add_element_class(yesno.YesNoInput, *args, **kwargs) + + def add_single_choice_input(self, *args, **kwargs): + """ + Creates a new single choice input. + """ + return self._add_element_class(choice.SingleChoiceInput, *args, **kwargs) + + def add_multiple_choice_input(self, *args, **kwargs): + """ + Creates a new multiple choice input. + """ + return self._add_element_class(choice.MultipleChoiceInput, *args, **kwargs) + + def _get_value(self, *args, **kwargs): + return self.parent_form._get_value(*args, **kwargs) + + +class FormGroupMixin(object): + def add_group(self, *args, **kwargs): + group = Group(self, *args, **kwargs) + self._elements.append(group) + + return group + + +class Form(_Form, FormGroupMixin): + def __init__(self, handler, method="POST", action=""): + _Form.__init__(self, None) + self.handler = handler + + self.method = method + self.action = action + + # Text that is shown on the submit button + self.submit_text = None + + def add_fieldset(self, *args, **kwargs): + fs = Fieldset(self, *args, **kwargs) + self._elements.append(fs) + + return fs + + def export(self): + """ + Returns a dictionary with all keys and values + of the form. + """ + ret = {} + + for e in self.elements: + ret[e.name] = e.value + + return ret + + def _get_value(self, name, default=None): + return self.handler.get_argument(name, default) + + +class Fieldset(_Form, FormGroupMixin): + def __init__(self, *args, label=None, help=None, **kwargs): + _Form.__init__(self, *args, **kwargs) + + self.label = label + self.help = help + + +class Group(_Form): + def __init__(self, *args, label=None, **kwargs): + _Form.__init__(self, *args, **kwargs) + + self.label = label + self.help = None diff --git a/src/westferry/backend/forms/text.py b/src/westferry/backend/forms/text.py new file mode 100644 index 0000000..11b42fa --- /dev/null +++ b/src/westferry/backend/forms/text.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base + +class TextInput(base.Element): + type = "text" + + def _set_defaults(self): + self.prefix = None + self.suffix = None + + self._placeholder = None + + def get_placeholder(self): + return self._placeholder or self.label + + def set_placeholder(self, placeholder): + self._placeholder = placeholder + + placeholder = property(get_placeholder, set_placeholder) + + +class MultilineTextInput(TextInput): + type = "multiline-text" + + def _set_defaults(self): + TextInput._set_defaults(self) + + # Default number of rows + self.lines = 3 + + +class PasswordInput(TextInput): + type = "password" diff --git a/src/westferry/backend/forms/yesno.py b/src/westferry/backend/forms/yesno.py new file mode 100644 index 0000000..17b7f44 --- /dev/null +++ b/src/westferry/backend/forms/yesno.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base + +class YesNoInput(base.Element): + type = "yesno" + + def _set_defaults(self): + self.description = None + + def get_value(self): + value = base.Element.get_value(self) + + # This only returns true or false + if value == "on": + return True + + return False diff --git a/src/westferry/handlers/__init__.py b/src/westferry/handlers/__init__.py index bd3ac0b..60e04f7 100644 --- a/src/westferry/handlers/__init__.py +++ b/src/westferry/handlers/__init__.py @@ -21,6 +21,7 @@ from . import base from . import analytics +from . import demo from . import index def get_handlers(): diff --git a/src/westferry/handlers/demo.py b/src/westferry/handlers/demo.py new file mode 100644 index 0000000..d327899 --- /dev/null +++ b/src/westferry/handlers/demo.py @@ -0,0 +1,104 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from . import base +from .. import backend +from .. import ui + +from ..i18n import N_, _ + +class DemoBaseHandler(base.BaseHandler): + @property + def menu(self): + _ = self.locale.translate + + m = ui.menu.Menu(self, _("Demo")) + + # Overview + m.add_handler(DemoOverviewHandler) + m.add_divider() + + # Forms + m.add_handler(DemoFormHandler) + + return m + + +class DemoOverviewHandler(DemoBaseHandler): + url = r"/demo" + title = N_("Overview") + + def get(self): + self.render("base.html") + + +class DemoFormHandler(DemoBaseHandler): + url = r"/demo/forms" + title = N_("Forms") + + def get(self): + self.render("demo.html", form=self.form) + + def post(self): + data = self.form.export() + self.finish(data) + + @property + def form(self): + """ + This creates a simple form that asks the user + for their address. + """ + _ = self.locale.translate + + form = backend.forms.Form(self) + form.submit_text = _("Order") + + # First name + e = form.add_text_input("first_name", label=_("First Name"), placeholder="John") + e.help = _("Please enter your first name") + + # Last name + e = form.add_text_input("last_name", label=_("Last Name"), placeholder="Doe") + e.help = _("Please enter your last name") + + # We create a fieldset for the address + fs1 = form.add_fieldset(label=_("Address")) + fs1.help = _("Please enter your address") + + e = fs1.add_text_input("street", label=_("Street")) + e = fs1.add_text_input("city", label=_("City")) + e = fs1.add_text_input("post_code", label=_("Postcode")) + + # Fieldset without label + fs2 = form.add_fieldset() + + # Donation amount + e = fs2.add_text_input("donation", label=_("Donation"), + placeholder=_("Donation"), suffix="€") + + # Notes + e = fs2.add_multiline_text_input("notes", label=_("Notes")) + + # Subscribe to newsletter? + e = fs2.add_yesno_input("subscribe_to_newsletter", description=_("Subscribe to newsletter?")) + + return form diff --git a/src/westferry/ui/__init__.py b/src/westferry/ui/__init__.py index 89ade9a..7bcce3f 100644 --- a/src/westferry/ui/__init__.py +++ b/src/westferry/ui/__init__.py @@ -20,6 +20,7 @@ ############################################################################### from . import base +from . import forms from . import graphs from . import menu from . import utils diff --git a/src/westferry/ui/forms.py b/src/westferry/ui/forms.py new file mode 100644 index 0000000..ddd3ec1 --- /dev/null +++ b/src/westferry/ui/forms.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Westferry - The IPFire web user interface # +# Copyright (C) 2015 IPFire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU 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 General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +from ..backend import forms +from . import base + +class FormModule(base.BaseUIModule): + def render(self, form): + return self.render_string("modules/forms/index.html", + f=form, forms=forms) + + +class FormElementModule(base.BaseUIModule): + def render(self, element): + assert isinstance(element, forms.Element) + + return self.render_string("modules/forms/element.html", e=element) + + +class FormFieldsetModule(base.BaseUIModule): + def render(self, fieldset): + return self.render_string("modules/forms/fieldset.html", fs=fieldset) + + +class FormGroupModule(base.BaseUIModule): + def render(self, group): + return self.render_string("modules/forms/group.html", group=group) + + +class FormInputModule(base.BaseUIModule): + """ + This is a proxy module that will pick the right + *InputModule. + """ + def render(self, input): + return self.render_string("modules/forms/inputs/base.html", input=input) + + +class TextInputModule(base.BaseUIModule): + def render(self, input): + assert isinstance(input, forms.TextInput) + + return self.render_string("modules/forms/inputs/text.html", input=input) + + +class MultilineTextInputModule(base.BaseUIModule): + def render(self, input): + assert isinstance(input, forms.MultilineTextInput) + + return self.render_string("modules/forms/inputs/textarea.html", input=input) + + +class YesNoInputModule(base.BaseUIModule): + def render(self, input): + assert isinstance(input, forms.YesNoInput) + + return self.render_string("modules/forms/inputs/checkbox.html", input=input) -- 2.39.2