doc/book/en/development/devweb/views.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 12 Apr 2010 19:36:51 +0200
branchstable
changeset 5222 ed6905d98a5e
parent 4936 a4b772a0d801
child 5262 ebd90d2a5639
permissions -rw-r--r--
[doc/book] more flesh to views/primary view
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
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    11
We'll start with a description of the interface providing you with a basic
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    12
understanding of the available classes and methods, then detail the view
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    13
selection principle which makes *CubicWeb* web interface very flexible.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    15
A `View` is an object applied to another object such as an entity.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    16
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
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
    18
~~~~~~~~~~~~~~~~~~~~~
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
Class `View` (`cubicweb.view`)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    21
`````````````````````````````````````
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
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
    24
renderable object such as views, templates, graphic components, etc.
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
A `View` is instantiated to render a result set or part of a result set. `View`
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
subclasses may be parametrized using the following class attributes:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    28
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    29
* `templatable` indicates if the view may be embedded in a main
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    30
  template or if it has to be rendered standalone (i.e. XML views must
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    31
  not be embedded in the main template for HTML pages)
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    32
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    33
* if the view is not templatable, it should set the `content_type`
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    34
  class attribute to the correct MIME type (text/xhtml by default)
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    35
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    36
* the `category` attribute may be used in the interface to regroup
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    37
  related objects together
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    38
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    39
A view writes to its output stream thanks to its attribute `w` (an
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    40
`UStreamIO`, except for binary views).
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
    42
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
    43
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
    44
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    45
The basic interface for views is as follows (remember that the result set has a
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    46
tabular structure with rows and columns, hence cells):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    47
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    48
* `render(**context)`, render the view by calling `call` or
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
  `cell_call` depending on the given parameters
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    50
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    51
* `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
    52
  (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
    53
  result set)
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    54
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    55
* `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
    56
  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
    57
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    58
* `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
    59
  result set
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
    60
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    61
* `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
    62
  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
    63
  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
    64
  not applicable to the result set.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    65
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    66
* `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
    67
  main template
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    68
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    69
* `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
    70
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    71
Other basic view classes
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    72
````````````````````````
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    73
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
    74
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
    75
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    76
* `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
    77
