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