doc/book/en/development/devcore/vreg.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Sep 2009 12:27:41 +0200
branchstable
changeset 3128 37c310ca6506
parent 3048 84c3e8f7e0cb
child 3258 6536ee4f37f7
permissions -rw-r--r--
include facets.css so we're sure it's there when needed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
     1
. -*- coding: utf-8 -*-
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     2
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
The VRegistry
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
--------------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
The recording process on startup
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     9
Details of the recording process
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
````````````````````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    12
XXX this part needs to be updated and checked
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    13
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    14
* by default all objects are registered automatically
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    16
* if some objects have to replace other objects or be included only if a
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    17
  condition is true,
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    18
  - explicitly register the object by defining `registration_callback(vreg)`
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    19
  - call registration methods on objects listed in the vreg registry
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    20
2042
d524ae901b31 Add a note concerning explicit object registering.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1714
diff changeset
    21
.. note::
d524ae901b31 Add a note concerning explicit object registering.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1714
diff changeset
    22
    Once the function `registration_callback(vreg)` is implemented, all the objects
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    23
    have to be explicitly registered as it disables the automatic object registering.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2043
diff changeset
    24
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    25
* the old registration mechanism will be removed when there will be no reference
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    26
  left to the registerers module in cubicweb and the library of cubes.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    28
Examples:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    29
2545
f8246ed962f6 [doc] replace code-block with sourcecode
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    30
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    31
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
   # web/views/basecomponents.py
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    33
   def registration_callback(vreg):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
      vreg.register_all(globals().values(), __name__, (SeeAlsoVComponent,))
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    35
      if 'see_also' in vreg.schema:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
          vreg.register(SeeAlsoVComponent)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
   # goa/appobjects/sessions.py
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
   def registration_callback(vreg):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    40
      vreg.register(SessionsCleaner)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
      vreg.register(GAEAuthenticationManager, clear=True)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    42
      vreg.register(GAEPersistentSessionManager, clear=True)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    43
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    44
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
API d'enregistrement des objets
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
```````````````````````````````
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    47
2545
f8246ed962f6 [doc] replace code-block with sourcecode
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    48
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    50
   register(obj, registryname=None, oid=None, clear=False)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
   register_all(objects, modname, butclasses=())
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    54
   unregister(obj, registryname=None)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    55
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    56
   register_and_replace(obj, replaced, registryname=None)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
   register_if_interface_found(obj, ifaces, **kwargs)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    59
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    60
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    61
Runtime objects selection
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    62
~~~~~~~~~~~~~~~~~~~~~~~~~
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    64
Defining selectors
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    65
``````````````````
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    66
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    67
The object's selector is defined by its `__select__` class attribute.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
When two selectors are combined using the `&` operator (former `chainall`), it
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    70
means that both should return a positive score. On success, the sum of scores is returned.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
When two selectors are combined using the `|` operator (former `chainfirst`), it
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
means that one of them should return a positive score. On success, the first
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
positive score is returned.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
Of course you can use paren to balance expressions.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    77
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    78
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
For instance, if you're selecting the primary (eg `id = 'primary'`) view (eg
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
`__registry__ = 'view'`) for a result set containing a `Card` entity, 2 objects
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
will probably be selectable:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    82
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    83
* the default primary view (`__select__ = implements('Any')`), meaning that the object is selectable for any kind of entity type
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
* the specific `Card` primary view (`__select__ = implements('Card')`, meaning that the object is selectable for Card entities
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
Other primary views specific to other entity types won't be selectable in this
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
case. Among selectable objects, the implements selector will return a higher score
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
to the second view since it's more specific, so it will be selected as expected.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
Example
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    95
XXX this part needs to be translated
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
    97
The goal : when on a Blog, one wants the RSS link to refer to blog
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
    98
entries, not to the blog entity itself.
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    99
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   100
To do that, one defines a method on entity classes that returns the
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   101
RSS stream url for a given entity. With a default implementation on
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   102
AnyEntity and a specific implementation on Blog, which will do what we
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   103
want.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   105
There's a limitation to this schema : when we have a result set
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   106
containing several Blog entities (or different entities), we don't
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   107
know on which entity to call the aforementioned method. In this case,
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   108
we keep the current behaviour (e.g : call to limited_rql).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   109
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   110
Hence we want two cases here, one for a single-entity rsets, the other
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   111
for multi-entities rsets.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   113
In web/views/boxes.py lies the RSSIconBox class. Look at its selector ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
  class RSSIconBox(ExtResourcesBoxTemplate):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   116
    """just display the RSS icon on uniform result set"""
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
    __select__ = ExtResourcesBoxTemplate.__select__ & non_final_entity()
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   118
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   119
It takes into account :
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   121
* the inherited selection criteria (one has to look them up in the
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   122
  class hierarchy to know the details)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   124
* non_final_entity, which filters on rsets containing non final
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   125
  entities (a 'final entity' being synonym for entity attribute)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   126
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   127
This matches our second case. Hence we have to provide a specific
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   128
component for the first case ::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
  class EntityRSSIconBox(RSSIconBox):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
    """just display the RSS icon on uniform result set for a single entity"""
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   132
    __select__ = RSSIconBox.__select__ & one_line_rset()
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   133
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   134
Here, one adds the one_line_rset selector, which filters result sets
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   135
of size 1. When one chains selectors, the final score is the sum of
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   136
the score of each individual selector (unless one of them returns 0,
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   137
in which case the object is non selectable). Thus, on a multiple
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   138
entities selector, one_line_rset makes the EntityRSSIconBox class non
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   139
selectable. For an rset with one entity, the EntityRSSIconBox class
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   140
will have a higher score then RSSIconBox, which is what we wanted.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   142
Of course, once this is done, you have to ::
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   143
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   144
* fill in the call method of EntityRSSIconBox
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   146
* provide the default implementation of the method returning the RSS
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   147
  stream url on AnyEntity
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   148
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   149
* redefine this method on Blog.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   150
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   151
When to use selectors?
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   152
```````````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   153
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   154
Selectors are to be used whenever arises the need of dispatching on
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   155
the shape or content of a result set.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
Debugging
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
`````````
3048
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   159
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   160
Once in a while, one needs to understand why a view (or any AppObject)
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   161
is, or is not selected appropriately. Looking at which selectors fired
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   162
(or did not) is the way. There exists a traced_selection context
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   163
manager to help with that.
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   164
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   165
Here is an example ::
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   166
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   167
.. sourcecode:: python
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   168
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   169
    def possible_objects(self, registry, *args, **kwargs):
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   170
        """return an iterator on possible objects in a registry for this result set
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   171
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   172
        actions returned are classes, not instances
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   173
        """
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   174
        from cubicweb.selectors import traced_selection
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   175
        with traced_selection():
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   176
            for vobjects in self.registry(registry).values():
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   177
                try:
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   178
                    yield self.select(vobjects, *args, **kwargs)
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   179
                except NoSelectableObject:
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   180
                    continue
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   181
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   182
Don't forget the 'from __future__ import with_statement' at the module
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   183
top-level.
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   184
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   185
This will yield additional WARNINGs in the logs, like this::
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   186
84c3e8f7e0cb translate bits of the doc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   187
    2009-01-09 16:43:52 - (cubicweb.selectors) WARNING: selector one_line_rset returned 0 for <class 'cubicweb.web.views.basecomponents.WFHistoryVComponent'>