author | sylvain.thenault@logilab.fr |
Thu, 30 Apr 2009 08:51:54 +0200 | |
branch | tls-sprint |
changeset 1564 | de0548cb0ab8 |
parent 1545 | 53d3d783370f |
parent 1561 | 85752d14c15d |
child 1651 | 675873ed82e3 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract form classes for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
751 | 4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
9 |
from warnings import warn |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
10 |
|
844 | 11 |
from logilab.common.compat import any |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
12 |
from logilab.common.decorators import iclassmethod |
0 | 13 |
|
1147 | 14 |
from cubicweb.appobject import AppRsetObject |
15 |
from cubicweb.selectors import yes, non_final_entity, match_kwargs, one_line_rset |
|
1133 | 16 |
from cubicweb.view import NOINDEX, NOFOLLOW |
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
17 |
from cubicweb.common import tags |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
18 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param, stdmsgs |
0 | 19 |
from cubicweb.web.httpcache import NoHTTPCacheManager |
1147 | 20 |
from cubicweb.web.controller import NAV_FORM_PARAMETERS |
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
21 |
from cubicweb.web.formfields import (Field, StringField, RelationField, |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
22 |
HiddenInitialValueField) |
1147 | 23 |
from cubicweb.web.formrenderers import FormRenderer |
24 |
from cubicweb.web import formwidgets as fwdgs |
|
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
25 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
26 |
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
|
27 |
"""abstract form view mix-in""" |
0 | 28 |
category = 'form' |
29 |
controller = 'edit' |
|
30 |
http_cache_manager = NoHTTPCacheManager |
|
31 |
add_to_breadcrumbs = False |
|
1437 | 32 |
|
0 | 33 |
def html_headers(self): |
34 |
"""return a list of html headers (eg something to be inserted between |
|
35 |
<head> and </head> of the returned page |
|
36 |
||
37 |
by default forms are neither indexed nor followed |
|
38 |
""" |
|
39 |
return [NOINDEX, NOFOLLOW] |
|
1437 | 40 |
|
0 | 41 |
def linkable(self): |
42 |
"""override since forms are usually linked by an action, |
|
43 |
so we don't want them to be listed by appli.possible_views |
|
44 |
""" |
|
45 |
return False |
|
46 |
||
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
47 |
|
1437 | 48 |
# 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
|
49 |
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
|
50 |
"""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
|
51 |
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
|
52 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
53 |
|
1289
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
54 |
def initialize_varmaker(self): |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
55 |
varmaker = self.req.get_page_data('rql_varmaker') |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
56 |
if varmaker is None: |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
57 |
varmaker = self.req.varmaker |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
58 |
self.req.set_page_data('rql_varmaker', varmaker) |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
59 |
self.varmaker = varmaker |
0 | 60 |
|
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 |
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
|
62 |
"""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
|
63 |
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
|
64 |
""" |
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
|
65 |
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
|
66 |
|
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 |
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
|
68 |
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
|
69 |
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
|
70 |
|
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
|
71 |
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
|
72 |
# 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
|
73 |
# 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
|
74 |
# 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
|
75 |
# 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
|
76 |
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
|
77 |
if forminfo: |
1545 | 78 |
self.req.data['formvalues'] = forminfo['values'] |
79 |
self.req.data['formerrors'] = errex = forminfo['errors'] |
|
80 |
self.req.data['displayederrors'] = 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
|
81 |
# 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
|
82 |
# get the original variable name from its attributed eid |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
83 |
foreid = errex.entity |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
84 |
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
|
85 |
if foreid == eid: |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
86 |
errex.eid = var |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
87 |
break |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
88 |
else: |
1437 | 89 |
errex.eid = foreid |
90 |
||
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
91 |
# 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
|
92 |
|
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
93 |
domid = 'entityForm' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
94 |
category = 'form' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
95 |
controller = 'edit' |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
96 |
http_cache_manager = NoHTTPCacheManager |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
97 |
add_to_breadcrumbs = False |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
98 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
99 |
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
|
100 |
"""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
|
101 |
<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
|
102 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
103 |
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
|
104 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
105 |
return [NOINDEX, NOFOLLOW] |
1437 | 106 |
|
1318
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
107 |
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
|
108 |
"""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
|
109 |
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
|
110 |
""" |
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
111 |
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
|
112 |
|
50e1a778c5ee
new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents:
1315
diff
changeset
|
113 |
|
1147 | 114 |
def button(self, label, klass='validateButton', tabindex=None, **kwargs): |
115 |
if tabindex is None: |
|
116 |
tabindex = self.req.next_tabindex() |
|
117 |
return tags.input(value=label, klass=klass, **kwargs) |
|
118 |
||
119 |
def action_button(self, label, onclick=None, __action=None, **kwargs): |
|
120 |
if onclick is None: |
|
121 |
onclick = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
122 |
__action, label, self.domid) |
|
123 |
return self.button(label, onclick=onclick, **kwargs) |
|
124 |
||
125 |
def button_ok(self, label=None, type='submit', name='defaultsubmit', |
|
126 |
**kwargs): |
|
127 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
128 |
return self.button(label, name=name, type=type, **kwargs) |
|
1437 | 129 |
|
1147 | 130 |
def button_apply(self, label=None, type='button', **kwargs): |
131 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
132 |
return self.action_button(label, __action='apply', type=type, **kwargs) |
|
133 |
||
134 |
def button_delete(self, label=None, type='button', **kwargs): |
|
135 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
136 |
return self.action_button(label, __action='delete', type=type, **kwargs) |
|
1437 | 137 |
|
1147 | 138 |
def button_cancel(self, label=None, type='button', **kwargs): |
139 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
140 |
return self.action_button(label, __action='cancel', type=type, **kwargs) |
|
1437 | 141 |
|
1147 | 142 |
def button_reset(self, label=None, type='reset', name='__action_cancel', |
143 |
**kwargs): |
|
144 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
145 |
return self.button(label, type=type, **kwargs) |
|
146 |
||
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
147 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
148 |
"""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
|
149 |
""" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
150 |
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
|
151 |
if entity.get_widget(rschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
152 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
153 |
# 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
|
154 |
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
|
155 |
assert len(targettypes) == 1, \ |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
156 |
"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
|
157 |
ttype = targettypes[0] |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
162 |
return False |
1437 | 163 |
|
0 | 164 |
def error_message(self): |
165 |
"""return formatted error message |
|
166 |
||
167 |
This method should be called once inlined field errors has been consumed |
|
168 |
""" |
|
169 |
errex = self.req.data.get('formerrors') |
|
170 |
# get extra errors |
|
171 |
if errex is not None: |
|
172 |
errormsg = self.req._('please correct the following errors:') |
|
173 |
displayed = self.req.data['displayederrors'] |
|
174 |
errors = sorted((field, err) for field, err in errex.errors.items() |
|
175 |
if not field in displayed) |
|
176 |
if errors: |
|
177 |
if len(errors) > 1: |
|
1437 | 178 |
templstr = '<li>%s</li>\n' |
0 | 179 |
else: |
180 |
templstr = ' %s\n' |
|
181 |
for field, err in errors: |
|
182 |
if field is None: |
|
183 |
errormsg += templstr % err |
|
184 |
else: |
|
185 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
186 |
if len(errors) > 1: |
|
187 |
errormsg = '<ul>%s</ul>' % errormsg |
|
188 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
189 |
return u'' |
|
844 | 190 |
|
191 |
||
192 |
############################################################################### |
|
193 |
||
194 |
class metafieldsform(type): |
|
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
195 |
"""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
|
196 |
and put them into a single ordered list: '_fields_'. |
1393 | 197 |
""" |
844 | 198 |
def __new__(mcs, name, bases, classdict): |
199 |
allfields = [] |
|
200 |
for base in bases: |
|
201 |
if hasattr(base, '_fields_'): |
|
202 |
allfields += base._fields_ |
|
203 |
clsfields = (item for item in classdict.items() |
|
204 |
if isinstance(item[1], Field)) |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
205 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 206 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
207 |
field.set_name(fieldname) |
844 | 208 |
allfields.append(field) |
209 |
classdict['_fields_'] = allfields |
|
210 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
1270 | 211 |
|
1437 | 212 |
|
1270 | 213 |
class FieldNotFound(Exception): |
214 |
"""raised by field_by_name when a field with the given name has not been |
|
215 |
found |
|
216 |
""" |
|
1437 | 217 |
|
1147 | 218 |
class FieldsForm(FormMixIn, AppRsetObject): |
844 | 219 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
220 |
__registry__ = 'forms' |
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
221 |
__select__ = yes() |
1437 | 222 |
|
1147 | 223 |
is_subform = False |
1437 | 224 |
|
1147 | 225 |
# attributes overrideable through __init__ |
1082 | 226 |
internal_fields = ('__errorurl',) + NAV_FORM_PARAMETERS |
1311
4cc6e2723dc7
move ajax.js to base form class
sylvain.thenault@logilab.fr
parents:
1308
diff
changeset
|
227 |
needs_js = ('cubicweb.ajax.js', 'cubicweb.edition.js',) |
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
228 |
needs_css = ('cubicweb.form.css',) |
1147 | 229 |
domid = 'form' |
230 |
title = None |
|
231 |
action = None |
|
232 |
onsubmit = "return freezeFormButtons('%(domid)s');" |
|
233 |
cssclass = None |
|
234 |
cssstyle = None |
|
235 |
cwtarget = None |
|
236 |
redirect_path = None |
|
237 |
set_error_url = True |
|
238 |
copy_nav_params = False |
|
1304 | 239 |
form_buttons = None # form buttons (button widgets instances) |
1437 | 240 |
|
1271
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
241 |
def __init__(self, req, rset=None, row=None, col=None, submitmsg=None, |
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
242 |
**kwargs): |
1147 | 243 |
super(FieldsForm, self).__init__(req, rset, row=row, col=col) |
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
244 |
self.fields = list(self.__class__._fields_) |
1147 | 245 |
for key, val in kwargs.items(): |
1400
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
246 |
if key in NAV_FORM_PARAMETERS: |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
247 |
self.form_add_hidden(key, val) |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
248 |
else: |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
249 |
assert hasattr(self.__class__, key) and not key[0] == '_', key |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
250 |
setattr(self, key, val) |
1147 | 251 |
if self.set_error_url: |
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
|
252 |
self.form_add_hidden('__errorurl', self.session_key()) |
1147 | 253 |
if self.copy_nav_params: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
254 |
for param in NAV_FORM_PARAMETERS: |
1400
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
255 |
if not param in kwargs: |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
256 |
value = req.form.get(param) |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
257 |
if value: |
5926626fb27b
fix navigation parameters handling: those given in kwargs should always be there
sylvain.thenault@logilab.fr
parents:
1393
diff
changeset
|
258 |
self.form_add_hidden(param, value) |
1271
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
259 |
if submitmsg is not None: |
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
260 |
self.form_add_hidden('__message', submitmsg) |
1053 | 261 |
self.context = None |
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
|
262 |
if 'domid' in kwargs:# session key changed |
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
|
263 |
self.restore_previous_post(self.session_key()) |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
264 |
|
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
265 |
@iclassmethod |
1183
62afd820d3ae
field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents:
1175
diff
changeset
|
266 |
def field_by_name(cls_or_self, name, role='subject'): |
1270 | 267 |
"""return field with the given name and role""" |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
268 |
if isinstance(cls_or_self, type): |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
269 |
fields = cls_or_self._fields_ |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
270 |
else: |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
271 |
fields = cls_or_self.fields |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
272 |
for field in fields: |
1183
62afd820d3ae
field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents:
1175
diff
changeset
|
273 |
if field.name == name and field.role == role: |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
274 |
return field |
1270 | 275 |
raise FieldNotFound(name) |
1437 | 276 |
|
1147 | 277 |
@iclassmethod |
278 |
def remove_field(cls_or_self, field): |
|
1391 | 279 |
"""remove a field from form class or instance""" |
1147 | 280 |
if isinstance(cls_or_self, type): |
281 |
fields = cls_or_self._fields_ |
|
282 |
else: |
|
283 |
fields = cls_or_self.fields |
|
284 |
fields.remove(field) |
|
1437 | 285 |
|
1272 | 286 |
@iclassmethod |
287 |
def append_field(cls_or_self, field): |
|
1391 | 288 |
"""append a field to form class or instance""" |
1272 | 289 |
if isinstance(cls_or_self, type): |
290 |
fields = cls_or_self._fields_ |
|
291 |
else: |
|
292 |
fields = cls_or_self.fields |
|
293 |
fields.append(field) |
|
1437 | 294 |
|
844 | 295 |
@property |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
296 |
def form_needs_multipart(self): |
1391 | 297 |
"""true if the form needs enctype=multipart/form-data""" |
1437 | 298 |
return any(field.needs_multipart for field in self.fields) |
0 | 299 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
300 |
def form_add_hidden(self, name, value=None, **kwargs): |
1391 | 301 |
"""add an hidden field to the form""" |
1147 | 302 |
field = StringField(name=name, widget=fwdgs.HiddenInput, initial=value, |
303 |
**kwargs) |
|
1338
9e038f84dbb3
setdomid should be true when id specified in kwargs
sylvain.thenault@logilab.fr
parents:
1326
diff
changeset
|
304 |
if 'id' in kwargs: |
9e038f84dbb3
setdomid should be true when id specified in kwargs
sylvain.thenault@logilab.fr
parents:
1326
diff
changeset
|
305 |
# by default, hidden input don't set id attribute. If one is |
9e038f84dbb3
setdomid should be true when id specified in kwargs
sylvain.thenault@logilab.fr
parents:
1326
diff
changeset
|
306 |
# explicitly specified, ensure it will be set |
9e038f84dbb3
setdomid should be true when id specified in kwargs
sylvain.thenault@logilab.fr
parents:
1326
diff
changeset
|
307 |
field.widget.setdomid = True |
1406
133476216f4a
define self.fields before it is used ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1400
diff
changeset
|
308 |
self.append_field(field) |
1147 | 309 |
return field |
1437 | 310 |
|
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
311 |
def add_media(self): |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
312 |
"""adds media (CSS & JS) required by this widget""" |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
313 |
if self.needs_js: |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
314 |
self.req.add_js(self.needs_js) |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
315 |
if self.needs_css: |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
316 |
self.req.add_css(self.needs_css) |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
317 |
|
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
318 |
def form_render(self, **values): |
1391 | 319 |
"""render this form, using the renderer given in args or the default |
320 |
FormRenderer() |
|
321 |
""" |
|
844 | 322 |
renderer = values.pop('renderer', FormRenderer()) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
323 |
return renderer.render(self, values) |
844 | 324 |
|
1391 | 325 |
def form_build_context(self, rendervalues=None): |
326 |
"""build form context values (the .context attribute which is a |
|
327 |
dictionary with field instance as key associated to a dictionary |
|
328 |
containing field 'name' (qualified), 'id', 'value' (for display, always |
|
329 |
a string). |
|
330 |
||
331 |
rendervalues is an optional dictionary containing extra kwargs given to |
|
332 |
form_render() |
|
333 |
""" |
|
844 | 334 |
self.context = context = {} |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
335 |
# on validation error, we get a dictionary of previously submitted |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
336 |
# values |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
337 |
self._previous_values = self.req.data.get('formvalues', {}) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
338 |
# ensure rendervalues is a dict |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
339 |
if rendervalues is None: |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
340 |
rendervalues = {} |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
341 |
for field in self.fields: |
894 | 342 |
for field in field.actual_fields(self): |
1304 | 343 |
field.form_init(self) |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
344 |
value = self.form_field_display_value(field, rendervalues) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
345 |
context[field] = {'value': value, |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
346 |
'name': self.form_field_name(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
347 |
'id': self.form_field_id(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
348 |
} |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
349 |
|
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
350 |
def form_field_display_value(self, field, rendervalues, load_bytes=False): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
351 |
"""return field's *string* value to use for display |
1437 | 352 |
|
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
353 |
looks in |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
354 |
1. previously submitted form values if any (eg on validation error) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
355 |
2. req.form |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
356 |
3. extra kw args given to render_form |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
357 |
4. field's typed value |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
358 |
|
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
359 |
values found in 1. and 2. are expected te be already some 'display' |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
360 |
value while those found in 3. and 4. are expected to be correctly typed. |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
361 |
""" |
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
362 |
qname = self.form_field_name(field) |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
363 |
if qname in self._previous_values: |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
364 |
value = self._previous_values[qname] |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
365 |
elif qname in self.req.form: |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
366 |
value = self.req.form[qname] |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
367 |
else: |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
368 |
if field.name in rendervalues: |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
369 |
value = rendervalues[field.name] |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
370 |
else: |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
371 |
value = self.form_field_value(field, load_bytes) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
372 |
if callable(value): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
373 |
value = value(self) |
1437 | 374 |
if value != INTERNAL_FIELD_VALUE: |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
375 |
value = field.format_value(self.req, value) |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
376 |
return value |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
377 |
|
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
378 |
def form_field_value(self, field, load_bytes=False): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
379 |
"""return field's *typed* value""" |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
380 |
value = field.initial |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
381 |
if callable(value): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
382 |
value = value(self) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
383 |
return value |
1437 | 384 |
|
1147 | 385 |
def form_field_error(self, field): |
386 |
"""return validation error for widget's field, if any""" |
|
387 |
errex = self.req.data.get('formerrors') |
|
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
388 |
if errex and self._errex_match_field(errex, field): |
1147 | 389 |
self.req.data['displayederrors'].add(field.name) |
390 |
return u'<span class="error">%s</span>' % errex.errors[field.name] |
|
391 |
return u'' |
|
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
392 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
393 |
def form_field_format(self, field): |
1393 | 394 |
"""return MIME type used for the given (text or bytes) field""" |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
395 |
return self.req.property_value('ui.default-text-format') |
1437 | 396 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
397 |
def form_field_encoding(self, field): |
1393 | 398 |
"""return encoding used for the given (text) field""" |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
399 |
return self.req.encoding |
1437 | 400 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
401 |
def form_field_name(self, field): |
1393 | 402 |
"""return qualified name for the given field""" |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
403 |
return field.name |
844 | 404 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
405 |
def form_field_id(self, field): |
1393 | 406 |
"""return dom id for the given field""" |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
407 |
return field.id |
1437 | 408 |
|
1183
62afd820d3ae
field_by_name now takes a "role" argument, override it in AutomaticForm to call guess field if necessary
sylvain.thenault@logilab.fr
parents:
1175
diff
changeset
|
409 |
def form_field_vocabulary(self, field, limit=None): |
1393 | 410 |
"""return vocabulary for the given field. Should be overriden in |
411 |
specific forms using fields which requires some vocabulary |
|
412 |
""" |
|
844 | 413 |
raise NotImplementedError |
414 |
||
1393 | 415 |
def _errex_match_field(self, errex, field): |
416 |
"""return true if the field has some error in given validation exception |
|
417 |
""" |
|
418 |
return field.name in errex.errors |
|
419 |
||
1437 | 420 |
|
844 | 421 |
class EntityFieldsForm(FieldsForm): |
1147 | 422 |
__select__ = (match_kwargs('entity') | (one_line_rset & non_final_entity())) |
1437 | 423 |
|
1561
85752d14c15d
[form] __maineid missing from the special fields list
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1530
diff
changeset
|
424 |
internal_fields = FieldsForm.internal_fields + ('__type', 'eid', '__maineid') |
1147 | 425 |
domid = 'entityForm' |
1437 | 426 |
|
844 | 427 |
def __init__(self, *args, **kwargs): |
1147 | 428 |
self.edited_entity = kwargs.pop('entity', None) |
429 |
msg = kwargs.pop('submitmsg', None) |
|
844 | 430 |
super(EntityFieldsForm, self).__init__(*args, **kwargs) |
1147 | 431 |
if self.edited_entity is None: |
1369
49497136e977
must specify col where entity's eid is
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
432 |
self.edited_entity = self.complete_entity(self.row, self.col) |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
433 |
self.form_add_hidden('__type', eidparam=True) |
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
434 |
self.form_add_hidden('eid') |
1147 | 435 |
if msg is not None: |
436 |
# If we need to directly attach the new object to another one |
|
437 |
for linkto in self.req.list_form_param('__linkto'): |
|
438 |
self.form_add_hidden('__linkto', linkto) |
|
439 |
msg = '%s %s' % (msg, self.req._('and linked')) |
|
440 |
self.form_add_hidden('__message', msg) |
|
1519
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
441 |
# in case of direct instanciation |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
442 |
self.schema = self.edited_entity.schema |
5cfc5cc1dd20
fix retreival of previously submitted error for entity forms
sylvain.thenault@logilab.fr
parents:
1451
diff
changeset
|
443 |
self.vreg = self.edited_entity.vreg |
1437 | 444 |
|
1393 | 445 |
def _errex_match_field(self, errex, field): |
446 |
"""return true if the field has some error in given validation exception |
|
447 |
""" |
|
448 |
return errex.eid == self.edited_entity.eid and field.name in errex.errors |
|
449 |
||
450 |
def _relation_vocabulary(self, rtype, targettype, role, |
|
451 |
limit=None, done=None): |
|
452 |
"""return unrelated entities for a given relation and target entity type |
|
453 |
for use in vocabulary |
|
454 |
""" |
|
455 |
if done is None: |
|
456 |
done = set() |
|
457 |
rset = self.edited_entity.unrelated(rtype, targettype, role, limit) |
|
458 |
res = [] |
|
459 |
for entity in rset.entities(): |
|
460 |
if entity.eid in done: |
|
461 |
continue |
|
462 |
done.add(entity.eid) |
|
463 |
res.append((entity.view('combobox'), entity.eid)) |
|
464 |
return res |
|
465 |
||
466 |
def _form_field_default_value(self, field, load_bytes): |
|
467 |
defaultattr = 'default_%s' % field.name |
|
468 |
if hasattr(self.edited_entity, defaultattr): |
|
469 |
# XXX bw compat, default_<field name> on the entity |
|
470 |
warn('found %s on %s, should be set on a specific form' |
|
471 |
% (defaultattr, self.edited_entity.id), DeprecationWarning) |
|
472 |
value = getattr(self.edited_entity, defaultattr) |
|
473 |
if callable(value): |
|
474 |
value = value() |
|
475 |
else: |
|
476 |
value = super(EntityFieldsForm, self).form_field_value(field, |
|
477 |
load_bytes) |
|
478 |
return value |
|
1437 | 479 |
|
1315
86bddd181e03
should set None as default here as well
sylvain.thenault@logilab.fr
parents:
1312
diff
changeset
|
480 |
def form_build_context(self, values=None): |
1393 | 481 |
"""overriden to add edit[s|o] hidden fields and to ensure schema fields |
482 |
have eidparam set to True |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
483 |
|
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
484 |
edit[s|o] hidden fields are used to indicate the value for the |
1393 | 485 |
associated field before the (potential) modification made when |
486 |
submitting the form. |
|
487 |
""" |
|
488 |
eschema = self.edited_entity.e_schema |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
489 |
for field in self.fields[:]: |
894 | 490 |
for field in field.actual_fields(self): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
491 |
fieldname = field.name |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
492 |
if fieldname != 'eid' and ( |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
493 |
(eschema.has_subject_relation(fieldname) or |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
494 |
eschema.has_object_relation(fieldname))): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
495 |
field.eidparam = True |
1393 | 496 |
self.fields.append(HiddenInitialValueField(field)) |
497 |
return super(EntityFieldsForm, self).form_build_context(values) |
|
1437 | 498 |
|
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
499 |
def form_field_value(self, field, load_bytes=False): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
500 |
"""return field's *typed* value |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
501 |
|
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
502 |
overriden to deal with |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
503 |
* special eid / __type / edits- / edito- fields |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
504 |
* lookup for values on edited entities |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
505 |
""" |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
506 |
attr = field.name |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
507 |
entity = self.edited_entity |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
508 |
if attr == 'eid': |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
509 |
return entity.eid |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
510 |
if not field.eidparam: |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
511 |
return super(EntityFieldsForm, self).form_field_value(field, load_bytes) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
512 |
if attr.startswith('edits-') or attr.startswith('edito-'): |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
513 |
# edit[s|o]- fieds must have the actual value stored on the entity |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
514 |
assert hasattr(field, 'visible_field') |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
515 |
vfield = field.visible_field |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
516 |
assert vfield.eidparam |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
517 |
if entity.has_eid(): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
518 |
return self.form_field_value(vfield) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
519 |
return INTERNAL_FIELD_VALUE |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
520 |
if attr == '__type': |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
521 |
return entity.id |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
522 |
if field.role == 'object': |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
523 |
attr = 'reverse_' + attr |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
524 |
elif entity.e_schema.subject_relation(attr).is_final(): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
525 |
attrtype = entity.e_schema.destination(attr) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
526 |
if attrtype == 'Password': |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
527 |
return entity.has_eid() and INTERNAL_FIELD_VALUE or '' |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
528 |
if attrtype == 'Bytes': |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
529 |
if entity.has_eid(): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
530 |
if load_bytes: |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
531 |
return getattr(entity, attr) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
532 |
# XXX value should reflect if some file is already attached |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
533 |
return True |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
534 |
return False |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
535 |
if entity.has_eid(): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
536 |
value = getattr(entity, attr) |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
537 |
else: |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
538 |
value = self._form_field_default_value(field, load_bytes) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
539 |
return value |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
540 |
# non final relation field |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
541 |
if entity.has_eid(): |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
542 |
value = [ent.eid for ent in getattr(entity, attr)] |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
543 |
else: |
1392
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
544 |
value = self._form_field_default_value(field, load_bytes) |
d6279efff7b3
refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents:
1391
diff
changeset
|
545 |
return value |
1437 | 546 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
547 |
def form_field_format(self, field): |
1393 | 548 |
"""return MIME type used for the given (text or bytes) field""" |
1147 | 549 |
entity = self.edited_entity |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
550 |
if field.eidparam and entity.e_schema.has_metadata(field.name, 'format') and ( |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
551 |
entity.has_eid() or '%s_format' % field.name in entity): |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1338
diff
changeset
|
552 |
return self.edited_entity.attr_metadata(field.name, 'format') |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
553 |
return self.req.property_value('ui.default-text-format') |
844 | 554 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
555 |
def form_field_encoding(self, field): |
1393 | 556 |
"""return encoding used for the given (text) field""" |
1147 | 557 |
entity = self.edited_entity |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
558 |
if field.eidparam and entity.e_schema.has_metadata(field.name, 'encoding') and ( |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
559 |
entity.has_eid() or '%s_encoding' % field.name in entity): |
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1338
diff
changeset
|
560 |
return self.edited_entity.attr_metadata(field.name, 'encoding') |
1437 | 561 |
return super(EntityFieldsForm, self).form_field_encoding(field) |
562 |
||
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
563 |
def form_field_name(self, field): |
1393 | 564 |
"""return qualified name for the given field""" |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
565 |
if field.eidparam: |
1147 | 566 |
return eid_param(field.name, self.edited_entity.eid) |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
567 |
return field.name |
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
568 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
569 |
def form_field_id(self, field): |
1393 | 570 |
"""return dom id for the given field""" |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
571 |
if field.eidparam: |
1147 | 572 |
return eid_param(field.id, self.edited_entity.eid) |
847
27c4ebe90d03
prefixed form methods to avoid potential conflicts with field names, button related method, a bit more serious renderer
sylvain.thenault@logilab.fr
parents:
844
diff
changeset
|
573 |
return field.id |
1437 | 574 |
|
1031
1a89683cb687
restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
575 |
def form_field_vocabulary(self, field, limit=None): |
1393 | 576 |
"""return vocabulary for the given field""" |
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
577 |
role, rtype = field.role, field.name |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
578 |
method = '%s_%s_vocabulary' % (role, rtype) |
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
579 |
try: |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
580 |
vocabfunc = getattr(self, method) |
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
581 |
except AttributeError: |
1265
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
582 |
try: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
583 |
# XXX bw compat, <role>_<rtype>_vocabulary on the entity |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
584 |
vocabfunc = getattr(self.edited_entity, method) |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
585 |
except AttributeError: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
586 |
vocabfunc = getattr(self, '%s_relation_vocabulary' % role) |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
587 |
else: |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
588 |
warn('found %s on %s, should be set on a specific form' |
e5cdd5c0dce3
handle sort/internationalizable on Field base class + fix guess_field to deal with internationalizable and default values
sylvain.thenault@logilab.fr
parents:
1184
diff
changeset
|
589 |
% (method, self.edited_entity.id), DeprecationWarning) |
1009 | 590 |
# NOTE: it is the responsibility of `vocabfunc` to sort the result |
591 |
# (direclty through RQL or via a python sort). This is also |
|
592 |
# important because `vocabfunc` might return a list with |
|
593 |
# couples (label, None) which act as separators. In these |
|
594 |
# cases, it doesn't make sense to sort results afterwards. |
|
1031
1a89683cb687
restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
595 |
return vocabfunc(rtype, limit) |
844 | 596 |
|
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
597 |
def subject_relation_vocabulary(self, rtype, limit=None): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
598 |
"""defaut vocabulary method for the given relation, looking for |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
599 |
relation's object entities (i.e. self is the subject) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
600 |
""" |
1147 | 601 |
entity = self.edited_entity |
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
602 |
if isinstance(rtype, basestring): |
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
603 |
rtype = entity.schema.rschema(rtype) |
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
604 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
605 |
assert not rtype.is_final(), rtype |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
606 |
if entity.has_eid(): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
607 |
done = set(e.eid for e in getattr(entity, str(rtype))) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
608 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
609 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
610 |
for objtype in rtype.objects(entity.e_schema): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
611 |
if limit is not None: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
612 |
rsetsize = limit - len(result) |
1393 | 613 |
result += self._relation_vocabulary(rtype, objtype, 'subject', |
614 |
rsetsize, done) |
|
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
615 |
if limit is not None and len(result) >= limit: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
616 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
617 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
618 |
|
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
619 |
def object_relation_vocabulary(self, rtype, limit=None): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
620 |
"""defaut vocabulary method for the given relation, looking for |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
621 |
relation's subject entities (i.e. self is the object) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
622 |
""" |
1147 | 623 |
entity = self.edited_entity |
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
624 |
if isinstance(rtype, basestring): |
1528
864ae7c15ef5
other fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1451
diff
changeset
|
625 |
rtype = entity.schema.rschema(rtype) |
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
626 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
627 |
if entity.has_eid(): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
628 |
done = set(e.eid for e in getattr(entity, 'reverse_%s' % rtype)) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
629 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
630 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
631 |
for subjtype in rtype.subjects(entity.e_schema): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
632 |
if limit is not None: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
633 |
rsetsize = limit - len(result) |
1393 | 634 |
result += self._relation_vocabulary(rtype, subjtype, 'object', |
635 |
rsetsize, done) |
|
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
636 |
if limit is not None and len(result) >= limit: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
637 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
638 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
639 |
|
1451 | 640 |
def subject_in_state_vocabulary(self, rtype, limit=None): |
1393 | 641 |
"""vocabulary method for the in_state relation, looking for relation's |
642 |
object entities (i.e. self is the subject) according to initial_state, |
|
643 |
state_of and next_state relation |
|
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
644 |
""" |
1184
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
645 |
entity = self.edited_entity |
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
646 |
if not entity.has_eid() or not entity.in_state: |
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
647 |
# get the initial state |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
648 |
rql = 'Any S where S state_of ET, ET name %(etype)s, ET initial_state S' |
1184
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
649 |
rset = self.req.execute(rql, {'etype': str(entity.e_schema)}) |
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
650 |
if rset: |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
651 |
return [(rset.get_entity(0, 0).view('combobox'), rset[0][0])] |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
652 |
return [] |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
653 |
results = [] |
1184
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
654 |
for tr in entity.in_state[0].transitions(entity): |
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
655 |
state = tr.destination_state[0] |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
656 |
results.append((state.view('combobox'), state.eid)) |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
657 |
return sorted(results) |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
658 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
659 |
|
1147 | 660 |
class CompositeForm(FieldsForm): |
661 |
"""form composed for sub-forms""" |
|
1437 | 662 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
663 |
def __init__(self, *args, **kwargs): |
1147 | 664 |
super(CompositeForm, self).__init__(*args, **kwargs) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
665 |
self.forms = [] |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
666 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
667 |
def form_add_subform(self, subform): |
1393 | 668 |
"""mark given form as a subform and append it""" |
1147 | 669 |
subform.is_subform = True |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
670 |
self.forms.append(subform) |