author | sylvain.thenault@logilab.fr |
Wed, 11 Mar 2009 14:38:44 +0100 | |
branch | tls-sprint |
changeset 1061 | 7d2e6b5fae3c |
parent 1060 | d795477ad16b |
child 1062 | 185d630d1aef |
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 |
from datetime import date, datetime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
11 |
|
0 | 12 |
from simplejson import dumps |
13 |
||
844 | 14 |
from logilab.common.compat import any |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
15 |
from logilab.common.decorators import iclassmethod |
0 | 16 |
from logilab.mtconverter import html_escape |
17 |
||
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
18 |
from yams.constraints import SizeConstraint, StaticVocabularyConstraint |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
19 |
|
0 | 20 |
from cubicweb import typed_eid |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
21 |
from cubicweb.appobject import AppObject |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
22 |
from cubicweb.utils import ustrftime |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
23 |
from cubicweb.selectors import yes, match_form_params, non_final_entity |
751 | 24 |
from cubicweb.view import NOINDEX, NOFOLLOW, View, EntityView, AnyRsetView |
1048 | 25 |
from cubicweb.schema import FormatConstraint |
0 | 26 |
from cubicweb.common.registerers import accepts_registerer |
907 | 27 |
from cubicweb.common.uilib import toggle_action |
0 | 28 |
from cubicweb.web import stdmsgs |
29 |
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
|
30 |
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
|
31 |
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
|
32 |
|
0 | 33 |
|
34 |
def relation_id(eid, rtype, target, reid): |
|
35 |
if target == 'subject': |
|
36 |
return u'%s:%s:%s' % (eid, rtype, reid) |
|
37 |
return u'%s:%s:%s' % (reid, rtype, eid) |
|
844 | 38 |
|
39 |
def toggable_relation_link(eid, nodeid, label='x'): |
|
40 |
js = u"javascript: togglePendingDelete('%s', %s);" % (nodeid, html_escape(dumps(eid))) |
|
41 |
return u'[<a class="handle" href="%s" id="handle%s">%s</a>]' % (js, nodeid, label) |
|
0 | 42 |
|
43 |
||
44 |
class FormMixIn(object): |
|
944
e9da555f19c5
[forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
913
diff
changeset
|
45 |
"""abstract form mix-in |
e9da555f19c5
[forms] a note (understanding the cause would be nice)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
913
diff
changeset
|
46 |
XXX: you should inherit from this FIRST (obscure pb with super call)""" |
0 | 47 |
category = 'form' |
48 |
controller = 'edit' |
|
49 |
domid = 'entityForm' |
|
50 |
||
51 |
http_cache_manager = NoHTTPCacheManager |
|
52 |
add_to_breadcrumbs = False |
|
53 |
skip_relations = set() |
|
54 |
||
55 |
def __init__(self, req, rset): |
|
56 |
super(FormMixIn, self).__init__(req, rset) |
|
57 |
self.maxrelitems = self.req.property_value('navigation.related-limit') |
|
58 |
self.maxcomboitems = self.req.property_value('navigation.combobox-limit') |
|
59 |
self.force_display = not not req.form.get('__force_display') |
|
60 |
# get validation session data which may have been previously set. |
|
61 |
# deleting validation errors here breaks form reloading (errors are |
|
62 |
# no more available), they have to be deleted by application's publish |
|
63 |
# method on successful commit |
|
64 |
formurl = req.url() |
|
65 |
forminfo = req.get_session_data(formurl) |
|
66 |
if forminfo: |
|
67 |
req.data['formvalues'] = forminfo['values'] |
|
68 |
req.data['formerrors'] = errex = forminfo['errors'] |
|
69 |
req.data['displayederrors'] = set() |
|
70 |
# if some validation error occured on entity creation, we have to |
|
71 |
# get the original variable name from its attributed eid |
|
72 |
foreid = errex.entity |
|
73 |
for var, eid in forminfo['eidmap'].items(): |
|
74 |
if foreid == eid: |
|
75 |
errex.eid = var |
|
76 |
break |
|
77 |
else: |
|
78 |
errex.eid = foreid |
|
79 |
||
80 |
def html_headers(self): |
|
81 |
"""return a list of html headers (eg something to be inserted between |
|
82 |
<head> and </head> of the returned page |
|
83 |
||
84 |
by default forms are neither indexed nor followed |
|
85 |
""" |
|
86 |
return [NOINDEX, NOFOLLOW] |
|
87 |
||
88 |
def linkable(self): |
|
89 |
"""override since forms are usually linked by an action, |
|
90 |
so we don't want them to be listed by appli.possible_views |
|
91 |
""" |
|
92 |
return False |
|
93 |
||
94 |
@property |
|
95 |
def limit(self): |
|
96 |
if self.force_display: |
|
97 |
return None |
|
98 |
return self.maxrelitems + 1 |
|
99 |
||
100 |
def need_multipart(self, entity, categories=('primary', 'secondary')): |
|
101 |
"""return a boolean indicating if form's enctype should be multipart |
|
102 |
""" |
|
103 |
for rschema, _, x in entity.relations_by_category(categories): |
|
104 |
if entity.get_widget(rschema, x).need_multipart: |
|
105 |
return True |
|
106 |
# let's find if any of our inlined entities needs multipart |
|
107 |
for rschema, targettypes, x in entity.relations_by_category('inlineview'): |
|
108 |
assert len(targettypes) == 1, \ |
|
109 |
"I'm not able to deal with several targets and inlineview" |
|
110 |
ttype = targettypes[0] |
|
111 |
inlined_entity = self.vreg.etype_class(ttype)(self.req, None, None) |
|
112 |
for irschema, _, x in inlined_entity.relations_by_category(categories): |
|
113 |
if inlined_entity.get_widget(irschema, x).need_multipart: |
|
114 |
return True |
|
115 |
return False |
|
116 |
||
117 |
def error_message(self): |
|
118 |
"""return formatted error message |
|
119 |
||
120 |
This method should be called once inlined field errors has been consumed |
|
121 |
""" |
|
122 |
errex = self.req.data.get('formerrors') |
|
123 |
# get extra errors |
|
124 |
if errex is not None: |
|
125 |
errormsg = self.req._('please correct the following errors:') |
|
126 |
displayed = self.req.data['displayederrors'] |
|
127 |
errors = sorted((field, err) for field, err in errex.errors.items() |
|
128 |
if not field in displayed) |
|
129 |
if errors: |
|
130 |
if len(errors) > 1: |
|
131 |
templstr = '<li>%s</li>\n' |
|
132 |
else: |
|
133 |
templstr = ' %s\n' |
|
134 |
for field, err in errors: |
|
135 |
if field is None: |
|
136 |
errormsg += templstr % err |
|
137 |
else: |
|
138 |
errormsg += templstr % '%s: %s' % (self.req._(field), err) |
|
139 |
if len(errors) > 1: |
|
140 |
errormsg = '<ul>%s</ul>' % errormsg |
|
141 |
return u'<div class="errorMessage">%s</div>' % errormsg |
|
142 |
return u'' |
|
143 |
||
144 |
def restore_pending_inserts(self, entity, cell=False): |
|
145 |
"""used to restore edition page as it was before clicking on |
|
146 |
'search for <some entity type>' |
|
147 |
|
|
148 |
""" |
|
149 |
eid = entity.eid |
|
150 |
cell = cell and "div_insert_" or "tr" |
|
151 |
pending_inserts = set(self.req.get_pending_inserts(eid)) |
|
152 |
for pendingid in pending_inserts: |
|
153 |
eidfrom, rtype, eidto = pendingid.split(':') |
|
154 |
if typed_eid(eidfrom) == entity.eid: # subject |
|
155 |
label = display_name(self.req, rtype, 'subject') |
|
156 |
reid = eidto |
|
157 |
else: |
|
158 |
label = display_name(self.req, rtype, 'object') |
|
159 |
reid = eidfrom |
|
160 |
jscall = "javascript: cancelPendingInsert('%s', '%s', null, %s);" \ |
|
161 |
% (pendingid, cell, eid) |
|
162 |
rset = self.req.eid_rset(reid) |
|
163 |
eview = self.view('text', rset, row=0) |
|
164 |
# XXX find a clean way to handle baskets |
|
165 |
if rset.description[0][0] == 'Basket': |
|
166 |
eview = '%s (%s)' % (eview, display_name(self.req, 'Basket')) |
|
167 |
yield rtype, pendingid, jscall, label, reid, eview |
|
168 |
||
169 |
||
170 |
def force_display_link(self): |
|
171 |
return (u'<span class="invisible">' |
|
172 |
u'[<a href="javascript: window.location.href+=\'&__force_display=1\'">%s</a>]' |
|
173 |
u'</span>' % self.req._('view all')) |
|
174 |
||
175 |
def relations_table(self, entity): |
|
176 |
"""yiels 3-tuples (rtype, target, related_list) |
|
177 |
where <related_list> itself a list of : |
|
178 |
- node_id (will be the entity element's DOM id) |
|
179 |
- appropriate javascript's togglePendingDelete() function call |
|
180 |
- status 'pendingdelete' or '' |
|
181 |
- oneline view of related entity |
|
182 |
""" |
|
183 |
eid = entity.eid |
|
184 |
pending_deletes = self.req.get_pending_deletes(eid) |
|
185 |
# XXX (adim) : quick fix to get Folder relations |
|
186 |
for label, rschema, target in entity.srelations_by_category(('generic', 'metadata'), 'add'): |
|
187 |
if rschema in self.skip_relations: |
|
188 |
continue |
|
189 |
relatedrset = entity.related(rschema, target, limit=self.limit) |
|
190 |
toggable_rel_link = self.toggable_relation_link_func(rschema) |
|
191 |
related = [] |
|
192 |
for row in xrange(relatedrset.rowcount): |
|
193 |
nodeid = relation_id(eid, rschema, target, relatedrset[row][0]) |
|
194 |
if nodeid in pending_deletes: |
|
195 |
status = u'pendingDelete' |
|
196 |
label = '+' |
|
197 |
else: |
|
198 |
status = u'' |
|
199 |
label = 'x' |
|
200 |
dellink = toggable_rel_link(eid, nodeid, label) |
|
201 |
eview = self.view('oneline', relatedrset, row=row) |
|
202 |
related.append((nodeid, dellink, status, eview)) |
|
203 |
yield (rschema, target, related) |
|
204 |
||
205 |
def toggable_relation_link_func(self, rschema): |
|
206 |
if not rschema.has_perm(self.req, 'delete'): |
|
207 |
return lambda x, y, z: u'' |
|
208 |
return toggable_relation_link |
|
209 |
||
210 |
||
211 |
def redirect_url(self, entity=None): |
|
212 |
"""return a url to use as next direction if there are some information |
|
213 |
specified in current form params, else return the result the reset_url |
|
214 |
method which should be defined in concrete classes |
|
215 |
""" |
|
216 |
rparams = redirect_params(self.req.form) |
|
217 |
if rparams: |
|
218 |
return self.build_url('view', **rparams) |
|
219 |
return self.reset_url(entity) |
|
220 |
||
221 |
def reset_url(self, entity): |
|
222 |
raise NotImplementedError('implement me in concrete classes') |
|
223 |
||
224 |
BUTTON_STR = u'<input class="validateButton" type="submit" name="%s" value="%s" tabindex="%s"/>' |
|
225 |
ACTION_SUBMIT_STR = u'<input class="validateButton" type="button" onclick="postForm(\'%s\', \'%s\', \'%s\')" value="%s" tabindex="%s"/>' |
|
226 |
||
227 |
def button_ok(self, label=None, tabindex=None): |
|
228 |
label = self.req._(label or stdmsgs.BUTTON_OK).capitalize() |
|
229 |
return self.BUTTON_STR % ('defaultsubmit', label, tabindex or 2) |
|
230 |
||
231 |
def button_apply(self, label=None, tabindex=None): |
|
232 |
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
|
233 |
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
|
234 |
label, tabindex or 3) |
0 | 235 |
|
236 |
def button_delete(self, label=None, tabindex=None): |
|
237 |
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
|
238 |
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
|
239 |
label, tabindex or 3) |
0 | 240 |
|
241 |
def button_cancel(self, label=None, tabindex=None): |
|
242 |
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
|
243 |
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
|
244 |
label, tabindex or 4) |
0 | 245 |
|
246 |
def button_reset(self, label=None, tabindex=None): |
|
247 |
label = self.req._(label or stdmsgs.BUTTON_CANCEL).capitalize() |
|
248 |
return u'<input class="validateButton" type="reset" value="%s" tabindex="%s"/>' % ( |
|
249 |
label, tabindex or 4) |
|
844 | 250 |
|
251 |
||
252 |
############################################################################### |
|
253 |
||
254 |
from cubicweb.common import tags |
|
255 |
||
256 |
# widgets ############ |
|
257 |
||
258 |
class FieldWidget(object): |
|
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
259 |
needs_js = () |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
260 |
needs_css = () |
1032
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
261 |
setdomid = True |
1055 | 262 |
settabindex = True |
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
263 |
|
1055 | 264 |
def __init__(self, attrs=None, setdomid=None, settabindex=None): |
844 | 265 |
self.attrs = attrs or {} |
1032
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
266 |
if setdomid is not None: |
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
267 |
# override class's default value |
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
268 |
self.setdomid = setdomid |
1055 | 269 |
if settabindex is not None: |
270 |
# override class's default value |
|
271 |
self.settabindex = settabindex |
|
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
272 |
|
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
273 |
def add_media(self, form): |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
274 |
"""adds media (CSS & JS) required by this widget""" |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
275 |
req = form.req |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
276 |
if self.needs_js: |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
277 |
req.add_js(self.needs_js) |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
278 |
if self.needs_css: |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
279 |
req.add_css(self.needs_css) |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
280 |
|
844 | 281 |
def render(self, form, field): |
282 |
raise NotImplementedError |
|
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
|
283 |
|
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
|
284 |
def _render_attrs(self, form, field): |
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
|
285 |
name = form.context[field]['name'] |
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
|
286 |
values = form.context[field]['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
|
287 |
if not isinstance(values, (tuple, list)): |
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
|
288 |
values = (values,) |
907 | 289 |
attrs = dict(self.attrs) |
1032
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
290 |
if self.setdomid: |
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
291 |
attrs['id'] = form.context[field]['id'] |
1055 | 292 |
if self.settabindex: |
293 |
attrs['tabindex'] = form.req.next_tabindex() |
|
910
a86ab461b8fd
fix DateTimePicker implementation, it should now work
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
907
diff
changeset
|
294 |
return name, values, attrs |
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
|
295 |
|
844 | 296 |
class Input(FieldWidget): |
297 |
type = None |
|
298 |
||
299 |
def render(self, form, field): |
|
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
300 |
self.add_media(form) |
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
|
301 |
name, values, attrs = self._render_attrs(form, field) |
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
|
302 |
inputs = [tags.input(name=name, value=value, type=self.type, **attrs) |
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
|
303 |
for value in values] |
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
|
304 |
return u'\n'.join(inputs) |
844 | 305 |
|
306 |
class TextInput(Input): |
|
307 |
type = 'text' |
|
308 |
||
309 |
class PasswordInput(Input): |
|
310 |
type = 'password' |
|
908
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
311 |
|
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
312 |
def render(self, form, field): |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
313 |
self.add_media(form) |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
314 |
name, values, attrs = self._render_attrs(form, field) |
911 | 315 |
assert len(values) == 1 |
316 |
inputs = [tags.input(name=name, value=values[0], type=self.type, **attrs), |
|
908
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
317 |
'<br/>', |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
318 |
tags.input(name=name+'-confirm', type=self.type, **attrs), |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
319 |
' ', tags.span(form.req._('confirm password'), |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
320 |
**{'class': 'emphasis'})] |
136d91725ecf
confirmation in password widget
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
321 |
return u'\n'.join(inputs) |
844 | 322 |
|
323 |
class FileInput(Input): |
|
324 |
type = 'file' |
|
907 | 325 |
|
326 |
def _render_attrs(self, form, field): |
|
327 |
# ignore value which makes no sense here (XXX even on form validation error?) |
|
328 |
name, values, attrs = super(FileInput, self)._render_attrs(form, field) |
|
329 |
return name, ('',), attrs |
|
330 |
||
844 | 331 |
class HiddenInput(Input): |
332 |
type = 'hidden' |
|
1032
25ec009daa95
by default, don't set id attribute for hidden inputs
sylvain.thenault@logilab.fr
parents:
1031
diff
changeset
|
333 |
setdomid = False # by default, don't set id attribute on hidden input |
1055 | 334 |
settabindex = False |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
335 |
|
907 | 336 |
class ButtonInput(Input): |
844 | 337 |
type = 'button' |
338 |
||
339 |
class TextArea(FieldWidget): |
|
340 |
def render(self, form, field): |
|
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
341 |
name, values, attrs = self._render_attrs(form, field) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
342 |
attrs.setdefault('onkeypress', 'autogrow(this)') |
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
|
343 |
if not values: |
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
|
344 |
value = u'' |
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
|
345 |
elif len(values) == 1: |
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
|
346 |
value = values[0] |
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
|
347 |
else: |
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
|
348 |
raise ValueError('a textarea is not supposed to be multivalued') |
844 | 349 |
return tags.textarea(value, name=name, **attrs) |
350 |
||
907 | 351 |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
352 |
class FCKEditor(TextArea): |
1059 | 353 |
def __init__(self, *args, **kwargs): |
354 |
super(FCKEditor, self).__init__(*args, **kwargs) |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
355 |
self.attrs['cubicweb:type'] = 'wysiwyg' |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
356 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
357 |
def render(self, form, field): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
358 |
form.req.fckeditor_config() |
1059 | 359 |
return super(FCKEditor, self).render(form, field) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
360 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
361 |
|
894 | 362 |
#class EditableFile(Widget): |
363 |
# # XXX |
|
364 |
# pass |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
365 |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
366 |
class Select(FieldWidget): |
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
|
367 |
def __init__(self, attrs=None, multiple=False): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
368 |
super(Select, self).__init__(attrs) |
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
369 |
self.multiple = multiple |
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
370 |
|
844 | 371 |
def render(self, form, field): |
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
372 |
name, curvalues, attrs = self._render_attrs(form, field) |
844 | 373 |
options = [] |
1056 | 374 |
for label, value in field.vocabulary(form): |
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
|
375 |
if value in curvalues: |
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
|
376 |
options.append(tags.option(label, value=value, selected='selected')) |
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
|
377 |
else: |
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
|
378 |
options.append(tags.option(label, value=value)) |
844 | 379 |
if attrs is None: |
380 |
return tags.select(name=name, options=options) |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
381 |
return tags.select(name=name, multiple=self.multiple, |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
382 |
options=options, **attrs) |
844 | 383 |
|
384 |
||
907 | 385 |
class CheckBox(Input): |
386 |
type = 'checkbox' |
|
387 |
||
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
388 |
def _render_attrs(self, form, field): |
907 | 389 |
name, values, attrs = super(CheckBox, self)._render_attrs(form, field) |
390 |
if values and values[0]: |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
391 |
attrs['checked'] = u'checked' |
907 | 392 |
return name, values, attrs |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
393 |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
394 |
|
1056 | 395 |
class Radio(Input): |
396 |
type = 'radio' |
|
397 |
setdomid = False |
|
398 |
||
399 |
def render(self, form, field): |
|
400 |
name, curvalues, attrs = self._render_attrs(form, field) |
|
401 |
options = [] |
|
402 |
for label, value in field.vocabulary(form): |
|
403 |
if value in curvalues: |
|
404 |
options.append(tags.input(name=name, type=self.type, value=value, checked='checked', **attrs)) |
|
405 |
else: |
|
406 |
options.append(tags.option(name=name, type=self.type, value=value, **attrs)) |
|
407 |
options[-1] += label + '<br/>' |
|
408 |
return '\n'.join(options) |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
409 |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
410 |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
411 |
class DateTimePicker(TextInput): |
907 | 412 |
monthnames = ('january', 'february', 'march', 'april', |
413 |
'may', 'june', 'july', 'august', |
|
414 |
'september', 'october', 'november', 'december') |
|
415 |
daynames = ('monday', 'tuesday', 'wednesday', 'thursday', |
|
416 |
'friday', 'saturday', 'sunday') |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
417 |
|
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
418 |
needs_js = ('cubicweb.ajax.js', 'cubicweb.calendar.js') |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
419 |
needs_css = ('cubicweb.calendar_popup.css',) |
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
420 |
|
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
421 |
@classmethod |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
422 |
def add_localized_infos(cls, req): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
423 |
"""inserts JS variables defining localized months and days""" |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
424 |
# import here to avoid dependancy from cubicweb-common to simplejson |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
425 |
_ = req._ |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
426 |
monthnames = [_(mname) for mname in cls.monthnames] |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
427 |
daynames = [_(dname) for dname in cls.daynames] |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
428 |
req.html_headers.define_var('MONTHNAMES', monthnames) |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
429 |
req.html_headers.define_var('DAYNAMES', daynames) |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
430 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
431 |
def render(self, form, field): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
432 |
txtwidget = super(DateTimePicker, self).render(form, field) |
910
a86ab461b8fd
fix DateTimePicker implementation, it should now work
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
907
diff
changeset
|
433 |
self.add_localized_infos(form.req) |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
434 |
cal_button = self._render_calendar_popup(form, field) |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
435 |
return txtwidget + cal_button |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
436 |
|
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
437 |
def _render_calendar_popup(self, form, field): |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
438 |
req = form.req |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
439 |
value = form.context[field]['rawvalue'] |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
440 |
inputid = form.context[field]['id'] |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
441 |
helperid = '%shelper' % inputid |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
442 |
if not value: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
443 |
value = date.today() |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
444 |
year, month = value.year, value.month |
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
445 |
onclick = "toggleCalendar('%s', '%s', %s, %s);" % ( |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
446 |
helperid, inputid, year, month) |
865
90a1aea85637
DatePicker widget
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
864
diff
changeset
|
447 |
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
|
448 |
<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
|
449 |
% (helperid, inputid, year, month, |
867
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
450 |
req.external_resource('CALENDAR_ICON'), |
14a79faa3a56
fix implementation errors import DateTimePicker
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
865
diff
changeset
|
451 |
req._('calendar'), helperid) ) |
844 | 452 |
|
453 |
||
454 |
# fields ############ |
|
455 |
||
456 |
class Field(object): |
|
457 |
"""field class is introduced to control what's displayed in edition form |
|
458 |
""" |
|
459 |
widget = TextInput |
|
460 |
needs_multipart = False |
|
461 |
creation_rank = 0 |
|
904
4f1ce95aa686
fix label generation in forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
903
diff
changeset
|
462 |
|
844 | 463 |
def __init__(self, name=None, id=None, label=None, |
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
464 |
widget=None, required=False, initial=None, |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
465 |
choices=None, help=None, eidparam=False): |
844 | 466 |
self.required = required |
467 |
if widget is not None: |
|
468 |
self.widget = widget |
|
469 |
if isinstance(self.widget, type): |
|
470 |
self.widget = self.widget() |
|
471 |
self.name = name |
|
472 |
self.label = label or name |
|
473 |
self.id = id or name |
|
474 |
self.initial = initial |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
475 |
self.choices = choices or () |
844 | 476 |
self.help = help |
477 |
self.eidparam = eidparam |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
478 |
self.role = 'subject' |
844 | 479 |
# global fields ordering in forms |
480 |
Field.creation_rank += 1 |
|
481 |
||
482 |
def set_name(self, name): |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
483 |
assert name |
844 | 484 |
self.name = name |
485 |
if not self.id: |
|
486 |
self.id = name |
|
487 |
if not self.label: |
|
488 |
self.label = name |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
489 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
490 |
def is_visible(self): |
904
4f1ce95aa686
fix label generation in forms
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
903
diff
changeset
|
491 |
return not isinstance(self.widget, HiddenInput) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
492 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
493 |
def actual_fields(self, form): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
494 |
yield self |
844 | 495 |
|
900
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
496 |
def __unicode__(self): |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
497 |
return u'<%s name=%r label=%r id=%r initial=%r>' % ( |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
498 |
self.__class__.__name__, self.name, self.label, |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
499 |
self.id, self.initial) |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
500 |
|
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
501 |
def __repr__(self): |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
502 |
return self.__unicode__().encode('utf-8') |
8f95ebbe2fd8
field debug helper
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
899
diff
changeset
|
503 |
|
844 | 504 |
def format_value(self, req, value): |
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 |
if isinstance(value, (list, tuple)): |
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
|
506 |
return [self.format_single_value(req, val) for val 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
|
507 |
return self.format_single_value(req, 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
|
508 |
|
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 |
def format_single_value(self, req, 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
|
510 |
if value is None: |
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
|
511 |
return u'' |
844 | 512 |
return unicode(value) |
513 |
||
907 | 514 |
def get_widget(self, form): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
515 |
return self.widget |
907 | 516 |
|
517 |
def example_format(self, req): |
|
518 |
return u'' |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
519 |
|
907 | 520 |
def render(self, form, renderer): |
521 |
return self.get_widget(form).render(form, self) |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
522 |
|
844 | 523 |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
524 |
def vocabulary(self, form): |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
525 |
return self.choices |
907 | 526 |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
527 |
|
844 | 528 |
class StringField(Field): |
529 |
def __init__(self, max_length=None, **kwargs): |
|
530 |
super(StringField, self).__init__(**kwargs) |
|
531 |
self.max_length = max_length |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
532 |
|
907 | 533 |
|
844 | 534 |
class TextField(Field): |
907 | 535 |
widget = TextArea |
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
|
536 |
def __init__(self, rows=10, cols=80, **kwargs): |
907 | 537 |
super(TextField, self).__init__(**kwargs) |
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
|
538 |
self.rows = rows |
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
|
539 |
self.cols = cols |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
540 |
|
907 | 541 |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
542 |
class RichTextField(TextField): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
543 |
widget = None |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
544 |
def __init__(self, format_field=None, **kwargs): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
545 |
super(RichTextField, self).__init__(**kwargs) |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
546 |
self.format_field = format_field |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
547 |
|
907 | 548 |
def get_widget(self, form): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
549 |
if self.widget is None: |
907 | 550 |
if self.use_fckeditor(form): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
551 |
return FCKEditor() |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
552 |
return TextArea() |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
553 |
return self.widget |
844 | 554 |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
555 |
def get_format_field(self, form): |
907 | 556 |
if self.format_field: |
557 |
return self.format_field |
|
558 |
# we have to cache generated field since it's use as key in the |
|
559 |
# context dictionnary |
|
560 |
try: |
|
561 |
return form.req.data[self] |
|
562 |
except KeyError: |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
563 |
if self.use_fckeditor(form): |
907 | 564 |
# if fckeditor is used and format field isn't explicitly |
565 |
# deactivated, we want an hidden field for the format |
|
566 |
widget = HiddenInput() |
|
1048 | 567 |
choices = None |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
568 |
else: |
907 | 569 |
# else we want a format selector |
570 |
# XXX compute vocabulary |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
571 |
widget = Select |
1048 | 572 |
req = form.req |
573 |
choices = [(req._(format), format) for format in FormatConstraint().vocabulary(req=req)] |
|
574 |
field = StringField(name=self.name + '_format', widget=widget, |
|
575 |
choices=choices) |
|
907 | 576 |
form.req.data[self] = field |
577 |
return field |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
578 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
579 |
def actual_fields(self, form): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
580 |
yield self |
894 | 581 |
format_field = self.get_format_field(form) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
582 |
if format_field: |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
583 |
yield format_field |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
584 |
|
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
585 |
def use_fckeditor(self, form): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
586 |
"""return True if fckeditor should be used to edit entity's attribute named |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
587 |
`attr`, according to user preferences |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
588 |
""" |
1013
948a3882c94a
add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents:
1009
diff
changeset
|
589 |
if form.req.use_fckeditor(): |
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
590 |
return form.form_field_format(self) == 'text/html' |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
591 |
return False |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
592 |
|
907 | 593 |
def render(self, form, renderer): |
894 | 594 |
format_field = self.get_format_field(form) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
595 |
if format_field: |
907 | 596 |
result = format_field.render(form, renderer) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
597 |
else: |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
598 |
result = u'' |
907 | 599 |
return result + self.get_widget(form).render(form, self) |
600 |
||
601 |
||
602 |
class FileField(StringField): |
|
603 |
widget = FileInput |
|
604 |
needs_multipart = True |
|
605 |
||
606 |
def __init__(self, format_field=None, encoding_field=None, **kwargs): |
|
607 |
super(FileField, self).__init__(**kwargs) |
|
608 |
self.format_field = format_field |
|
609 |
self.encoding_field = encoding_field |
|
610 |
||
611 |
def actual_fields(self, form): |
|
612 |
yield self |
|
613 |
if self.format_field: |
|
614 |
yield self.format_field |
|
615 |
if self.encoding_field: |
|
616 |
yield self.encoding_field |
|
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
617 |
|
907 | 618 |
def render(self, form, renderer): |
619 |
wdgs = [self.get_widget(form).render(form, self)] |
|
620 |
if self.format_field or self.encoding_field: |
|
621 |
divid = '%s-advanced' % form.context[self]['name'] |
|
622 |
wdgs.append(u'<a href="%s" title="%s"><img src="%s" alt="%s"/></a>' % |
|
623 |
(html_escape(toggle_action(divid)), |
|
624 |
form.req._('show advanced fields'), |
|
625 |
html_escape(form.req.build_url('data/puce_down.png')), |
|
626 |
form.req._('show advanced fields'))) |
|
627 |
wdgs.append(u'<div id="%s" class="hidden">' % divid) |
|
628 |
if self.format_field: |
|
629 |
wdgs.append(self.render_subfield(form, self.format_field, renderer)) |
|
630 |
if self.encoding_field: |
|
631 |
wdgs.append(self.render_subfield(form, self.encoding_field, renderer)) |
|
632 |
wdgs.append(u'</div>') |
|
633 |
if not self.required and form.context[self]['value']: |
|
634 |
# trick to be able to delete an uploaded file |
|
635 |
wdgs.append(u'<br/>') |
|
636 |
wdgs.append(tags.input(name=u'%s__detach' % form.context[self]['name'], |
|
637 |
type=u'checkbox')) |
|
638 |
wdgs.append(form.req._('detach attached file')) |
|
639 |
return u'\n'.join(wdgs) |
|
640 |
||
641 |
def render_subfield(self, form, field, renderer): |
|
642 |
return (renderer.render_label(form, field) |
|
643 |
+ field.render(form, renderer) |
|
644 |
+ renderer.render_help(form, field) |
|
645 |
+ u'<br/>') |
|
646 |
||
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
647 |
|
844 | 648 |
class IntField(Field): |
649 |
def __init__(self, min=None, max=None, **kwargs): |
|
650 |
super(IntField, self).__init__(**kwargs) |
|
651 |
self.min = min |
|
652 |
self.max = max |
|
653 |
||
907 | 654 |
|
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
|
655 |
class BooleanField(Field): |
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
|
656 |
widget = Radio |
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
|
657 |
|
907 | 658 |
|
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
659 |
class FloatField(IntField): |
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
|
660 |
def format_single_value(self, req, value): |
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
661 |
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
|
662 |
if value is None: |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
663 |
return u'' |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
664 |
return formatstr % float(value) |
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
665 |
|
907 | 666 |
def render_example(self, req): |
667 |
return self.format_value(req, 1.234) |
|
668 |
||
669 |
||
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
670 |
class DateField(StringField): |
899
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
671 |
format_prop = 'ui.date-format' |
905
64fd6eaaa9a4
add needs_css and needs_js on widgets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
904
diff
changeset
|
672 |
widget = DateTimePicker |
844 | 673 |
|
899
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
674 |
def format_single_value(self, req, value): |
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
675 |
return value and ustrftime(value, req.property_value(self.format_prop)) or u'' |
844 | 676 |
|
907 | 677 |
def render_example(self, req): |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
1013
diff
changeset
|
678 |
return self.format_value(req, datetime.now()) |
907 | 679 |
|
680 |
||
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
681 |
class DateTimeField(DateField): |
899
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
682 |
format_prop = 'ui.datetime-format' |
864
e0947007542a
basic implementation of Date / Float fields
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
863
diff
changeset
|
683 |
|
907 | 684 |
|
899
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
685 |
class TimeField(DateField): |
48d465b5171a
rename propname into format_prop
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
898
diff
changeset
|
686 |
format_prop = 'ui.datetime-format' |
907 | 687 |
|
844 | 688 |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
689 |
class HiddenInitialValueField(Field): |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
690 |
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
|
691 |
super(HiddenInitialValueField, self).__init__(name=name, |
898
583f64567256
fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
897
diff
changeset
|
692 |
widget=HiddenInput, |
583f64567256
fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
897
diff
changeset
|
693 |
eidparam=True) |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
694 |
self.visible_field = visible_field |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
695 |
|
844 | 696 |
|
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
697 |
class RelationField(Field): |
903
63a8ab7eeb9c
The value as returned by FieldWidget._render_attrs() is now always a list
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
902
diff
changeset
|
698 |
def __init__(self, **kwargs): |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
699 |
super(RelationField, self).__init__(**kwargs) |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
700 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
701 |
@staticmethod |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
702 |
def fromcardinality(card, role, **kwargs): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
703 |
return RelationField(widget=Select(multiple=card in '*+'), |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
704 |
**kwargs) |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
705 |
|
901
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
706 |
def vocabulary(self, form): |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
707 |
entity = form.entity |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
708 |
req = entity.req |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
709 |
# first see if its specified by __linkto form parameters |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
710 |
linkedto = entity.linked_to(self.name, self.role) |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
711 |
if linkedto: |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
712 |
entities = (req.eid_rset(eid).get_entity(0, 0) for eid in linkedto) |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
713 |
return [(entity.view('combobox'), entity.eid) for entity in entities] |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
714 |
# it isn't, check if the entity provides a method to get correct values |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
715 |
res = [] |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
716 |
if not self.required: |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
717 |
res.append(('', INTERNAL_FIELD_VALUE)) |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
718 |
# vocabulary doesn't include current values, add them |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
719 |
if entity.has_eid(): |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
720 |
rset = entity.related(self.name, self.role) |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
721 |
relatedvocab = [(e.view('combobox'), e.eid) for e in rset.entities()] |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
722 |
else: |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
723 |
relatedvocab = [] |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
724 |
return res + form.form_field_vocabulary(self) + relatedvocab |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
725 |
|
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
726 |
def format_single_value(self, req, value): |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
727 |
return value |
0dcf01eb97a0
implement default vocabulary method on Field, provide a specific one on RelationField
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
900
diff
changeset
|
728 |
|
844 | 729 |
# forms ############ |
730 |
class metafieldsform(type): |
|
731 |
def __new__(mcs, name, bases, classdict): |
|
732 |
allfields = [] |
|
733 |
for base in bases: |
|
734 |
if hasattr(base, '_fields_'): |
|
735 |
allfields += base._fields_ |
|
736 |
clsfields = (item for item in classdict.items() |
|
737 |
if isinstance(item[1], Field)) |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
738 |
for fieldname, field in sorted(clsfields, key=lambda x: x[1].creation_rank): |
844 | 739 |
if not field.name: |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
740 |
field.set_name(fieldname) |
844 | 741 |
allfields.append(field) |
742 |
classdict['_fields_'] = allfields |
|
743 |
return super(metafieldsform, mcs).__new__(mcs, name, bases, classdict) |
|
744 |
||
745 |
||
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
746 |
class FieldsForm(FormMixIn, AppObject): |
844 | 747 |
__metaclass__ = metafieldsform |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
748 |
__registry__ = 'forms' |
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
749 |
__select__ = yes() |
844 | 750 |
|
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
751 |
def __init__(self, req, rset=None, domid=None, title=None, action='edit', |
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
752 |
onsubmit="return freezeFormButtons('%(domid)s');", |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
753 |
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
|
754 |
redirect_path=None, set_error_url=True, copy_nav_params=False): |
844 | 755 |
self.req = req |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
756 |
self.rset = rset |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
757 |
self.config = req.vreg.config |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
758 |
self.domid = domid or 'form' |
844 | 759 |
self.title = title |
760 |
self.action = action |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
761 |
self.onsubmit = onsubmit |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
762 |
self.cssclass = cssclass |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
763 |
self.cssstyle = cssstyle |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
764 |
self.cwtarget = cwtarget |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
765 |
self.redirect_path = redirect_path |
844 | 766 |
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
|
767 |
if set_error_url: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
768 |
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
|
769 |
if copy_nav_params: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
770 |
for param in NAV_FORM_PARAMETERS: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
771 |
value = req.form.get(param) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
772 |
if value: |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
773 |
self.form_add_hidden(param, initial=value) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
774 |
self.buttons = buttons or [] |
1053 | 775 |
self.context = None |
1058
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
776 |
|
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
777 |
@iclassmethod |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
778 |
def field_by_name(cls_or_self, name): |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
779 |
if isinstance(cls_or_self, type): |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
780 |
fields = cls_or_self._fields_ |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
781 |
else: |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
782 |
fields = cls_or_self.fields |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
783 |
for field in fields: |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
784 |
if field.name == name: |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
785 |
return field |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
786 |
raise Exception('field %s not found' % name) |
c27be37daef8
field_by_name method (on class or instance)
sylvain.thenault@logilab.fr
parents:
1056
diff
changeset
|
787 |
|
844 | 788 |
@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
|
789 |
def form_needs_multipart(self): |
844 | 790 |
return any(field.needs_multipart for field in self.fields) |
0 | 791 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
792 |
def form_add_hidden(self, name, value=None, **kwargs): |
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
|
793 |
self.fields.append(StringField(name=name, widget=HiddenInput, |
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
|
794 |
initial=value, **kwargs)) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
795 |
|
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
|
796 |
def form_render(self, **values): |
844 | 797 |
renderer = values.pop('renderer', FormRenderer()) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
798 |
return renderer.render(self, values) |
844 | 799 |
|
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
|
800 |
def form_build_context(self, values): |
844 | 801 |
self.context = context = {} |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
802 |
# on validation error, we get a dictionnary of previously submitted values |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
803 |
previous_values = self.req.data.get('formvalues') |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
804 |
if previous_values: |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
805 |
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
|
806 |
for field in self.fields: |
894 | 807 |
for field in field.actual_fields(self): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
808 |
value = self.form_field_value(field, values) |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
809 |
context[field] = {'value': field.format_value(self.req, value), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
810 |
'rawvalue': value, |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
811 |
'name': self.form_field_name(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
812 |
'id': self.form_field_id(field), |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
813 |
} |
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
|
814 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
815 |
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
|
816 |
"""looks for field's value in |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
817 |
1. kw args given to render_form (including previously submitted form |
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
818 |
values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
819 |
2. req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
820 |
3. field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
821 |
""" |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
822 |
if field.name in values: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
823 |
value = values[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
824 |
elif field.name in self.req.form: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
825 |
value = self.req.form[field.name] |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
826 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
827 |
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
|
828 |
return value |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
829 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
830 |
def form_field_format(self, field): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
831 |
return self.req.property_value('ui.default-text-format') |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
832 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
833 |
def form_field_encoding(self, field): |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
834 |
return self.req.encoding |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
835 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
836 |
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
|
837 |
return field.name |
844 | 838 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
839 |
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
|
840 |
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
|
841 |
|
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
|
842 |
def form_field_vocabulary(self, field): |
844 | 843 |
raise NotImplementedError |
844 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
845 |
def form_buttons(self): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
846 |
return self.buttons |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
847 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
848 |
|
844 | 849 |
class EntityFieldsForm(FieldsForm): |
1047
21d4d5e6aa45
make forms selectable (appobject)
sylvain.thenault@logilab.fr
parents:
1032
diff
changeset
|
850 |
__select__ = non_final_entity() |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
851 |
|
844 | 852 |
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
|
853 |
kwargs.setdefault('domid', 'entityForm') |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
854 |
self.entity = kwargs.pop('entity', None) |
844 | 855 |
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
|
856 |
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
|
857 |
self.form_add_hidden('eid') |
844 | 858 |
|
874
90d98d80af82
EntityFieldsForm.form_render() doesn't need entity anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
869
diff
changeset
|
859 |
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
|
860 |
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
|
861 |
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
|
862 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
863 |
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
|
864 |
for field in self.fields[:]: |
894 | 865 |
for field in field.actual_fields(self): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
866 |
fieldname = field.name |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
867 |
if fieldname != 'eid' and ( |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
868 |
(eschema.has_subject_relation(fieldname) or |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
869 |
eschema.has_object_relation(fieldname))): |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
870 |
field.eidparam = True |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
871 |
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
|
872 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
873 |
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
|
874 |
"""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
|
875 |
before the modification |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
876 |
""" |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
877 |
# 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
|
878 |
# 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
|
879 |
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
|
880 |
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
|
881 |
else: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
882 |
name = 'edito-%s' % field.name |
898
583f64567256
fix HiddenRelationField bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
897
diff
changeset
|
883 |
return HiddenInitialValueField(field, name=name) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
884 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
885 |
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
|
886 |
"""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
|
887 |
1. handle special __type and eid fields |
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
888 |
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
|
889 |
form values if any) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
890 |
3. looks in req.form |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
891 |
4. if entity has an eid: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
892 |
1. looks for an associated attribute / method |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
893 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
894 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
895 |
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
|
896 |
2. use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
897 |
|
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
898 |
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
|
899 |
""" |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
900 |
fieldname = field.name |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
901 |
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
|
902 |
# 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
|
903 |
if self.entity.has_eid(): |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
904 |
value = self._form_field_entity_value(field.visible_field, |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
905 |
default_initial=False) |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
906 |
else: |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
907 |
value = INTERNAL_FIELD_VALUE |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
908 |
elif fieldname == '__type': |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
909 |
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
|
910 |
elif fieldname == 'eid': |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
911 |
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
|
912 |
elif fieldname in values: |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
913 |
value = values[fieldname] |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
914 |
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
|
915 |
value = self.req.form[fieldname] |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
916 |
else: |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
917 |
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
|
918 |
# 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
|
919 |
# 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
|
920 |
# undesired value) |
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
921 |
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
|
922 |
load_bytes=load_bytes) |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
923 |
else: |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
924 |
defaultattr = 'default_%s' % fieldname |
848
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
925 |
if hasattr(self.entity, defaultattr): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
926 |
# 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
|
927 |
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
|
928 |
% (defaultattr, self.entity.id), DeprecationWarning) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
929 |
value = getattr(self.entity, defaultattr) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
930 |
elif hasattr(self, defaultattr): |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
931 |
# 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
|
932 |
value = getattr(self, defaultattr) |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
933 |
else: |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
934 |
# use field's initial value |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
935 |
value = field.initial |
f5aca5c5f6ca
set __errorurl, fix retreiving of field's value
sylvain.thenault@logilab.fr
parents:
847
diff
changeset
|
936 |
if callable(value): |
1059 | 937 |
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
|
938 |
return value |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
939 |
|
1060
d795477ad16b
rename method to form_field_format
sylvain.thenault@logilab.fr
parents:
1059
diff
changeset
|
940 |
def form_field_format(self, field): |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
941 |
entity = self.entity |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
942 |
if field.eidparam and entity.has_format(field.name) and ( |
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
943 |
entity.has_eid() or '%s_format' % field.name in entity): |
1059 | 944 |
return self.entity.format(field.name) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
945 |
return self.req.property_value('ui.default-text-format') |
844 | 946 |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
947 |
def form_field_encoding(self, field): |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
948 |
entity = self.entity |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
949 |
if field.eidparam and entity.has_encoding(field.name) and ( |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
950 |
entity.has_eid() or '%s_encoding' % field.name in entity): |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
951 |
return self.entity.text_encoding() |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
952 |
return super(EntityFieldsForm, self).form_field_encoding(field) |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
953 |
|
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
954 |
def _form_field_entity_value(self, field, default_initial=True, load_bytes=False): |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
955 |
attr = field.name |
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
956 |
if field.role == 'object': |
1049
f8e92c2f3bef
fix attribute name for object relation
sylvain.thenault@logilab.fr
parents:
1048
diff
changeset
|
957 |
attr = 'reverse_' + attr |
907 | 958 |
else: |
959 |
attrtype = self.entity.e_schema.destination(attr) |
|
960 |
if attrtype == 'Password': |
|
961 |
return self.entity.has_eid() and INTERNAL_FIELD_VALUE or '' |
|
962 |
if attrtype == 'Bytes': |
|
1061
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
963 |
if self.entity.has_eid(): |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
964 |
if load_bytes: |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
965 |
return getattr(self.entity, attr) |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
966 |
# 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
|
967 |
return True |
7d2e6b5fae3c
* new load_bytes argument to form_field_value
sylvain.thenault@logilab.fr
parents:
1060
diff
changeset
|
968 |
return False |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
969 |
if default_initial: |
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
|
970 |
value = getattr(self.entity, attr, field.initial) |
888
603327e9aef4
fix field value for hidden edit[s|Ã ] fields
sylvain.thenault@logilab.fr
parents:
887
diff
changeset
|
971 |
else: |
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
|
972 |
value = getattr(self.entity, attr) |
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
|
973 |
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
|
974 |
# 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
|
975 |
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
|
976 |
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
|
977 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
978 |
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
|
979 |
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
|
980 |
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
|
981 |
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
|
982 |
|
849
8591d896db7e
update some prototype, ChangeStateForm
sylvain.thenault@logilab.fr
parents:
848
diff
changeset
|
983 |
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
|
984 |
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
|
985 |
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
|
986 |
return field.id |
844 | 987 |
|
1031
1a89683cb687
restore limit on form_field_vocabulary, actually used
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
988 |
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
|
989 |
role, rtype = field.role, field.name |
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
|
990 |
try: |
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
|
991 |
vocabfunc = getattr(self.entity, '%s_%s_vocabulary' % (role, rtype)) |
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
|
992 |
except AttributeError: |
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
|
993 |
vocabfunc = getattr(self, '%s_relation_vocabulary' % role) |
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
|
994 |
else: |
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
|
995 |
# XXX bw compat, default_<field name> on the entity |
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
|
996 |
warn('found %s_%s_vocabulary on %s, should be set on a specific form' |
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
|
997 |
% (role, rtype, self.entity.id), DeprecationWarning) |
1009 | 998 |
# NOTE: it is the responsibility of `vocabfunc` to sort the result |
999 |
# (direclty through RQL or via a python sort). This is also |
|
1000 |
# important because `vocabfunc` might return a list with |
|
1001 |
# couples (label, None) which act as separators. In these |
|
1002 |
# 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
|
1003 |
return vocabfunc(rtype, limit) |
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
|
1004 |
## XXX BACKPORT ME |
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
|
1005 |
## if self.sort: |
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
|
1006 |
## choices = sorted(choices) |
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
|
1007 |
## if self.rschema.rproperty(self.subjtype, self.objtype, 'internationalizable'): |
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
|
1008 |
## return zip((entity.req._(v) for v in choices), choices) |
844 | 1009 |
|
902
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1010 |
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
|
1011 |
"""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
|
1012 |
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
|
1013 |
""" |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1014 |
entity = self.entity |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1015 |
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
|
1016 |
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
|
1017 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1018 |
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
|
1019 |
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
|
1020 |
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
|
1021 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1022 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1023 |
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
|
1024 |
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
|
1025 |
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
|
1026 |
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
|
1027 |
rsetsize, done) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1028 |
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
|
1029 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1030 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1031 |
|
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1032 |
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
|
1033 |
"""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
|
1034 |
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
|
1035 |
""" |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1036 |
entity = self.entity |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1037 |
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
|
1038 |
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
|
1039 |
done = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1040 |
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
|
1041 |
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
|
1042 |
result = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1043 |
rsetsize = None |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1044 |
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
|
1045 |
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
|
1046 |
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
|
1047 |
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
|
1048 |
rsetsize, done) |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1049 |
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
|
1050 |
break |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1051 |
return result |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1052 |
|
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1053 |
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
|
1054 |
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
|
1055 |
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
|
1056 |
done = set() |
1009 | 1057 |
rset = self.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
|
1058 |
res = [] |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1059 |
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
|
1060 |
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
|
1061 |
continue |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1062 |
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
|
1063 |
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
|
1064 |
return res |
e4de959c76af
vocabulary methods are now defined on forms, not on entities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
901
diff
changeset
|
1065 |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1066 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1067 |
class MultipleFieldsForm(FieldsForm): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1068 |
def __init__(self, *args, **kwargs): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1069 |
super(MultipleFieldsForm, self).__init__(*args, **kwargs) |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1070 |
self.forms = [] |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1071 |
|
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1072 |
def form_add_subform(self, subform): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1073 |
self.forms.append(subform) |
907 | 1074 |
|
1075 |
||
844 | 1076 |
# form renderers ############ |
1077 |
class FormRenderer(object): |
|
907 | 1078 |
|
1079 |
# renderer interface ###################################################### |
|
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
|
1080 |
|
907 | 1081 |
def render(self, form, values, display_help=True): |
844 | 1082 |
data = [] |
1083 |
w = data.append |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1084 |
w(self.open_form(form)) |
896
6e0379fc1988
put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
894
diff
changeset
|
1085 |
w(u'<div id="progress">%s</div>' % form.req._('validating...')) |
844 | 1086 |
w(u'<fieldset>') |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
1087 |
w(tags.input(type='hidden', name='__form_id', value=form.domid)) |
844 | 1088 |
if form.redirect_path: |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
1089 |
w(tags.input(type='hidden', name='__redirectpath', value=form.redirect_path)) |
907 | 1090 |
self.render_fields(w, form, values, display_help) |
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
|
1091 |
self.render_buttons(w, form) |
844 | 1092 |
w(u'</fieldset>') |
1093 |
w(u'</form>') |
|
1094 |
return '\n'.join(data) |
|
907 | 1095 |
|
1096 |
def render_label(self, form, field): |
|
1097 |
label = form.req._(field.label) |
|
1098 |
attrs = {'for': form.context[field]['id']} |
|
1099 |
if field.required: |
|
1100 |
attrs['class'] = 'required' |
|
1101 |
return tags.label(label, **attrs) |
|
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
|
1102 |
|
907 | 1103 |
def render_help(self, form, field): |
1104 |
help = [ u'<br/>' ] |
|
1105 |
descr = field.help |
|
1106 |
if descr: |
|
1107 |
help.append('<span class="helper">%s</span>' % req._(descr)) |
|
1108 |
example = field.example_format(form.req) |
|
1109 |
if example: |
|
1110 |
help.append('<span class="helper">(%s: %s)</span>' |
|
1111 |
% (req._('sample format'), example)) |
|
1112 |
return u' '.join(help) |
|
1113 |
||
1114 |
# specific methods (mostly to ease overriding) ############################# |
|
1115 |
||
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1116 |
def open_form(self, form): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1117 |
if form.form_needs_multipart: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1118 |
enctype = 'multipart/form-data' |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1119 |
else: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1120 |
enctype = 'application/x-www-form-urlencoded' |
1009 | 1121 |
tag = ('<form action="%s" method="post" id="%s" enctype="%s"' % ( |
887
51e371245bc5
various fixes to have change state and deletion forms working
sylvain.thenault@logilab.fr
parents:
874
diff
changeset
|
1122 |
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
|
1123 |
if form.onsubmit: |
1050
c9815bef16d7
expand onsubmit using form's __dict__
sylvain.thenault@logilab.fr
parents:
1049
diff
changeset
|
1124 |
tag += ' onsubmit="%s"' % html_escape(form.onsubmit % form.__dict__) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1125 |
if form.cssstyle: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1126 |
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
|
1127 |
if form.cssclass: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1128 |
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
|
1129 |
if form.cwtarget: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1130 |
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
|
1131 |
return tag + '>' |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1132 |
|
907 | 1133 |
def render_fields(self, w, form, values, display_help=True): |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1134 |
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
|
1135 |
fields = form.fields[:] |
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
1136 |
for field in form.fields: |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
1137 |
if not field.is_visible(): |
907 | 1138 |
w(field.render(form, self)) |
863
4fde01fc39ca
restore previous hidden input behaviour (edits- / edito- fields)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
849
diff
changeset
|
1139 |
fields.remove(field) |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1140 |
if fields: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1141 |
w(u'<table>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1142 |
for field in fields: |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1143 |
w(u'<tr>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1144 |
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
|
1145 |
w(u'<td style="width:100%;">') |
907 | 1146 |
w(field.render(form, self)) |
1147 |
if display_help == True: |
|
1148 |
w(self.render_help(form, field)) |
|
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1149 |
w(u'</td></tr>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1150 |
w(u'</table>') |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1151 |
for childform in getattr(form, 'forms', []): |
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1152 |
self.render_fields(w, childform, values) |
892
1558772340a7
rich field / fckeditor handling
sylvain.thenault@logilab.fr
parents:
888
diff
changeset
|
1153 |
|
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
|
1154 |
def render_buttons(self, w, form): |
896
6e0379fc1988
put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
894
diff
changeset
|
1155 |
w(u'<table class="formButtonBar">\n<tr>\n') |
869
168ad6d424d1
form to edit multiple entities, use it in DeleteConfForm
sylvain.thenault@logilab.fr
parents:
867
diff
changeset
|
1156 |
for button in form.form_buttons(): |
896
6e0379fc1988
put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
894
diff
changeset
|
1157 |
w(u'<td>%s</td>\n' % button) |
6e0379fc1988
put form buttons in a separate table
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
894
diff
changeset
|
1158 |
w(u'</tr></table>') |
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1159 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1160 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1161 |
def stringfield_from_constraints(constraints, **kwargs): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1162 |
field = None |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1163 |
for cstr in constraints: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1164 |
if isinstance(cstr, StaticVocabularyConstraint): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1165 |
return StringField(widget=Select(vocabulary=cstr.vocabulary), |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1166 |
**kwargs) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1167 |
if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1168 |
if cstr.max > 257: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1169 |
field = textfield_from_constraint(cstr, **kwargs) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1170 |
else: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1171 |
field = StringField(max_length=cstr.max, **kwargs) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1172 |
return field or TextField(**kwargs) |
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
|
1173 |
|
897
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1174 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1175 |
def textfield_from_constraint(constraint, **kwargs): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1176 |
if 256 < constraint.max < 513: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1177 |
rows, cols = 5, 60 |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1178 |
else: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1179 |
rows, cols = 10, 80 |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1180 |
return TextField(rows, cols, **kwargs) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1181 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1182 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1183 |
def find_field(eclass, subjschema, rschema, role='subject'): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1184 |
"""return the most adapated widget to edit the relation |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1185 |
'subjschema rschema objschema' according to information found in the schema |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1186 |
""" |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1187 |
fieldclass = None |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1188 |
if role == 'subject': |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1189 |
objschema = rschema.objects(subjschema)[0] |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1190 |
cardidx = 0 |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1191 |
else: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1192 |
objschema = rschema.subjects(subjschema)[0] |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1193 |
cardidx = 1 |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1194 |
card = rschema.rproperty(subjschema, objschema, 'cardinality')[cardidx] |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1195 |
required = card in '1+' |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1196 |
if rschema in eclass.widgets: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1197 |
fieldclass = eclass.widgets[rschema] |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1198 |
if isinstance(fieldclass, basestring): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1199 |
return StringField(name=rschema.type) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1200 |
elif not rschema.is_final(): |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1201 |
return RelationField.fromcardinality(card, role,name=rschema.type, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1202 |
required=required) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1203 |
else: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1204 |
fieldclass = FIELDS[objschema] |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1205 |
if fieldclass is StringField: |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1206 |
constraints = rschema.rproperty(subjschema, objschema, 'constraints') |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1207 |
return stringfield_from_constraints(constraints, name=rschema.type, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1208 |
required=required) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1209 |
return fieldclass(name=rschema.type, required=required) |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1210 |
|
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1211 |
FIELDS = { |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1212 |
'Boolean': BooleanField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1213 |
'Bytes': FileField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1214 |
'Date': DateField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1215 |
'Datetime': DateTimeField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1216 |
'Int': IntField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1217 |
'Float': FloatField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1218 |
'Decimal': StringField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1219 |
'Password': StringField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1220 |
'String' : StringField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1221 |
'Time': TimeField, |
f5b91f11d8b6
provide basic methods to build fields from schema constraints / cardinality
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
896
diff
changeset
|
1222 |
} |