doc/book/en/development/devweb/views.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 14 Apr 2010 18:10:47 +0200
branchstable
changeset 5262 ebd90d2a5639
parent 5222 ed6905d98a5e
permissions -rw-r--r--
[doc/book] move a big chunk of primary view customisation example in the primary view chapter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
     1
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
     2
.. _Views:
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
     3
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     4
Views
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
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     7
This chapter aims to describe the concept of a `view` used all along
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     8
the development of a web application and how it has been implemented
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
     9
in *CubicWeb*.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    10
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: 5222
diff changeset
    11
We'll start with a description of the interface providing you with 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: 5222
diff changeset
    12
basic understanding of the available classes and methods, then detail
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    13
the view selection principle.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
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: 5222
diff changeset
    15
A `View` is an object responsible for the rendering of data from 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: 5222
diff changeset
    16
model into an end-user consummable form. They typically churn out an
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    17
XHTML stream, but there are views concerned with email other non-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: 5222
diff changeset
    18
outputs.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
Basic class for views
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
    21
~~~~~~~~~~~~~~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
Class `View` (`cubicweb.view`)
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: 5222
diff changeset
    24
```````````````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    26
This class is an abstraction of a view class, used as a base class for every
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
renderable object such as views, templates, graphic components, etc.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
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: 5222
diff changeset
    29
A `View` is instantiated to render a result set or part of a 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: 5222
diff changeset
    30
set. `View` subclasses may be parametrized using the following 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: 5222
diff changeset
    31
attributes:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    33
* `templatable` indicates if the view may be embedded in a main
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: 5222
diff changeset
    34
  template or if it has to be rendered standalone (i.e. pure XML views
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
    35
  must not be embedded in the main template of HTML pages)
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    36
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    37
* if the view is not templatable, it should set the `content_type`
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: 5222
diff changeset
    38
  class attribute to the correct MIME type (text/xhtml being 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: 5222
diff changeset
    39
  default)
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    40
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    41
* the `category` attribute may be used in the interface to regroup
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: 5222
diff changeset
    42
  related view kinds together
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    43
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: 5222
diff changeset
    44
A view writes to its output stream thanks to its attribute `w` (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: 5222
diff changeset
    45
append method of an `UStreamIO`, except for binary views).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    47
At instantiation time, the standard `_cw` and `cw_rset` attributes are
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    48
added and the `w` attribute will be set at rendering time.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
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: 5222
diff changeset
    50
The basic interface for views is as follows (remember that the 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: 5222
diff changeset
    51
set has a tabular structure with rows and columns, hence cells):
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    52
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    53
* `render(**context)`, render the view by calling `call` or
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: 5222
diff changeset
    54
  `cell_call` depending on the context
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    55
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    56
* `call(**kwargs)`, call the view for a complete result set or null
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    57
  (the default implementation calls `cell_call()` on each cell of the
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    58
  result set)
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    59
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    60
* `cell_call(row, col, **kwargs)`, call the view for a given cell of a
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    61
  result set (`row` and `col` being integers used to access the cell)
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    62
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    63
* `url()`, returns the URL enabling us to get the view with the current
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    64
  result set
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    65
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    66
* `wview(__vid, rset, __fallback_vid=None, **kwargs)`, call the view of
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    67
  identifier `__vid` on the given result set. It is possible to give a
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    68
  fallback view identifier that will be used if the requested view is
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    69
  not applicable to the result set.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    70
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    71
* `html_headers()`, returns a list of HTML headers to be set by the
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    72
  main template
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    74
* `page_title()`, returns the title to use in the HTML header `title`
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
Other basic view classes
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
Here are some of the subclasses of `View` defined in `cubicweb.common.view`
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
that are more concrete as they relate to data rendering within the application:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
* `EntityView`, view applying to lines or cell containing an entity (e.g. an eid)
4446
a413fac5ff5e damn me, more stupid sed fix...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4445
diff changeset
    82
* `StartupView`, start view that does not require a result set to apply to
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    83
* `AnyRsetView`, view applicable to any result set
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    85
Examples of views class
1714
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
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
- Using `templatable`, `content_type` and HTTP cache configuration
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    90
.. sourcecode:: python
1714
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
    class RSSView(XMLView):
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    93
        __regid__ = 'rss'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
        title = _('rss')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
        templatable = False
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
        content_type = 'text/xml'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
        http_cache_manager = MaxAgeHTTPCacheManager
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    98
        cache_max_age = 60*60*2 # stay in http cache for 2 hours by default
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    99
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
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: 5222
diff changeset
   101
- Using a custom selector
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   102
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   103
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
    class SearchForAssociationView(EntityView):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        """view called by the edition view when the user asks
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   107
        to search for something to link to the edited eid
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
        """
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   109
        __regid__ = 'search-associate'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
        title = _('search for association')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
        __select__ = one_line_rset() & match_search_state('linksearch') & implements('Any')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   113
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   114
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   115
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   116
XML views, binaries views...
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   117
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   118
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
For views generating other formats than HTML (an image generated dynamically
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   120
for example), and which can not simply be included in the HTML page generated
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   121
by the main template (see above), you have to:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   122
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   123
* set the attribute `templatable` of the class to `False`
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: 5222
diff changeset
   124
* set, through the attribute `content_type` of the class, the MIME
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   125
  type generated by the view to `application/octet-stream` or any
ebd90d2a5639 [doc/book] move a big chunk of primary view customisation example in the primary view chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5222
diff changeset
   126
  relevant and more specialised mime type
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   127
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   128
For views dedicated to binary content creation (like dynamically generated
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   129
images), we have to set the attribute `binary` of the class to `True` (which
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
implies that `templatable == False`, so that the attribute `w` of the view could be
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   131
replaced by a binary flow instead of unicode).