doc/book/devweb/views/views.rst
author Laurent Peuch <cortex@worlddomination.be>
Thu, 21 Feb 2019 18:48:07 +0100
changeset 12391 e4e5814fd70c
parent 10491 c67bcee93248
permissions -rw-r--r--
[doc] recommend sqlite during tutorial because it's simplier to use
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
5269
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     4
Principles
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     5
----------
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     6
5269
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     7
We'll start with a description of the interface providing a basic
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     8
understanding of the available classes and methods, then detail the
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
     9
view selection principle.
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
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
    12
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
    13
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
    14
outputs.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
5284
ad922b7449aa [doc/book] rework the templates chapter (wipe out horrible example, talk more about structure)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5269
diff changeset
    16
.. _views_base_class:
ad922b7449aa [doc/book] rework the templates chapter (wipe out horrible example, talk more about structure)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5269
diff changeset
    17
5312
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    18
Discovering possible views
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    19
~~~~~~~~~~~~~~~~~~~~~~~~~~
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    20
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    21
It is possible to configure the web user interface to have a left box
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    22
showing all the views than can be applied to the current result set.
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    23
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    24
To enable this, click on your login at the top right corner. Chose
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    25
"user preferences", then "boxes", then "possible views box" and check
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    26
"visible = yes" before validating your changes.
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    27
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    28
The views listed there we either not selected because of a lower
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    29
score, or they were deliberately excluded by the main template logic.
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    30
d2dbba898a96 [doc/book] misc on views, docstrings
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5284
diff changeset
    31
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    32
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
    33
~~~~~~~~~~~~~~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    34
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8419
diff changeset
    35
Class :class:`~cubicweb.view.View`
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8419
diff changeset
    36
``````````````````````````````````
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
8419
dd222fcc85d3 [doc] cubicweb.view documentation (closes: #2099962)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6832
diff changeset
    38
.. autoclass:: cubicweb.view.View
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    39
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
    40
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
    41
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
    42
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    43
* `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
    44
  `cell_call` depending on the context
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    45
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    46
* `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
    47
  (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
    48
  result set)
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    49
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    50
* `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
    51
  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
    52
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    53
* `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
    54
  result set
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    55
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    56
* `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
    57
  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
    58
  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
    59
  not applicable to the result set.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    60
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    61
* `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
    62
  main template
1714
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
* `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
    65
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    66
Other basic view classes
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    67
````````````````````````
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8419
diff changeset
    68
Here are some of the subclasses of :class:`~cubicweb.view.View` defined in :mod:`cubicweb.view`
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
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
    70
8419
dd222fcc85d3 [doc] cubicweb.view documentation (closes: #2099962)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6832
diff changeset
    71
.. autoclass:: cubicweb.view.EntityView
dd222fcc85d3 [doc] cubicweb.view documentation (closes: #2099962)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6832
diff changeset
    72
.. autoclass:: cubicweb.view.StartupView
dd222fcc85d3 [doc] cubicweb.view documentation (closes: #2099962)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6832
diff changeset
    73
.. autoclass:: cubicweb.view.EntityStartupView
dd222fcc85d3 [doc] cubicweb.view documentation (closes: #2099962)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6832
diff changeset
    74
.. autoclass:: cubicweb.view.AnyRsetView
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    75
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    76
Examples of views class
5269
2e5bc78d05f3 [doc/book] missing .svg files, refresh .pngs, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5266
diff changeset
    77
```````````````````````
1714
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
- Using `templatable`, `content_type` and HTTP cache configuration
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    80
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    81
.. sourcecode:: python
1714
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
    class RSSView(XMLView):
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    84
        __regid__ = 'rss'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    85
        title = _('rss')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    86
        templatable = False
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    87
        content_type = 'text/xml'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    88
        http_cache_manager = MaxAgeHTTPCacheManager
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
        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
    90
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
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
    92
- Using a custom selector
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    94
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
    class SearchForAssociationView(EntityView):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
        """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
    98
        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
    99
        """
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   100
        __regid__ = 'search-associate'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        title = _('search for association')
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   102
        __select__ = one_line_rset() & match_search_state('linksearch') & is_instance('Any')
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   103
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
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
   105
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
   106
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   107
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
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
   109
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
   110
by the main template (see above), you have to:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   111
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   112
* 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
   113
* 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
   114
  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
   115
  relevant and more specialised mime type
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   116
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   117
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
   118
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
   119
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
   120
replaced by a binary flow instead of unicode).