doc/tutorials/base/customizing-the-application.rst
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 21 Mar 2019 14:33:54 +0100 (2019-03-21)
changeset 12530 9d88e1177c35
parent 12394 e847b5d1ffff
child 12534 e0e7d8ca051f
permissions -rw-r--r--
Remove Twisted web server Twisted web server is not used anymore and has been superseded by pyramid many years ago. Furthermore, our usage is not compatible with Python 3. So we drop the "etwist" sub-package. As a consequence, "all-in-one" configuration type gets dropped as it was Twisted-specific. We resurrect it in cubicweb/pyramid/config.py by only keeping options used by the "pyramid". Similarly, we introduce a AllInOneCreateHandler in cubicweb/pyramid/pyramidctl.py that is basically the one that lived in cubicweb/etwist/twctl.py and is used to create the "all-in-one" instance. Added a TODO here about "pyramid.ini" that could be generated at the end of bootstrap() method. In cubicweb/devtools/httptest.py, CubicWebServerTC is now equivalent to CubicWebWsgiTC and the latter is dropped.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
.. _TutosBaseCustomizingTheApplication:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
Customizing your application
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
----------------------------
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
So far so good. The point is that usually, you won't get enough by assembling
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
     9
cubes out-of-the-box. You will want to customize them, have a personal look and
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
feel, add your own data model and so on. Or maybe start from scratch?
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
So let's get a bit deeper and start coding our own cube. In our case, we want
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
to customize the blog we created to add more features to it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
Create your own cube
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    19
First, notice that if you've installed |cubicweb| using Debian packages, you will
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
need the additional ``cubicweb-dev`` package to get the commands necessary to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
|cubicweb| development. All `cubicweb-ctl` commands are described in details in
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
:ref:`cubicweb-ctl`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
Once your |cubicweb| development environment is set up, you can create a new
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
cube::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
  cubicweb-ctl newcube myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
9917
a98e60464c26 [book] stop talking about the hg `forest` extension
Julien Cristau <julien.cristau@logilab.fr>
parents: 8658
diff changeset
    29
This will create in the cubes directory (:file:`/path/to/grshell/cubes` for source
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    30
installation, :file:`/usr/share/cubicweb/cubes` for Debian packages installation)
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
a directory named :file:`blog` reflecting the structure described in
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
:ref:`cubelayout`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
For packages installation, you can still create new cubes in your home directory
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
using the following configuration. Let's say you want to develop your new cubes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
in `~src/cubes`, then set the following environment variables: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
  CW_CUBES_PATH=~/src/cubes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
and then create your new cube using: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
  cubicweb-ctl newcube --directory=~/src/cubes myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    44
.. Note::
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
   We previously used `myblog` as the name of our *instance*. We're now creating
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
   a *cube* with the same name. Both are different things. We'll now try to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
   specify when we talk about one or another, but keep in mind this difference.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    51
