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