author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 20 Aug 2009 14:48:35 +0200 | |
branch | 3.5 |
changeset 2935 | e06b3eadef31 |
parent 2866 | 4c804711548d |
child 2867 | e8581a4f1bae |
child 4212 | ab6573088b4a |
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 |
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>') |
|
2866 | 48 |
for boxitem in self.unrelated_boxitems(entity): |
0 | 49 |
boxitem.render(self.w) |
50 |
self.w(u'</ul></div>') |
|
51 |
||
52 |
def unrelated_entities(self, entity): |
|
53 |
"""returns the list of unrelated entities |
|
54 |
||
55 |
if etype is not defined on the Box's class, the default |
|
56 |
behaviour is to use the entity's appropraite vocabulary function |
|
57 |
""" |
|
58 |
# use entity.unrelated if we've been asked for a particular etype |
|
59 |
if getattr(self, 'etype', None): |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
60 |
rset = entity.unrelated(self.rtype, self.etype, role(self), |
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
61 |
ordermethod='fetch_order') |
0 | 62 |
self.pagination(self.req, rset, w=self.w) |
63 |
return rset.entities() |
|
64 |
# in other cases, use vocabulary functions |
|
65 |
entities = [] |
|
1622
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
66 |
# XXX to update for 3.2 |
1a1c494b88e2
xtarget doesn't exists anymore
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
67 |
for _, eid in entity.vocabulary(self.rtype, role(self)): |
0 | 68 |
if eid is not None: |
69 |
rset = self.req.eid_rset(eid) |
|
70 |
entities.append(rset.get_entity(0, 0)) |
|
71 |
return entities |