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