author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Fri, 14 Nov 2008 19:02:07 +0100 | |
changeset 78 | 079c4d11a616 |
parent 71 | 69a4bf8f5e49 |
child 82 | 57f37f7af21b |
permissions | -rw-r--r-- |
0 | 1 |
.. -*- coding: utf-8 -*- |
2 |
||
3 |
||
4 |
Définition de vues |
|
5 |
================== |
|
6 |
||
7 |
Les classes de base des vues |
|
8 |
---------------------------- |
|
9 |
||
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
10 |
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
|
11 |
```````````````````````````````````````` |
0 | 12 |
Un vue écrit dans son flux de sortie via son attribut `w` (`UStreamIO`). |
13 |
||
14 |
L'interface de base des vues est la suivante : |
|
15 |
||
16 |
* `dispatch(**context)`, appelle ("rend") la vue en appellent `call` ou |
|
17 |
`cell_call` en fonction des arguments passé |
|
18 |
* `call(**kwargs)`, appelle la vue pour un result set complet ou nul |
|
19 |
* `cell_call(row, col, **kwargs)`, appelle la vue pour une cellule donnée d'un |
|
20 |
result set |
|
21 |
* `url()`, retourne l'url permettant d'obtenir cette vue avec le result set en |
|
22 |
cours |
|
23 |
* `view(__vid, rset, __fallback_vid=None, **kwargs)`, appelle la vue |
|
24 |
d'identificant `__vid` sur le result set donné. Il est possible de données un |
|
25 |
identificant de vue de "fallback" qui sera utilisé si la vue demandée n'est |
|
26 |
pas applicable au result set |
|
27 |
||
28 |
* `wview(__vid, rset, __fallback_vid=None, **kwargs)`, pareil que `view` mais |
|
29 |
passe automatiquement le flux en argument |
|
30 |
||
31 |
* `html_headers()`, retourne une liste d'en-tête HTML à placer par le template |
|
32 |
principal |
|
33 |
||
34 |
* `page_title()`, retourne le titre à utiliser dans l'en tête HTML `title` |
|
35 |
||
36 |
* `creator(eid)`, retourne l'eid et le login du créateur de l'entité ayant |
|
37 |
l'eid passé en argument |
|
38 |
||
39 |
Autres classes de base : |
|
40 |
||
41 |
* `EntityView`, vue s'appliquant à aux lignes ou cellule contenant une entité |
|
42 |
(eg un eid) |
|
43 |
* `StartupView`, vue de départ n'ayant pas besoin de result set |
|
44 |
* `AnyRsetView`, vue s'appliquant à n'importe quelle result set |
|
45 |
||
24
b5303abf484a
Add a FAQ and started a section on views selector.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
46 |
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
|
47 |
--------------------------------- |
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 |
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
|
50 |
`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
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
|
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
55 |
[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
|
56 |
|
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
61 |
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
|
62 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
63 |
- 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
|
64 |
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
|
65 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
66 |
- 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
|
67 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
68 |
`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
|
69 |
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
|
70 |
improve doc). |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
71 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
72 |
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
|
73 |
a single entity. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
74 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
78 |
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
|
79 |
02. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
80 |
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
|
81 |
04. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
82 |
05. accepts = ('BlogEntry',) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
83 |
06. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
91 |
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
|
92 |
``BlogEntry`` (`line 05`). |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
93 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
104 |
.. image:: ../images/lax-book.09-new-view-blogentry.en.png |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
105 |
: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
|
106 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
107 |
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
|
108 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
109 |
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
|
110 |
02. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
111 |
03. accepts = ('Blog',) |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
112 |
04. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
120 |
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
|
121 |
view we defined. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
122 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
``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
|
126 |
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
|
127 |
``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
|
128 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
129 |
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
|
130 |
`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
|
131 |
HTML. |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
132 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
133 |
**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
|
134 |
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
|
135 |
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
|
136 |
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
|
137 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
141 |
.. image:: ../images/lax-book.10-blog-with-two-entries.en.png |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
142 |
: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
|
143 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
144 |
**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
|
145 |
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
|
146 |
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
|
147 |
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
|
148 |
flexibility comes from.** |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
149 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
150 |
[WRITE ME] |
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
151 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
152 |
* 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
|
153 |
* 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
|
154 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
159 |
* 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
|
160 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
161 |
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
|
162 |
"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
|
163 |
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
|
164 |
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
|
165 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
166 |
* 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
|
167 |
* 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
|
168 |
|
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
[FILLME] |
0 | 172 |
|
173 |
Les templates ou patron |
|
174 |
----------------------- |
|
175 |
||
176 |
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
|
177 |
priori pas d'un result set. La classe de base `Template` (`cubicweb.common.view`) |
0 | 178 |
est une classe dérivée de la classe `View`. |
179 |
||
180 |
Pour construire une page HTML, un *template principal* est utilisé. Généralement |
|
181 |
celui possédant l'identifiant 'main' est utilisé (ce n'est pas le cas lors |
|
182 |
d'erreur dans celui-ci ou pour le formulaire de login par exemple). Ce patron |
|
183 |
utilise d'autres patrons en plus des vues dépendants du contenu pour générer la |
|
184 |
page à renvoyer. |
|
185 |
||
186 |
C'est ce template qui est chargé : |
|
187 |
||
188 |
1. d'éxécuter la requête RQL des données à afficher le cas échéant |
|
189 |
2. éventuellement de déterminer la vue à utiliser pour l'afficher si non |
|
190 |
spécifiée |
|
191 |
3. de composer la page à retourner |
|
192 |
||
193 |
||
26
eaaa848cf401
Cubicweb renaming.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
24
diff
changeset
|
194 |
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
|
195 |
----------------------------------------------------------------------------------- |
0 | 196 |
|
197 |
Le template principal par défaut construit la page selon la décomposition |
|
198 |
suivante : |
|
199 |
||
71
69a4bf8f5e49
Moved content from tutoriel to introduction and to wrokflow chapter.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
53
diff
changeset
|
200 |
.. image:: ../images/main_template_layout.png |
0 | 201 |
|
202 |
Le rectancle contenant le `view.dispatch()` représente l'emplacement où est |
|
203 |
inséré la vue de contenu à afficher. Les autres représentent des sous-templates |
|
204 |
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
|
205 |
templates sont dans le module `cubicweb.web.views.basetemplates`. Vous pouvez |
0 | 206 |
évidemment surcharger l'un des sous-templates pour modifier l'aspect visuel |
207 |
d'une partie désirée de la page. |
|
208 |
||
209 |
On peut également contrôler certains comportements du template principal à |
|
210 |
l'aide des paramètres de formulaire suivante : |
|
211 |
||
212 |
* `__notemplate`, si présente (quelque soit la valeur associée), seule la vue de |
|
213 |
contenu est renvoyée |
|
214 |
* `__force_display`, si présente et contient une valeur non nulle, pas de |
|
215 |
navigation quelque soit le nombre d'entités à afficher |
|
216 |
* `__method`, si le result set à afficher ne contient qu'une entité et que ce |
|
217 |
paramètre est spécifié, celui-ci désigne une méthode à appeler sur l'entité |
|
218 |
en lui donnant en argument le dictionnaire des paramètres de formulaire, avant |
|
219 |
de reprendre le comportement classique (s'insère entre les étapes 1. et |
|
220 |
2. décrites ci-dessus) |
|
221 |
||
222 |
||
223 |
.. include:: sect_stdlib_vues.txt |
|
224 |
||
225 |
||
226 |
Vues xml, binaires... |
|
227 |
--------------------- |
|
228 |
Pour les vues générants autre que du html (une image générée dynamiquement par |
|
229 |
exemple), et qui ne peuvent donc généralement pas être incluse dans la page |
|
230 |
HTML générée par le template principal (voir ci-dessus), il faut : |
|
231 |
||
232 |
* placer l'attribut `templatable` de la classe à `False` |
|
233 |
* indiquer via l'attribut `content_type` de la classe le type MIME généré par la |
|
234 |
vue 'application/octet-stream' |
|
235 |
||
236 |
Pour les vues générants un contenu binaire (une image générée dynamiquement par |
|
237 |
exemple), il faut également placer l'attribut `binary` de la classe à `True` (ce |
|
238 |
qui implique `templatable == False` afin que l'attribut `w` de la vue soit |
|
239 |
remplacé par un flux binaire plutôt que unicode. |
|
240 |
||
241 |
||
242 |
Quelques trucs (X)HTML à respecter |
|
243 |
---------------------------------- |
|
244 |
Certains navigateurs (dont firefox) n'aime pas les `<div>` vides (par vide |
|
245 |
j'entend sans contenu dans la balise, il peut y avoir des attributs), faut |
|
246 |
toujours mettre `<div></div>` même s'il n'y a rien dedans, et non `<div/>`. |