author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 03 Aug 2009 14:14:07 +0200 | |
changeset 2656 | a93ae0f6c0ad |
parent 2005 | e8032965f37a |
child 2798 | 9c650701cb17 |
child 2996 | 866a2c135c33 |
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' |
19 |
controller = 'edit' |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
20 |
http_cache_manager = httpcache.NoHTTPCacheManager |
0 | 21 |
add_to_breadcrumbs = False |
1437 | 22 |
|
0 | 23 |
def html_headers(self): |
24 |
"""return a list of html headers (eg something to be inserted between |
|
25 |
<head> and </head> of the returned page |
|
26 |
||
27 |
by default forms are neither indexed nor followed |
|
28 |
""" |
|
29 |
return [NOINDEX, NOFOLLOW] |
|
1437 | 30 |
|
0 | 31 |
def linkable(self): |
32 |
"""override since forms are usually linked by an action, |
|
33 |
so we don't want them to be listed by appli.possible_views |
|
34 |
""" |
|
35 |
return False |
|
36 |
||
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
37 |
|
1437 | 38 |
# 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
|
39 |
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
|
40 |
"""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
|
41 |
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
|
42 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
43 |
|
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
|
44 |
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
|
45 |
"""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
|
46 |
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
|
47 |
""" |
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 |
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
|
49 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
50 |
def __init__(self, req, rset, **kwargs): |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
51 |
super(FormMixIn, self).__init__(req, 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
|
52 |
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
|
53 |
|
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
|
54 |
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
|
55 |
# 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
|
56 |
# 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
|
57 |
# 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
|
58 |
# 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
|
59 |
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
|
60 |
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
|
61 |
# 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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
# 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
|
66 |
# 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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
break |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
self.form_valerror = None |
1437 | 77 |
|
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
78 |
# 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
|
79 |
|
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
80 |
domid = 'entityForm' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
81 |
category = 'form' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
82 |
controller = 'edit' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
83 |
http_cache_manager = httpcache.NoHTTPCacheManager |
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
84 |
add_to_breadcrumbs = False |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
85 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
86 |
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
|
87 |
"""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
|
88 |
<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
|
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 |
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
|
91 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
92 |
return [NOINDEX, NOFOLLOW] |
1437 | 93 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
94 |
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
|
95 |
"""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
|
96 |
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
|
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 |
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
|
99 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
100 |
|
1147 | 101 |
def button(self, label, klass='validateButton', tabindex=None, **kwargs): |
102 |
if tabindex is None: |
|
103 |
tabindex = self.req.next_tabindex() |
|
104 |
return tags.input(value=label, klass=klass, **kwargs) |
|
105 |
||
106 |
def action_button(self, label, onclick=None, __action=None, **kwargs): |
|
107 |
if onclick is None: |
|
108 |
onclick = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
109 |
__action, label, self.domid) |
|
110 |
return self.button(label, onclick=onclick, **kwargs) |
|
111 |
||
112 |
def button_ok(self, label=None, type='submit', name='defaultsubmit', |
|
113 |
**kwargs): |
|
114 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
115 |
return self.button(label, name=name, type=type, **kwargs) |
|
1437 | 116 |
|
1147 | 117 |
def button_apply(self, label=None, type='button', **kwargs): |
118 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
119 |
return self.action_button(label, __action='apply', type=type, **kwargs) |
|
120 |
||
121 |
def button_delete(self, label=None, type='button', **kwargs): |
|
122 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
123 |
return self.action_button(label, __action='delete', type=type, **kwargs) |
|
1437 | 124 |
|
1147 | 125 |
def button_cancel(self, label=None, type='button', **kwargs): |
126 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
127 |
return self.action_button(label, __action='cancel', type=type, **kwargs) |
|
1437 | 128 |
|
1147 | 129 |
def button_reset(self, label=None, type='reset', name='__action_cancel', |
130 |
**kwargs): |
|
131 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
132 |
return self.button(label, type=type, **kwargs) |
|
133 |
||
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
134 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
135 |
"""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
|
136 |
""" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
137 |
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
|
138 |
if entity.get_widget(rschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
139 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
140 |
# 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
|
141 |
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
|
142 |
assert len(targettypes) == 1, \ |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
143 |
"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
|
144 |
ttype = targettypes[0] |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
145 |
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
|
146 |
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
|
147 |
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
|
148 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
149 |
return False |
1437 | 150 |
|
0 | 151 |
def error_message(self): |
152 |
"""return formatted error message |
|
153 |
||
154 |
This method should be called once inlined field errors has been consumed |
|
155 |
""" |
|
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
|
156 |
errex = self.req.data.get('formerrors') or self.form_valerror |
0 | 157 |
# get extra errors |
158 |
if errex is not None: |
|
159 |
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
|
160 |
displayed = self.req.data.get('displayederrors') or self.form_displayed_errors |
0 | 161 |
errors = sorted((field, err) for field, err in errex.errors.items() |
162 |
if not field in displayed) |
|
163 |
if errors: |
|
164 |
if len(errors) > 1: |
|
1437 | 165 |
templstr = '<li>%s</li>\n' |
0 | 166 |
else: |
167 |
templstr = ' %s\n' |
|
168 |
for field, err in errors: |
|
169 |
if field is None: |
|
170 |
errormsg += templstr % err |
|
171 |
else: |
|
172 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
173 |
if len(errors) > 1: |
|
174 |
errormsg = '<ul>%s</ul>' % errormsg |
|
175 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
176 |
return u'' |
|
844 | 177 |
|
178 |
||
179 |
############################################################################### |
|
180 |
||
181 |
class metafieldsform(type): |
|
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
182 |
"""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
|
183 |
and put them into a single ordered list: '_fields_'. |
1393 | 184 |
""" |
844 | 185 |
def __new__(mcs, name, bases, classdict): |
186 |
allfields = [] |
|
187 |
for base in bases: |
|
188 |
if hasattr(base, '_fields_'): |
|
189 |
allfields += base._fields_ |
|
190 |
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
|
191 |
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
|
192 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 193 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
194 |
field.set_name(fieldname) |
844 | 195 |
allfields.append(field) |
196 |
classdict['_fields_'] = allfields |
|
197 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
1270 | 198 |
|
1437 | 199 |
|
1270 | 200 |
class FieldNotFound(Exception): |
201 |
"""raised by field_by_name when a field with the given name has not been |
|
202 |
found |
|
203 |
""" |
|
1437 | 204 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
205 |
class Form(FormMixIn, AppObject): |
844 | 206 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
207 |
__registry__ = 'forms' |