doc/book/en/devweb/views/primary.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 28 Jun 2011 17:59:31 +0200
changeset 7570 648bf83945a5
parent 6880 4be32427b2b9
child 7838 ea1360938033
permissions -rw-r--r--
[etwist] log missing file even in debug mode
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5266
84f285d96363 [doc/book] regroup views chapters under common umbrella (in the development part) & some fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5262
diff changeset
     1
.. _primary_view:
4936
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4751
diff changeset
     2
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     3
The Primary View
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     4
-----------------
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     5
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     6
By default, *CubicWeb* provides a view that fits every available
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     7
entity type. This is the first view you might be interested in
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     8
modifying. It is also one of the richest and most complex.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
     9
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    10
It is automatically selected on a one line result set containing an
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    11
entity.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    13
This view is supposed to render a maximum of informations about the
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    14
entity.
4430
0b6a069eb29e le patch added_tips a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3258
diff changeset
    15
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    16
It lives in the :mod:`cubicweb.web.views.primary` module.
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    17
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    18
.. _primary_view_layout:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    19
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    20
Layout
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    21
``````
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    22
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    23
The primary view has the following layout.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    24
5394
105011657405 [doc/book] move devweb up from development, turn development into devrepo (much better structure)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5388
diff changeset
    25
.. image:: ../../images/primaryview_template.png
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    26
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    27
.. _primary_view_configuration:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    28
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    29
Primary view configuration
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    30
``````````````````````````
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    31
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    32
If you want to customize the primary view of an entity, overriding the primary
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    33
view class may not be necessary. For simple adjustments (attributes or relations
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    34
display locations and styles), a much simpler way is to use uicfg.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    35
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    36
Attributes/relations display location
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    37
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    38
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    39
In the primary view, there are three sections where attributes and
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    40
relations can be displayed (represented in pink in the image above):
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    41
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    42
* 'attributes'
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    43
* 'relations'
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    44
* 'sideboxes'
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    45
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    46
**Attributes** can only be displayed in the attributes section (default
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    47
  behavior). They can also be hidden. By default, attributes of type `Password`
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    48
  and `Bytes` are hidden.
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    49
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    50
For instance, to hide the ``title`` attribute of the ``Blog`` entity:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    51
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    52
.. sourcecode:: python
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    53
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    54
   from cubicweb.web import uicfg
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    55
   uicfg.primaryview_section.tag_attribute(('Blog', 'title'), 'hidden')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    56
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    57
**Relations** can be either displayed in one of the three sections or hidden.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    58
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    59
For relations, there are two methods:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    60
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    61
* ``tag_object_of`` for modifying the primary view of the object
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    62
* ``tag_subject_of`` for modifying the primary view of the subject
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    63
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    64
These two methods take two arguments:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    65
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    66
* a triplet ``(subject, relation_name, object)``, where subject or object can be replaced with ``'*'``
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    67
* the section name or ``hidden``
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    68
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    69
.. sourcecode:: python
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    70
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    71
   pv_section = uicfg.primaryview_section
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    72
   # hide every relation `entry_of` in the `Blog` primary view
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    73
   pv_section.tag_object_of(('*', 'entry_of', 'Blog'), 'hidden')
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    75
   # display `entry_of` relations in the `relations`
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    76
   # section in the `BlogEntry` primary view
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    77
   pv_section.tag_subject_of(('BlogEntry', 'entry_of', '*'), 'relations')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    78
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    79
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    80
Display content
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    81
^^^^^^^^^^^^^^^
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    82
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    83
You can use ``primaryview_display_ctrl`` to customize the display of attributes
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    84
or relations. Values of ``primaryview_display_ctrl`` are dictionaries.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    85
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    87
Common keys for attributes and relations are:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    88
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    89
* ``vid``: specifies the regid of the view for displaying the attribute or the relation.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    90
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    91
  If ``vid`` is not specified, the default value depends on the section:
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    92
    * ``attributes`` section: 'reledit' view
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    93
    * ``relations`` section: 'autolimited' view
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    94
    * ``sideboxes`` section: 'sidebox' view
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    95
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    96
* ``order``: int used to control order within a section. When not specified,
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    97
  automatically set according to order in which tags are added.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    98
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    99
* ``label``: label for the relations section or side box
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   100
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   101
* ``showlabel``: boolean telling whether the label is displayed
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   102
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   103
.. sourcecode:: python
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   104
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   105
   # let us remind the schema of a blog entry
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   106
   class BlogEntry(EntityType):
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   107
       title = String(required=True, fulltextindexed=True, maxsize=256)
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   108
       publish_date = Date(default='TODAY')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   109
       content = String(required=True, fulltextindexed=True)
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   110
       entry_of = SubjectRelation('Blog', cardinality='?*')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   111
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   112
   # now, we want to show attributes
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   113
   # with an order different from that in the schema definition
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   114
   view_ctrl = uicfg.primaryview_display_ctrl
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   115
   for index, attr in enumerate('title', 'content', 'publish_date'):
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   116
       view_ctrl.tag_attribute(('BlogEntry', attr), {'order': index})
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   117
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   118
By default, relations displayed in the 'relations' section are being displayed by
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   119
the 'autolimited' view. This view will use comma separated values, or list view
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   120
and/or limit your rset if there is too much items in it (and generate the "view
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   121
all" link in this case).
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   122
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   123
You can control this view by setting the following values in the
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   124
`primaryview_display_ctrl` relation tag:
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   125
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   126
* `limit`, maximum number of entities to display. The value of the
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   127
  'navigation.related-limit'  cwproperty is used by default (which is 8 by default).
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   128
  If None, no limit.
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   129
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   130
* `use_list_limit`, number of entities until which they should be display as a list
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   131
  (eg using the 'list' view). Below that limit, the 'csv' view is used. If None,
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   132
  display using 'csv' anyway.
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   133
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   134
* `subvid`, the subview identifier (eg view that should be used of each item in the
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   135
  list)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   136
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   137
Notice you can also use the `filter` key to set up a callback taking the related
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   138
result set as argument and returning it filtered, to do some arbitrary filtering
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   139
that can't be done using rql for instance.
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   140
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   141
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   142
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   143
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   144
.. sourcecode:: python
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   145
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   146
   pv_section = uicfg.primaryview_section
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   147
   # in `CWUser` primary view, display `created_by`
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   148
   # relations in relations section
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   149
   pv_section.tag_object_of(('*', 'created_by', 'CWUser'), 'relations')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   150
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   151
   # display this relation as a list, sets the label,
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   152
   # limit the number of results and filters on comments
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   153
   def filter_comment(rset):
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   154
       return rset.filtered_rset(lambda x: x.e_schema == 'Comment')
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   155
   pv_ctrl = uicfg.primaryview_display_ctrl
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   156
   pv_ctrl.tag_object_of(('*', 'created_by', 'CWUser'),
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   157
                         {'vid': 'list', 'label': _('latest comment(s):'),
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   158
                          'limit': True,
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   159
                          'filter': filter_comment})
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   160
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   161
.. warning:: with the ``primaryview_display_ctrl`` rtag, the subject or the
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   162
   object of the relation is ignored for respectively ``tag_object_of`` or
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   163
   ``tag_subject_of``. To avoid warnings during execution, they should be set to
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   164
   ``'*'``.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   165
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   166
Rendering methods and attributes
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   167
````````````````````````````````
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   168
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   169
The basic layout of a primary view is as in the
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   170
:ref:`primary_view_layout` section. This layout is actually drawn by
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   171
the `render_entity` method.
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   172
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   173
The methods you may want to modify while customizing a ``PrimaryView``
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   174
are:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   175
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   176
*render_entity_title(self, entity)*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   177
    Renders the entity title, by default using entity's :meth:`dc_title()` method.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   178
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   179
*render_entity_attributes(self, entity)*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   180
    Renders all attributes and relations in the 'attributes' section . The
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   181
    :attr:`skip_none` attribute controls the display of `None` valued attributes.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   182
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   183
*render_entity_relations(self, entity)*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   184
    Renders all relations in the 'relations' section.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   185
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2545
diff changeset
   186
*render_side_boxes(self, entity, boxes)*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   187
    Renders side boxes on the right side of the content. This will generate a box
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   188
    for each relation in the 'sidebox' section, as well as explicit box
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   189
    appobjects selectable in this context.
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   190
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   191
The placement of relations in the relations section or in side boxes
5227
ed1c138299f2 [doc/book] primary view section enhancements
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   192
can be controlled through the :ref:`primary_view_configuration` mechanism.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   193
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   194
*content_navigation_components(self, context)*
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   195
    This method is applicable only for entity type implementing the interface
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   196
    `IPrevNext`. This interface is for entities which can be linked to a previous
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   197
    and/or next entity. This method will render the navigation links between
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   198
    entities of this type, either at the top or at the bottom of the page
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   199
    given the context (navcontent{top|bottom}).
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   200
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   201
Also, please note that by setting the following attributes in your
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   202
subclass, you can already customize some of the rendering:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   203
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   204
*show_attr_label*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   205
    Renders the attribute label next to the attribute value if set to `True`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   206
    Otherwise, does only display the attribute value.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   207
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   208
*show_rel_label*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   209
    Renders the relation label next to the relation value if set to `True`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
    Otherwise, does only display the relation value.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   211
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   212
*skip_none*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   213
    Does not render an attribute value that is None if set to `True`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   214
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   215
*main_related_section*
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   216
    Renders the relations of the entity if set to `True`.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   218
A good practice is for you to identify the content of your entity type for which
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   219
the default rendering does not answer your need so that you can focus on the specific
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   220
method (from the list above) that needs to be modified. We do not advise you to
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   221
overwrite ``render_entity`` unless you want a completely different layout.
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   222
6303
1aad5389c013 [book] backport documentation about primary/base views sent on the list
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   223
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   224
Example of customization and creation
5312
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
   225
`````````````````````````````````````
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   226
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   227
We'll show you now an example of a ``primary`` view and how to customize it.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   228
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   229
If you want to change the way a ``BlogEntry`` is displayed, just
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   230
override the method ``cell_call()`` of the view ``primary`` in
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   231
``BlogDemo/views.py``.
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   232
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   233
.. sourcecode:: python
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   234
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5400
diff changeset
   235
   from cubicweb.selectors import is_instance
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   236
   from cubicweb.web.views.primary import Primaryview
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   237
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   238
   class BlogEntryPrimaryView(PrimaryView):
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5400
diff changeset
   239
     __select__ = PrimaryView.__select__ & is_instance('BlogEntry')
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   240
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   241
       def render_entity_attributes(self, entity):
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   242
           self.w(u'<p>published on %s</p>' %
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   243
                  entity.publish_date.strftime('%Y-%m-%d'))
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   244
           super(BlogEntryPrimaryView, self).render_entity_attributes(entity)
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   245
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   246
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   247
The above source code defines a new primary view for
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6303
diff changeset
   248
``BlogEntry``. The `__reid__` class attribute is not repeated there since it
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   249
is inherited through the `primary.PrimaryView` class.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   250
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   251
The selector for this view chains the selector of the inherited class
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   252
with its own specific criterion.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   253
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   254
The view method ``self.w()`` is used to output data. Here `lines
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   255
08-09` output HTML for the publication date of the entry.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   256
5394
105011657405 [doc/book] move devweb up from development, turn development into devrepo (much better structure)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5388
diff changeset
   257
.. image:: ../../images/lax-book_09-new-view-blogentry_en.png
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   258
   :alt: blog entries now look much nicer
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   259
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   260
Let us now improve the primary view of a blog
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   261
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   262
.. sourcecode:: python
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   263
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   264
 from logilab.mtconverter import xml_escape
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5400
diff changeset
   265
 from cubicweb.selectors import is_instance, one_line_rset
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   266
 from cubicweb.web.views.primary import Primaryview
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   267
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   268
 class BlogPrimaryView(PrimaryView):
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   269
     __regid__ = 'primary'
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5400
diff changeset
   270
     __select__ = PrimaryView.__select__ & is_instance('Blog')
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   271
     rql = 'Any BE ORDERBY D DESC WHERE BE entry_of B, BE publish_date D, B eid %(b)s'
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   272
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   273
     def render_entity_relations(self, entity):
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   274
         rset = self._cw.execute(self.rql, {'b' : entity.eid})
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   275
         for entry in rset.entities():
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   276
             self.w(u'<p>%s</p>' % entry.view('inblogcontext'))
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   277
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   278
 class BlogEntryInBlogView(EntityView):
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   279
     __regid__ = 'inblogcontext'
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5400
diff changeset
   280
     __select__ = is_instance('BlogEntry')
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   281
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   282
     def cell_call(self, row, col):
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   283
         entity = self.cw_rset.get_entity(row, col)
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   284
         self.w(u'<a href="%s" title="%s">%s</a>' %
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   285
                entity.absolute_url(),
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   286
                xml_escape(entity.content[:50]),
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   287
                xml_escape(entity.description))
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   288
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   289
This happens in two places. First we override the
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   290
render_entity_relations method of a Blog's primary view. Here we want
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   291
to display our blog entries in a custom way.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   292
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   293
At `line 10`, a simple request is made to build a result set with all
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   294
the entities linked to the current ``Blog`` entity by the relationship
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   295
``entry_of``. The part of the framework handling the request knows
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   296
about the schema and infers that such entities have to be of the
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   297
``BlogEntry`` kind and retrieves them (in the prescribed publish_date
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   298
order).
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   299
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   300
The request returns a selection of data called a result set. Result
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   301
set objects have an .entities() method returning a generator on
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   302
requested entities (going transparently through the `ORM` layer).
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   303
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   304
At `line 13` the view 'inblogcontext' is applied to each blog entry to
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   305
output HTML. (Note that the 'inblogcontext' view is not defined
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   306
whatsoever in *CubicWeb*. You are absolutely free to define whole view
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   307
families.) We juste arrange to wrap each blogentry output in a 'p'
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   308
html element.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   309
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   310
Next, we define the 'inblogcontext' view. This is NOT a primary view,
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   311
with its well-defined sections (title, metadata, attribtues,
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   312
relations/boxes). All a basic view has to define is cell_call.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   313
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   314
Since views are applied to result sets which can be tables of data, we
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   315
have to recover the entity from its (row,col)-coordinates (`line
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   316
20`). Then we can spit some HTML.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   317
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   318
.. warning::
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   319
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   320
  Be careful: all strings manipulated in *CubicWeb* are actually
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   321
  unicode strings. While web browsers are usually tolerant to
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   322
  incoherent encodings they are being served, we should not abuse
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   323
  it. Hence we have to properly escape our data. The xml_escape()
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   324
  function has to be used to safely fill (X)HTML elements from Python
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   325
  unicode strings.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   326
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   327
Assuming we added entries to the blog titled `MyLife`, displaying it
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   328
now allows to read its description and all its entries.
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   329
5394
105011657405 [doc/book] move devweb up from development, turn development into devrepo (much better structure)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5388
diff changeset
   330
.. image:: ../../images/lax-book_10-blog-with-two-entries_en.png
5262
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5227
diff changeset
   331
   :alt: a blog and all its entries