author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 28 Jun 2011 17:59:31 +0200 | |
changeset 7570 | 648bf83945a5 |
parent 6324 | bdb85e3602c8 |
child 8032 | bcb87336c7d2 |
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 |
|
6324
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
13 |
In |cubicweb| adapters provide logical functionalities to entity types. They |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
14 |
are introduced in version `3.9`. Before that one had to implement Interfaces in |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
15 |
entity classes to achieve a similar goal. However, the problem with this |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
16 |
approach is that is clutters the entity class's namespace, exposing name |
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
17 |
collision risks with schema attributes/relations or even methods names |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
18 |
(different interfaces may define the same method with not necessarily the same |
5882
4c7a0b139830
[doc] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5879
diff
changeset
|
19 |
behaviour expected). |
5879
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 |
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
|
22 |
itself (found in :mod:`cubicweb.entities.adapters`): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
23 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
24 |
.. sourcecode:: python |
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 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
27 |
class ITreeAdapter(EntityAdapter): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
28 |
"""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
|
29 |
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
|
30 |
benefit from this default implementation |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
31 |
""" |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
32 |
__regid__ = 'ITree' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
33 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
34 |
child_role = 'subject' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
35 |
parent_role = 'object' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
36 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
37 |
def children_rql(self): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
38 |
"""returns RQL to get children """ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
39 |
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
|
40 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
41 |
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
|
42 |
entity being adapted. |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
43 |
|
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
44 |
.. Note:: |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
45 |
|
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
46 |
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
|
47 |
of an adapters, hence dropping the need for explicit interface and the |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
48 |
:class:`cubicweb.selectors.implements` selector. You should instead use |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
49 |
:class:`cubicweb.selectors.is_instance` when you want to select on an entity |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
50 |
type, or :class:`cubicweb.selectors.adaptable` when you want to select on a |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
51 |
service. |
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
52 |
|
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
53 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
54 |
Specializing and binding an adapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
55 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
56 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
57 |
.. sourcecode:: python |
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 |
from cubicweb.entities.adapters import ITreeAdapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
60 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
61 |
class MyEntityITreeAdapter(ITreeAdapter): |
5893
4469f85ace9f
[doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5882
diff
changeset
|
62 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
63 |
tree_relation = 'filed_under' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
64 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
65 |
The ITreeAdapter here provides a default implementation. The |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
66 |
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
|
67 |
to help implement correct behaviour. |
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 |
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
|
70 |
``MyEntity`` entity type (the `adaptee`). |
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 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
73 |
.. _interfaces_to_adapters: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
74 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
75 |
Converting code from Interfaces/Mixins to Adapters |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
76 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
77 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
78 |
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
|
79 |
|
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
80 |
.. sourcecode:: python |
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
81 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
82 |
from cubicweb.selectors import implements |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
83 |
from cubicweb.interfaces import ITree |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
84 |
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
|
85 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
86 |
class MyEntity(ITreeMixIn, AnyEntity): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
87 |
__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
|
88 |
|
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
90 |
class ITreeView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
91 |
__select__ = implements('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
92 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
93 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
94 |
children = entity.children() |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
95 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
96 |
After: |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
97 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
98 |
.. sourcecode:: python |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
99 |
|
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
100 |
from cubicweb.selectors import adaptable, is_instance |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
101 |
from cubicweb.entities.adapters import ITreeAdapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
102 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
103 |
class MyEntityITreeAdapter(ITreeAdapter): |
6157
81ae5bc958db
[doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
104 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
105 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
106 |
class ITreeView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
107 |
__select__ = adaptable('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
108 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
109 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
110 |
itree = entity.cw_adapt_to('ITree') |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
111 |
children = itree.children() |
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 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
interface on the adapter |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
118 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
119 |
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
|
120 |
start from: |
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1714
diff
changeset
|
121 |
|
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
122 |
.. sourcecode:: python |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
123 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
124 |
class IFoo(Interface): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
125 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
126 |
raise NotImplementedError |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
127 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
128 |
class MyEntity(AnyEntity): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
129 |
__regid__ = 'MyEntity' |
6324
bdb85e3602c8
[book] minor update about windmill + reformat
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6157
diff
changeset
|
130 |
__implements__ = AnyEntity.__implements__ + (IFoo,) |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
131 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
132 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
133 |
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
|
134 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
135 |
class FooView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
136 |
__regid__ = 'mycube.fooview' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
137 |
__select__ = implements('IFoo') |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
139 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
140 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
141 |
self.w('bar: %s' % entity.bar()) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
142 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
143 |
Converting to: |
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 |
.. sourcecode:: python |
4750
875dc33551a9
[book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4437
diff
changeset
|
146 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
147 |
class IFooAdapter(EntityAdapter): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
148 |
__regid__ = 'IFoo' |
5893
4469f85ace9f
[doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5882
diff
changeset
|
149 |
__select__ = is_instance('MyEntity') |
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
150 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
151 |
def bar(self, *args): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
152 |
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
|
153 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
154 |
class FooView(EntityView): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
155 |
__regid__ = 'mycube.fooview' |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
156 |
__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
|
157 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
158 |
def cell_call(self, row, col): |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
159 |
entity = self.cw_rset.get_entity(row, col) |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
160 |
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
|
161 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
162 |
.. note:: |
1714
a721966779be
new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
163 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
164 |
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
|
165 |
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
|
166 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
167 |
Adapters defined in the library |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
168 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
169 |
|
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
170 |
.. 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
|
171 |
:members: |
2539
0f26a76b0348
[doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1714
diff
changeset
|
172 |
|
5879
7d3044271a29
[doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5394
diff
changeset
|
173 |
More are defined in web/views. |