author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 02 Sep 2009 09:30:50 +0200 | |
branch | stable |
changeset 3092 | c153b1ae9875 |
parent 2998 | da622f980470 |
child 3157 | f4b94d03f86f |
permissions | -rw-r--r-- |
0 | 1 |
"""The edit controller, handling form submitting. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1948
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1948
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
1948 | 9 |
|
0 | 10 |
from decimal import Decimal |
11 |
||
12 |
from rql.utils import rqlvar_maker |
|
13 |
||
14 |
from cubicweb import Binary, ValidationError, typed_eid |
|
15 |
from cubicweb.web import INTERNAL_FIELD_VALUE, RequestError, NothingToEdit |
|
16 |
from cubicweb.web.controller import parse_relations_descr |
|
17 |
from cubicweb.web.views.basecontrollers import ViewController |
|
18 |
||
19 |
||
20 |
class ToDoLater(Exception): |
|
21 |
"""exception used in the edit controller to indicate that a relation |
|
22 |
can't be handled right now and have to be handled later |
|
23 |
""" |
|
24 |
||
25 |
class EditController(ViewController): |
|
26 |
id = 'edit' |
|
27 |
||
2255
c346af0727ca
more generic way to detect json requests (not yet perfect though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
def publish(self, rset=None): |
0 | 29 |
"""edit / create / copy / delete entity / relations""" |
884 | 30 |
for key in self.req.form: |
0 | 31 |
# There should be 0 or 1 action |
32 |
if key.startswith('__action_'): |
|
33 |
cbname = key[1:] |
|
34 |
try: |
|
35 |
callback = getattr(self, cbname) |
|
36 |
except AttributeError: |
|
884 | 37 |
raise RequestError(self.req._('invalid action %r' % key)) |
0 | 38 |
else: |
39 |
return callback() |
|
40 |
self._default_publish() |
|
41 |
self.reset() |
|
42 |
||
43 |
def _default_publish(self): |
|
44 |
req = self.req |
|
45 |
form = req.form |
|
46 |
# no specific action, generic edition |
|
47 |
self._to_create = req.data['eidmap'] = {} |
|
48 |
self._pending_relations = [] |
|
49 |
todelete = self.req.get_pending_deletes() |
|
50 |
toinsert = self.req.get_pending_inserts() |
|
51 |
try: |
|
52 |
methodname = form.pop('__method', None) |
|
53 |
for eid in req.edited_eids(): |
|
54 |
formparams = req.extract_entity_params(eid) |
|
55 |
if methodname is not None: |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
56 |
entity = req.entity_from_eid(eid) |
0 | 57 |
method = getattr(entity, methodname) |
58 |
method(formparams) |
|
59 |
eid = self.edit_entity(formparams) |
|
60 |
except (RequestError, NothingToEdit): |
|
61 |
if '__linkto' in form and 'eid' in form: |
|
62 |
self.execute_linkto() |
|
63 |
elif not ('__delete' in form or '__insert' in form or todelete or toinsert): |
|
64 |
raise ValidationError(None, {None: req._('nothing to edit')}) |
|
65 |
# handle relations in newly created entities |
|
66 |
if self._pending_relations: |
|
67 |
for rschema, formparams, x, entity in self._pending_relations: |
|
68 |
self.handle_relation(rschema, formparams, x, entity, True) |
|
1753 | 69 |
|
0 | 70 |
# XXX this processes *all* pending operations of *all* entities |
71 |
if form.has_key('__delete'): |
|
72 |
todelete += req.list_form_param('__delete', form, pop=True) |
|
73 |
if todelete: |
|
74 |
self.delete_relations(parse_relations_descr(todelete)) |
|
75 |
if form.has_key('__insert'): |
|
76 |
toinsert = req.list_form_param('__insert', form, pop=True) |
|
77 |
if toinsert: |
|
78 |
self.insert_relations(parse_relations_descr(toinsert)) |
|
79 |
self.req.remove_pending_operations() |
|
1753 | 80 |
|
0 | 81 |
def edit_entity(self, formparams, multiple=False): |
82 |
"""edit / create / copy an entity and return its eid""" |
|
83 |
etype = formparams['__type'] |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2256
diff
changeset
|
84 |
entity = self.vreg['etypes'].etype_class(etype)(self.req) |
0 | 85 |
entity.eid = eid = self._get_eid(formparams['eid']) |
86 |
edited = self.req.form.get('__maineid') == formparams['eid'] |
|
87 |
# let a chance to do some entity specific stuff. |
|
1753 | 88 |
entity.pre_web_edit() |
0 | 89 |
# create a rql query from parameters |
90 |
self.relations = [] |
|
91 |
self.restrictions = [] |
|
92 |
# process inlined relations at the same time as attributes |
|
93 |
# this is required by some external source such as the svn source which |
|
94 |
# needs some information provided by those inlined relation. Moreover |
|
95 |
# this will generate less write queries. |
|
96 |
for rschema in entity.e_schema.subject_relations(): |
|
97 |
if rschema.is_final(): |
|
98 |
self.handle_attribute(entity, rschema, formparams) |
|
99 |
elif rschema.inlined: |
|
100 |
self.handle_inlined_relation(rschema, formparams, entity) |
|
101 |
execute = self.req.execute |
|
102 |
if eid is None: # creation or copy |
|
1753 | 103 |
if self.relations: |
0 | 104 |
rql = 'INSERT %s X: %s' % (etype, ','.join(self.relations)) |
105 |
else: |
|
106 |
rql = 'INSERT %s X' % etype |
|
107 |
if self.restrictions: |
|
108 |
rql += ' WHERE %s' % ','.join(self.restrictions) |
|
109 |
try: |
|
1753 | 110 |
# get the new entity (in some cases, the type might have |
0 | 111 |
# changed as for the File --> Image mutation) |
112 |
entity = execute(rql, formparams).get_entity(0, 0) |
|
113 |
eid = entity.eid |
|
114 |
except ValidationError, ex: |
|
115 |
self._to_create[formparams['eid']] = ex.entity |
|
2255
c346af0727ca
more generic way to detect json requests (not yet perfect though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
116 |
if self.req.json_request: # XXX (syt) why? |
0 | 117 |
ex.entity = formparams['eid'] |
118 |
raise |
|
119 |
self._to_create[formparams['eid']] = eid |
|
120 |
elif self.relations: # edition of an existant entity |
|
121 |
varmaker = rqlvar_maker() |
|
122 |
var = varmaker.next() |
|
123 |
while var in formparams: |
|
124 |
var = varmaker.next() |
|
125 |
rql = 'SET %s WHERE X eid %%(%s)s' % (','.join(self.relations), var) |
|
126 |
if self.restrictions: |
|
127 |
rql += ', %s' % ','.join(self.restrictions) |
|
128 |
formparams[var] = eid |
|
129 |
execute(rql, formparams) |
|
130 |
for rschema in entity.e_schema.subject_relations(): |
|
131 |
if rschema.is_final() or rschema.inlined: |
|
132 |
continue |
|
133 |
self.handle_relation(rschema, formparams, 'subject', entity) |
|
134 |
for rschema in entity.e_schema.object_relations(): |
|
135 |
if rschema.is_final(): |
|
136 |
continue |
|
137 |
self.handle_relation(rschema, formparams, 'object', entity) |
|
138 |
if edited: |
|
139 |
self.notify_edited(entity) |
|
140 |
if formparams.has_key('__delete'): |
|
141 |
todelete = self.req.list_form_param('__delete', formparams, pop=True) |
|
142 |
self.delete_relations(parse_relations_descr(todelete)) |
|
143 |
if formparams.has_key('__cloned_eid'): |
|
144 |
entity.copy_relations(formparams['__cloned_eid']) |
|
145 |
if formparams.has_key('__insert'): |
|
146 |
toinsert = self.req.list_form_param('__insert', formparams, pop=True) |
|
147 |
self.insert_relations(parse_relations_descr(toinsert)) |
|
148 |
if edited: # only execute linkto for the main entity |
|
149 |
self.execute_linkto(eid) |
|
150 |
return eid |
|
151 |
||
152 |
def _action_apply(self): |
|
153 |
self._default_publish() |
|
154 |
self.reset() |
|
1753 | 155 |
|
0 | 156 |
def _action_cancel(self): |
157 |
errorurl = self.req.form.get('__errorurl') |
|
158 |
if errorurl: |
|
159 |
self.req.cancel_edition(errorurl) |
|
2998
da622f980470
B #323887 misleading message after hitting cancel form button
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2680
diff
changeset
|
160 |
self.req.message = self.req._('edit canceled') |
0 | 161 |
return self.reset() |
162 |
||
163 |
def _action_delete(self): |
|
164 |
self.delete_entities(self.req.edited_eids(withtype=True)) |
|
165 |
return self.reset() |
|
166 |
||
1162
f210dce0dc47
fix for booelan attribute which have empty string as false value and didn't work if default value for this attribute was True.
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
167 |
def _needs_edition(self, rtype, formparams, entity): |
0 | 168 |
"""returns True and and the new value if `rtype` was edited""" |
169 |
editkey = 'edits-%s' % rtype |
|
170 |
if not editkey in formparams: |
|
171 |
return False, None # not edited |
|
172 |
value = formparams.get(rtype) or None |
|
1162
f210dce0dc47
fix for booelan attribute which have empty string as false value and didn't work if default value for this attribute was True.
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
173 |
if entity.has_eid() and (formparams.get(editkey) or None) == value: |
0 | 174 |
return False, None # not modified |
175 |
if value == INTERNAL_FIELD_VALUE: |
|
1753 | 176 |
value = None |
0 | 177 |
return True, value |
178 |
||
179 |
def handle_attribute(self, entity, rschema, formparams): |
|
180 |
"""append to `relations` part of the rql query to edit the |
|
181 |
attribute described by the given schema if necessary |
|
182 |
""" |
|
183 |
attr = rschema.type |
|
1162
f210dce0dc47
fix for booelan attribute which have empty string as false value and didn't work if default value for this attribute was True.
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
0
diff
changeset
|
184 |
edition_needed, value = self._needs_edition(attr, formparams, entity) |
0 | 185 |
if not edition_needed: |
186 |
return |
|
187 |
# test if entity class defines a special handler for this attribute |
|
188 |
custom_edit = getattr(entity, 'custom_%s_edit' % attr, None) |
|
189 |
if custom_edit: |
|
190 |
custom_edit(formparams, value, self.relations) |
|
191 |
return |
|
192 |
attrtype = rschema.objects(entity.e_schema)[0].type |
|
193 |
# on checkbox or selection, the field may not be in params |
|
3092
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
194 |
# NOTE: raising ValidationError here is not a good solution because |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
195 |
# we can't gather all errors at once. Hopefully, the new 3.6.x |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
196 |
# form handling will fix that |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
197 |
if attrtype == 'Int': |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
198 |
try: |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
199 |
value = int(value) |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
200 |
except ValueError: |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
201 |
raise ValidationError(entity.eid, |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
202 |
{attr: self.req._("invalid integer value")}) |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
203 |
elif attrtype == 'Float': |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
204 |
try: |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
205 |
value = float(value) |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
206 |
except ValueError: |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
207 |
raise ValidationError(entity.eid, |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
208 |
{attr: self.req._("invalid float value")}) |
c153b1ae9875
[edition] convert integer and float values coming from edit forms so that values are correctly typed in before_xxx hooks
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2998
diff
changeset
|
209 |
elif attrtype == 'Boolean': |
0 | 210 |
value = bool(value) |
211 |
elif attrtype == 'Decimal': |
|
212 |
value = Decimal(value) |
|
213 |
elif attrtype == 'Bytes': |
|
214 |
# if it is a file, transport it using a Binary (StringIO) |
|
907 | 215 |
# XXX later __detach is for the new widget system, the former is to |
216 |
# be removed once web/widgets.py has been dropped |
|
217 |
if formparams.has_key('__%s_detach' % attr) or formparams.has_key('%s__detach' % attr): |
|
0 | 218 |
# drop current file value |
219 |
value = None |
|
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
220 |
# no need to check value when nor explicit detach nor new file |
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
221 |
# submitted, since it will think the attribute is not modified |
0 | 222 |
elif isinstance(value, unicode): |
223 |
# file modified using a text widget |
|
1360
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
224 |
encoding = entity.attr_metadata(attr, 'encoding') |
13ae1121835e
rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
225 |
value = Binary(value.encode(encoding)) |
1765
a25c7c73c8f6
check a file is submitted first
sylvain.thenault@logilab.fr
parents:
1753
diff
changeset
|
226 |
elif value: |
1361
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
227 |
# value is a 3-uple (filename, mimetype, stream) |
0 | 228 |
val = Binary(value[2].read()) |
229 |
if not val.getvalue(): # usually an unexistant file |
|
230 |
value = None |
|
231 |
else: |
|
232 |
val.filename = value[0] |
|
1361
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
233 |
# ignore browser submitted MIME type since it may be buggy |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
234 |
# XXX add a config option to tell if we should consider it |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
235 |
# or not? |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
236 |
#if entity.e_schema.has_metadata(attr, 'format'): |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
237 |
# key = '%s_format' % attr |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
238 |
# formparams[key] = value[1] |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
239 |
# self.relations.append('X %s_format %%(%s)s' |
c558a88bb85d
ignore browser submitted values for file's MIME type
sylvain.thenault@logilab.fr
parents:
1360
diff
changeset
|
240 |
# % (attr, key)) |
1101
0c067de38e46
unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents:
907
diff
changeset
|
241 |
# XXX suppose a File compatible schema |
0 | 242 |
if entity.e_schema.has_subject_relation('name') \ |
243 |
and not formparams.get('name'): |
|
244 |
formparams['name'] = value[0] |
|
245 |
self.relations.append('X name %(name)s') |
|
246 |
value = val |
|
1940
2565aae48d48
fix so that one can edit an entity with a Bytes field without specifying a new file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
247 |
else: |
2565aae48d48
fix so that one can edit an entity with a Bytes field without specifying a new file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
248 |
# no specified value, skip |
2565aae48d48
fix so that one can edit an entity with a Bytes field without specifying a new file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
249 |
return |
0 | 250 |
elif value is not None: |
251 |
if attrtype in ('Date', 'Datetime', 'Time'): |
|
252 |
try: |
|
253 |
value = self.parse_datetime(value, attrtype) |
|
254 |
except ValueError: |
|
255 |
raise ValidationError(entity.eid, |
|
256 |
{attr: self.req._("invalid date")}) |
|
257 |
elif attrtype == 'Password': |
|
258 |
# check confirmation (see PasswordWidget for confirmation field name) |
|
259 |
confirmval = formparams.get(attr + '-confirm') |
|
260 |
if confirmval != value: |
|
261 |
raise ValidationError(entity.eid, |
|
262 |
{attr: self.req._("password and confirmation don't match")}) |
|
263 |
# password should *always* be utf8 encoded |
|
264 |
value = value.encode('UTF8') |
|
265 |
else: |
|
266 |
# strip strings |
|
267 |
value = value.strip() |
|
268 |
elif attrtype == 'Password': |
|
269 |
# skip None password |
|
270 |
return # unset password |
|
271 |
formparams[attr] = value |
|
272 |
self.relations.append('X %s %%(%s)s' % (attr, attr)) |
|
273 |
||
274 |
def _relation_values(self, rschema, formparams, x, entity, late=False): |
|
275 |
"""handle edition for the (rschema, x) relation of the given entity |
|
276 |
""" |
|
277 |
rtype = rschema.type |
|
278 |
editkey = 'edit%s-%s' % (x[0], rtype) |
|
279 |
if not editkey in formparams: |
|
280 |
return # not edited |
|
281 |
try: |
|
282 |
values = self._linked_eids(self.req.list_form_param(rtype, formparams), late) |
|
283 |
except ToDoLater: |
|
284 |
self._pending_relations.append((rschema, formparams, x, entity)) |
|
285 |
return |
|
286 |
origvalues = set(typed_eid(eid) for eid in self.req.list_form_param(editkey, formparams)) |
|
287 |
return values, origvalues |
|
288 |
||
289 |
def handle_inlined_relation(self, rschema, formparams, entity, late=False): |
|
290 |
"""handle edition for the (rschema, x) relation of the given entity |
|
291 |
""" |
|
292 |
try: |
|
293 |
values, origvalues = self._relation_values(rschema, formparams, |
|
294 |
'subject', entity, late) |
|
295 |
except TypeError: |
|
296 |
return # not edited / to do later |
|
297 |
if values == origvalues: |
|
298 |
return # not modified |
|
299 |
attr = str(rschema) |
|
300 |
if values: |
|
301 |
formparams[attr] = iter(values).next() |
|
302 |
self.relations.append('X %s %s' % (attr, attr.upper())) |
|
303 |
self.restrictions.append('%s eid %%(%s)s' % (attr.upper(), attr)) |
|
304 |
elif entity.has_eid(): |
|
305 |
self.handle_relation(rschema, formparams, 'subject', entity, late) |
|
1753 | 306 |
|
0 | 307 |
def handle_relation(self, rschema, formparams, x, entity, late=False): |
308 |
"""handle edition for the (rschema, x) relation of the given entity |
|
309 |
""" |
|
310 |
try: |
|
311 |
values, origvalues = self._relation_values(rschema, formparams, x, |
|
312 |
entity, late) |
|
313 |
except TypeError: |
|
314 |
return # not edited / to do later |
|
315 |
etype = entity.e_schema |
|
316 |
if values == origvalues: |
|
317 |
return # not modified |
|
318 |
if x == 'subject': |
|
319 |
desttype = rschema.objects(etype)[0] |
|
320 |
card = rschema.rproperty(etype, desttype, 'cardinality')[0] |
|
321 |
subjvar, objvar = 'X', 'Y' |
|
322 |
else: |
|
323 |
desttype = rschema.subjects(etype)[0] |
|
324 |
card = rschema.rproperty(desttype, etype, 'cardinality')[1] |
|
325 |
subjvar, objvar = 'Y', 'X' |
|
326 |
eid = entity.eid |
|
327 |
if x == 'object' or not rschema.inlined or not values: |
|
328 |
# this is not an inlined relation or no values specified, |
|
329 |
# explicty remove relations |
|
1798
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
330 |
rql = 'DELETE %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % ( |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
331 |
subjvar, rschema, objvar) |
0 | 332 |
for reid in origvalues.difference(values): |
333 |
self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y')) |
|
1798
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
334 |
seteids = values.difference(origvalues) |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
335 |
if seteids: |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
336 |
rql = 'SET %s %s %s WHERE X eid %%(x)s, Y eid %%(y)s' % ( |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
337 |
subjvar, rschema, objvar) |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
338 |
for reid in seteids: |
cc86fe8efaaa
pass default values along the whole call chain, fix hidden field update bug
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1765
diff
changeset
|
339 |
self.req.execute(rql, {'x': eid, 'y': reid}, ('x', 'y')) |
1753 | 340 |
|
0 | 341 |
def _get_eid(self, eid): |
342 |
# should be either an int (existant entity) or a variable (to be |
|
343 |
# created entity) |
|
344 |
assert eid or eid == 0, repr(eid) # 0 is a valid eid |
|
345 |
try: |
|
346 |
return typed_eid(eid) |
|
347 |
except ValueError: |
|
348 |
try: |
|
349 |
return self._to_create[eid] |
|
350 |
except KeyError: |
|
351 |
self._to_create[eid] = None |
|
352 |
return None |
|
353 |
||
354 |
def _linked_eids(self, eids, late=False): |
|
355 |
"""return a list of eids if they are all known, else raise ToDoLater |
|
356 |
""" |
|
357 |
result = set() |
|
358 |
for eid in eids: |
|
359 |
if not eid: # AutoCompletionWidget |
|
360 |
continue |
|
361 |
eid = self._get_eid(eid) |
|
362 |
if eid is None: |
|
363 |
if not late: |
|
364 |
raise ToDoLater() |
|
365 |
# eid is still None while it's already a late call |
|
366 |
# this mean that the associated entity has not been created |
|
367 |
raise Exception('duh') |
|
368 |
result.add(eid) |
|
369 |
return result |
|
370 |
||
1753 | 371 |