author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 13 Nov 2009 15:36:09 +0100 | |
branch | stable |
changeset 3847 | 7cf2b338a8e7 |
parent 3512 | 2ceaa4e40348 |
child 3524 | a3431f4e2f40 |
child 3922 | 69020a7c234a |
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 |
||
3512
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
10 |
from logilab.common.decorators import iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
11 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
12 |
from cubicweb.appobject import AppObject |
1133 | 13 |
from cubicweb.view import NOINDEX, NOFOLLOW |
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
14 |
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
|
15 |
from cubicweb.web import stdmsgs, httpcache, formfields |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
16 |
|
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
17 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
18 |
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
|
19 |
"""abstract form view mix-in""" |
0 | 20 |
category = 'form' |
21 |
controller = 'edit' |
|
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
http_cache_manager = httpcache.NoHTTPCacheManager |
0 | 23 |
add_to_breadcrumbs = False |
1437 | 24 |
|
0 | 25 |
def html_headers(self): |
26 |
"""return a list of html headers (eg something to be inserted between |
|
27 |
<head> and </head> of the returned page |
|
28 |
||
29 |
by default forms are neither indexed nor followed |
|
30 |
""" |
|
31 |
return [NOINDEX, NOFOLLOW] |
|
1437 | 32 |
|
0 | 33 |
def linkable(self): |
34 |
"""override since forms are usually linked by an action, |
|
35 |
so we don't want them to be listed by appli.possible_views |
|
36 |
""" |
|
37 |
return False |
|
38 |
||
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 |
|
1437 | 40 |
# 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
|
41 |
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
|
42 |
"""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
|
43 |
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
|
44 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
45 |
|
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
|
46 |
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
|
47 |
"""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
|
48 |
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
|
49 |
""" |
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
|
50 |
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
|
51 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
|
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
|
56 |
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
|
57 |
# 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
|
58 |
# 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
|
59 |
# 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
|
60 |
# 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
|
61 |
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
|
62 |
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
|
63 |
# 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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
# 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
|
68 |
# 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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
break |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
self.form_valerror = None |
1437 | 79 |
|
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
80 |
# 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
|
81 |
|
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
82 |
domid = 'entityForm' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
83 |
category = 'form' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
84 |
controller = 'edit' |
1995
ec95eaa2b711
turn renderers into appobjects
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
85 |
http_cache_manager = httpcache.NoHTTPCacheManager |
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
86 |
add_to_breadcrumbs = False |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
87 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
88 |
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
|
89 |
"""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
|
90 |
<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
|
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 |
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
|
93 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
94 |
return [NOINDEX, NOFOLLOW] |
1437 | 95 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
96 |
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
|
97 |
"""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
|
98 |
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
|
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 |
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
|
101 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
102 |
|
1147 | 103 |
def button(self, label, klass='validateButton', tabindex=None, **kwargs): |
104 |
if tabindex is None: |
|
105 |
tabindex = self.req.next_tabindex() |
|
106 |
return tags.input(value=label, klass=klass, **kwargs) |
|
107 |
||
108 |
def action_button(self, label, onclick=None, __action=None, **kwargs): |
|
109 |
if onclick is None: |
|
110 |
onclick = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
111 |
__action, label, self.domid) |
|
112 |
return self.button(label, onclick=onclick, **kwargs) |
|
113 |
||
114 |
def button_ok(self, label=None, type='submit', name='defaultsubmit', |
|
115 |
**kwargs): |
|
116 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
117 |
return self.button(label, name=name, type=type, **kwargs) |
|
1437 | 118 |
|
1147 | 119 |
def button_apply(self, label=None, type='button', **kwargs): |
120 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
121 |
return self.action_button(label, __action='apply', type=type, **kwargs) |
|
122 |
||
123 |
def button_delete(self, label=None, type='button', **kwargs): |
|
124 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
125 |
return self.action_button(label, __action='delete', type=type, **kwargs) |
|
1437 | 126 |
|
1147 | 127 |
def button_cancel(self, label=None, type='button', **kwargs): |
128 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
129 |
return self.action_button(label, __action='cancel', type=type, **kwargs) |
|
1437 | 130 |
|
1147 | 131 |
def button_reset(self, label=None, type='reset', name='__action_cancel', |
132 |
**kwargs): |
|
133 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
134 |
return self.button(label, type=type, **kwargs) |
|
135 |
||
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
136 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
137 |
"""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
|
138 |
""" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
139 |
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
|
140 |
if entity.get_widget(rschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
141 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
142 |
# 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
|
143 |
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
|
144 |
assert len(targettypes) == 1, \ |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
145 |
"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
|
146 |
ttype = targettypes[0] |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
151 |
return False |
1437 | 152 |
|
0 | 153 |
def error_message(self): |
154 |
"""return formatted error message |
|
155 |
||
156 |
This method should be called once inlined field errors has been consumed |
|
157 |
""" |
|
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 |
errex = self.req.data.get('formerrors') or self.form_valerror |
0 | 159 |
# get extra errors |
160 |
if errex is not None: |
|
161 |
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
|
162 |
displayed = self.req.data.get('displayederrors') or self.form_displayed_errors |
0 | 163 |
errors = sorted((field, err) for field, err in errex.errors.items() |
164 |
if not field in displayed) |
|
165 |
if errors: |
|
166 |
if len(errors) > 1: |
|
1437 | 167 |
templstr = '<li>%s</li>\n' |
0 | 168 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
169 |
templstr = ' %s\n' |
0 | 170 |
for field, err in errors: |
171 |
if field is None: |
|
172 |
errormsg += templstr % err |
|
173 |
else: |
|
174 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
175 |
if len(errors) > 1: |
|
176 |
errormsg = '<ul>%s</ul>' % errormsg |
|
177 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
178 |
return u'' |
|
844 | 179 |
|
180 |
||
181 |
############################################################################### |
|
182 |
||
183 |
class metafieldsform(type): |
|
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
184 |
"""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
|
185 |
and put them into a single ordered list: '_fields_'. |
1393 | 186 |
""" |
844 | 187 |
def __new__(mcs, name, bases, classdict): |
188 |
allfields = [] |
|
189 |
for base in bases: |
|
190 |
if hasattr(base, '_fields_'): |
|
191 |
allfields += base._fields_ |
|
192 |
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
|
193 |
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
|
194 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 195 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
196 |
field.set_name(fieldname) |
844 | 197 |
allfields.append(field) |
198 |
classdict['_fields_'] = allfields |
|
199 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
1270 | 200 |
|
1437 | 201 |
|
1270 | 202 |
class FieldNotFound(Exception): |
203 |
"""raised by field_by_name when a field with the given name has not been |
|
204 |
found |
|
205 |
""" |
|
1437 | 206 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2005
diff
changeset
|
207 |
class Form(FormMixIn, AppObject): |
844 | 208 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
209 |
__registry__ = 'forms' |
3512
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
210 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
211 |
parent_form = None |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
212 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
213 |
@property |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
214 |
def root_form(self): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
215 |
"""return the root form""" |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
216 |
if self.parent_form is None: |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
217 |
return self |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
218 |
return self.parent_form.root_form |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
219 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
220 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
221 |
def _fieldsattr(cls_or_self): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
222 |
if isinstance(cls_or_self, type): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
223 |
fields = cls_or_self._fields_ |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
224 |
else: |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
225 |
fields = cls_or_self.fields |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
226 |
return fields |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
227 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
228 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
229 |
def field_by_name(cls_or_self, name, role='subject'): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
230 |
"""return field with the given name and role. |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
231 |
Raise FieldNotFound if the field can't be found. |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
232 |
""" |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
233 |
for field in cls_or_self._fieldsattr(): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
234 |
if field.name == name and field.role == role: |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
235 |
return field |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
236 |
raise FieldNotFound(name) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
237 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
238 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
239 |
def fields_by_name(cls_or_self, name, role='subject'): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
240 |
"""return a list of fields with the given name and role""" |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
241 |
return [field for field in cls_or_self._fieldsattr() |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
242 |
if field.name == name and field.role == role] |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
243 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
244 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
245 |
def remove_field(cls_or_self, field): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
246 |
"""remove a field from form class or instance""" |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
247 |
cls_or_self._fieldsattr().remove(field) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
248 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
249 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
250 |
def append_field(cls_or_self, field): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
251 |
"""append a field to form class or instance""" |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
252 |
cls_or_self._fieldsattr().append(field) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
253 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
254 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
255 |
def insert_field_before(cls_or_self, new_field, name, role='subject'): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
256 |
field = cls_or_self.field_by_name(name, role) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
257 |
fields = cls_or_self._fieldsattr() |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
258 |
fields.insert(fields.index(field), new_field) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
259 |
|
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
260 |
@iclassmethod |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
261 |
def insert_field_after(cls_or_self, new_field, name, role='subject'): |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
262 |
field = cls_or_self.field_by_name(name, role) |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
263 |
fields = cls_or_self._fieldsattr() |
2ceaa4e40348
move low-level form handling to base form class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
264 |
fields.insert(fields.index(field)+1, new_field) |