author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Oct 2009 15:13:07 +0200 | |
branch | stable |
changeset 3774 | 1eca47d59fd9 |
parent 93 | 9c919a47e140 |
child 2789 | 39712da6f397 |
permissions | -rw-r--r-- |
0 | 1 |
.. -*- coding: utf-8 -*- |
2 |
||
82
57f37f7af21b
Add more explanation on views in introduction.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
71
diff
changeset
|
3 |
.. _DefinitionVues: |
0 | 4 |
|
5 |
Définition de vues |
|
6 |
================== |
|
7 |
||
8 |
Les classes de base des vues |
|
9 |
---------------------------- |
|
10 |
||
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
11 |
La class `View` (`cubicweb.common.view`) |
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
12 |
```````````````````````````````````````` |
0 | 13 |
Un vue écrit dans son flux de sortie via son attribut `w` (`UStreamIO`). |
14 |
||
15 |
L'interface de base des vues est la suivante : |
|
16 |
||
17 |
* `dispatch(**context)`, appelle ("rend") la vue en appellent `call` ou |
|
18 |
`cell_call` en fonction des arguments passé |
|
19 |
* `call(**kwargs)`, appelle la vue pour un result set complet ou nul |
|
20 |
* `cell_call(row, col, **kwargs)`, appelle la vue pour une cellule donnée d'un |
|
21 |
result set |
|
22 |
* `url()`, retourne l'url permettant d'obtenir cette vue avec le result set en |
|
23 |
cours |
|
24 |
* `view(__vid, rset, __fallback_vid=None, **kwargs)`, appelle la vue |
|
25 |
d'identificant `__vid` sur le result set donné. Il est possible de données un |
|
26 |
identificant de vue de "fallback" qui sera utilisé si la vue demandée n'est |
|
27 |
pas applicable au result set |
|
28 |
||
29 |
* `wview(__vid, rset, __fallback_vid=None, **kwargs)`, pareil que `view` mais |
|
30 |
passe automatiquement le flux en argument |
|
31 |
||
32 |
* `html_headers()`, retourne une liste d'en-tête HTML à placer par le template |
|
33 |
principal |
|
34 |
||
35 |
* `page_title()`, retourne le titre à utiliser dans l'en tête HTML `title` |
|
36 |
||
37 |
* `creator(eid)`, retourne l'eid et le login du créateur de l'entité ayant |
|
38 |
l'eid passé en argument |
|
39 |
||
40 |
Autres classes de base : |
|
41 |
||
42 |
* `EntityView`, vue s'appliquant à aux lignes ou cellule contenant une entité |
|
43 |
(eg un eid) |
|
44 |
* `StartupView`, vue de départ n'ayant pas besoin de result set |
|
45 |
* `AnyRsetView`, vue s'appliquant à n'importe quelle result set |
|
46 |
||
24
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
47 |
Le mecanisme de selection de vues |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
48 |
--------------------------------- |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
49 |
|
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
50 |
Pour un identifiant de vue donne, plusieurs vues peuvent etre definies. |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
51 |
`CubicWeb` utilise un selecteur qui permet de calculer un score et d'identifier |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
52 |
la vue la plus appropriee a appliquer dans le contexte. La librairie du selecteur |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
53 |
se trouve dans ``cubicweb.common.selector`` et une librairie des methodes utilisees |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
54 |
pour calculer les scores est dans ``cubicweb.vregistry.vreq``. |
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
55 |
|
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
56 |
[FROM-LAX-BOOK] |
24
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
57 |
|
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
58 |
Tip: when modifying views, you do not need to restart the local |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
59 |
server. Just save the file in your editor and reload the page in your |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
60 |
browser to see the changes. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
61 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
62 |
With `LAX`, views are defined by Python classes. A view includes : |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
63 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
64 |
- an identifier (all objects in `LAX` are entered in a registry |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
65 |
and this identifier will be used as a key) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
66 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
67 |
- a filter to select the resulsets it can be applied to |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
68 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
69 |
`LAX` provides a lot of standard views, for a complete list, you |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
70 |
will have to read the code in directory ``ginco/web/views/`` (XXX |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
71 |
improve doc). |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
72 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
73 |
For example, the view named ``primary`` is the one used to display |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
74 |
a single entity. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
75 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
76 |
If you want to change the way a ``BlogEntry`` is displayed, just |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
77 |
override the view ``primary`` in ``BlogDemo/views.py`` :: |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
78 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
79 |
01. from ginco.web.views import baseviews |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
80 |
02. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
81 |
03. class BlogEntryPrimaryView(baseviews.PrimaryView): |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
82 |
04. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
83 |
05. accepts = ('BlogEntry',) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
84 |
06. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
85 |
07. def cell_call(self, row, col): |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
86 |
08. entity = self.entity(row, col) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
87 |
09. self.w(u'<h1>%s</h1>' % entity.title) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
88 |
10. self.w(u'<p>published on %s in category %s</p>' % \ |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
89 |
11. (entity.publish_date.strftime('%Y-%m-%d'), entity.category)) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
90 |
12. self.w(u'<p>%s</p>' % entity.text) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
91 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
92 |
The above source code defines a new primary view (`line 03`) for |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
93 |
``BlogEntry`` (`line 05`). |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
94 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
95 |
Since views are applied to resultsets and resulsets can be tables of |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
96 |
data, it is needed to recover the entity from its (row,col) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
97 |
coordinates (`line 08`). We will get to this in more detail later. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
98 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
99 |
The view has a ``self.w()`` method that is used to output data. Here `lines |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
100 |
09-12` output HTML tags and values of the entity's attributes. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
101 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
102 |
When displaying same blog entry as before, you will notice that the |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
103 |
page is now looking much nicer. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
104 |
|
90
7fd4ffeb082b
[doc] images were renamed and table of content changed
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
82
diff
changeset
|
105 |
.. image:: images/lax-book.09-new-view-blogentry.fr.png |
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
106 |
:alt: blog entries now look much nicer |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
107 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
108 |
Let us now improve the primary view of a blog :: |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
109 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
110 |
01. class BlogPrimaryView(baseviews.PrimaryView): |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
111 |
02. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
112 |
03. accepts = ('Blog',) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
113 |
04. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
114 |
05. def cell_call(self, row, col): |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
115 |
06. entity = self.entity(row, col) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
116 |
07. self.w(u'<h1>%s</h1>' % entity.title) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
117 |
08. self.w(u'<p>%s</p>' % entity.description) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
118 |
09. rset = self.req.execute('Any E WHERE E entry_of B, B eid "%s"' % entity.eid) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
119 |
10. self.wview('primary', rset) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
120 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
121 |
In the above source code, `lines 01-08` are similar to the previous |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
122 |
view we defined. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
123 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
124 |
At `line 09`, a simple request in made to build a resultset with all |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
125 |
the entities linked to the current ``Blog`` entity by the relationship |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
126 |
``entry_of``. The part of the framework handling the request knows |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
127 |
about the schema and infer that such entities have to be of the |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
128 |
``BlogEntry`` kind and retrieves them. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
129 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
130 |
The request returns a selection of data called a resultset. At |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
131 |
`line 10` the view 'primary' is applied to this resultset to output |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
132 |
HTML. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
133 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
134 |
**This is to be compared to interfaces and protocols in object-oriented |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
135 |
languages. Applying a given view to all the entities of a resultset only |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
136 |
requires the availability, for each entity of this resultset, of a |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
137 |
view with that name that can accepts the entity.** |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
138 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
139 |
Assuming we added entries to the blog titled `MyLife`, displaying it |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
140 |
now allows to read its description and all its entries. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
141 |
|
90
7fd4ffeb082b
[doc] images were renamed and table of content changed
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
82
diff
changeset
|
142 |
.. image:: images/lax-book.10-blog-with-two-entries.fr.png |
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
143 |
:alt: a blog and all its entries |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
144 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
145 |
**Before we move forward, remember that the selection/view principle is |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
146 |
at the core of `LAX`. Everywhere in the engine, data is requested |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
147 |
using the RQL language, then HTML/XML/text/PNG is output by applying a |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
148 |
view to the resultset returned by the query. That is where most of the |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
149 |
flexibility comes from.** |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
150 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
151 |
[WRITE ME] |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
152 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
153 |
* implementing interfaces, calendar for blog entries |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
154 |
* show that a calendar view can export data to ical |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
155 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
156 |
We will implement the ginco.interfaces.ICalendarable interfaces on |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
157 |
entities.BloEntry and apply the OneMonthCalendar and iCalendar views |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
158 |
to resultsets like "Any E WHERE E is BlogEntry" |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
159 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
160 |
* create view "blogentry table" with title, publish_date, category |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
161 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
162 |
We will show that by default the view that displays |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
163 |
"Any E,D,C WHERE E publish_date D, E category C" is the table view. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
164 |
Of course, the same can be obtained by calling |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
165 |
self.wview('table',rset) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
166 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
167 |
* in view blog, select blogentries and apply view "blogentry table" |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
168 |
* demo ajax by filtering blogentry table on category |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
169 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
170 |
we did the same with 'primary', but with tables we can turn on filters |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
171 |
and show that ajax comes for free. |
24
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
172 |
[FILLME] |
0 | 173 |
|
174 |
Les templates ou patron |
|
175 |
----------------------- |
|
176 |
||
177 |
Les patrons (ou *template*) sont des cas particulier de vue ne dépendant a |
|
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
178 |
priori pas d'un result set. La classe de base `Template` (`cubicweb.common.view`) |
0 | 179 |
est une classe dérivée de la classe `View`. |
180 |
||
181 |
Pour construire une page HTML, un *template principal* est utilisé. Généralement |
|
182 |
celui possédant l'identifiant 'main' est utilisé (ce n'est pas le cas lors |
|
183 |
d'erreur dans celui-ci ou pour le formulaire de login par exemple). Ce patron |
|
184 |
utilise d'autres patrons en plus des vues dépendants du contenu pour générer la |
|
185 |
page à renvoyer. |
|
186 |
||
187 |
C'est ce template qui est chargé : |
|
188 |
||
189 |
1. d'éxécuter la requête RQL des données à afficher le cas échéant |
|
190 |
2. éventuellement de déterminer la vue à utiliser pour l'afficher si non |
|
191 |
spécifiée |
|
192 |
3. de composer la page à retourner |
|
193 |
||
194 |
||
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
195 |
Le patron principal par défaut (`cubicweb.web.views.basetemplates.TheMainTemplate`) |
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
196 |
----------------------------------------------------------------------------------- |
0 | 197 |
|
198 |
Le template principal par défaut construit la page selon la décomposition |
|
199 |
suivante : |
|
200 |
||
82
57f37f7af21b
Add more explanation on views in introduction.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
71
diff
changeset
|
201 |
.. image:: images/main_template_layout.png |
0 | 202 |
|
203 |
Le rectancle contenant le `view.dispatch()` représente l'emplacement où est |
|
204 |
inséré la vue de contenu à afficher. Les autres représentent des sous-templates |
|
205 |
appelé pour construire la page. Les implémentations par défaut de tout ces |
|
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
206 |
templates sont dans le module `cubicweb.web.views.basetemplates`. Vous pouvez |
0 | 207 |
évidemment surcharger l'un des sous-templates pour modifier l'aspect visuel |
208 |
d'une partie désirée de la page. |
|
209 |
||
210 |
On peut également contrôler certains comportements du template principal à |
|
211 |
l'aide des paramètres de formulaire suivante : |
|
212 |
||
213 |
* `__notemplate`, si présente (quelque soit la valeur associée), seule la vue de |
|
214 |
contenu est renvoyée |
|
215 |
* `__force_display`, si présente et contient une valeur non nulle, pas de |
|
216 |
navigation quelque soit le nombre d'entités à afficher |
|
217 |
* `__method`, si le result set à afficher ne contient qu'une entité et que ce |
|
218 |
paramètre est spécifié, celui-ci désigne une méthode à appeler sur l'entité |
|
219 |
en lui donnant en argument le dictionnaire des paramètres de formulaire, avant |
|
220 |
de reprendre le comportement classique (s'insère entre les étapes 1. et |
|
221 |
2. décrites ci-dessus) |
|
222 |
||
223 |
||
93
9c919a47e140
[doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
90
diff
changeset
|
224 |
.. include:: 05-01-views-stdlib.fr.txt |
0 | 225 |
|
226 |
||
227 |
Vues xml, binaires... |
|
228 |
--------------------- |
|
229 |
Pour les vues générants autre que du html (une image générée dynamiquement par |
|
230 |
exemple), et qui ne peuvent donc généralement pas être incluse dans la page |
|
231 |
HTML générée par le template principal (voir ci-dessus), il faut : |
|
232 |
||
233 |
* placer l'attribut `templatable` de la classe à `False` |
|
234 |
* indiquer via l'attribut `content_type` de la classe le type MIME généré par la |
|
235 |
vue 'application/octet-stream' |
|
236 |
||
237 |
Pour les vues générants un contenu binaire (une image générée dynamiquement par |
|
238 |
exemple), il faut également placer l'attribut `binary` de la classe à `True` (ce |
|
239 |
qui implique `templatable == False` afin que l'attribut `w` de la vue soit |
|
240 |
remplacé par un flux binaire plutôt que unicode. |
|
241 |
||
242 |
||
243 |
Quelques trucs (X)HTML à respecter |
|
244 |
---------------------------------- |
|
245 |
Certains navigateurs (dont firefox) n'aime pas les `<div>` vides (par vide |
|
246 |
j'entend sans contenu dans la balise, il peut y avoir des attributs), faut |
|
247 |
toujours mettre `<div></div>` même s'il n'y a rien dedans, et non `<div/>`. |