]> git.ipfire.org Git - people/ms/westferry.git/blob - src/westferry/backend/forms/base.py
Create a simple engine for forms
[people/ms/westferry.git] / src / westferry / backend / forms / base.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # Westferry - The IPFire web user interface #
5 # Copyright (C) 2015 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 class Element(object):
23 # The element type
24 type = None
25
26 def __init__(self, form, name=None, label=None, default=None, **kwargs):
27 assert self.type
28
29 self.form = form
30 self.name = name
31 self.label = label
32 self.default = default
33
34 # Is this element editable?
35 self.disabled = False
36 self.readonly = False
37
38 # Help text
39 self.help = None
40
41 # Set defaults for inheriting classes
42 self._set_defaults()
43
44 # Set args
45 for k, v in kwargs.items():
46 setattr(self, k, v)
47
48 self.init(**kwargs)
49
50 def _set_defaults(self):
51 pass
52
53 def init(self, *args, **kwargs):
54 pass
55
56 @property
57 def value(self):
58 """
59 Returns the value that the user entered for this element.
60 """
61 return self.get_value()
62
63 def get_value(self):
64 return self.form._get_value(self.name, self.default)