doc/tutorials/base/customizing-the-application.rst
author Elodie Thieblin <ethieblin@logilab.fr>
Wed, 18 Mar 2020 09:58:05 +0100
changeset 12924 7a76f1fcc4a3
parent 12923 341de318d6de
child 12925 eb26c593c6f6
permissions -rw-r--r--
[doc] Add command pip install of new cube
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
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
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
    20
cube::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
  cubicweb-ctl newcube myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
12548
6eec3213bec9 [doc] better documentation for "cubicweb-ctl newcube"
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12534
diff changeset
    24
This will create a a directory named :file:`cubicweb-myblog` reflecting the
6eec3213bec9 [doc] better documentation for "cubicweb-ctl newcube"
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12534
diff changeset
    25
structure described in :ref:`cubelayout`.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
12924
7a76f1fcc4a3 [doc] Add command pip install of new cube
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12923
diff changeset
    27
To install your new cube on the virtual environment, run in
7a76f1fcc4a3 [doc] Add command pip install of new cube
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12923
diff changeset
    28
the :file:`cubicweb-myblog` directory::
7a76f1fcc4a3 [doc] Add command pip install of new cube
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12923
diff changeset
    29
7a76f1fcc4a3 [doc] Add command pip install of new cube
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12923
diff changeset
    30
  pip install -e .
7a76f1fcc4a3 [doc] Add command pip install of new cube
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12923
diff changeset
    31
12658
fdd7165fe1f3 [doc] Update documentation to match current Debian packages
Jérémy Bobbio <jeremy.bobbio@irq7.fr>
parents: 12548
diff changeset
    32
All `cubicweb-ctl` commands are described in details in
fdd7165fe1f3 [doc] Update documentation to match current Debian packages
Jérémy Bobbio <jeremy.bobbio@irq7.fr>
parents: 12548
diff changeset
    33
:ref:`cubicweb-ctl`.
fdd7165fe1f3 [doc] Update documentation to match current Debian packages
Jérémy Bobbio <jeremy.bobbio@irq7.fr>
parents: 12548
diff changeset
    34
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    35
.. Note::
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
   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
    38
   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
    39
   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
    40
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    42
Cube metadata
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    43
~~~~~~~~~~~~~
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
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
    46
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
    47
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
    48