Cube metadata
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    52
~~~~~~~~~~~~~
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
A simple set of metadata about your cube are stored in the :file:`__pkginfo__.py`
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
file. In our case, we want to extend the blog cube, so we have to tell that our
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
cube depends on this cube, by modifying the ``__depends__`` dictionary in that
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
file:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
   __depends__ =  {'cubicweb': '>= 3.10.7',
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
                   'cubicweb-blog': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
where the ``None`` means we do not depends on a particular version of the cube.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    66
.. _TutosBaseCustomizingTheApplicationDataModel:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
Extending the data model
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
The data model or schema is the core of your |cubicweb| application.  It defines
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
the type of content your application will handle. It is defined in the file
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    73
:file:`schema.py` of the cube.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
Defining our model
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
******************
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
For the sake of example, let's say we want a new entity type named `Community`
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
with a name, a description. A `Community` will hold several blogs.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
  from yams.buildobjs import EntityType, RelationDefinition, String, RichString
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
  class Community(EntityType):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
      name = String(maxsize=50, required=True)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
      description = RichString()
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
  class community_blog(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
      subject = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
      object = 'Blog'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
      cardinality = '*?'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
      composite = 'subject'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
The first step is the import from the :mod:`yams` package necessary classes to build
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
the schema.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
This file defines the following:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
12365
be86a3a86af7 [doc] typo, the field is "name" not "title"
Laurent Peuch <cortex@worlddomination.be>
parents: 12364
diff changeset
   101
* a `Community` has a name and a description as attributes
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
  - the name is a string that is required and can't be longer than 50 characters
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
  - the description is a string that is not constrained and may contains rich
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   106
    content such as HTML or Restructured text.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   108
* a `Community` may be linked to a `Blog` using the `community_blog` relation
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   110
  - ``*`` means a community may be linked to 0 to N blog, ``?`` means a blog may
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   111
    be linked to 0 to 1 community. For completeness, you can also use ``+`` for
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   112
    1 to N, and ``1`` for single, mandatory relation (e.g. one to one);
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   113
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   114
  - this is a composite relation where `Community` (e.g. the subject of the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   115
    relation) is the composite. That means that if you delete a community, its
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
    blog will be deleted as well.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
Of course, there are a lot of other data types and things such as constraints,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   119
permissions, etc, that may be defined in the schema, but those won't be covered
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
in this tutorial.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   122
Notice that our schema refers to the `Blog` entity type which is not defined
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   123
here.  But we know this type is available since we depend on the `blog` cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
which is defining it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
Applying changes to the model into our instance
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
***********************************************
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
Now the problem is that we created an instance using the `blog` cube, not our
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
`myblog` cube, so if we don't do anything there is no way that we'll see anything
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
changing in the instance.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   133
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
One easy way, as we've no really valuable data in the instance would be to trash and recreated it::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   136
  cubicweb-ctl stop myblog # or Ctrl-C in the terminal running the server in debug mode
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
  cubicweb-ctl delete myblog
12364
64a72b2c9c27 [doc] fix, command "create" is missing base cube
Laurent Peuch <cortex@worlddomination.be>
parents: 10949
diff changeset
   138
  cubicweb-ctl create myblog myblog
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   139
  cubicweb-ctl start -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
Another way is to add our cube to the instance using the cubicweb-ctl shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
facility. It's a python shell connected to the instance with some special
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
commands available to manipulate it (the same as you'll have in migration
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
scripts, which are not covered in this tutorial). In that case, we're interested
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
in the `add_cube` command: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   147
  $ cubicweb-ctl stop myblog # or Ctrl-C in the terminal running the server in debug mode
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
  just type migration commands or arbitrary python code and type ENTER to execute it
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
  type "exit" or Ctrl-D to quit the shell and resume operation
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   152
  >>> add_cube('myblog')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
  >>>
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   154
  $ cubicweb-ctl start -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   156
The `add_cube` command is enough since it automatically updates our
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   157
application to the cube's schema. There are plenty of other migration
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   158
commands of a more finer grain. They are described in :ref:`migration`
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
As explained, leave the shell by typing Ctrl-D. If you restart the instance and
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
take another look at the schema, you'll see that changes to the data model have
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
actually been applied (meaning database schema updates and all necessary stuff
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
has been done).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
.. image:: ../../images/tutos-base_myblog-schema_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   166
   :alt: the instance schema after adding our cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
If you follow the 'info' link in the user pop-up menu, you'll also see that the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   169
instance is using blog and myblog cubes.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
.. image:: ../../images/tutos-base_myblog-siteinfo_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   172
   :alt: the instance schema after adding our cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   174
You can now add some communities, link them to blog, etc... You'll see that the
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   175
framework provides default views for this entity type (we have not yet defined any
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   176
view for it!), and also that the blog primary view will show the community it's
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
linked to if any. All this thanks to the model driven interface provided by the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
framework.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   180
You'll then be able to redefine each of them according to your needs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   181
and preferences. We'll now see how to do such thing.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   182
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   183
.. _TutosBaseCustomizingTheApplicationCustomViews:
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   184
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   185
Defining your views
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   186
~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   187
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   188
|cubicweb| provides a lot of standard views in the directory
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   189
:file:`cubicweb/web/views/`. We already talked about 'primary' and 'list' views,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   190
which are views which apply to one ore more entities.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   191
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   192
A view is defined by a python class which includes:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   193
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   194
  - an identifier: all objects used to build the user interface in |cubicweb| are
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   195
    recorded in a registry and this identifier will be used as a key in that
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   196
    registry to store the view. There may be multiple views for the same identifier.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   197
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   198
  - a *selector*, which is a kind of filter telling how well a view suit to a
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   199
    particular context. When looking for a particular view (e.g. given an
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   200
    identifier), |cubicweb| computes for each available view with that identifier
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   201
    a score which is returned by the selector. Then the view with the highest
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7827
diff changeset
   202
    score is used. The standard library of predicates is in
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7827
diff changeset
   203
    :mod:`cubicweb.predicates`.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   204
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   205
A view has a set of methods inherited from the :class:`cubicweb.view.View` class,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   206
though you usually don't derive directly from this class but from one of its more
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   207
specific child class.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   208
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   209
Last but not least, |cubicweb| provides a set of default views accepting any kind
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   210
of entities.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   211
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   212
Want a proof? Create a community as you've already done for other entity types
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   213
through the index page, you'll then see something like that:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   214
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   215
.. image:: ../../images/tutos-base_myblog-community-default-primary_en.png
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
   :alt: the default primary view for our community entity type
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   217
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   218
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   219
If you notice the weird messages that appear in the page: those are messages
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   220
generated for the new data model, which have no translation yet. To fix that,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   221
we'll have to use dedicated `cubicweb-ctl` commands:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   222
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   223
.. sourcecode: bash
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   224
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   225
  cubicweb-ctl i18ncube myblog # build/update cube's message catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   226
  # then add translation into .po file into the cube's i18n directory
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   227
  cubicweb-ctl i18ninstance myblog # recompile instance's message catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   228
  cubicweb-ctl restart -D myblog # instance has to be restarted to consider new catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   229
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   230
You'll then be able to redefine each of them according to your needs and
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   231
preferences. So let's see how to do such thing.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   232
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   233
Changing the layout of the application
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   234
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   235
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   236
The layout is the general organization of the pages in the site. Views that generate
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   237
the layout are sometimes referred to as 'templates'. They are implemented in the
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   238
framework in the module :mod:`cubicweb.web.views.basetemplates`. By overriding
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   239
classes in this module, you can customize whatever part you wish of the default
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   240
layout.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   241
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   242
But notice that |cubicweb| provides many other ways to customize the
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   243
interface, thanks to actions and components (which you can individually
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   244
(de)activate, control their location, customize their look...) as well as
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   245
"simple" CSS customization. You should first try to achieve your goal using such
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   246
fine grained parametrization rather then overriding a whole template, which usually
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   247
embeds customisation access points that you may loose in the process.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   248
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   249
But for the sake of example, let's say we want to change the generic page
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   250
footer...  We can simply add to the module ``views`` of our cube,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   251
e.g. :file:`cubes/myblog/views.py`, the code below:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   252
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   253
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   254
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   255
  from cubicweb.web.views import basetemplates
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   256
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   257
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   258
  class MyHTMLPageFooter(basetemplates.HTMLPageFooter):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   259
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   260
      def footer_content(self):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   261
          self.w(u'This website has been created with <a href="http://cubicweb.org">CubicWeb</a>.')
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   262
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   263
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   264
  def registration_callback(vreg):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   265
      vreg.register_all(globals().values(), __name__, (MyHTMLPageFooter,))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   266
      vreg.register_and_replace(MyHTMLPageFooter, basetemplates.HTMLPageFooter)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   267
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   268
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   269
* Our class inherits from the default page footer to ease getting things right,
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   270
  but this is not mandatory.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   271
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   272
* When we want to write something to the output stream, we simply call `self.w`,
10222
75d6096216d7 [docstrings] fix project-wide English language mistake
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 9917
diff changeset
   273
  which *must be passed a unicode string*.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   274
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   275
* The latest function is the most exotic stuff. The point is that without it, you
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   276
  would get an error at display time because the framework wouldn't be able to
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   277
  choose which footer to use between :class:`HTMLPageFooter` and
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   278
  :class:`MyHTMLPageFooter`, since both have the same selector, hence the same
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   279
  score...  In this case, we want our footer to replace the default one, so we have
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   280
  to define a :func:`registration_callback` function to control object
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   281
  registration: the first instruction tells to register everything in the module
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   282
  but the :class:`MyHTMLPageFooter` class, then the second to register it instead
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   283
  of :class:`HTMLPageFooter`. Without this function, everything in the module is
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   284
  registered blindly.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   285
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   286
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   287
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   288
  When a view is modified while running in debug mode, it is not required to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   289
  restart the instance server. Save the Python file and reload the page in your
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   290
  web browser to view the changes.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   291
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   292
We will now have this simple footer on every page of the site.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   293
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   294
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   295
Primary view customization
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   296
~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   297
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   298
The 'primary' view (i.e. any view with the identifier set to 'primary') is the one used to
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   299
display all the information about a single entity. The standard primary view is one
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   300
of the most sophisticated views of all. It has several customisation points, but
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   301
its power comes with `uicfg`, allowing you to control it without having to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   302
subclass it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   303
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   304
However this is a bit off-topic for this first tutorial. Let's say we simply want a
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   305
custom primary view for my `Community` entity type, using directly the view
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   306
interface without trying to benefit from the default implementation (you should
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   307
do that though if you're rewriting reusable cubes; everything is described in more
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   308
details in :ref:`primary_view`).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   309
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   310
12394
e847b5d1ffff [doc] always put file name for code example and uses :file:`path` syntax everywhere
Laurent Peuch <cortex@worlddomination.be>
parents: 12369
diff changeset
   311
So... Some code! That we'll put again in the module ``views`` (:file:`myblog/views.py`) of our cube.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   312
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   313
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   314
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7827
diff changeset
   315
  from cubicweb.predicates import is_instance
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   316
  from cubicweb.web.views import primary
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   317
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   318
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   319
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   320
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   321
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   322
      def cell_call(self, row, col):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   323
          entity = self.cw_rset.get_entity(row, col)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   324
          self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   325
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   326
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   327
              self.w(u'<p>%s</p>' % entity.printable_value('description'))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   328
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   329
What's going on here?
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   330
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   331
* Our class inherits from the default primary view, here mainly to get the correct
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   332
  view identifier, since we don't use any of its features.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   333
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   334
* We set on it a selector telling that it only applies when trying to display
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   335
  some entity of the `Community` type. This is enough to get an higher score than
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   336
  the default view for entities of this type.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   337
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   338
* A view that applies to an entity usually have to define the method
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   339
  `cell_call` as an entry point. This method will received the arguments
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   340
  `row` and `col` that tell to which entity in the result set the view is
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   341
  applied. We can then get this entity from the result set (`self.cw_rset`) by
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   342
  using the `get_entity` method.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   343
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   344
* To ease thing, we access our entity's attribute for display using its
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   345
  printable_value method, which will handle formatting and escaping when
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   346
  necessary. As you can see, you can also access attributes by their name on the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   347
  entity to get the raw value.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   348
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   349
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   350
You can now reload the page of the community we just created and see the changes.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   351
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   352
.. image:: ../../images/tutos-base_myblog-community-custom-primary_en.png
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   353
   :alt: the custom primary view for our community entity type
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   354
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   355
We've seen here a lot of thing you'll have to deal with to write views in
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   356
|cubicweb|. The good news is that this is almost everything that is used to
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   357
build higher level layers.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   358
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   359
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   360
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   361
  As things get complicated and the volume of code in your cube increases, you can
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   362
  of course still split your views module into a python package with subpackages.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   363
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   364
You can find more details about views and selectors in :ref:`Views`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   365
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   366
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   367
Write entities to add logic in your data
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   368
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   369
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   370
|cubicweb| provides an ORM to easily programmaticaly manipulate
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   371
entities (just like the one we have fetched earlier by calling
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   372
`get_entity` on a result set). By default, entity
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   373
types are instances of the :class:`AnyEntity` class, which holds a set of
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   374
predefined methods as well as property automatically generated for
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   375
attributes/relations of the type it represents.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   376
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   377
You can redefine each entity to provide additional methods or whatever you want
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   378
to help you write your application. Customizing an entity requires that your
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   379
entity:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   380
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   381
- inherits from :class:`cubicweb.entities.AnyEntity` or any subclass
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   382
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   383
- defines a :attr:`__regid__` linked to the corresponding data type of your schema
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   384
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   385
You may then want to add your own methods, override default implementation of some
12394
e847b5d1ffff [doc] always put file name for code example and uses :file:`path` syntax everywhere
Laurent Peuch <cortex@worlddomination.be>
parents: 12369
diff changeset
   386
method, etc... To do so, write this code in :file:`myblog/entities.py`:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   387
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   388
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   389
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   390
    from cubicweb.entities import AnyEntity, fetch_config
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   391
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   392
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   393
    class Community(AnyEntity):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   394
        """customized class for Community entities"""
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   395
        __regid__ = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   396
7827
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6880
diff changeset
   397
        fetch_attrs, cw_fetch_order = fetch_config(['name'])
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   398
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   399
        def dc_title(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   400
            return self.name
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   401
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   402
        def display_cw_logo(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   403
            return 'CubicWeb' in self.description
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   404
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   405
In this example:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   406
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   407
* we used convenience :func:`fetch_config` function to tell which attributes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   408
  should be prefetched by the ORM when looking for some related entities of this
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   409
  type, and how they should be ordered
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   410
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   411
* we overrode the standard `dc_title` method, used in various place in the interface
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   412
  to display the entity (though in this case the default implementation would
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   413
  have had the same result)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   414
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   415
* we implemented here a method :meth:`display_cw_logo` which tests if the blog
12369
f83ebc68f02e [doc] typo, 'CW' doesn't exist in the example
Laurent Peuch <cortex@worlddomination.be>
parents: 12368
diff changeset
   416
  entry title contains 'CubicWeb'. It can then be used when you're writing code
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   417
  involving 'Community' entities in your views, hooks, etc. For instance, you can
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   418
  modify your previous views as follows:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   419
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   420
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   421
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   422
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   423
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   424
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   425
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   426
      def cell_call(self, row, col):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   427
          entity = self.cw_rset.get_entity(row, col)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   428
          self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   429
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   430
          if entity.display_cw_logo():
10949
408867c79d9e [doc] Minor changes to the blog tutorial
Jean-Pierre Vergnes <jean-pierre.vergnes@logilab.fr>
parents: 10491
diff changeset
   431
              self.w(u'<img src="https://docs.cubicweb.org/_static/logo-cubicweb-small.svg"/>')
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   432
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   433
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   434
              self.w(u'<p>%s</p>' % entity.printable_value('description'))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   435
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   436
Then each community whose description contains 'CW' is shown with the |cubicweb|
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   437
logo in front of it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   438
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   439
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   440
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   441
  As for view, you don't have to restart your instance when modifying some entity
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   442
  classes while your server is running in debug mode, the code will be
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   443
  automatically reloaded.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   444
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   445
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   446
Extending the application by using more cubes!
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   447
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   448
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   449
One of the goal of the |cubicweb| framework was to have truly reusable
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   450
components. To do so, they must both behave nicely when plugged into the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   451
application and be easily customisable, from the data model to the user
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   452
interface. And I think the result is pretty successful, thanks to system such as
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   453
the selection mechanism and the choice to write views as python code which allows
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   454
to build our page using true object oriented programming techniques, that no
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   455
template language provides.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   456
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   457
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   458
A library of standard cubes is available from `CubicWeb Forge`_, to address a
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   459
lot of common concerns such has manipulating files, people, things to do, etc. In
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   460
our community blog case, we could be interested for instance in functionalities
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   461
provided by the `comment` and `tag` cubes. The former provides threaded
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   462
discussion functionalities, the latter a simple tag mechanism to classify content.
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   463
Let's say we want to try those. We will first modify our cube's :file:`__pkginfo__.py`
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   464
file:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   465
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   466
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   467
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   468
   __depends__ =  {'cubicweb': '>= 3.10.7',
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   469
                   'cubicweb-blog': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   470
                   'cubicweb-comment': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   471
                   'cubicweb-tag': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   472
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   473
Now, we'll simply tell on which entity types we want to activate the 'comment'
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   474
and 'tag' facilities by adding respectively the 'comments' and 'tags' relations on
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   475
them in our schema (:file:`schema.py`).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   476
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   477
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   478
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   479
  class comments(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   480
      subject = 'Comment'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   481
      object = 'BlogEntry'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   482
      cardinality = '1*'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   483
      composite = 'object'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   484
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   485
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   486
  class tags(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   487
      subject = 'Tag'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   488
      object = ('Community', 'BlogEntry')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   489
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   490
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   491
So in the case above we activated comments on `BlogEntry` entities and tags on
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   492
both `Community` and `BlogEntry`. Various views from both `comment` and `tag`
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   493
cubes will then be automatically displayed when one of those relations is
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   494
supported.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   495
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   496
Let's install the cubes and synchronize the data model as we've done earlier: ::
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   497
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   498
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   499
  $ cubicweb-ctl stop myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   500
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   501
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   502
  just type migration commands or arbitrary python code and type ENTER to execute it
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   503
  type "exit" or Ctrl-D to quit the shell and resume operation
8658
11707295d4e6 [doc/book/] Corrected typo in en/tutorials/base/customizing-the-application.rst, for the add_cubes function call
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 8190
diff changeset
   504
  >>> add_cubes(('comment', 'tag'))
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   505
  >>>
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   506
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   507
Then restart the instance. Let's look at a blog entry:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   508
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   509
.. image:: ../../images/tutos-base_myblog-blogentry-taggable-commentable-primary_en.png
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   510
   :alt: the primary view for a blog entry with comments and tags activated
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   511
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   512
As you can see, we now have a box displaying tags and a section proposing to add
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   513
a comment and displaying existing one below the post. All this without changing
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   514
anything in our views, thanks to the design of generic views provided by the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   515
framework. Though if we take a look at a community, we won't see the tags box!
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   516
That's because by default this box try to locate itself in the left column within
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   517
the white frame, and this column is handled by the primary view we
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   518
hijacked. Let's change our view to make it more extensible, by keeping both our
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   519
custom rendering but also extension points provided by the default
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   520
implementation.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   521
12394
e847b5d1ffff [doc] always put file name for code example and uses :file:`path` syntax everywhere
Laurent Peuch <cortex@worlddomination.be>
parents: 12369
diff changeset
   522
In :file:`myblog/views.py`:
12368
feb60b438c1b [doc] always indicate the file in which the code should be put
Laurent Peuch <cortex@worlddomination.be>
parents: 12367
diff changeset
   523
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   524
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   525
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   526
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   527
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   528
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   529
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   530
      def render_entity_title(self, entity):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   531
          self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   532
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   533
      def render_entity_attributes(self, entity):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   534
          if entity.display_cw_logo():
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   535
              self.w(u'<img src="https://docs.cubicweb.org/_static/logo-cubicweb-small.svg"/>')
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   536
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   537
          if entity.description:
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   538
              self.w(u'<p>%s</p>' % entity.printable_value('description'))
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   539
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   540
It appears now properly:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   541
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   542
.. image:: ../../images/tutos-base_myblog-community-taggable-primary_en.png
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   543
   :alt: the custom primary view for a community entry with tags activated
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   544
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   545
You can control part of the interface independently from each others, piece by
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   546
piece. Really.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   547
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   548
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   549
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   550
.. _`CubicWeb Forge`: http://www.cubicweb.org/project