167 :alt: graphical view of the schema (aka data-model) |
167 :alt: graphical view of the schema (aka data-model) |
168 |
168 |
169 |
169 |
170 .. _DefineViews: |
170 .. _DefineViews: |
171 |
171 |
172 Define your entities views |
172 Define your entity views |
173 -------------------------- |
173 ------------------------ |
174 |
174 |
175 Each entity defined in a model inherits defaults views allowing |
175 Each entity defined in a model inherits default views allowing |
176 different rendering of the data. You can redefine each of them |
176 different rendering of the data. You can redefine each of them |
177 according to your needs and preferences. If you feel like it then |
177 according to your needs and preferences. So let's see how the |
178 you have to know how a view is defined. |
178 views are defined. |
179 |
179 |
180 |
180 |
181 The views selection principle |
181 The view selection principle |
182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
183 |
183 |
184 A view is defined by a Python class which includes: |
184 A view is defined by a Python class which includes: |
185 |
185 |
186 - an identifier (all objects in `CubicWeb` are entered in a registry |
186 - an identifier (all objects in `CubicWeb` are entered in a registry |
187 and this identifier will be used as a key) |
187 and this identifier will be used as a key) |
188 |
188 |
189 - a filter to select the resulsets it can be applied to |
189 - a filter to select the result sets it can be applied to |
190 |
190 |
191 A view has a set of methods complying |
191 A view has a set of methods complying |
192 with the `View` class interface (`cubicweb.common.view`). |
192 with the `View` class interface (`cubicweb.common.view`). |
193 |
193 |
194 `CubicWeb` provides a lot of standard views for the type |
194 `CubicWeb` provides a lot of standard views for the type `EntityView`; |
195 `EntityView`, for a complete list, you |
195 for a complete list, read the code in directory ``cubicweb/web/views/``. |
196 will have to read the code in directory ``cubicweb/web/views/`` |
|
197 |
196 |
198 A view is applied on a `result set` which contains a set of |
197 A view is applied on a `result set` which contains a set of |
199 entities we are trying to display. `CubicWeb` uses a selector |
198 entities we are trying to display. `CubicWeb` uses a selector |
200 mechanism which computes a score used to identify which view |
199 mechanism which computes for each available view a score: |
201 is the best to apply for the `result set` we are trying to |
200 the view with the highest score is then used to display the given `result set`. |
202 display. The standard library of selectors is in |
201 The standard library of selectors is in |
203 ``cubicweb.common.selector`` and a library of methods used to |
202 ``cubicweb.common.selector`` and a library of methods used to |
204 compute scores is available in ``cubicweb.vregistry.vreq``. |
203 compute scores is available in ``cubicweb.vregistry.vreq``. |
205 |
204 |
206 It is possible to define multiple views for the same identifier |
205 It is possible to define multiple views for the same identifier |
207 and to associate selectors and filters to allow the application |
206 and to associate selectors and filters to allow the application |
208 to find the best way to render the data. We will see more details |
207 to find the best way to render the data. We will see more details |
209 on this in :ref:`DefinitionVues`. |
208 on this in :ref:`ViewDefinition`. |
210 |
209 |
211 For example, the view named ``primary`` is the one used to display |
210 For example, the view named ``primary`` is the one used to display |
212 a single entity. We will now show you hos to customize this view. |
211 a single entity. We will now show you how to customize this view. |
213 |
212 |
214 |
213 |
215 View customization |
214 View customization |
216 ~~~~~~~~~~~~~~~~~~ |
215 ~~~~~~~~~~~~~~~~~~ |
217 |
216 |
218 If you wish to modify the way a `BlogEntry` is rendered, you will have to |
217 If you wish to modify the way a `BlogEntry` is rendered, you will have to |
219 overwrite the `primary` view defined in the module ``views`` of the cube |
218 overwrite the `primary` view defined in the module ``views`` of the cube |
220 ``cubes/blog/views.py``. |
219 ``cubes/blog/views.py``. |
221 |
220 |
222 We can for example add in front of the pulication date a prefix specifying |
221 We can for example add in front of the publication date a prefix specifying |
223 the date we see is the publication date. |
222 that the date we see is the publication date. |
224 |
223 |
225 To do so, please apply the following changes: |
224 To do so, please apply the following changes: |
226 |
225 |
227 :: |
226 :: |
228 |
227 |
261 |
260 |
262 .. image:: images/cbw-update-primary-view.en.png |
261 .. image:: images/cbw-update-primary-view.en.png |
263 :alt: modified primary view |
262 :alt: modified primary view |
264 |
263 |
265 |
264 |
266 The above source code defines a new primary view for |
265 The above source code defines a new primary view for ``BlogEntry``. |
267 ``BlogEntry``. |
266 |
268 |
267 Since views are applied to result sets and result sets can be tables of |
269 Since views are applied to resultsets and resulsets can be tables of |
268 data, we have to recover the entity from its (row,col)-coordinates. |
270 data, it is needed to recover the entity from its (row,col) |
269 We will get to this in more detail later. |
271 coordinates. We will get to this in more detail later. |
|
272 |
270 |
273 The view has a ``self.w()`` method that is used to output data. In our |
271 The view has a ``self.w()`` method that is used to output data. In our |
274 example we use it to output HTML tags and values of the entity's attributes. |
272 example we use it to output HTML tags and values of the entity's attributes. |
|
273 |