author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 05 Aug 2009 09:15:56 +0200 | |
changeset 3387 | a357d4147eee |
parent 3386 | ab797c5374b7 |
child 3388 | b8be8fc77c27 |
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 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
15 |
from cubicweb.web import INTERNAL_FIELD_VALUE, RequestError, NothingToEdit, ProcessFormError |
0 | 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 |
||
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
25 |
class RqlQuery(object): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
26 |
def __init__(self): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
27 |
self.edited = [] |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
28 |
self.restrictions = [] |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
29 |
self.kwargs = {} |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
30 |
|
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
31 |
def insert_query(self, etype): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
32 |
if self.edited: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
33 |
rql = 'INSERT %s X: %s' % (etype, ','.join(self.edited)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
34 |
else: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
35 |
rql = 'INSERT %s X' % etype |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
36 |
if self.restrictions: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
37 |
rql += ' WHERE %s' % ','.join(self.restrictions) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
38 |
return rql |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
39 |
|
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
40 |
def update_query(self, eid): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
41 |
varmaker = rqlvar_maker() |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
42 |
var = varmaker.next() |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
43 |
while var in self.kwargs: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
44 |
var = varmaker.next() |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
45 |
rql = 'SET %s WHERE X eid %%(%s)s' % (','.join(self.edited), var) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
46 |
if self.restrictions: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
47 |
rql += ', %s' % ','.join(self.restrictions) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
48 |
self.kwargs[var] = eid |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
49 |
return rql |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
50 |
|
0 | 51 |
class EditController(ViewController): |
52 |
id = 'edit' |
|
53 |
||
2255
c346af0727ca
more generic way to detect json requests (not yet perfect though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
def publish(self, rset=None): |
0 | 55 |
"""edit / create / copy / delete entity / relations""" |
884 | 56 |
for key in self.req.form: |
0 | 57 |
# There should be 0 or 1 action |
58 |
if key.startswith('__action_'): |
|
59 |
cbname = key[1:] |
|
60 |
try: |
|
61 |
callback = getattr(self, cbname) |
|
62 |
except AttributeError: |
|
884 | 63 |
raise RequestError(self.req._('invalid action %r' % key)) |
0 | 64 |
else: |
65 |
return callback() |
|
66 |
self._default_publish() |
|
67 |
self.reset() |
|
68 |
||
69 |
def _default_publish(self): |
|
70 |
req = self.req |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
71 |
self.errors = [] |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
72 |
self.relations_rql = [] |
0 | 73 |
# no specific action, generic edition |
74 |
self._to_create = req.data['eidmap'] = {} |
|
75 |
self._pending_relations = [] |
|
76 |
todelete = self.req.get_pending_deletes() |
|
77 |
toinsert = self.req.get_pending_inserts() |
|
78 |
try: |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
79 |
methodname = req.form.pop('__method', None) |
0 | 80 |
for eid in req.edited_eids(): |
81 |
formparams = req.extract_entity_params(eid) |
|
82 |
if methodname is not None: |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
83 |
entity = req.entity_from_eid(eid) |
0 | 84 |
method = getattr(entity, methodname) |
85 |
method(formparams) |
|
86 |
eid = self.edit_entity(formparams) |
|
87 |
except (RequestError, NothingToEdit): |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
88 |
if '__linkto' in req.form and 'eid' in req.form: |
0 | 89 |
self.execute_linkto() |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
90 |
elif not ('__delete' in req.form or '__insert' in req.form or todelete or toinsert): |
0 | 91 |
raise ValidationError(None, {None: req._('nothing to edit')}) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
92 |
for querydef in self.relations_rql: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
93 |
self.req.execute(*querydef) |
0 | 94 |
# handle relations in newly created entities |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
95 |
# XXX find a way to merge _pending_relations and relations_rql |
0 | 96 |
if self._pending_relations: |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
97 |
for form, field, entity in self._pending_relations: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
98 |
for querydef in self.handle_relation(form, field, entity, True): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
99 |
self.req.execute(*querydef) |
0 | 100 |
# XXX this processes *all* pending operations of *all* entities |
3384
f76a38731bc1
[forms] work-in-progress: get rid of edits- / edito-
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
101 |
if req.form.has_key('__delete'): |
f76a38731bc1
[forms] work-in-progress: get rid of edits- / edito-
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
102 |
todelete += req.list_form_param('__delete', req.form, pop=True) |
0 | 103 |
if todelete: |
104 |
self.delete_relations(parse_relations_descr(todelete)) |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
105 |
if req.form.has_key('__insert'): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
106 |
toinsert = req.list_form_param('__insert', req.form, pop=True) |
0 | 107 |
if toinsert: |
108 |
self.insert_relations(parse_relations_descr(toinsert)) |
|
109 |
self.req.remove_pending_operations() |
|
1753 | 110 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
111 |
def _insert_entity(self, etype, eid, rqlquery): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
112 |
rql = rqlquery.insert_query(etype) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
113 |
try: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
114 |
# get the new entity (in some cases, the type might have |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
115 |
# changed as for the File --> Image mutation) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
116 |
entity = self.req.execute(rql, rqlquery.kwargs).get_entity(0, 0) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
117 |
neweid = entity.eid |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
118 |
except ValidationError, ex: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
119 |
self._to_create[eid] = ex.entity |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
120 |
if self.req.json_request: # XXX (syt) why? |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
121 |
ex.entity = eid |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
122 |
raise |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
123 |
self._to_create[eid] = neweid |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
124 |
return neweid |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
125 |
|
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
126 |
def _update_entity(self, eid, rqlquery): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
127 |
rql = rqlquery.update_query(eid) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
128 |
self.req.execute(rql, rqlquery.kwargs) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
129 |
|
0 | 130 |
def edit_entity(self, formparams, multiple=False): |
131 |
"""edit / create / copy an entity and return its eid""" |
|
132 |
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
|
133 |
entity = self.vreg['etypes'].etype_class(etype)(self.req) |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
134 |
entity.eid = formparams['eid'] |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
135 |
eid = self._get_eid(entity.eid) |
3386
ab797c5374b7
[editcontroller] rename is_edited variable into is_main_entity to make things clearer
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3384
diff
changeset
|
136 |
is_main_entity = self.req.form.get('__maineid') == formparams['eid'] |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
137 |
# let a chance to do some entity specific stuff.tn |
1753 | 138 |
entity.pre_web_edit() |
0 | 139 |
# create a rql query from parameters |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
140 |
rqlquery = RqlQuery() |
0 | 141 |
# process inlined relations at the same time as attributes |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
142 |
# this will generate less rql queries and might be useful in |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
143 |
# a few dark corners |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
144 |
formid = self.req.form.get('__form_id', 'edition') |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
145 |
form = self.vreg['forms'].select(formid, self.req, entity=entity) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
146 |
for field in form.fields: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
147 |
if form.form_field_modified(field): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
148 |
self.handle_formfield(form, field, entity, rqlquery) |
0 | 149 |
if eid is None: # creation or copy |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
150 |
entity.eid = self._insert_entity(etype, formparams['eid'], rqlquery) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
151 |
elif rqlquery.edited: # edition of an existant entity |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
152 |
self._update_entity(eid, rqlquery) |
3386
ab797c5374b7
[editcontroller] rename is_edited variable into is_main_entity to make things clearer
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3384
diff
changeset
|
153 |
if is_main_entity: |
0 | 154 |
self.notify_edited(entity) |
155 |
if formparams.has_key('__delete'): |
|
156 |
todelete = self.req.list_form_param('__delete', formparams, pop=True) |
|
157 |
self.delete_relations(parse_relations_descr(todelete)) |
|
158 |
if formparams.has_key('__cloned_eid'): |
|
159 |
entity.copy_relations(formparams['__cloned_eid']) |
|
160 |
if formparams.has_key('__insert'): |
|
161 |
toinsert = self.req.list_form_param('__insert', formparams, pop=True) |
|
162 |
self.insert_relations(parse_relations_descr(toinsert)) |
|
3386
ab797c5374b7
[editcontroller] rename is_edited variable into is_main_entity to make things clearer
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3384
diff
changeset
|
163 |
if is_main_entity: # only execute linkto for the main entity |
0 | 164 |
self.execute_linkto(eid) |
165 |
return eid |
|
166 |
||
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
167 |
def handle_formfield(self, form, field, entity, rqlquery): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
168 |
eschema = entity.e_schema |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
169 |
try: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
170 |
for attr, value in field.process_posted(form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
171 |
if not ( |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
172 |
(field.role == 'subject' and eschema.has_subject_relation(field.name)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
173 |
or |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
174 |
(field.role == 'object' and eschema.has_object_relation(field.name))): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
175 |
continue |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
176 |
rschema = self.schema.rschema(field.name) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
177 |
if rschema.is_final(): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
178 |
rqlquery.kwargs[attr] = value |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
179 |
rqlquery.edited.append('X %s %%(%s)s' % (attr, attr)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
180 |
elif rschema.inlined: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
181 |
self.handle_inlined_relation(form, field, entity, rqlquery) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
182 |
else: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
183 |
self.relations_rql += self.handle_relation( |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
184 |
form, field, entity) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
185 |
except ProcessFormError, exc: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
186 |
self.errors.append((field, exc)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
187 |
|
0 | 188 |
def _action_apply(self): |
189 |
self._default_publish() |
|
190 |
self.reset() |
|
1753 | 191 |
|
0 | 192 |
def _action_cancel(self): |
193 |
errorurl = self.req.form.get('__errorurl') |
|
194 |
if errorurl: |
|
195 |
self.req.cancel_edition(errorurl) |
|
196 |
return self.reset() |
|
197 |
||
198 |
def _action_delete(self): |
|
199 |
self.delete_entities(self.req.edited_eids(withtype=True)) |
|
200 |
return self.reset() |
|
201 |
||
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
202 |
def _relation_values(self, form, field, entity, late=False): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
203 |
"""handle edition for the (rschema, x) relation of the given entity |
0 | 204 |
""" |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
205 |
values = set() |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
206 |
for eid in field.process_form_data(form): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
207 |
if not eid: # AutoCompletionWidget |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
208 |
continue |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
209 |
typed_eid = self._get_eid(eid) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
210 |
if typed_eid is None: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
211 |
if late: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
212 |
# eid is still None while it's already a late call |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
213 |
# this mean that the associated entity has not been created |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
214 |
raise Exception("eid %s is still not created" % eid) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
215 |
self._pending_relations.append( (form, field, entity) ) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
216 |
return None |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
217 |
values.add(typed_eid) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
218 |
return values |
0 | 219 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
220 |
def handle_inlined_relation(self, form, field, entity, rqlquery): |
0 | 221 |
"""handle edition for the (rschema, x) relation of the given entity |
222 |
""" |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
223 |
origvalues = set(row[0] for row in entity.related(field.name, field.role)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
224 |
values = self._relation_values(form, field, entity) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
225 |
if values is None or values == origvalues: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
226 |
return # not edited / not modified / to do later |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
227 |
attr = field.name |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
228 |
if values: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
229 |
rqlquery.kwargs[attr] = iter(values).next() |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
230 |
rqlquery.edition.append('X %s %s' % (attr, attr.upper())) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
231 |
rqlquery.restrictions.append('%s eid %%(%s)s' % (attr.upper(), attr)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
232 |
elif entity.has_eid(): |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
233 |
self.relations_rql += self.handle_relation(form, field, entity) |
0 | 234 |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
235 |
def handle_relation(self, form, field, entity, late=False): |
0 | 236 |
"""handle edition for the (rschema, x) relation of the given entity |
237 |
""" |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
238 |
origvalues = set(row[0] for row in entity.related(field.name, field.role)) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
239 |
values = self._relation_values(form, field, entity, late) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
240 |
if values is None or values == origvalues: |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
241 |
return # not edited / not modified / to do later |
0 | 242 |
etype = entity.e_schema |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
243 |
rschema = self.schema.rschema(field.name) |
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
244 |
if field.role == 'subject': |
0 | 245 |
desttype = rschema.objects(etype)[0] |
246 |
card = rschema.rproperty(etype, desttype, 'cardinality')[0] |
|
247 |
subjvar, objvar = 'X', 'Y' |
|
248 |
else: |
|
249 |
desttype = rschema.subjects(etype)[0] |
|
250 |
card = rschema.rproperty(desttype, etype, 'cardinality')[1] |
|
251 |
subjvar, objvar = 'Y', 'X' |
|
252 |
eid = entity.eid |
|
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
253 |
if field.role == 'object' or not rschema.inlined or not values: |
0 | 254 |
# this is not an inlined relation or no values specified, |
255 |
# 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
|
256 |
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
|
257 |
subjvar, rschema, objvar) |
0 | 258 |
for reid in origvalues.difference(values): |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
259 |
yield (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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
for reid in seteids: |
3387
a357d4147eee
[forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3386
diff
changeset
|
265 |
yield (rql, {'x': eid, 'y': reid}, ('x', 'y')) |
1753 | 266 |
|
0 | 267 |
def _get_eid(self, eid): |
268 |
# should be either an int (existant entity) or a variable (to be |
|
269 |
# created entity) |
|
270 |
assert eid or eid == 0, repr(eid) # 0 is a valid eid |
|
271 |
try: |
|
272 |
return typed_eid(eid) |
|
273 |
except ValueError: |
|
274 |
try: |
|
275 |
return self._to_create[eid] |
|
276 |
except KeyError: |
|
277 |
self._to_create[eid] = None |
|
278 |
return None |
|
279 |
||
280 |
def _linked_eids(self, eids, late=False): |
|
281 |
"""return a list of eids if they are all known, else raise ToDoLater |
|
282 |
""" |
|
283 |
||
1753 | 284 |