* `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
    78
* `AnyRsetView`, view applicable to any result set
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    79
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    80
Examples of views class
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    81
-----------------------
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
- Using `templatable`, `content_type` and HTTP cache configuration
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    84
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    85
.. sourcecode:: python
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
    class RSSView(XMLView):
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    88
        __regid__ = 'rss'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    89
        title = _('rss')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
        templatable = False
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    91
        content_type = 'text/xml'
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    92
        http_cache_manager = MaxAgeHTTPCacheManager
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    93
        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
    94
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
- Using custom selector
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    97
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
    98
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    99
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   100
    class SearchForAssociationView(EntityView):
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   101
        """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
   102
        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
   103
        """
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   104
        __regid__ = 'search-associate'
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   105
        title = _('search for association')
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   106
        __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
   107
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   108
5222
ed6905d98a5e [doc/book] more flesh to views/primary view
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
   109
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   110
Example of view customization and creation
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
   111
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1714
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
We'll show you now an example of a ``primary`` view and how to customize it.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   114
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   115
If you want to change the way a ``BlogEntry`` is displayed, just override
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   116
the method ``cell_call()`` of the view ``primary`` in ``BlogDemo/views.py``:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   117
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   118
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   119
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   120
  from cubicweb.selectors import implements
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   121
  from cubicweb.web.views.primary improt Primaryview
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   122
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   123
  class BlogEntryPrimaryView(PrimaryView):
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   124
    __select__ = PrimaryView.__select__ & implements('BlogEntry')
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   125
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   126
      def render_entity_attributes(self, entity):
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   127
          self.w(u'<p>published on %s</p>' %
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   128
                 entity.publish_date.strftime('%Y-%m-%d'))
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   129
          super(BlogEntryPrimaryView, self).render_entity_attributes(entity)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   130
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   131
The above source code defines a new primary view for
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   132
``BlogEntry``. The `id` class attribute is not repeated there since it
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   133
is inherited through the `primary.PrimaryView` class.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   134
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   135
The selector for this view chains the selector of the inherited class
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   136
with its own specific criterion.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   137
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   138
The view method ``self.w()`` is used to output data. Here `lines
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   139
08-09` output HTML for the publication date of the entry.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   140
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   141
.. image:: ../../images/lax-book.09-new-view-blogentry.en.png
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   142
   :alt: blog entries now look much nicer
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   143
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   144
Let us now improve the primary view of a blog
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   145
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   146
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   147
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   148
 from logilab.mtconverter import xml_escape
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   149
 from cubicweb.selectors import implements, one_line_rset
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   150
 from cubicweb.web.views.primary import Primaryview
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   151
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   152
 class BlogPrimaryView(PrimaryView):
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   153
     __regid__ = 'primary'
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   154
     __select__ = PrimaryView.__select__ & implements('Blog')
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   155
     rql = 'Any BE ORDERBY D DESC WHERE BE entry_of B, BE publish_date D, B eid %(b)s'
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   156
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   157
     def render_entity_relations(self, entity):
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
   158
         rset = self._cw.execute(self.rql, {'b' : entity.eid})
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   159
         for entry in rset.entities():
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   160
             self.w(u'<p>%s</p>' % entry.view('inblogcontext'))
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   161
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   162
 class BlogEntryInBlogView(EntityView):
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   163
     __regid__ = 'inblogcontext'
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   164
     __select__ = implements('BlogEntry')
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   165
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   166
     def cell_call(self, row, col):
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
   167
         entity = self.cw_rset.get_entity(row, col)
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   168
         self.w(u'<a href="%s" title="%s">%s</a>' %
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   169
                entity.absolute_url(),
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   170
                xml_escape(entity.content[:50]),
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   171
                xml_escape(entity.description))
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   173
This happens in two places. First we override the
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   174
render_entity_relations method of a Blog's primary view. Here we want
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   175
to display our blog entries in a custom way.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   176
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
   177
At `line 10`, a simple request is made to build a result set with all
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   178
the entities linked to the current ``Blog`` entity by the relationship
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
   179
``entry_of``. The part of the framework handling the request knows
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   180
about the schema and infers that such entities have to be of the
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   181
``BlogEntry`` kind and retrieves them (in the prescribed publish_date
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   182
order).
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   183
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
   184
The request returns a selection of data called a result set. Result
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   185
set objects have an .entities() method returning a generator on
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   186
requested entities (going transparently through the `ORM` layer).
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   187
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   188
At `line 13` the view 'inblogcontext' is applied to each blog entry to
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   189
output HTML. (Note that the 'inblogcontext' view is not defined
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   190
whatsoever in *CubicWeb*. You are absolutely free to define whole view
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   191
families.) We juste arrange to wrap each blogentry output in a 'p'
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   192
html element.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   193
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   194
Next, we define the 'inblogcontext' view. This is NOT a primary view,
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   195
with its well-defined sections (title, metadata, attribtues,
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   196
relations/boxes). All a basic view has to define is cell_call.
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   197
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   198
Since views are applied to result sets which can be tables of data, we
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   199
have to recover the entity from its (row,col)-coordinates (`line
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   200
20`). Then we can spit some HTML.
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   201
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   202
But careful: all strings manipulated in *CubicWeb* are actually
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   203
unicode strings. While web browsers are usually tolerant to incoherent
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   204
encodings they are being served, we should not abuse it. Hence we have
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   205
to properly escape our data. The xml_escape() function has to be used
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   206
to safely fill (X)HTML elements from Python unicode strings.
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   207
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   208
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   209
**This is to be compared to interfaces and protocols in object-oriented
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   210
languages. Applying a given view called 'a_view' to all the entities
4446
a413fac5ff5e damn me, more stupid sed fix...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4445
diff changeset
   211
of a result set only requires to have for each entity of this result set,
4936
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4751
diff changeset
   212
an available view called 'a_view' which accepts the entity.**
3258
6536ee4f37f7 update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2544
diff changeset
   213
4936
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4751
diff changeset
   214
**Instead of merely using type based dispatch, we do predicate dispatch
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4751
diff changeset
   215
which is quite more powerful.**
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   216
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   217
Assuming we added entries to the blog titled `MyLife`, displaying it
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   218
now allows to read its description and all its entries.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   219
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   220
.. image:: ../../images/lax-book.10-blog-with-two-entries.en.png
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   221
   :alt: a blog and all its entries
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   222
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   223
**Before we move forward, remember that the selection/view principle is
4442
7bc0e4ed4109 fix stupid sed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
   224
at the core of *CubicWeb*. Everywhere in the engine, data is requested
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   225
using the RQL language, then HTML/XML/text/PNG is output by applying a
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   226
view to the result set returned by the query. That is where most of the
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   227
flexibility comes from.**
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   228
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   229
[WRITE ME]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   230
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   231
* implementing interfaces, calendar for blog entries
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   232
* show that a calendar view can export data to ical
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   233
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   234
We will implement the `cubicweb.interfaces.ICalendarable` interfaces on
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   235
entities.BlogEntry and apply the OneMonthCalendar and iCalendar views
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   236
to result sets like "Any E WHERE E is BlogEntry"
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   237
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   238
* create view "blogentry table" with title, publish_date, category
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   239
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   240
We will show that by default the view that displays
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   241
"Any E,D,C WHERE E publish_date D, E category C" is the table view.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   242
Of course, the same can be obtained by calling
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   243
self.wview('table',rset)
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   244
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   245
* in view blog, select blogentries and apply view "blogentry table"
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   246
* demo ajax by filtering blogentry table on category
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   247
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   248
we did the same with 'primary', but with tables we can turn on filters
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   249
and show that ajax comes for free.
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   250
[FILLME]
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   251
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   252
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
   253
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
   254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2175
diff changeset
   255
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   256
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
   257
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
   258
by the main template (see above), you have to:
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   259
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   260
* set the attribute `templatable` of the class to `False`
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   261
* set, through the attribute `content_type` of the class, the MIME type generated
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   262
  by the view to `application/octet-stream`
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   263
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   264
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
   265
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
   266
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
   267
replaced by a binary flow instead of unicode).