file:
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
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
12923
341de318d6de [doc] update cubicweb version
Elodie Thieblin <ethieblin@logilab.fr>
parents: 12658
diff changeset
    52
   __depends__ =  {'cubicweb': '>= 3.27.3',
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
                   'cubicweb-blog': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
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
    56
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    57
.. _TutosBaseCustomizingTheApplicationDataModel:
6835
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
Extending the data model
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
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
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
    63
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
    64
: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
    65
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
Defining our model
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
******************
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
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
    71
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
    72
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
.. sourcecode:: python
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
  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
    76
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
  class Community(EntityType):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
      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
    79
      description = RichString()
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
  class community_blog(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
      subject = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
      object = 'Blog'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
      cardinality = '*?'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
      composite = 'subject'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
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
    88
the schema.
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
This file defines the following:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
12365
be86a3a86af7 [doc] typo, the field is "name" not "title"
Laurent Peuch <cortex@worlddomination.be>
parents: 12364
diff changeset
    92
* 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
    93
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
  - 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
    95
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
  - 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
    97
    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
    98
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    99
* 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
   100
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
  - ``*`` 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
   102
    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
   103
    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
   104
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
  - 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
   106
    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
   107
    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
   108
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
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
   110
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
   111
in this tutorial.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   112
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   113
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
   114
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
   115
which is defining it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
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
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
   119
***********************************************
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
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
   122
`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
   123
changing in the instance.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
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
   126
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   127
  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
   128
  cubicweb-ctl delete myblog
12364
64a72b2c9c27 [doc] fix, command "create" is missing base cube
Laurent Peuch <cortex@worlddomination.be>
parents: 10949
diff changeset
   129
  cubicweb-ctl create myblog myblog
12534
e0e7d8ca051f [doc] replace cubicweb-ctl start by cubicweb-ctl pyramid
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12394
diff changeset
   130
  cubicweb-ctl pyramid -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
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
   133
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
   134
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
   135
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
   136
in the `add_cube` command: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   138
  $ 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
   139
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
  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
   142
  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
   143
  >>> add_cube('myblog')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
  >>>
12534
e0e7d8ca051f [doc] replace cubicweb-ctl start by cubicweb-ctl pyramid
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12394
diff changeset
   145
  $ cubicweb-ctl pyramid -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   147
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
   148
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
   149
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
   150
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
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
   152
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
   153
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
   154
has been done).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
.. image:: ../../images/tutos-base_myblog-schema_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   157
   :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
   158
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
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
   160
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
   161
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
.. image:: ../../images/tutos-base_myblog-siteinfo_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   163
   :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
   164
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   165
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
   166
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
   167
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
   168
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
   169
framework.
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
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
   172
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
   173
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   174
.. _TutosBaseCustomizingTheApplicationCustomViews:
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   175
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
Defining your views
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   179
|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
   180
: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
   181
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
   182
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   183
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
   184
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   185
  - 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
   186
    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
   187
    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
   188
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   189
  - 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
   190
    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
   191
    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
   192
    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
   193
    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
   194
    :mod:`cubicweb.predicates`.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   195
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   196
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
   197
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
   198
specific child class.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   199
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   200
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
   201
of entities.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   202
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   203
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
   204
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
   205
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   206
.. 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
   207
   :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
   208
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   209
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   210
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
   211
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
   212
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
   213
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   214
.. sourcecode: bash
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   215
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
  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
   217
  # 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
   218
  cubicweb-ctl i18ninstance myblog # recompile instance's message catalogs
12534
e0e7d8ca051f [doc] replace cubicweb-ctl start by cubicweb-ctl pyramid
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12394
diff changeset
   219
  # instance has to be restarted to consider new catalogs
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   220
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   221
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
   222
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
   223
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   224
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
   225
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   226
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   227
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
   228
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
   229
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
   230
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
   231
layout.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   232
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   233
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
   234
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
   235
(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
   236
"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
   237
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
   238
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
   239
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   240
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
   241
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
   242
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
   243
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   244
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   245
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   246
  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
   247
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   248
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   249
  class MyHTMLPageFooter(basetemplates.HTMLPageFooter):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   250
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   251
      def footer_content(self):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   252
          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
   253
6835
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
  def registration_callback(vreg):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   256
      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
   257
      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
   258
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   259
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   260
* 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
   261
  but this is not mandatory.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   262
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   263
* 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
   264
  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
   265
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   266
* 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
   267
  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
   268
  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
   269
  :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
   270
  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
   271
  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
   272
  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
   273
  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
   274
  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
   275
  registered blindly.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   276
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   277
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   278
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   279
  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
   280
  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
   281
  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
   282
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   283
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
   284
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
Primary view customization
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
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   289
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
   290
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
   291
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
   292
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
   293
subclass it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   294
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   295
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
   296
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
   297
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
   298
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
   299
details in :ref:`primary_view`).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   300
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   301
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
   302
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
   303
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   304
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   305
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7827
diff changeset
   306
  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
   307
  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
   308
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   309
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   310
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   311
      __select__ = is_instance('Community')
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
      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
   314
          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
   315
          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
   316
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   317
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   318
              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
   319
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   320
What's going on here?
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   321
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   322
* 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
   323
  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
   324
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   325
* 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
   326
  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
   327
  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
   328
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   329
* 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
   330
  `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
   331
  `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
   332
  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
   333
  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
   334
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   335
* 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
   336
  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
   337
  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
   338
  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
   339
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   340
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   341
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
   342
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   343
.. 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
   344
   :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
   345
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   346
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
   347
|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
   348
build higher level layers.
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
.. Note::
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
  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
   353
  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
   354
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   355
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
   356
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   357
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   358
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
   359
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   360
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   361
|cubicweb| provides an ORM to easily programmaticaly manipulate
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   362
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
   363
`get_entity` on a result set). By default, entity
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   364
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
   365
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
   366
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
   367
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   368
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
   369
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
   370
entity:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   371
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   372
- 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
   373
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   374
- 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
   375
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   376
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
   377
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
   378
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   379
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   380
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   381
    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
   382
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   383
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   384
    class Community(AnyEntity):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   385
        """customized class for Community entities"""
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   386
        __regid__ = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   387
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
   388
        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
   389
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   390
        def dc_title(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   391
            return self.name
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
        def display_cw_logo(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   394
            return 'CubicWeb' in self.description
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   395
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   396
In this example:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   397
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   398
* 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
   399
  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
   400
  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
   401
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   402
* 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
   403
  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
   404
  have had the same result)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   405
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   406
* 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
   407
  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
   408
  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
   409
  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
   410
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   411
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   412
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   413
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   414
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   415
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   416
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   417
      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
   418
          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
   419
          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
   420
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   421
          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
   422
              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
   423
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   424
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   425
              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
   426
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   427
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
   428
logo in front of it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   429
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   430
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   431
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   432
  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
   433
  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
   434
  automatically reloaded.
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
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   437
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
   438
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   439
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   440
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
   441
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
   442
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
   443
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
   444
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
   445
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
   446
template language provides.
6835
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
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   449
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
   450
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
   451
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
   452
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
   453
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
   454
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
   455
file:
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
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   458
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   459
   __depends__ =  {'cubicweb': '>= 3.10.7',
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   460
                   'cubicweb-blog': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   461
                   'cubicweb-comment': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   462
                   'cubicweb-tag': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   463
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   464
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
   465
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
   466
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
   467
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   468
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   469
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   470
  class comments(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   471
      subject = 'Comment'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   472
      object = 'BlogEntry'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   473
      cardinality = '1*'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   474
      composite = 'object'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   475
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   476
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   477
  class tags(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   478
      subject = 'Tag'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   479
      object = ('Community', 'BlogEntry')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   480
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   481
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   482
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
   483
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
   484
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
   485
supported.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   486
12366
72ba5f5278e0 [doc] serie a small improvments
Laurent Peuch <cortex@worlddomination.be>
parents: 12365
diff changeset
   487
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
   488
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
  $ cubicweb-ctl stop myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   491
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   492
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   493
  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
   494
  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
   495
  >>> add_cubes(('comment', 'tag'))
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   496
  >>>
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
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
   499
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   500
.. 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
   501
   :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
   502
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   503
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
   504
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
   505
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
   506
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
   507
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
   508
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
   509
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
   510
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
   511
implementation.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   512
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
   513
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
   514
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   515
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   516
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   517
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   518
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   519
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   520
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   521
      def render_entity_title(self, entity):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   522
          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
   523
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   524
      def render_entity_attributes(self, entity):
12367
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   525
          if entity.display_cw_logo():
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   526
              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
   527
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   528
          if entity.description:
77342fae06fb [doc] respect pep8 in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 12366
diff changeset
   529
              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
   530
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   531
It appears now properly:
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
.. 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
   534
   :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
   535
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   536
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
   537
piece. Really.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   538
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
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   541
.. _`CubicWeb Forge`: http://www.cubicweb.org/project