author | sylvain.thenault@logilab.fr |
Tue, 12 May 2009 11:56:12 +0200 | |
branch | tls-sprint |
changeset 1739 | 78b0819162a8 |
parent 1622 | 1a1c494b88e2 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""Set of views allowing edition of entities/relations using ajax |
2 |
||
3 |
:organization: Logilab |
|
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
|
4 |
:copyright: 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 |
||
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
9 |
from cubicweb import role |
1132 | 10 |
from cubicweb.selectors import match_form_params, match_kwargs |
0 | 11 |
from cubicweb.web.box import EditRelationBoxTemplate |
12 |
||
13 |
class AddRelationView(EditRelationBoxTemplate): |
|
14 |
"""base class for view which let add entities linked |
|
15 |
by a given relation |
|
16 |
||
17 |
subclasses should define at least id, rtype and target |
|
18 |
class attributes. |
|
19 |
""" |
|
20 |
__registry__ = 'views' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
21 |
__select__ = (match_form_params('rtype', 'target') |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
22 |
| match_kwargs('rtype', 'target')) |
0 | 23 |
property_defs = {} # don't want to inherit this from Box |
24 |
id = 'xaddrelation' |
|
25 |
expected_kwargs = form_params = ('rtype', 'target') |
|
26 |
||
27 |
build_js = EditRelationBoxTemplate.build_reload_js_call |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
28 |
|
0 | 29 |
def cell_call(self, row, col, rtype=None, target=None, etype=None): |
30 |
self.rtype = rtype or self.req.form['rtype'] |
|
31 |
self.target = target or self.req.form['target'] |
|
32 |
self.etype = etype or self.req.form.get('etype') |
|
33 |
entity = self.entity(row, col) |
|
34 |
rschema = self.schema.rschema(self.rtype) |
|
35 |
if not self.etype: |
|
36 |
if self.target == 'object': |
|
37 |
etypes = rschema.objects(entity.e_schema) |
|
38 |
else: |
|
39 |
etypes = rschema.subjects(entity.e_schema) |
|
40 |
if len(etypes) == 1: |
|
41 |
self.etype = etypes[0] |
|
42 |
fakebox = [] |
|
43 |
self.w(u'<div id="%s">' % self.id) |
|
44 |
self.w(u'<h1>%s</h1>' % self.req._('relation %(relname)s of %(ent)s') |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
45 |
% {'relname': rschema.display_name(self.req, role(self)), |
0 | 46 |
'ent': entity.view('incontext')}) |
47 |
self.w(u'<ul>') |
|
48 |
self.w_unrelated(fakebox, entity) |
|
49 |
for boxitem in fakebox: |
|
50 |
boxitem.render(self.w) |
|
51 |
self.w(u'</ul></div>') |
|
52 |
||
53 |
def unrelated_entities(self, entity): |
|
54 |
"""returns the list of unrelated entities |
|
55 |
||
56 |
if etype is not defined on the Box's class, the default |
|
57 |
behaviour is to use the entity's appropraite vocabulary function |
|
58 |
""" |
|
59 |
# use entity.unrelated if we've been asked for a particular etype |
|
60 |
if getattr(self, 'etype', None): |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
61 |
rset = entity.unrelated(self.rtype, self.etype, role(self), |
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
62 |
ordermethod='fetch_order') |
0 | 63 |
self.pagination(self.req, rset, w=self.w) |
64 |
return rset.entities() |
|
65 |
# in other cases, use vocabulary functions |
|
66 |
entities = [] |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
67 |
# XXX to update for 3.2 |
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
68 |
for _, eid in entity.vocabulary(self.rtype, role(self)): |
0 | 69 |
if eid is not None: |
70 |
rset = self.req.eid_rset(eid) |
|
71 |
entities.append(rset.get_entity(0, 0)) |
|
72 |
return entities |