author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Tue, 08 Dec 2009 12:46:45 +0100 | |
changeset 4040 | 00e2f1ae625d |
parent 3451 | 6b46d73823f5 |
child 4045 | f4a52abb6f4f |
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' |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2867
diff
changeset
|
22 |
__regid__ = 'xaddrelation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
23 |
__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
|
24 |
| match_kwargs('rtype', 'target')) |
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
25 |
cw_property_defs = {} # don't want to inherit this from Box |
0 | 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): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
31 |
self.rtype = rtype or self._cw.form['rtype'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
32 |
self.target = target or self._cw.form['target'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
33 |
self.etype = etype or self._cw.form.get('etype') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
34 |
entity = self.cw_rset.get_entity(row, col) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
35 |
rschema = self._cw.schema.rschema(self.rtype) |
0 | 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] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
43 |
self.w(u'<div id="%s">' % self.__regid__) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
44 |
self.w(u'<h1>%s</h1>' % self._cw._('relation %(relname)s of %(ent)s') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
45 |
% {'relname': rschema.display_name(self._cw, 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') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
62 |
self.pagination(self._cw, rset, w=self.w) |
0 | 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: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
69 |
rset = self._cw.eid_rset(eid) |
0 | 70 |
entities.append(rset.get_entity(0, 0)) |
71 |
return entities |