author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 27 Jan 2017 16:26:09 +0100 | |
changeset 11929 | fcbd6b251d81 |
parent 10491 | c67bcee93248 |
permissions | -rw-r--r-- |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
1 |
.. _adapters: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
2 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
3 |
Interfaces and Adapters |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
4 |
----------------------- |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
5 |
|
6324
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
6 |
Interfaces are the same thing as object-oriented programming `interfaces`_. |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
7 |
Adapter refers to a well-known `adapter`_ design pattern that helps separating |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
8 |
concerns in object oriented applications. |
5144
5a09bea07302
[doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4936
diff
changeset
|
9 |
|
5a09bea07302
[doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4936
diff
changeset
|
10 |
.. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
11 |
.. _`adapter`: http://en.wikipedia.org/wiki/Adapter_pattern |
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1714
diff
changeset
|
12 |
|
9256
697a8181ba30
remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8190
diff
changeset
|
13 |
In |cubicweb| adapters provide logical functionalities to entity types. |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
14 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
15 |
Definition of an adapter is quite trivial. An excerpt from cubicweb |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
16 |
itself (found in :mod:`cubicweb.entities.adapters`): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
17 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
18 |
.. sourcecode:: python |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
19 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
20 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
21 |
class ITreeAdapter(EntityAdapter): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
22 |
"""This adapter has to be overriden to be configured using the |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
23 |
tree_relation, child_role and parent_role class attributes to |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
24 |
benefit from this default implementation |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
25 |
""" |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
26 |
__regid__ = 'ITree' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
27 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
28 |
child_role = 'subject' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
29 |
parent_role = 'object' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
30 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
31 |
def children_rql(self): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
32 |
"""returns RQL to get children """ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
33 |
return self.entity.cw_related_rql(self.tree_relation, self.parent_role) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
34 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
35 |
The adapter object has ``self.entity`` attribute which represents the |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
36 |
entity being adapted. |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
37 |
|
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
38 |
.. Note:: |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
39 |
|
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
40 |
Adapters came with the notion of service identified by the registry identifier |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
41 |
of an adapters, hence dropping the need for explicit interface and the |
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8032
diff
changeset
|
42 |
:class:`cubicweb.predicates.implements` selector. You should instead use |
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8032
diff
changeset
|
43 |
:class:`cubicweb.predicates.is_instance` when you want to select on an entity |
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8032
diff
changeset
|
44 |
type, or :class:`cubicweb.predicates.adaptable` when you want to select on a |
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
45 |
service. |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
46 |
|
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
47 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
48 |
Specializing and binding an adapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
49 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
50 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
51 |
.. sourcecode:: python |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
52 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
53 |
from cubicweb.entities.adapters import ITreeAdapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
54 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
55 |
class MyEntityITreeAdapter(ITreeAdapter): |
5893
4469f85ace9f
[doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5882
diff
changeset
|
56 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
57 |
tree_relation = 'filed_under' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
58 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
59 |
The ITreeAdapter here provides a default implementation. The |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
60 |
tree_relation class attribute is actually used by this implementation |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
61 |
to help implement correct behaviour. |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
62 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
63 |
Here we provide a specific implementation which will be bound for |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
64 |
``MyEntity`` entity type (the `adaptee`). |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
65 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
66 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
67 |
.. _interfaces_to_adapters: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
68 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
69 |
Converting code from Interfaces/Mixins to Adapters |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
70 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
71 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
72 |
Here we go with a small example. Before: |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
73 |
|
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
74 |
.. sourcecode:: python |
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
75 |
|
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8032
diff
changeset
|
76 |
from cubicweb.predicates import implements |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
77 |
from cubicweb.interfaces import ITree |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
78 |
from cubicweb.mixins import ITreeMixIn |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
79 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
80 |
class MyEntity(ITreeMixIn, AnyEntity): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
81 |
__implements__ = AnyEntity.__implements__ + (ITree,) |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
82 |
|
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
84 |
class ITreeView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
85 |
__select__ = implements('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
86 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
87 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
88 |
children = entity.children() |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
89 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
90 |
After: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
91 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
92 |
.. sourcecode:: python |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
93 |
|
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8032
diff
changeset
|
94 |
from cubicweb.predicates import adaptable, is_instance |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
95 |
from cubicweb.entities.adapters import ITreeAdapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
96 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
97 |
class MyEntityITreeAdapter(ITreeAdapter): |
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
98 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
99 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
100 |
class ITreeView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
101 |
__select__ = adaptable('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
102 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
103 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
104 |
itree = entity.cw_adapt_to('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
105 |
children = itree.children() |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
106 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
107 |
As we can see, the interface/mixin duality disappears and the entity |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
108 |
class itself is completely freed from these concerns. When you want |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
109 |
to use the ITree interface of an entity, call its `cw_adapt_to` method |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
110 |
to get an adapter for this interface, then access to members of the |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
111 |
interface on the adapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
112 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
113 |
Let's look at an example where we defined everything ourselves. We |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
114 |
start from: |
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1714
diff
changeset
|
115 |
|
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
116 |
.. sourcecode:: python |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
117 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
118 |
class IFoo(Interface): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
119 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
120 |
raise NotImplementedError |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
121 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
122 |
class MyEntity(AnyEntity): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
123 |
__regid__ = 'MyEntity' |
6324
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
124 |
__implements__ = AnyEntity.__implements__ + (IFoo,) |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
125 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
126 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
127 |
return sum(captain.age for captain in self.captains) |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
128 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
129 |
class FooView(EntityView): |
8032
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6324
diff
changeset
|
130 |
__regid__ = 'mycube.fooview' |
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6324
diff
changeset
|
131 |
__select__ = implements('IFoo') |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
132 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
133 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
134 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
135 |
self.w('bar: %s' % entity.bar()) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
136 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
137 |
Converting to: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
138 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
139 |
.. sourcecode:: python |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
140 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
141 |
class IFooAdapter(EntityAdapter): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
142 |
__regid__ = 'IFoo' |
5893
4469f85ace9f
[doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5882
diff
changeset
|
143 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
144 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
145 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
146 |
return sum(captain.age for captain in self.entity.captains) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
147 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
148 |
class FooView(EntityView): |
8032
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6324
diff
changeset
|
149 |
__regid__ = 'mycube.fooview' |
bcb87336c7d2
[doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6324
diff
changeset
|
150 |
__select__ = adaptable('IFoo') |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
151 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
152 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
153 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
154 |
self.w('bar: %s' % entity.cw_adapt_to('IFoo').bar()) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
155 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
156 |
.. note:: |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
157 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
158 |
When migrating an entity method to an adapter, the code can be moved as is |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
159 |
except for the `self` of the entity class, which in the adapter must become `self.entity`. |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
160 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
161 |
Adapters defined in the library |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
162 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
163 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
164 |
.. automodule:: cubicweb.entities.adapters |
4437
21f2e01fdd6a
update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2539
diff
changeset
|
165 |
:members: |
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1714
diff
changeset
|
166 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
167 |
More are defined in web/views. |