author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 15:26:47 +0200 | |
changeset 3435 | 84036ad2c82d |
parent 3023 | 7864fee8b4ec |
child 3451 | 6b46d73823f5 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract form classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1962
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1962
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
10 |
from cubicweb.appobject import AppObject |
1133 | 11 |
from cubicweb.view import NOINDEX, NOFOLLOW |
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
12 |
from cubicweb.common import tags |
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
13 |
from cubicweb.web import stdmsgs, httpcache, formfields |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
14 |
|
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
15 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
16 |
class FormViewMixIn(object): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
17 |
"""abstract form view mix-in""" |
0 | 18 |
category = 'form' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
19 |
http_cache_manager = httpcache.NoHTTPCacheManager |
0 | 20 |
add_to_breadcrumbs = False |
1437 | 21 |
|
0 | 22 |
def html_headers(self): |
23 |
"""return a list of html headers (eg something to be inserted between |
|
24 |
<head> and </head> of the returned page |
|
25 |
||
26 |
by default forms are neither indexed nor followed |
|
27 |
""" |
|
28 |
return [NOINDEX, NOFOLLOW] |
|
1437 | 29 |
|
0 | 30 |
def linkable(self): |
31 |
"""override since forms are usually linked by an action, |
|
32 |
so we don't want them to be listed by appli.possible_views |
|
33 |
""" |
|
34 |
return False |
|
35 |
||
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
36 |
|
1437 | 37 |
# XXX should disappear |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
38 |
class FormMixIn(object): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
39 |
"""abstract form mix-in |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
40 |
XXX: you should inherit from this FIRST (obscure pb with super call) |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
41 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
42 |
|
1525
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
43 |
def session_key(self): |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
44 |
"""return the key that may be used to store / retreive data about a |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
45 |
previous post which failed because of a validation error |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
46 |
""" |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
47 |
return '%s#%s' % (self.req.url(), self.domid) |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
48 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
49 |
def __init__(self, req, rset, **kwargs): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2798
diff
changeset
|
50 |
super(FormMixIn, self).__init__(req, rset=rset, **kwargs) |
1525
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
51 |
self.restore_previous_post(self.session_key()) |
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
52 |
|
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
53 |
def restore_previous_post(self, sessionkey): |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
54 |
# get validation session data which may have been previously set. |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
55 |
# deleting validation errors here breaks form reloading (errors are |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
56 |
# no more available), they have to be deleted by application's publish |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
57 |
# method on successful commit |
1525
cc2e2cbd7019
include dom id of the form in __errorurl in case there are multiple forms in a page
sylvain.thenault@logilab.fr
parents:
1519
diff
changeset
|
58 |
forminfo = self.req.get_session_data(sessionkey, pop=True) |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
59 |
if forminfo: |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
60 |
# XXX remove req.data assigment once cw.web.widget is killed |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
61 |
self.req.data['formvalues'] = self.form_previous_values = forminfo['values'] |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
62 |
self.req.data['formerrors'] = self.form_valerror = forminfo['errors'] |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
63 |
self.req.data['displayederrors'] = self.form_displayed_errors = set() |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
64 |
# if some validation error occured on entity creation, we have to |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
65 |
# get the original variable name from its attributed eid |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
66 |
foreid = self.form_valerror.entity |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
67 |
for var, eid in forminfo['eidmap'].items(): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
68 |
if foreid == eid: |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
69 |
self.form_valerror.eid = var |
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
70 |
break |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
71 |
else: |
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
72 |
self.form_valerror.eid = foreid |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
73 |
else: |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
74 |
self.form_previous_values = {} |
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
75 |
self.form_valerror = None |
1437 | 76 |
|
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
77 |
# XXX deprecated with new form system. Should disappear |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
78 |
|
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
79 |
domid = 'entityForm' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
80 |
category = 'form' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
81 |
http_cache_manager = httpcache.NoHTTPCacheManager |
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
82 |
add_to_breadcrumbs = False |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
83 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
84 |
def html_headers(self): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
85 |
"""return a list of html headers (eg something to be inserted between |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
86 |
<head> and </head> of the returned page |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
87 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
88 |
by default forms are neither indexed nor followed |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
89 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
90 |
return [NOINDEX, NOFOLLOW] |
1437 | 91 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
92 |
def linkable(self): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
93 |
"""override since forms are usually linked by an action, |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
94 |
so we don't want them to be listed by appli.possible_views |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
95 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
96 |
return False |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
97 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
98 |
|
1147 | 99 |
def button(self, label, klass='validateButton', tabindex=None, **kwargs): |
100 |
if tabindex is None: |
|
101 |
tabindex = self.req.next_tabindex() |
|
102 |
return tags.input(value=label, klass=klass, **kwargs) |
|
103 |
||
104 |
def action_button(self, label, onclick=None, __action=None, **kwargs): |
|
105 |
if onclick is None: |
|
106 |
onclick = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
107 |
__action, label, self.domid) |
|
108 |
return self.button(label, onclick=onclick, **kwargs) |
|
109 |
||
110 |
def button_ok(self, label=None, type='submit', name='defaultsubmit', |
|
111 |
**kwargs): |
|
112 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
113 |
return self.button(label, name=name, type=type, **kwargs) |
|
1437 | 114 |
|
1147 | 115 |
def button_apply(self, label=None, type='button', **kwargs): |
116 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
117 |
return self.action_button(label, __action='apply', type=type, **kwargs) |
|
118 |
||
119 |
def button_delete(self, label=None, type='button', **kwargs): |
|
120 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
121 |
return self.action_button(label, __action='delete', type=type, **kwargs) |
|
1437 | 122 |
|
1147 | 123 |
def button_cancel(self, label=None, type='button', **kwargs): |
124 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
125 |
return self.action_button(label, __action='cancel', type=type, **kwargs) |
|
1437 | 126 |
|
1147 | 127 |
def button_reset(self, label=None, type='reset', name='__action_cancel', |
128 |
**kwargs): |
|
129 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
130 |
return self.button(label, type=type, **kwargs) |
|
131 |
||
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
132 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
133 |
"""return a boolean indicating if form's enctype should be multipart |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
134 |
""" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
135 |
for rschema, _, x in entity.relations_by_category(categories): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
136 |
if entity.get_widget(rschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
137 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
138 |
# let's find if any of our inlined entities needs multipart |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
139 |
for rschema, targettypes, x in entity.relations_by_category('inlineview'): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
140 |
assert len(targettypes) == 1, \ |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
141 |
"I'm not able to deal with several targets and inlineview" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
142 |
ttype = targettypes[0] |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
143 |
inlined_entity = self.vreg.etype_class(ttype)(self.req, None, None) |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
144 |
for irschema, _, x in inlined_entity.relations_by_category(categories): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
145 |
if inlined_entity.get_widget(irschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
146 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
147 |
return False |
1437 | 148 |
|
0 | 149 |
def error_message(self): |
150 |
"""return formatted error message |
|
151 |
||
152 |
This method should be called once inlined field errors has been consumed |
|
153 |
""" |
|
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
154 |
errex = self.req.data.get('formerrors') or self.form_valerror |
0 | 155 |
# get extra errors |
156 |
if errex is not None: |
|
157 |
errormsg = self.req._('please correct the following errors:') |
|
1710
8c717cc0b353
refactor error handling: get validation error information from a form attribute instead of req.data to avoid pb when multiple forms are displayed
sylvain.thenault@logilab.fr
parents:
1701
diff
changeset
|
158 |
displayed = self.req.data.get('displayederrors') or self.form_displayed_errors |
0 | 159 |
errors = sorted((field, err) for field, err in errex.errors.items() |
160 |
if not field in displayed) |
|
161 |
if errors: |
|
162 |
if len(errors) > 1: |
|
1437 | 163 |
templstr = '<li>%s</li>\n' |
0 | 164 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
165 |
templstr = ' %s\n' |
0 | 166 |
for field, err in errors: |
167 |
if field is None: |
|
168 |
errormsg += templstr % err |
|
169 |
else: |
|
170 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
171 |
if len(errors) > 1: |
|
172 |
errormsg = '<ul>%s</ul>' % errormsg |
|
173 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
174 |
return u'' |
|
844 | 175 |
|
176 |
||
177 |
############################################################################### |
|
178 |
||
179 |
class metafieldsform(type): |
|
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
180 |
"""metaclass for FieldsForm to retrieve fields defined as class attributes |
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
181 |
and put them into a single ordered list: '_fields_'. |
1393 | 182 |
""" |
844 | 183 |
def __new__(mcs, name, bases, classdict): |
184 |
allfields = [] |
|
185 |
for base in bases: |
|
186 |
if hasattr(base, '_fields_'): |
|
187 |
allfields += base._fields_ |
|
188 |
clsfields = (item for item in classdict.items() |
|
2005
e8032965f37a
turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1995
diff
changeset
|
189 |
if isinstance(item[1], formfields.Field)) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
190 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 191 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
192 |
field.set_name(fieldname) |
844 | 193 |
allfields.append(field) |
194 |
classdict['_fields_'] = allfields |
|
195 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
1270 | 196 |
|
1437 | 197 |
|
1270 | 198 |
class FieldNotFound(Exception): |
199 |
"""raised by field_by_name when a field with the given name has not been |
|
200 |
found |
|
201 |
""" |
|
1437 | 202 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
203 |
class Form(FormMixIn, AppObject): |
844 | 204 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
205 |
__registry__ = 'forms' |