author | sylvain.thenault@logilab.fr |
Fri, 20 Feb 2009 10:38:34 +0100 | |
branch | tls-sprint |
changeset 888 | 603327e9aef4 |
parent 887 | 51e371245bc5 |
child 892 | 1558772340a7 |
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 |
0 | 10 |
from simplejson import dumps |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
11 |
from mx.DateTime import today |
0 | 12 |
|
844 | 13 |
from logilab.common.compat import any |
0 | 14 |
from logilab.mtconverter import html_escape |
15 |
||
16 |
from cubicweb import typed_eid |
|
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
17 |
from cubicweb.utils import ustrftime |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
18 |
from cubicweb.selectors import match_form_params |
751 | 19 |
from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView |
0 | 20 |
from cubicweb.common.registerers import accepts_registerer |
21 |
from cubicweb.web import stdmsgs |
|
22 |
from cubicweb.web.httpcache import NoHTTPCacheManager |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
23 |
from cubicweb.web.controller import NAV_FORM_PARAMETERS, redirect_params |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
24 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
25 |
|
0 | 26 |
|
27 |
def relation_id(eid, rtype, target, reid): |
|
28 |
if target == 'subject': |
|
29 |
return u'%s:%s:%s' % (eid, rtype, reid) |
|
30 |
return u'%s:%s:%s' % (reid, rtype, eid) |
|
844 | 31 |
|
32 |
def toggable_relation_link(eid, nodeid, label='x'): |
|
33 |
js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid))) |
|
34 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label) |
|
0 | 35 |
|
36 |
||
37 |
class FormMixIn(object): |
|
38 |
"""abstract form mix-in""" |
|
39 |
category = 'form' |
|
40 |
controller = 'edit' |
|
41 |
domid = 'entityForm' |
|
42 |
||
43 |
http_cache_manager = NoHTTPCacheManager |
|
44 |
add_to_breadcrumbs = False |
|
45 |
skip_relations = set() |
|
46 |
||
47 |
def __init__(self, req, rset): |
|
48 |
super(FormMixIn, self).__init__(req, rset) |
|
49 |
self.maxrelitems = self.req.property_value('navigation.related-limit') |
|
50 |
self.maxcomboitems = self.req.property_value('navigation.combobox-limit') |
|
51 |
self.force_display = not not req.form.get('__force_display') |
|
52 |
# get validation session data which may have been previously set. |
|
53 |
# deleting validation errors here breaks form reloading (errors are |
|
54 |
# no more available), they have to be deleted by application's publish |
|
55 |
# method on successful commit |
|
56 |
formurl = req.url() |
|
57 |
forminfo = req.get_session_data(formurl) |
|
58 |
if forminfo: |
|
59 |
req.data['formvalues'] = forminfo['values'] |
|
60 |
req.data['formerrors'] = errex = forminfo['errors'] |
|
61 |
req.data['displayederrors'] = set() |
|
62 |
# if some validation error occured on entity creation, we have to |
|
63 |
# get the original variable name from its attributed eid |
|
64 |
foreid = errex.entity |
|
65 |
for var, eid in forminfo['eidmap'].items(): |
|
66 |
if foreid == eid: |
|
67 |
errex.eid = var |
|
68 |
break |
|
69 |
else: |
|
70 |
errex.eid = foreid |
|
71 |
||
72 |
def html_headers(self): |
|
73 |
"""return a list of html headers (eg something to be inserted between |
|
74 |
<head> and </head> of the returned page |
|
75 |
||
76 |
by default forms are neither indexed nor followed |
|
77 |
""" |
|
78 |
return [NOINDEX, NOFOLLOW] |
|
79 |
||
80 |
def linkable(self): |
|
81 |
"""override since forms are usually linked by an action, |
|
82 |
so we don't want them to be listed by appli.possible_views |
|
83 |
""" |
|
84 |
return False |
|
85 |
||
86 |
@property |
|
87 |
def limit(self): |
|
88 |
if self.force_display: |
|
89 |
return None |
|
90 |
return self.maxrelitems + 1 |
|
91 |
||
92 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
|
93 |
"""return a boolean indicating if form's enctype should be multipart |
|
94 |
""" |
|
95 |
for rschema, _, x in entity.relations_by_category(categories): |
|
96 |
if entity.get_widget(rschema, x).need_multipart: |
|
97 |
return True |
|
98 |
# let's find if any of our inlined entities needs multipart |
|
99 |
for rschema, targettypes, x in entity.relations_by_category('inlineview'): |
|
100 |
assert len(targettypes) == 1, \ |
|
101 |
"I'm not able to deal with several targets and inlineview" |
|
102 |
ttype = targettypes[0] |
|
103 |
inlined_entity = self.vreg.etype_class(ttype)(self.req, None, None) |
|
104 |
for irschema, _, x in inlined_entity.relations_by_category(categories): |
|
105 |
if inlined_entity.get_widget(irschema, x).need_multipart: |
|
106 |
return True |
|
107 |
return False |
|
108 |
||
109 |
def error_message(self): |
|
110 |
"""return formatted error message |
|
111 |
||
112 |
This method should be called once inlined field errors has been consumed |
|
113 |
""" |
|
114 |
errex = self.req.data.get('formerrors') |
|
115 |
# get extra errors |
|
116 |
if errex is not None: |
|
117 |
errormsg = self.req._('please correct the following errors:') |
|
118 |
displayed = self.req.data['displayederrors'] |
|
119 |
errors = sorted((field, err) for field, err in errex.errors.items() |
|
120 |
if not field in displayed) |
|
121 |
if errors: |
|
122 |
if len(errors) > 1: |
|
123 |
templstr = '<li>%s</li>\n' |
|
124 |
else: |
|
125 |
templstr = ' %s\n' |
|
126 |
for field, err in errors: |
|
127 |
if field is None: |
|
128 |
errormsg += templstr % err |
|
129 |
else: |
|
130 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
131 |
if len(errors) > 1: |
|
132 |
errormsg = '<ul>%s</ul>' % errormsg |
|
133 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
134 |
return u'' |
|
135 |
||
136 |
def restore_pending_inserts(self, entity, cell=False): |
|
137 |
"""used to restore edition page as it was before clicking on |
|
138 |
'search for <some entity type>' |
|
139 |
|
|
140 |
""" |
|
141 |
eid = entity.eid |
|
142 |
cell = cell and "div_insert_" or "tr" |
|
143 |
pending_inserts = set(self.req.get_pending_inserts(eid)) |
|
144 |
for pendingid in pending_inserts: |
|
145 |
eidfrom, rtype, eidto = pendingid.split(':') |
|
146 |
if typed_eid(eidfrom) == entity.eid: # subject |
|
147 |
label = display_name(self.req, rtype, 'subject') |
|
148 |
reid = eidto |
|
149 |
else: |
|
150 |
label = display_name(self.req, rtype, 'object') |
|
151 |
reid = eidfrom |
|
152 |
jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \ |
|
153 |
% (pendingid, cell, eid) |
|
154 |
rset = self.req.eid_rset(reid) |
|
155 |
eview = self.view('text', rset, row=0) |
|
156 |
# XXX find a clean way to handle baskets |
|
157 |
if rset.description[0][0] == 'Basket': |
|
158 |
eview = '%s (%s)' % (eview, display_name(self.req, 'Basket')) |
|
159 |
yield rtype, pendingid, jscall, label, reid, eview |
|
160 |
||
161 |
||
162 |
def force_display_link(self): |
|
163 |
return (u'<span class="invisible">' |
|
164 |
u'[<a href="javascript: window.location.href+=\'&__force_display=1\'">%s</a>]' |
|
165 |
u'</span>' % self.req._('view all')) |
|
166 |
||
167 |
def relations_table(self, entity): |
|
168 |
"""yiels 3-tuples (rtype, target, related_list) |
|
169 |
where <related_list> itself a list of : |
|
170 |
- node_id (will be the entity element's DOM id) |
|
171 |
- appropriate javascript's togglePendingDelete() function call |
|
172 |
- status 'pendingdelete' or '' |
|
173 |
- oneline view of related entity |
|
174 |
""" |
|
175 |
eid = entity.eid |
|
176 |
pending_deletes = self.req.get_pending_deletes(eid) |
|
177 |
# XXX (adim) : quick fix to get Folder relations |
|
178 |
for label, rschema, target in entity.srelations_by_category(('generic', 'metadata'), 'add'): |
|
179 |
if rschema in self.skip_relations: |
|
180 |
continue |
|
181 |
relatedrset = entity.related(rschema, target, limit=self.limit) |
|
182 |
toggable_rel_link = self.toggable_relation_link_func(rschema) |
|
183 |
related = [] |
|
184 |
for row in xrange(relatedrset.rowcount): |
|
185 |
nodeid = relation_id(eid, rschema, target, relatedrset[row][0]) |
|
186 |
if nodeid in pending_deletes: |
|
187 |
status = u'pendingDelete' |
|
188 |
label = '+' |
|
189 |
else: |
|
190 |
status = u'' |
|
191 |
label = 'x' |
|
192 |
dellink = toggable_rel_link(eid, nodeid, label) |
|
193 |
eview = self.view('oneline', relatedrset, row=row) |
|
194 |
related.append((nodeid, dellink, status, eview)) |
|
195 |
yield (rschema, target, related) |
|
196 |
||
197 |
def toggable_relation_link_func(self, rschema): |
|
198 |
if not rschema.has_perm(self.req, 'delete'): |
|
199 |
return lambda x, y, z: u'' |
|
200 |
return toggable_relation_link |
|
201 |
||
202 |
||
203 |
def redirect_url(self, entity=None): |
|
204 |
"""return a url to use as next direction if there are some information |
|
205 |
specified in current form params, else return the result the reset_url |
|
206 |
method which should be defined in concrete classes |
|
207 |
""" |
|
208 |
rparams = redirect_params(self.req.form) |
|
209 |
if rparams: |
|
210 |
return self.build_url('view', **rparams) |
|
211 |
return self.reset_url(entity) |
|
212 |
||
213 |
def reset_url(self, entity): |
|
214 |
raise NotImplementedError('implement me in concrete classes') |
|
215 |
||
216 |
BUTTON_STR = u'<input class="validateButton" type="submit" name="%s" value="%s" tabindex="%s"/>' |
|
217 |
ACTION_SUBMIT_STR = u'<input class="validateButton" type="button" onclick="postForm(\'%s\', \'%s\', \'%s\')" value="%s" tabindex="%s"/>' |
|
218 |
||
219 |
def button_ok(self, label=None, tabindex=None): |
|
220 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
221 |
return self.BUTTON_STR % ('defaultsubmit', label, tabindex or 2) |
|
222 |
||
223 |
def button_apply(self, label=None, tabindex=None): |
|
224 |
label = self.req._(label or stdmsgs.BUTTON_APPLY).capitalize() |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
225 |
return self.ACTION_SUBMIT_STR % ('__action_apply', label, self.domid, |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
226 |
label, tabindex or 3) |
0 | 227 |
|
228 |
def button_delete(self, label=None, tabindex=None): |
|
229 |
label = self.req._(label or stdmsgs.BUTTON_DELETE).capitalize() |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
230 |
return self.ACTION_SUBMIT_STR % ('__action_delete', label, self.domid, |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
231 |
label, tabindex or 3) |
0 | 232 |
|
233 |
def button_cancel(self, label=None, tabindex=None): |
|
234 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
235 |
return self.ACTION_SUBMIT_STR % ('__action_cancel', label, self.domid, |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
236 |
label, tabindex or 4) |
0 | 237 |
|
238 |
def button_reset(self, label=None, tabindex=None): |
|
239 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
240 |
return u'<input class="validateButton" type="reset" value="%s" tabindex="%s"/>' % ( |
|
241 |
label, tabindex or 4) |
|
844 | 242 |
|
243 |
||
244 |
############################################################################### |
|
245 |
||
246 |
from cubicweb.common import tags |
|
247 |
||
248 |
# widgets ############ |
|
249 |
||
250 |
class FieldWidget(object): |
|
251 |
def __init__(self, attrs=None): |
|
252 |
self.attrs = attrs or {} |
|
253 |
||
254 |
def render(self, form, field): |
|
255 |
raise NotImplementedError |
|
256 |
||
257 |
class Input(FieldWidget): |
|
258 |
type = None |
|
259 |
||
260 |
def render(self, form, field): |
|
261 |
name, value, attrs = self._render_attrs(form, field) |
|
262 |
if attrs is None: |
|
263 |
return tags.input(name=name, value=value) |
|
264 |
return tags.input(name=name, value=value, type=self.type, **attrs) |
|
265 |
||
266 |
def _render_attrs(self, form, field): |
|
267 |
name = form.context[field]['name'] # qualified name |
|
268 |
value = form.context[field]['value'] |
|
269 |
#fattrs = field.widget_attributes(self) |
|
270 |
attrs = self.attrs.copy() |
|
271 |
#attrs.update(fattrs) |
|
272 |
# XXX id |
|
273 |
return name, value, attrs |
|
274 |
||
275 |
class TextInput(Input): |
|
276 |
type = 'text' |
|
277 |
||
278 |
class PasswordInput(Input): |
|
279 |
type = 'password' |
|
280 |
||
281 |
class FileInput(Input): |
|
282 |
type = 'file' |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
283 |
|
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
284 |
def _render_attrs(self, form, field): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
285 |
name = form.context[field]['name'] # qualified name |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
286 |
return name, None, {} |
844 | 287 |
|
288 |
class HiddenInput(Input): |
|
289 |
type = 'hidden' |
|
290 |
||
291 |
class Button(Input): |
|
292 |
type = 'button' |
|
293 |
||
294 |
class TextArea(FieldWidget): |
|
295 |
def render(self, form, field): |
|
296 |
name, value, attrs = self._render_attrs(form, field) |
|
297 |
if attrs is None: |
|
298 |
return tags.textarea(value, name=name) |
|
299 |
return tags.textarea(value, name=name, **attrs) |
|
300 |
||
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
301 |
class Select(FieldWidget): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
302 |
def __init__(self, vocabulary=()): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
303 |
self.vocabulary = () |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
304 |
|
844 | 305 |
def render(self, form, field): |
306 |
name, value, attrs = self._render_attrs(form, field) |
|
307 |
if self.vocabulary: |
|
308 |
# static vocabulary defined in form definition |
|
309 |
vocab = self.vocabulary |
|
310 |
else: |
|
311 |
vocab = form.get_vocabulary(field) |
|
312 |
options = [] |
|
313 |
for label, value in vocab: |
|
314 |
options.append(tags.option(label, value=value)) |
|
315 |
if attrs is None: |
|
316 |
return tags.select(name=name, options=options) |
|
317 |
return tags.select(name=name, options=options, **attrs) |
|
318 |
||
319 |
||
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
320 |
class CheckBox(FieldWidget): |
844 | 321 |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
322 |
def _render_attrs(self, form, field): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
323 |
name, value, attrs = super(CheckBox, self)._render_attrs(form, field) |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
324 |
if value: |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
325 |
attrs['checked'] = u'checked' |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
326 |
return name, None, attrs |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
327 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
328 |
class Radio(FieldWidget): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
329 |
pass |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
330 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
331 |
class DateTimePicker(TextInput): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
332 |
monthnames = ("january", "february", "march", "april", |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
333 |
"may", "june", "july", "august", |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
334 |
"september", "october", "november", "december") |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
335 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
336 |
daynames = ("monday", "tuesday", "wednesday", "thursday", |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
337 |
"friday", "saturday", "sunday") |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
338 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
339 |
@classmethod |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
340 |
def add_localized_infos(cls, req): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
341 |
"""inserts JS variables defining localized months and days""" |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
342 |
# import here to avoid dependancy from cubicweb-common to simplejson |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
343 |
_ = req._ |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
344 |
monthnames = [_(mname) for mname in cls.monthnames] |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
345 |
daynames = [_(dname) for dname in cls.daynames] |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
346 |
req.html_headers.define_var('MONTHNAMES', monthnames) |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
347 |
req.html_headers.define_var('DAYNAMES', daynames) |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
348 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
349 |
def render(self, form, field): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
350 |
txtwidget = super(DateTimePicker, self).render(form, field) |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
351 |
cal_button = self._render_calendar_popup(form, field) |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
352 |
return txtwidget + cal_button |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
353 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
354 |
def _render_calendar_popup(self, form, field): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
355 |
req = form.req |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
356 |
value = form.context[field]['rawvalue'] |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
357 |
inputid = form.context[field]['id'] |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
358 |
helperid = '%shelper' % inputid |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
359 |
if not value: |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
360 |
value = today() |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
361 |
year, month = value.year, value.month |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
362 |
onclick = "toggleCalendar('%s', '%s', %s, %s);" % ( |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
363 |
helperid, inputid, year, month) |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
364 |
return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper"> |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
365 |
<img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>""" |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
366 |
% (helperid, inputid, year, month, |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
367 |
req.external_resource('CALENDAR_ICON'), |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
368 |
req._('calendar'), helperid) ) |
844 | 369 |
|
370 |
||
371 |
# fields ############ |
|
372 |
||
373 |
class Field(object): |
|
374 |
"""field class is introduced to control what's displayed in edition form |
|
375 |
""" |
|
376 |
widget = TextInput |
|
377 |
needs_multipart = False |
|
378 |
creation_rank = 0 |
|
379 |
||
380 |
def __init__(self, name=None, id=None, label=None, |
|
381 |
widget=None, required=False, initial=None, help=None, |
|
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
382 |
eidparam=False): |
844 | 383 |
self.required = required |
384 |
if widget is not None: |
|
385 |
self.widget = widget |
|
386 |
if isinstance(self.widget, type): |
|
387 |
self.widget = self.widget() |
|
388 |
self.name = name |
|
389 |
self.label = label or name |
|
390 |
self.id = id or name |
|
391 |
self.initial = initial |
|
392 |
self.help = help |
|
393 |
self.eidparam = eidparam |
|
394 |
# global fields ordering in forms |
|
395 |
Field.creation_rank += 1 |
|
396 |
||
397 |
def set_name(self, name): |
|
398 |
self.name = name |
|
399 |
if not self.id: |
|
400 |
self.id = name |
|
401 |
if not self.label: |
|
402 |
self.label = name |
|
403 |
||
404 |
def format_value(self, req, value): |
|
405 |
return unicode(value) |
|
406 |
||
407 |
def render(self, form): |
|
408 |
return self.widget.render(form, self) |
|
409 |
||
410 |
class StringField(Field): |
|
411 |
def __init__(self, max_length=None, **kwargs): |
|
412 |
super(StringField, self).__init__(**kwargs) |
|
413 |
self.max_length = max_length |
|
0 | 414 |
|
844 | 415 |
class TextField(Field): |
416 |
widget = TextArea |
|
417 |
def __init__(self, row=None, col=None, **kwargs): |
|
418 |
super(TextField, self).__init__(**kwargs) |
|
419 |
self.row = row |
|
420 |
self.col = col |
|
421 |
||
422 |
class RichTextField(Field): |
|
423 |
pass |
|
424 |
||
425 |
class IntField(Field): |
|
426 |
def __init__(self, min=None, max=None, **kwargs): |
|
427 |
super(IntField, self).__init__(**kwargs) |
|
428 |
self.min = min |
|
429 |
self.max = max |
|
430 |
||
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
431 |
class FloatField(IntField): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
432 |
def format_value(self, req, value): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
433 |
formatstr = entity.req.property_value('ui.float-format') |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
434 |
if value is None: |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
435 |
return u'' |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
436 |
return formatstr % float(value) |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
437 |
|
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
438 |
class DateField(StringField): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
439 |
propname = 'ui.date-format' |
844 | 440 |
|
441 |
def format_value(self, req, value): |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
442 |
return value and ustrftime(value, req.property_value(self.propname)) or u'' |
844 | 443 |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
444 |
class DateTimeField(DateField): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
445 |
propname = 'ui.datetime-format' |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
446 |
|
844 | 447 |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
448 |
class FileField(StringField): |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
449 |
needs_multipart = True |
844 | 450 |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
451 |
class HiddenInitialValueField(Field): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
452 |
def __init__(self, visible_field, name): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
453 |
super(HiddenInitialValueField, self).__init__(name=name, |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
454 |
widget=HiddenInput) |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
455 |
self.visible_field = visible_field |
844 | 456 |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
457 |
class RelationField(Field): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
458 |
def __init__(self, role='subject', **kwargs): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
459 |
super(RelationField, self).__init__(**kwargs) |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
460 |
self.role = role |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
461 |
|
844 | 462 |
# forms ############ |
463 |
class metafieldsform(type): |
|
464 |
def __new__(mcs, name, bases, classdict): |
|
465 |
allfields = [] |
|
466 |
for base in bases: |
|
467 |
if hasattr(base, '_fields_'): |
|
468 |
allfields += base._fields_ |
|
469 |
clsfields = (item for item in classdict.items() |
|
470 |
if isinstance(item[1], Field)) |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
471 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 472 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
473 |
field.set_name(fieldname) |
844 | 474 |
allfields.append(field) |
475 |
classdict['_fields_'] = allfields |
|
476 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
477 |
||
478 |
||
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
479 |
class FieldsForm(FormMixIn): |
844 | 480 |
__metaclass__ = metafieldsform |
481 |
||
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
482 |
def __init__(self, req, domid=None, title=None, action='edit', |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
483 |
onsubmit="return freezeFormButtons('%s');", |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
484 |
cssclass=None, cssstyle=None, cwtarget=None, buttons=None, |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
485 |
redirect_path=None, set_error_url=True, copy_nav_params=False): |
844 | 486 |
self.req = req |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
487 |
self.domid = domid or 'form' |
844 | 488 |
self.title = title |
489 |
self.action = action |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
490 |
self.onsubmit = onsubmit |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
491 |
self.cssclass = cssclass |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
492 |
self.cssstyle = cssstyle |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
493 |
self.cwtarget = cwtarget |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
494 |
self.redirect_path = redirect_path |
844 | 495 |
self.fields = list(self.__class__._fields_) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
496 |
if set_error_url: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
497 |
self.form_add_hidden('__errorurl', req.url()) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
498 |
if copy_nav_params: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
499 |
for param in NAV_FORM_PARAMETERS: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
500 |
value = req.form.get(param) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
501 |
if value: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
502 |
self.form_add_hidden('__errorurl', initial=value) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
503 |
self.buttons = buttons or [] |
844 | 504 |
self.context = {} |
505 |
||
506 |
@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
|
507 |
def form_needs_multipart(self): |
844 | 508 |
return any(field.needs_multipart for field in self.fields) |
0 | 509 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
510 |
def form_add_hidden(self, name, value=None, **kwargs): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
511 |
self.fields.append(TextField(name=name, widget=HiddenInput, |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
512 |
initial=value, **kwargs)) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
513 |
|
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
|
514 |
def form_render(self, **values): |
844 | 515 |
renderer = values.pop('renderer', FormRenderer()) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
516 |
return renderer.render(self, values) |
844 | 517 |
|
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
|
518 |
def form_build_context(self, values): |
844 | 519 |
self.context = context = {} |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
520 |
# on validation error, we get a dictionnary of previously submitted values |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
521 |
previous_values = self.req.data.get('formvalues') |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
522 |
if previous_values: |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
523 |
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
|
524 |
for field in self.fields: |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
525 |
value = self.form_field_value(field, values) |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
526 |
context[field] = {'value': field.format_value(self.req, value), |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
527 |
'rawvalue': value, |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
528 |
'name': self.form_field_name(field), |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
529 |
'id': self.form_field_id(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
|
530 |
} |
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
|
531 |
|
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
|
532 |
def form_field_value(self, field, values): |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
533 |
"""looks for field's value in |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
534 |
1. kw args given to render_form (including previously submitted form |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
535 |
values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
536 |
2. req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
537 |
3. field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
538 |
""" |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
539 |
if field.name in values: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
540 |
value = values[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
541 |
elif field.name in self.req.form: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
542 |
value = self.req.form[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
543 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
544 |
value = field.initial |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
545 |
return value # field.format_value(self.req, value) |
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
|
546 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
547 |
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
|
548 |
return field.name |
844 | 549 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
550 |
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
|
551 |
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
|
552 |
|
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
|
553 |
def form_field_vocabulary(self, field): |
844 | 554 |
raise NotImplementedError |
555 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
556 |
def form_buttons(self): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
557 |
return self.buttons |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
558 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
559 |
|
844 | 560 |
class EntityFieldsForm(FieldsForm): |
561 |
def __init__(self, *args, **kwargs): |
|
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
562 |
kwargs.setdefault('domid', 'entityForm') |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
563 |
self.entity = kwargs.pop('entity', None) |
844 | 564 |
super(EntityFieldsForm, self).__init__(*args, **kwargs) |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
565 |
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
|
566 |
self.form_add_hidden('eid') |
844 | 567 |
|
874
90d98d80af82
EntityFieldsForm.form_render() doesn't need entity anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
869
diff
changeset
|
568 |
def form_render(self, **values): |
90d98d80af82
EntityFieldsForm.form_render() doesn't need entity anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
869
diff
changeset
|
569 |
self.form_add_entity_hiddens(self.entity.e_schema) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
570 |
return super(EntityFieldsForm, self).form_render(**values) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
571 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
572 |
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
|
573 |
for field in self.fields[:]: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
574 |
fieldname = field.name |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
575 |
if fieldname != 'eid' and ( |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
576 |
(eschema.has_subject_relation(fieldname) or |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
577 |
eschema.has_object_relation(fieldname))): |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
578 |
field.eidparam = True |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
579 |
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
|
580 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
581 |
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
|
582 |
"""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
|
583 |
before the modification |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
584 |
""" |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
585 |
# 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
|
586 |
# 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
|
587 |
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
|
588 |
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
|
589 |
else: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
590 |
name = 'edito-%s' % field.name |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
591 |
return HiddenInitialValueField(field, name=name, eidparam=True) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
592 |
|
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
|
593 |
def form_field_value(self, field, values): |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
594 |
"""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
|
595 |
1. handle special __type and eid fields |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
596 |
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
|
597 |
form values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
598 |
3. looks in req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
599 |
4. if entity has an eid: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
600 |
1. looks for an associated attribute / method |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
601 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
602 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
603 |
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
|
604 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
605 |
|
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
606 |
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
|
607 |
""" |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
608 |
fieldname = field.name |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
609 |
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
|
610 |
# edit[s|o]- fieds must have the actual value stored on the entity |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
611 |
if self.entity.has_eid(): |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
612 |
value = self.form_field_entity_value(field, default_initial=False) |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
613 |
else: |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
614 |
value = INTERNAL_FIELD_VALUE |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
615 |
elif fieldname == '__type': |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
616 |
value = self.entity.id |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
617 |
elif fieldname == 'eid': |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
618 |
value = self.entity.eid |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
619 |
elif fieldname in values: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
620 |
value = values[fieldname] |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
621 |
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
|
622 |
value = self.req.form[fieldname] |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
623 |
else: |
888
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
624 |
if self.entity.has_eid() and field.eidparam: |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
625 |
# 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
|
626 |
# 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
|
627 |
# undesired value) |
888
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
628 |
value = self.form_field_entity_value(field, default_initial=True) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
629 |
else: |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
630 |
defaultattr = 'default_%s' % fieldname |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
631 |
if hasattr(self.entity, defaultattr): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
632 |
# 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
|
633 |
warn('found %s on %s, should be set on a specific form' |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
634 |
% (defaultattr, self.entity.id), DeprecationWarning) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
635 |
value = getattr(self.entity, defaultattr) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
636 |
elif hasattr(self, defaultattr): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
637 |
# 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
|
638 |
value = getattr(self, defaultattr) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
639 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
640 |
# use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
641 |
value = field.initial |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
642 |
if callable(value): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
643 |
values = value() |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
644 |
return value # field.format_value(self.req, value) |
844 | 645 |
|
888
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
646 |
def form_field_entity_value(self, field, default_initial=True): |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
647 |
attr = field.name |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
648 |
if field.role == 'object': |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
649 |
attr += '_object' |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
650 |
if default_initial: |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
651 |
return getattr(self.entity, attr, field.initial) |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
652 |
else: |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
653 |
return getattr(self.entity, attr) |
603327e9aef4
fix field value for hidden edit[s|à] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
654 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
655 |
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
|
656 |
if field.eidparam: |
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
|
657 |
return eid_param(field.name, self.entity.eid) |
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
|
658 |
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
|
659 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
660 |
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
|
661 |
if field.eidparam: |
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
|
662 |
return eid_param(field.id, self.entity.eid) |
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
|
663 |
return field.id |
844 | 664 |
|
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
|
665 |
def form_field_vocabulary(self, field): |
844 | 666 |
choices = self.vocabfunc(entity) |
667 |
if self.sort: |
|
668 |
choices = sorted(choices) |
|
669 |
if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'): |
|
670 |
return zip((entity.req._(v) for v in choices), choices) |
|
671 |
return zip(choices, choices) |
|
672 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
673 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
674 |
class MultipleFieldsForm(FieldsForm): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
675 |
def __init__(self, *args, **kwargs): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
676 |
super(MultipleFieldsForm, self).__init__(*args, **kwargs) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
677 |
self.forms = [] |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
678 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
679 |
def form_add_subform(self, subform): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
680 |
self.forms.append(subform) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
681 |
|
844 | 682 |
# form renderers ############ |
683 |
||
684 |
class FormRenderer(object): |
|
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
|
685 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
686 |
def render(self, form, values): |
844 | 687 |
data = [] |
688 |
w = data.append |
|
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
|
689 |
# XXX form_needs_multipart |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
690 |
w(self.open_form(form)) |
844 | 691 |
w(u'<div id="progress">%s</div>' % _('validating...')) |
692 |
w(u'<fieldset>') |
|
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
693 |
w(tags.input(type='hidden', name='__form_id', value=form.domid)) |
844 | 694 |
if form.redirect_path: |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
695 |
w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path)) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
696 |
self.render_fields(w, form, 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
|
697 |
self.render_buttons(w, form) |
844 | 698 |
w(u'</fieldset>') |
699 |
w(u'</form>') |
|
700 |
return '\n'.join(data) |
|
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
|
701 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
702 |
def open_form(self, form): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
703 |
if form.form_needs_multipart: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
704 |
enctype = 'multipart/form-data' |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
705 |
else: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
706 |
enctype = 'application/x-www-form-urlencoded' |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
707 |
tag = ('<form action="%s" methody="post" id="%s" enctype="%s"' % ( |
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
708 |
html_escape(form.action or '#'), form.domid, enctype)) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
709 |
if form.onsubmit: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
710 |
tag += ' onsubmit="%s"' % html_escape(form.onsubmit) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
711 |
if form.cssstyle: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
712 |
tag += ' style="%s"' % html_escape(form.cssstyle) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
713 |
if form.cssclass: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
714 |
tag += ' class="%s"' % html_escape(form.cssclass) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
715 |
if form.cwtarget: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
716 |
tag += ' cubicweb:target="%s"' % html_escape(form.cwtarget) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
717 |
return tag + '>' |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
718 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
719 |
def render_fields(self, w, form, values): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
720 |
form.form_build_context(values) |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
721 |
fields = form.fields[:] |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
722 |
for field in form.fields: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
723 |
if isinstance(field.widget, HiddenInput): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
724 |
w(field.render(form)) |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
725 |
fields.remove(field) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
726 |
if fields: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
727 |
w(u'<table>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
728 |
for field in fields: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
729 |
w(u'<tr>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
730 |
w('<th>%s</th>' % self.render_label(form, field)) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
731 |
w(u'<td style="width:100%;">') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
732 |
w(field.render(form)) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
733 |
w(u'</td></tr>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
734 |
w(u'</table>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
735 |
for childform in getattr(form, 'forms', []): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
736 |
self.render_fields(w, childform, values) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
737 |
|
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
|
738 |
def render_buttons(self, w, form): |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
739 |
for button in form.form_buttons(): |
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
|
740 |
w(button) |
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
|
741 |
|
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
|
742 |
def render_label(self, form, field): |
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
|
743 |
label = form.req._(field.label) |
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
|
744 |
attrs = {'for': form.context[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
|
745 |
if field.required: |
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
|
746 |
attrs['class'] = 'required' |
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
|
747 |
return tags.label(label, **attrs) |
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
|
748 |