author | sylvain.thenault@logilab.fr |
Thu, 09 Apr 2009 12:55:00 +0200 | |
branch | tls-sprint |
changeset 1315 | 86bddd181e03 |
parent 1312 | df7f847e1388 |
child 1318 | 50e1a778c5ee |
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 |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
26 |
|
0 | 27 |
|
1147 | 28 |
# XXX should disappear |
0 | 29 |
class FormMixIn(object): |
944
e9da555f19c5
[forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
913
diff
changeset
|
30 |
"""abstract form mix-in |
e9da555f19c5
[forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
913
diff
changeset
|
31 |
XXX: you should inherit from this FIRST (obscure pb with super call)""" |
0 | 32 |
category = 'form' |
33 |
controller = 'edit' |
|
34 |
domid = 'entityForm' |
|
35 |
||
36 |
http_cache_manager = NoHTTPCacheManager |
|
37 |
add_to_breadcrumbs = False |
|
38 |
skip_relations = set() |
|
39 |
||
1147 | 40 |
def __init__(self, req, rset, **kwargs): |
41 |
super(FormMixIn, self).__init__(req, rset, **kwargs) |
|
0 | 42 |
self.maxrelitems = self.req.property_value('navigation.related-limit') |
43 |
self.maxcomboitems = self.req.property_value('navigation.combobox-limit') |
|
44 |
self.force_display = not not req.form.get('__force_display') |
|
45 |
# get validation session data which may have been previously set. |
|
46 |
# deleting validation errors here breaks form reloading (errors are |
|
47 |
# no more available), they have to be deleted by application's publish |
|
48 |
# method on successful commit |
|
49 |
formurl = req.url() |
|
50 |
forminfo = req.get_session_data(formurl) |
|
51 |
if forminfo: |
|
52 |
req.data['formvalues'] = forminfo['values'] |
|
53 |
req.data['formerrors'] = errex = forminfo['errors'] |
|
54 |
req.data['displayederrors'] = set() |
|
55 |
# if some validation error occured on entity creation, we have to |
|
56 |
# get the original variable name from its attributed eid |
|
57 |
foreid = errex.entity |
|
58 |
for var, eid in forminfo['eidmap'].items(): |
|
59 |
if foreid == eid: |
|
60 |
errex.eid = var |
|
61 |
break |
|
62 |
else: |
|
63 |
errex.eid = foreid |
|
64 |
||
65 |
def html_headers(self): |
|
66 |
"""return a list of html headers (eg something to be inserted between |
|
67 |
<head> and </head> of the returned page |
|
68 |
||
69 |
by default forms are neither indexed nor followed |
|
70 |
""" |
|
71 |
return [NOINDEX, NOFOLLOW] |
|
72 |
||
73 |
def linkable(self): |
|
74 |
"""override since forms are usually linked by an action, |
|
75 |
so we don't want them to be listed by appli.possible_views |
|
76 |
""" |
|
77 |
return False |
|
78 |
||
79 |
@property |
|
80 |
def limit(self): |
|
81 |
if self.force_display: |
|
82 |
return None |
|
83 |
return self.maxrelitems + 1 |
|
1147 | 84 |
|
1289
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
85 |
def initialize_varmaker(self): |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
86 |
varmaker = self.req.get_page_data('rql_varmaker') |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
87 |
if varmaker is None: |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
88 |
varmaker = self.req.varmaker |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
89 |
self.req.set_page_data('rql_varmaker', varmaker) |
5eff2ef92977
move initialize_varmaker on the FormMixIn class
sylvain.thenault@logilab.fr
parents:
1272
diff
changeset
|
90 |
self.varmaker = varmaker |
0 | 91 |
|
1147 | 92 |
def button(self, label, klass='validateButton', tabindex=None, **kwargs): |
93 |
if tabindex is None: |
|
94 |
tabindex = self.req.next_tabindex() |
|
95 |
return tags.input(value=label, klass=klass, **kwargs) |
|
96 |
||
97 |
def action_button(self, label, onclick=None, __action=None, **kwargs): |
|
98 |
if onclick is None: |
|
99 |
onclick = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
100 |
__action, label, self.domid) |
|
101 |
return self.button(label, onclick=onclick, **kwargs) |
|
102 |
||
103 |
def button_ok(self, label=None, type='submit', name='defaultsubmit', |
|
104 |
**kwargs): |
|
105 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
106 |
return self.button(label, name=name, type=type, **kwargs) |
|
107 |
||
108 |
def button_apply(self, label=None, type='button', **kwargs): |
|
109 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
110 |
return self.action_button(label, __action='apply', type=type, **kwargs) |
|
111 |
||
112 |
def button_delete(self, label=None, type='button', **kwargs): |
|
113 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
114 |
return self.action_button(label, __action='delete', type=type, **kwargs) |
|
115 |
||
116 |
def button_cancel(self, label=None, type='button', **kwargs): |
|
117 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
118 |
return self.action_button(label, __action='cancel', type=type, **kwargs) |
|
119 |
||
120 |
def button_reset(self, label=None, type='reset', name='__action_cancel', |
|
121 |
**kwargs): |
|
122 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
123 |
return self.button(label, type=type, **kwargs) |
|
124 |
||
125 |
# XXX deprecated with new form system |
|
1305
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
126 |
|
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
127 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
128 |
"""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
|
129 |
""" |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
130 |
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
|
131 |
if entity.get_widget(rschema, x).need_multipart: |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
132 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
133 |
# 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
|
134 |
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
|
135 |
assert len(targettypes) == 1, \ |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
136 |
"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
|
137 |
ttype = targettypes[0] |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
return True |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
142 |
return False |
395ef7f2b95b
cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
143 |
|
0 | 144 |
def error_message(self): |
145 |
"""return formatted error message |
|
146 |
||
147 |
This method should be called once inlined field errors has been consumed |
|
148 |
""" |
|
149 |
errex = self.req.data.get('formerrors') |
|
150 |
# get extra errors |
|
151 |
if errex is not None: |
|
152 |
errormsg = self.req._('please correct the following errors:') |
|
153 |
displayed = self.req.data['displayederrors'] |
|
154 |
errors = sorted((field, err) for field, err in errex.errors.items() |
|
155 |
if not field in displayed) |
|
156 |
if errors: |
|
157 |
if len(errors) > 1: |
|
158 |
templstr = '<li>%s</li>\n' |
|
159 |
else: |
|
160 |
templstr = ' %s\n' |
|
161 |
for field, err in errors: |
|
162 |
if field is None: |
|
163 |
errormsg += templstr % err |
|
164 |
else: |
|
165 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
166 |
if len(errors) > 1: |
|
167 |
errormsg = '<ul>%s</ul>' % errormsg |
|
168 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
169 |
return u'' |
|
844 | 170 |
|
171 |
||
172 |
############################################################################### |
|
173 |
||
174 |
class metafieldsform(type): |
|
175 |
def __new__(mcs, name, bases, classdict): |
|
176 |
allfields = [] |
|
177 |
for base in bases: |
|
178 |
if hasattr(base, '_fields_'): |
|
179 |
allfields += base._fields_ |
|
180 |
clsfields = (item for item in classdict.items() |
|
181 |
if isinstance(item[1], Field)) |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
182 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 183 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
184 |
field.set_name(fieldname) |
844 | 185 |
allfields.append(field) |
186 |
classdict['_fields_'] = allfields |
|
187 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
1270 | 188 |
|
844 | 189 |
|
1270 | 190 |
class FieldNotFound(Exception): |
191 |
"""raised by field_by_name when a field with the given name has not been |
|
192 |
found |
|
193 |
""" |
|
194 |
||
1147 | 195 |
class FieldsForm(FormMixIn, AppRsetObject): |
844 | 196 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
197 |
__registry__ = 'forms' |
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
198 |
__select__ = yes() |
1147 | 199 |
|
200 |
is_subform = False |
|
201 |
||
202 |
# attributes overrideable through __init__ |
|
1082 | 203 |
internal_fields = ('__errorurl',) + NAV_FORM_PARAMETERS |
1311
4cc6e2723dc7
move ajax.js to base form class
sylvain.thenault@logilab.fr
parents:
1308
diff
changeset
|
204 |
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
|
205 |
needs_css = ('cubicweb.form.css',) |
1147 | 206 |
domid = 'form' |
207 |
title = None |
|
208 |
action = None |
|
209 |
onsubmit = "return freezeFormButtons('%(domid)s');" |
|
210 |
cssclass = None |
|
211 |
cssstyle = None |
|
212 |
cwtarget = None |
|
213 |
redirect_path = None |
|
214 |
set_error_url = True |
|
215 |
copy_nav_params = False |
|
1304 | 216 |
form_buttons = None # form buttons (button widgets instances) |
1147 | 217 |
|
1271
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
218 |
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
|
219 |
**kwargs): |
1147 | 220 |
super(FieldsForm, self).__init__(req, rset, row=row, col=col) |
221 |
for key, val in kwargs.items(): |
|
222 |
assert hasattr(self.__class__, key) and not key[0] == '_', key |
|
223 |
setattr(self, key, val) |
|
844 | 224 |
self.fields = list(self.__class__._fields_) |
1147 | 225 |
if self.set_error_url: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
226 |
self.form_add_hidden('__errorurl', req.url()) |
1147 | 227 |
if self.copy_nav_params: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
228 |
for param in NAV_FORM_PARAMETERS: |
1147 | 229 |
value = kwargs.get(param, req.form.get(param)) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
230 |
if value: |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
231 |
self.form_add_hidden(param, initial=value) |
1271
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
232 |
if submitmsg is not None: |
0a5257f5d723
handle __message in FieldsForm, not only in EntityFieldsForm
sylvain.thenault@logilab.fr
parents:
1270
diff
changeset
|
233 |
self.form_add_hidden('__message', submitmsg) |
1053 | 234 |
self.context = None |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
235 |
|
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
236 |
@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
|
237 |
def field_by_name(cls_or_self, name, role='subject'): |
1270 | 238 |
"""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
|
239 |
if isinstance(cls_or_self, type): |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
240 |
fields = cls_or_self._fields_ |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
241 |
else: |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
242 |
fields = cls_or_self.fields |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
243 |
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
|
244 |
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
|
245 |
return field |
1270 | 246 |
raise FieldNotFound(name) |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
247 |
|
1147 | 248 |
@iclassmethod |
249 |
def remove_field(cls_or_self, field): |
|
250 |
if isinstance(cls_or_self, type): |
|
251 |
fields = cls_or_self._fields_ |
|
252 |
else: |
|
253 |
fields = cls_or_self.fields |
|
254 |
fields.remove(field) |
|
255 |
||
1272 | 256 |
@iclassmethod |
257 |
def append_field(cls_or_self, field): |
|
258 |
if isinstance(cls_or_self, type): |
|
259 |
fields = cls_or_self._fields_ |
|
260 |
else: |
|
261 |
fields = cls_or_self.fields |
|
262 |
fields.append(field) |
|
263 |
||
844 | 264 |
@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
|
265 |
def form_needs_multipart(self): |
844 | 266 |
return any(field.needs_multipart for field in self.fields) |
0 | 267 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
268 |
def form_add_hidden(self, name, value=None, **kwargs): |
1147 | 269 |
field = StringField(name=name, widget=fwdgs.HiddenInput, initial=value, |
270 |
**kwargs) |
|
271 |
self.fields.append(field) |
|
272 |
return field |
|
273 |
||
1097
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
274 |
def add_media(self): |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
275 |
"""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
|
276 |
if self.needs_js: |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
277 |
self.req.add_js(self.needs_js) |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
278 |
if self.needs_css: |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
279 |
self.req.add_css(self.needs_css) |
611bacbbe001
pylint fixes, media definitions on form as well
sylvain.thenault@logilab.fr
parents:
1082
diff
changeset
|
280 |
|
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
|
281 |
def form_render(self, **values): |
844 | 282 |
renderer = values.pop('renderer', FormRenderer()) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
283 |
return renderer.render(self, values) |
844 | 284 |
|
1312 | 285 |
def form_build_context(self, values=None): |
844 | 286 |
self.context = context = {} |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
287 |
# on validation error, we get a dictionnary of previously submitted values |
1312 | 288 |
if values is None: |
289 |
values = {} |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
290 |
previous_values = self.req.data.get('formvalues') |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
291 |
if previous_values: |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
292 |
values.update(previous_values) |
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
|
293 |
for field in self.fields: |
894 | 294 |
for field in field.actual_fields(self): |
1304 | 295 |
field.form_init(self) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
296 |
value = self.form_field_value(field, values) |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
297 |
context[field] = {'value': field.format_value(self.req, value), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
298 |
'rawvalue': value, |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
299 |
'name': self.form_field_name(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
300 |
'id': self.form_field_id(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
301 |
} |
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
|
302 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
303 |
def form_field_value(self, field, values, load_bytes=False): |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
304 |
"""looks for field's value in |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
305 |
1. kw args given to render_form (including previously submitted form |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
306 |
values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
307 |
2. req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
308 |
3. field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
309 |
""" |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
310 |
if field.name in values: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
311 |
value = values[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
312 |
elif field.name in self.req.form: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
313 |
value = self.req.form[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
314 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
315 |
value = field.initial |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
316 |
return value |
1147 | 317 |
|
318 |
def form_field_error(self, field): |
|
319 |
"""return validation error for widget's field, if any""" |
|
320 |
errex = self.req.data.get('formerrors') |
|
321 |
if errex and field.name in errex.errors: |
|
322 |
self.req.data['displayederrors'].add(field.name) |
|
323 |
return u'<span class="error">%s</span>' % errex.errors[field.name] |
|
324 |
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
|
325 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
326 |
def form_field_format(self, field): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
327 |
return self.req.property_value('ui.default-text-format') |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
328 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
329 |
def form_field_encoding(self, field): |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
330 |
return self.req.encoding |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
331 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
332 |
def form_field_name(self, 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
|
333 |
return field.name |
844 | 334 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
335 |
def form_field_id(self, 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
|
336 |
return field.id |
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
|
337 |
|
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
|
338 |
def form_field_vocabulary(self, field, limit=None): |
844 | 339 |
raise NotImplementedError |
340 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
341 |
|
844 | 342 |
class EntityFieldsForm(FieldsForm): |
1147 | 343 |
__select__ = (match_kwargs('entity') | (one_line_rset & non_final_entity())) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
344 |
|
1082 | 345 |
internal_fields = FieldsForm.internal_fields + ('__type', 'eid') |
1147 | 346 |
domid = 'entityForm' |
1082 | 347 |
|
844 | 348 |
def __init__(self, *args, **kwargs): |
1147 | 349 |
self.edited_entity = kwargs.pop('entity', None) |
350 |
msg = kwargs.pop('submitmsg', None) |
|
844 | 351 |
super(EntityFieldsForm, self).__init__(*args, **kwargs) |
1147 | 352 |
if self.edited_entity is None: |
353 |
self.edited_entity = self.complete_entity(self.row) |
|
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
354 |
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
|
355 |
self.form_add_hidden('eid') |
1147 | 356 |
if msg is not None: |
357 |
# If we need to directly attach the new object to another one |
|
358 |
for linkto in self.req.list_form_param('__linkto'): |
|
359 |
self.form_add_hidden('__linkto', linkto) |
|
360 |
msg = '%s %s' % (msg, self.req._('and linked')) |
|
361 |
self.form_add_hidden('__message', msg) |
|
844 | 362 |
|
1315
86bddd181e03
should set None as default here as well
sylvain.thenault@logilab.fr
parents:
1312
diff
changeset
|
363 |
def form_build_context(self, values=None): |
1147 | 364 |
self.form_add_entity_hiddens(self.edited_entity.e_schema) |
1308
3d01229d9f6a
override form_build_context instead of form_render so we get sure to call form_add_entity_hiddens even on subforms
sylvain.thenault@logilab.fr
parents:
1305
diff
changeset
|
365 |
return super(EntityFieldsForm, self).form_build_context(values) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
366 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
367 |
def form_add_entity_hiddens(self, eschema): |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
368 |
for field in self.fields[:]: |
894 | 369 |
for field in field.actual_fields(self): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
370 |
fieldname = field.name |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
371 |
if fieldname != 'eid' and ( |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
372 |
(eschema.has_subject_relation(fieldname) or |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
373 |
eschema.has_object_relation(fieldname))): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
374 |
field.eidparam = True |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
375 |
self.fields.append(self.form_entity_hidden_field(field)) |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
376 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
377 |
def form_entity_hidden_field(self, field): |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
378 |
"""returns the hidden field which will indicate the value |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
379 |
before the modification |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
380 |
""" |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
381 |
# Only RelationField has a `role` attribute, others are used |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
382 |
# to describe attribute fields => role is 'subject' |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
383 |
if getattr(field, 'role', 'subject') == 'subject': |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
384 |
name = 'edits-%s' % field.name |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
385 |
else: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
386 |
name = 'edito-%s' % field.name |
898
583f64567256
fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
897
diff
changeset
|
387 |
return HiddenInitialValueField(field, name=name) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
388 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
389 |
def form_field_value(self, field, values, load_bytes=False): |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
390 |
"""look for field's value with the following rules: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
391 |
1. handle special __type and eid fields |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
392 |
2. looks in kw args given to render_form (including previously submitted |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
393 |
form values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
394 |
3. looks in req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
395 |
4. if entity has an eid: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
396 |
1. looks for an associated attribute / method |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
397 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
398 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
399 |
1. looks for a default_<fieldname> attribute / method on the form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
400 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
401 |
|
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
402 |
values found in step 4 may be a callable which'll then be called. |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
403 |
""" |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
404 |
fieldname = field.name |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
405 |
if fieldname.startswith('edits-') or fieldname.startswith('edito-'): |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
406 |
# edit[s|o]- fieds must have the actual value stored on the entity |
1147 | 407 |
if hasattr(field, 'visible_field'): |
408 |
if self.edited_entity.has_eid(): |
|
409 |
value = self._form_field_entity_value(field.visible_field, |
|
410 |
default_initial=False) |
|
411 |
else: |
|
412 |
value = INTERNAL_FIELD_VALUE |
|
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
413 |
else: |
1147 | 414 |
value = field.initial |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
415 |
elif fieldname == '__type': |
1147 | 416 |
value = self.edited_entity.id |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
417 |
elif fieldname == 'eid': |
1147 | 418 |
value = self.edited_entity.eid |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
419 |
elif fieldname in values: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
420 |
value = values[fieldname] |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
421 |
elif fieldname in self.req.form: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
422 |
value = self.req.form[fieldname] |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
423 |
else: |
1147 | 424 |
if self.edited_entity.has_eid() and field.eidparam: |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
425 |
# use value found on the entity or field's initial value if it's |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
426 |
# not an attribute of the entity (XXX may conflicts and get |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
427 |
# undesired value) |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
428 |
value = self._form_field_entity_value(field, default_initial=True, |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
429 |
load_bytes=load_bytes) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
430 |
else: |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
431 |
defaultattr = 'default_%s' % fieldname |
1147 | 432 |
if hasattr(self.edited_entity, defaultattr): |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
433 |
# XXX bw compat, default_<field name> on the entity |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
434 |
warn('found %s on %s, should be set on a specific form' |
1147 | 435 |
% (defaultattr, self.edited_entity.id), DeprecationWarning) |
436 |
value = getattr(self.edited_entity, defaultattr) |
|
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
437 |
elif hasattr(self, defaultattr): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
438 |
# search for default_<field name> on the form instance |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
439 |
value = getattr(self, defaultattr) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
440 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
441 |
# use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
442 |
value = field.initial |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
443 |
if callable(value): |
1059 | 444 |
value = value() |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
445 |
return value |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
446 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
447 |
def form_field_format(self, field): |
1147 | 448 |
entity = self.edited_entity |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
449 |
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
|
450 |
entity.has_eid() or '%s_format' % field.name in entity): |
1147 | 451 |
return self.edited_entity.attribute_metadata(field.name, 'format') |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
452 |
return self.req.property_value('ui.default-text-format') |
844 | 453 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
454 |
def form_field_encoding(self, field): |
1147 | 455 |
entity = self.edited_entity |
1107
961a478593a5
has_metadata is a schema method
sylvain.thenault@logilab.fr
parents:
1101
diff
changeset
|
456 |
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
|
457 |
entity.has_eid() or '%s_encoding' % field.name in entity): |
1147 | 458 |
return self.edited_entity.attribute_metadata(field.name, 'encoding') |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
459 |
return super(EntityFieldsForm, self).form_field_encoding(field) |
1147 | 460 |
|
461 |
def form_field_error(self, field): |
|
462 |
"""return validation error for widget's field, if any""" |
|
463 |
errex = self.req.data.get('formerrors') |
|
464 |
if errex and errex.eid == self.edited_entity.eid and field.name in errex.errors: |
|
465 |
self.req.data['displayederrors'].add(field.name) |
|
466 |
return u'<span class="error">%s</span>' % errex.errors[field.name] |
|
467 |
return u'' |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
468 |
|
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
469 |
def _form_field_entity_value(self, field, default_initial=True, load_bytes=False): |
1147 | 470 |
attr = field.name |
471 |
entity = self.edited_entity |
|
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
472 |
if field.role == 'object': |
1049
f8e92c2f3bef
fix attribute name for object relation
sylvain.thenault@logilab.fr
parents:
1048
diff
changeset
|
473 |
attr = 'reverse_' + attr |
1147 | 474 |
elif entity.e_schema.subject_relation(attr).is_final(): |
475 |
attrtype = entity.e_schema.destination(attr) |
|
907 | 476 |
if attrtype == 'Password': |
1147 | 477 |
return entity.has_eid() and INTERNAL_FIELD_VALUE or '' |
907 | 478 |
if attrtype == 'Bytes': |
1147 | 479 |
if entity.has_eid(): |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
480 |
if load_bytes: |
1147 | 481 |
return getattr(entity, attr) |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
482 |
# XXX value should reflect if some file is already attached |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
483 |
return True |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
484 |
return False |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
485 |
if default_initial: |
1147 | 486 |
value = getattr(entity, attr, field.initial) |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
487 |
else: |
1147 | 488 |
value = getattr(entity, attr) |
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
|
489 |
if isinstance(field, RelationField): |
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
|
490 |
# in this case, value is the list of related entities |
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
|
491 |
value = [ent.eid for ent in value] |
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
|
492 |
return value |
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
|
493 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
494 |
def form_field_name(self, 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
|
495 |
if field.eidparam: |
1147 | 496 |
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
|
497 |
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
|
498 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
499 |
def form_field_id(self, 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
|
500 |
if field.eidparam: |
1147 | 501 |
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
|
502 |
return field.id |
844 | 503 |
|
1031
1a89683cb687
restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
504 |
def form_field_vocabulary(self, field, limit=None): |
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
|
505 |
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
|
506 |
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
|
507 |
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
|
508 |
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
|
509 |
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
|
510 |
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
|
511 |
# 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
|
512 |
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
|
513 |
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
|
514 |
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
|
515 |
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
|
516 |
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
|
517 |
% (method, self.edited_entity.id), DeprecationWarning) |
1009 | 518 |
# NOTE: it is the responsibility of `vocabfunc` to sort the result |
519 |
# (direclty through RQL or via a python sort). This is also |
|
520 |
# important because `vocabfunc` might return a list with |
|
521 |
# couples (label, None) which act as separators. In these |
|
522 |
# 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
|
523 |
return vocabfunc(rtype, limit) |
844 | 524 |
|
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
525 |
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
|
526 |
"""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
|
527 |
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
|
528 |
""" |
1147 | 529 |
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
|
530 |
if isinstance(rtype, basestring): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
531 |
rtype = entity.schema.rschema(rtype) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
532 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
533 |
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
|
534 |
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
|
535 |
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
|
536 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
537 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
538 |
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
|
539 |
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
|
540 |
rsetsize = limit - len(result) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
541 |
result += self.relation_vocabulary(rtype, objtype, 'subject', |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
542 |
rsetsize, done) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
543 |
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
|
544 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
545 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
546 |
|
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
547 |
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
|
548 |
"""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
|
549 |
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
|
550 |
""" |
1147 | 551 |
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
|
552 |
if isinstance(rtype, basestring): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
553 |
rtype = entity.schema.rschema(rtype) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
554 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
555 |
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
|
556 |
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
|
557 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
558 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
559 |
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
|
560 |
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
|
561 |
rsetsize = limit - len(result) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
562 |
result += self.relation_vocabulary(rtype, subjtype, 'object', |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
563 |
rsetsize, done) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
564 |
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
|
565 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
566 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
567 |
|
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
568 |
def relation_vocabulary(self, rtype, targettype, role, |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
569 |
limit=None, done=None): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
570 |
if done is None: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
571 |
done = set() |
1147 | 572 |
rset = self.edited_entity.unrelated(rtype, targettype, role, limit) |
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
573 |
res = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
574 |
for entity in rset.entities(): |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
575 |
if entity.eid in done: |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
576 |
continue |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
577 |
done.add(entity.eid) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
578 |
res.append((entity.view('combobox'), entity.eid)) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
579 |
return res |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
580 |
|
1175
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
581 |
def subject_in_state_vocabulary(self, rschema, limit=None): |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
582 |
"""vocabulary method for the in_state relation, looking for |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
583 |
relation's object entities (i.e. self is the subject) according |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
584 |
to initial_state, state_of and next_state relation |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
585 |
""" |
1184
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
586 |
entity = self.edited_entity |
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
587 |
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
|
588 |
# get the initial state |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
589 |
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
|
590 |
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
|
591 |
if rset: |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
592 |
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
|
593 |
return [] |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
594 |
results = [] |
1184
a33deae7c6a2
oops, this method doesn't belong anymore to entity class
sylvain.thenault@logilab.fr
parents:
1183
diff
changeset
|
595 |
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
|
596 |
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
|
597 |
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
|
598 |
return sorted(results) |
96747df28a1f
backport subject_in_state_vocabulary to EntityFieldsForm, deprecate the old one
sylvain.thenault@logilab.fr
parents:
1147
diff
changeset
|
599 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
600 |
|
1147 | 601 |
class CompositeForm(FieldsForm): |
602 |
"""form composed for sub-forms""" |
|
603 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
604 |
def __init__(self, *args, **kwargs): |
1147 | 605 |
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
|
606 |
self.forms = [] |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
607 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
608 |
def form_add_subform(self, subform): |
1147 | 609 |
subform.is_subform = True |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
610 |
self.forms.append(subform) |