From 84247a0bc63179309693a52ac2aad989fb546d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20Irl=C3=A4nder?= Date: Fri, 30 Jun 2017 15:37:25 +0200 Subject: [PATCH 1/3] Added FormdDataprovider class. FormDataprovider is a class which can be used to build instances whic provide data to the form. They are used as `item` on form initialisation. --- formbar/form.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/formbar/form.py b/formbar/form.py index 41c5afc..0dbf6d6 100644 --- a/formbar/form.py +++ b/formbar/form.py @@ -74,6 +74,42 @@ class ValidationException(Exception): pass +class FormDataprovider(object): + """Dummy class which can be used to provided dynamic data to the + form. It can be provided as item on the form init to provide + dynamically loaded options and values. + + As said the dataprovider can be user to provide `options` and `values`. + Both are provided by defining propertys. + + 1. Value + The function should simply return the pythonic value of the field: + + @property + def foo(self): + return "1" + + 1. Options + The function should return a list of tuples. The first value of the + tuple is the displayed label and the second value is the value of + the option: + + @property + def foo_options(self): + return [("Foo", "1"), ("Bar", "2")] + + or you can use the helper method `get_options(fieldname)` + """ + + def __init__(self, ctx): + self.ctx = ctx + """Can be anything. E.g db connection, request. It depends on the context.""" + + def get_options(self, name): + if hasattr(self, name+"_options"): + return getattr(self, name+"_options") + return [] + class Validator(object): """Validator class for external validators. External validators can be used to implement more complicated validations on the converted From 58c71c79a0405cf52eed22dfae28279a22ddf471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20Irl=C3=A4nder?= Date: Fri, 30 Jun 2017 15:38:56 +0200 Subject: [PATCH 2/3] Load options from FormDataprovider in Selectionfields if possible. --- formbar/fields.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/formbar/fields.py b/formbar/fields.py index 26bf8bc..54490f3 100644 --- a/formbar/fields.py +++ b/formbar/fields.py @@ -738,6 +738,9 @@ def get_options(self): elif isinstance(user_defined_options, str): for option in self._form.merged_data.get(user_defined_options): options.append((option[0], option[1], True)) + elif self._form._item and hasattr(self._form._item, "get_options"): + for option in self._form._item.get_options(self.name): + options.append((option[0], option[1], True)) return self.sort_options(options) def _from_python(self, value): From f69ba7f46e12adbccd294645cfdf1145ba2d03cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20Irl=C3=A4nder?= Date: Fri, 30 Jun 2017 15:41:08 +0200 Subject: [PATCH 3/3] Added Formdataprovider into Changes. --- CHANGES.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 478ce59..d09df20 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -2,6 +2,8 @@ ===== - Added "selected" config attribute to the radio renderer to preselect an entry from the option by its index. +- Added FormDataprovider. A Formdataprovider can be used to fill the form with + (dynamic) values and options. 1.0.2 =====