diff --git a/CHANGES.txt b/CHANGES.txt index b9b8d46..4727014 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -7,6 +7,8 @@ Improvements - Added optional timezone parameter to Form initialisation. New feature +- Added FormDataprovider. A Formdataprovider can be used to fill the form with + (dynamic) values and options. - Added datetime picker widget. See https://github.com/Eonasdan/bootstrap-datetimepicker - Added currency widget. - Added new "fields" command to generate.py command (in contrib) to export the @@ -28,7 +30,6 @@ Bugfix: Internal release only already includes most of the features from 1.1.0 but never has been released in the public. - 1.0.2 ===== Maintenance release. diff --git a/formbar/fields.py b/formbar/fields.py index f0dc74d..6723ffd 100644 --- a/formbar/fields.py +++ b/formbar/fields.py @@ -759,6 +759,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): diff --git a/formbar/form.py b/formbar/form.py index 3f872d2..6be2a05 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