doc/book/devrepo/entityclasses/application-logic.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 28 Jan 2016 18:17:30 +0100
changeset 11090 b4b854c25de5
parent 10491 c67bcee93248
permissions -rw-r--r--
[repository] set .eid on eschema when schema is loaded from the filesystem enforcing the contract that a repository's schema should have .eid attribute of entity schema set to the eid of the entity used to serialize them in the db. Before this cset, this was not true during tests or for some c-c commands where 'quick_start' is set (eg db-restore, i18ncube...). The change in server __init__ makes this assumption true during instance creation: the serializing code was actually setting eid on schema object, but a reference to a previously built schema was given instead of the one for the latest created repository. Closes #10450092
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     1
How to use entities objects and adapters
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     2
----------------------------------------
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     3
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     4
The previous chapters detailed the classes and methods available to
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
     5
the developer at the so-called `ORM`_ level. However they say little
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     6
about the common patterns of usage of these objects.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     7
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     8
.. _`ORM`: http://en.wikipedia.org/wiki/Object-relational_mapping
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
     9
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    10
Entities objects (and their adapters) are used in the repository and
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    11
web sides of CubicWeb. On the repository side of things, one should
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    12
manipulate them in Hooks and Operations.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    13
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    14
Hooks and Operations provide support for the implementation of rules
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    15
such as computed attributes, coherency invariants, etc (they play the
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    16
same role as database triggers, but in a way that is independent of
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    17
the actual data sources).
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    18
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    19
So a lot of an application's business rules will be written in Hooks
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    20
(or Operations).
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    21
5157
1202e6565aff [doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5152
diff changeset
    22
On the web side, views also typically operate using entity
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    23
objects. Obvious entity methods for use in views are the Dublin Core
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    24
methods like ``dc_title``. For separation of concerns reasons, one
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    25
should ensure no ui logic pervades the entities level, and also no
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    26
business logic should creep into the views.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    27
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    28
In the duration of a transaction, entities objects can be instantiated
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    29
many times, in views and hooks, even for the same database entity. For
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    30
instance, in a classic CubicWeb deployment setup, the repository and
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    31
the web front-end are separated process communicating over the
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    32
wire. There is no way state can be shared between these processes
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    33
(there is a specific API for that). Hence, it is not possible to use
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    34
entity objects as messengers between these components of an
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    35
application. It means that an attribute set as in ``obj.x = 42``,
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    36
whether or not x is actually an entity schema attribute, has a short
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    37
life span, limited to the hook, operation or view within which the
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    38
object was built.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    39
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    40
Setting an attribute or relation value can be done in the context of a
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    41
Hook/Operation, using the ``obj.cw_set(x=42)`` notation or a plain
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    42
RQL ``SET`` expression.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    43
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    44
In views, it would be preferable to encapsulate the necessary logic in
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    45
a method of an adapter for the concerned entity class(es). But of
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    46
course, this advice is also reasonable for Hooks/Operations, though
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    47
the separation of concerns here is less stringent than in the case of
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    48
views.
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    49
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    50
This leads to the practical role of objects adapters: it's where an
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    51
important part of the application logic lies (the other part being
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    52
located in the Hook/Operations).
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    53
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    54
Anatomy of an entity class
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    55
--------------------------
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    56
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    57
We can look now at a real life example coming from the `tracker`_
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    58
cube. Let us begin to study the ``entities/project.py`` content.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    59
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    60
.. sourcecode:: python
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    61
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    62
    from cubicweb.entities.adapters import ITreeAdapter
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    63
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    64
    class ProjectAdapter(ITreeAdapter):
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5879
diff changeset
    65
        __select__ = is_instance('Project')
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    66
        tree_relation = 'subproject_of'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    67
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    68
    class Project(AnyEntity):
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    69
        __regid__ = 'Project'
7827
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    70
        fetch_attrs, cw_fetch_order = fetch_config(('name', 'description',
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    71
                                                    'description_format', 'summary'))
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    72
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    73
        TICKET_DEFAULT_STATE_RESTR = 'S name IN ("created","identified","released","scheduled")'
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    74
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    75
        def dc_title(self):
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    76
            return self.name
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    77
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    78
The fact that the `Project` entity type implements an ``ITree``
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    79
interface is materialized by the ``ProjectAdapter`` class (inheriting
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    80
the pre-defined ``ITreeAdapter`` whose ``__regid__`` is of course
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    81
``ITree``), which will be selected on `Project` entity types because
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    82
of its selector. On this adapter, we redefine the ``tree_relation``
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    83
attribute of the ``ITreeAdapter`` class.
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    84
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    85
This is typically used in views concerned with the representation of
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    86
tree-like structures (CubicWeb provides several such views).
5152
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    87
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    88
It is important that the views themselves try not to implement this
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    89
logic, not only because such views would be hardly applyable to other
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    90
tree-like relations, but also because it is perfectly fine and useful
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    91
to use such an interface in Hooks.
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    92
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    93
In fact, Tree nature is a property of the data model that cannot be
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    94
fully and portably expressed at the level of database entities (think
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    95
about the transitive closure of the child relation). This is a further
35e6878e2fd0 [doc/book] adjust a bit
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5144
diff changeset
    96
argument to implement it at entity class level.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    97
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    98
``fetch_attrs`` configures which attributes should be pre-fetched when using ORM
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
    99
methods retrieving entity of this type. In a same manner, the ``cw_fetch_order`` is
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7827
diff changeset
   100
a class method allowing to control sort order. More on this in :ref:`FetchAttrs`.
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   101
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   102
We can observe the big ``TICKET_DEFAULT_STATE_RESTR`` is a pure
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   103
application domain piece of data. There is, of course, no limitation
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   104
to the amount of class attributes of this kind.
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
The ``dc_title`` method provides a (unicode string) value likely to be
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   107
consumed by views, but note that here we do not care about output
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   108
encodings. We care about providing data in the most universal format
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   109
possible, because the data could be used by a web view (which would be
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   110
responsible of ensuring XHTML compliance), or a console or file
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   111
oriented output (which would have the necessary context about the
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   112
needed byte stream encoding).
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   113
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   114
.. note::
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   115
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   116
  The Dublin Core `dc_xxx` methods are not moved to an adapter as they
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   117
  are extremely prevalent in CubicWeb and assorted cubes and should be
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   118
  available for all entity types.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   119
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   120
Let us now dig into more substantial pieces of code, continuing the
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   121
Project class.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   122
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   123
.. sourcecode:: python
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   124
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   125
    def latest_version(self, states=('published',), reverse=None):
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   126
        """returns the latest version(s) for the project in one of the given
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   127
        states.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   128
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   129
        when no states specified, returns the latest published version.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   130
        """
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   131
        order = 'DESC'
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   132
        if reverse is not None:
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   133
            warn('reverse argument is deprecated',
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   134
                 DeprecationWarning, stacklevel=1)
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   135
            if reverse:
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   136
                order = 'ASC'
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   137
        rset = self.versions_in_state(states, order, True)
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   138
        if rset:
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   139
            return rset.get_entity(0, 0)
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   140
        return None
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   141
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   142
    def versions_in_state(self, states, order='ASC', limit=False):
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   143
        """returns version(s) for the project in one of the given states, sorted
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   144
        by version number.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   145
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   146
        If limit is true, limit result to one version.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   147
        If reverse, versions are returned from the smallest to the greatest.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   148
        """
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   149
        if limit:
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   150
            order += ' LIMIT 1'
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   151
        rql = 'Any V,N ORDERBY version_sort_value(N) %s ' \
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   152
              'WHERE V num N, V in_state S, S name IN (%s), ' \
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   153
              'V version_of P, P eid %%(p)s' % (order, ','.join(repr(s) for s in states))
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   154
        return self._cw.execute(rql, {'p': self.eid})
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   155
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   156
.. _`tracker`: http://www.cubicweb.org/project/cubicweb-tracker/
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   157
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   158
These few lines exhibit the important properties we want to outline:
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   159
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   160
* entity code is concerned with the application domain
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   161
9316
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   162
* it is NOT concerned with database consistency (this is the realm of
38518284c200 [book] Typo and formatting fixes in devrepo/entityclasses/application-logic
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8483
diff changeset
   163
  Hooks/Operations); in other words, it assumes a consistent world
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   164
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   165
* it is NOT (directly) concerned with end-user interfaces
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   166
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   167
* however it can be used in both contexts
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   168
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   169
* it does not create or manipulate the internal object's state
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   170
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   171
* it plays freely with RQL expression as needed
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   172
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   173
* it is not concerned with internationalization
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   174
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   175
* it does not raise exceptions
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   176
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
   177