summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2016-02-02 04:18:47 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2016-02-02 04:44:07 +0100
commit7e678c9d85b84ac7e903a4bdc1d08890fa5f1d18 (patch)
tree21313f721a4a305156387555482e0718fdacb32a
parentf68ac601ef28b1aa27d14a0dcdd5c9fdf934b6d3 (diff)
downloadweb-7e678c9d85b84ac7e903a4bdc1d08890fa5f1d18.tar.gz
web-7e678c9d85b84ac7e903a4bdc1d08890fa5f1d18.tar.bz2
web-7e678c9d85b84ac7e903a4bdc1d08890fa5f1d18.zip
utils/console: Add helper class to handle forms
The wtforms classes need some helper to work on the command line.
-rw-r--r--accounts/utils/console.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/accounts/utils/console.py b/accounts/utils/console.py
index 8af3fb3..fb55030 100644
--- a/accounts/utils/console.py
+++ b/accounts/utils/console.py
@@ -78,3 +78,42 @@ class TablePrinter(object):
def _print_row(self, row):
print(self.format_string % tuple(row))
+
+
+class ConsoleForm(object):
+ _ready = False
+
+ def __init__(self, formcls, **kwargs):
+ self.form = formcls()
+ self._fill(kwargs)
+ self._ready = True
+
+ def _fill(self, data):
+ for key, value in data.items():
+ field = getattr(self.form, key, None)
+ if field is not None:
+ field.data = value
+
+ def print_errors(self):
+ for field, errors in self.form.errors.items():
+ if len(errors) > 1:
+ print('%s:' % field)
+ for error in errors:
+ print(' %s' % error)
+ else:
+ print('%s: %s' % (field, errors[0]))
+
+ def __getattr__(self, name):
+ return getattr(self.form, name)
+
+ def __setattr__(self, name, value):
+ if self._ready and name not in self.__dict__:
+ setattr(self.form, name, value)
+ else:
+ super(ConsoleForm, self).__setattr__(name, value)
+
+ def __delattr__(self, name):
+ if self._ready and name not in self.__dict__:
+ delattr(self.form, name)
+ else:
+ super(ConsoleForm, self).__delattr__(name)