doc/book/en/tutorials/base/customizing-the-application.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 28 Jun 2011 17:59:31 +0200
changeset 7570 648bf83945a5
parent 6880 4be32427b2b9
child 7827 9bbf83f68bcc
permissions -rw-r--r--
[etwist] log missing file even in debug mode
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
.. _TutosBaseCustomizingTheApplication:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
Customizing your application
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
----------------------------
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
So far so good. The point is that usually, you won't get enough by assembling
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
     9
cubes out-of-the-box. You will want to customize them, have a personal look and
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
feel, add your own data model and so on. Or maybe start from scratch?
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
So let's get a bit deeper and start coding our own cube. In our case, we want
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
to customize the blog we created to add more features to it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
Create your own cube
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    19
First, notice that if you've installed |cubicweb| using Debian packages, you will
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
need the additional ``cubicweb-dev`` package to get the commands necessary to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
|cubicweb| development. All `cubicweb-ctl` commands are described in details in
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
:ref:`cubicweb-ctl`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
Once your |cubicweb| development environment is set up, you can create a new
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
cube::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
  cubicweb-ctl newcube myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
This will create in the cubes directory (:file:`/path/to/forest/cubes` for source
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    30
installation, :file:`/usr/share/cubicweb/cubes` for Debian packages installation)
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
a directory named :file:`blog` reflecting the structure described in
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
:ref:`cubelayout`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
For packages installation, you can still create new cubes in your home directory
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
using the following configuration. Let's say you want to develop your new cubes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
in `~src/cubes`, then set the following environment variables: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
  CW_CUBES_PATH=~/src/cubes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
and then create your new cube using: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
  cubicweb-ctl newcube --directory=~/src/cubes myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    44
.. Note::
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
   We previously used `myblog` as the name of our *instance*. We're now creating
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
   a *cube* with the same name. Both are different things. We'll now try to
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
   specify when we talk about one or another, but keep in mind this difference.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    51
Cube metadata
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    52
~~~~~~~~~~~~~
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
A simple set of metadata about your cube are stored in the :file:`__pkginfo__.py`
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
file. In our case, we want to extend the blog cube, so we have to tell that our
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
cube depends on this cube, by modifying the ``__depends__`` dictionary in that
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
file:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
   __depends__ =  {'cubicweb': '>= 3.10.7',
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
                   'cubicweb-blog': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
where the ``None`` means we do not depends on a particular version of the cube.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
    66
.. _TutosBaseCustomizingTheApplicationDataModel:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
Extending the data model
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
The data model or schema is the core of your |cubicweb| application.  It defines
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
the type of content your application will handle. It is defined in the file
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
    73
:file:`schema.py` of the cube.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
Defining our model
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
******************
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
For the sake of example, let's say we want a new entity type named `Community`
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
with a name, a description. A `Community` will hold several blogs.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
  from yams.buildobjs import EntityType, RelationDefinition, String, RichString
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
  class Community(EntityType):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
      name = String(maxsize=50, required=True)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
      description = RichString()
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
  class community_blog(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
      subject = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
      object = 'Blog'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
      cardinality = '*?'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
      composite = 'subject'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
The first step is the import from the :mod:`yams` package necessary classes to build
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
the schema.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
This file defines the following:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
* a `Community` has a title and a description as attributes
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
  - the name is a string that is required and can't be longer than 50 characters
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
  - the description is a string that is not constrained and may contains rich
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   106
    content such as HTML or Restructured text.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   108
* a `Community` may be linked to a `Blog` using the `community_blog` relation
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   110
  - ``*`` means a community may be linked to 0 to N blog, ``?`` means a blog may
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   111
    be linked to 0 to 1 community. For completeness, remember that you can also
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   112
    use ``+`` for 1 to N, and ``1`` for single, mandatory relation (e.g. one to one);
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   113
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   114
  - this is a composite relation where `Community` (e.g. the subject of the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   115
    relation) is the composite. That means that if you delete a community, its
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
    blog will be deleted as well.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
Of course, there are a lot of other data types and things such as constraints,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   119
permissions, etc, that may be defined in the schema, but those won't be covered
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
in this tutorial.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   122
Notice that our schema refers to the `Blog` entity type which is not defined
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   123
here.  But we know this type is available since we depend on the `blog` cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
which is defining it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
Applying changes to the model into our instance
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
***********************************************
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
Now the problem is that we created an instance using the `blog` cube, not our
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
`myblog` cube, so if we don't do anything there is no way that we'll see anything
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
changing in the instance.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   133
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
One easy way, as we've no really valuable data in the instance would be to trash and recreated it::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   136
  cubicweb-ctl stop myblog # or Ctrl-C in the terminal running the server in debug mode
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
  cubicweb-ctl delete myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
  cubicweb-ctl create myblog
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   139
  cubicweb-ctl start -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
Another way is to add our cube to the instance using the cubicweb-ctl shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
facility. It's a python shell connected to the instance with some special
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
commands available to manipulate it (the same as you'll have in migration
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
scripts, which are not covered in this tutorial). In that case, we're interested
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
in the `add_cube` command: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   147
  $ cubicweb-ctl stop myblog # or Ctrl-C in the terminal running the server in debug mode
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
  just type migration commands or arbitrary python code and type ENTER to execute it
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
  type "exit" or Ctrl-D to quit the shell and resume operation
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   152
  >>> add_cube('myblog')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
  >>>
6839
829b068eb9c7 [tutorial] make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6837
diff changeset
   154
  $ cubicweb-ctl start -D myblog
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   156
The `add_cube` command is enough since it automatically updates our
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   157
application to the cube's schema. There are plenty of other migration
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   158
commands of a more finer grain. They are described in :ref:`migration`
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
As explained, leave the shell by typing Ctrl-D. If you restart the instance and
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
take another look at the schema, you'll see that changes to the data model have
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
actually been applied (meaning database schema updates and all necessary stuff
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
has been done).
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
.. image:: ../../images/tutos-base_myblog-schema_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   166
   :alt: the instance schema after adding our cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
If you follow the 'info' link in the user pop-up menu, you'll also see that the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   169
instance is using blog and myblog cubes.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
.. image:: ../../images/tutos-base_myblog-siteinfo_en.png
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   172
   :alt: the instance schema after adding our cube
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   174
You can now add some communities, link them to blog, etc... You'll see that the
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   175
framework provides default views for this entity type (we have not yet defined any
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   176
view for it!), and also that the blog primary view will show the community it's
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
linked to if any. All this thanks to the model driven interface provided by the
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
framework.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   180
You'll then be able to redefine each of them according to your needs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   181
and preferences. We'll now see how to do such thing.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   182
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   183
.. _TutosBaseCustomizingTheApplicationCustomViews:
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6839
diff changeset
   184
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   185
Defining your views
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   186
~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   187
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   188
|cubicweb| provides a lot of standard views in directory
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   189
:file:`cubicweb/web/views/`. We already talked about 'primary' and 'list' views,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   190
which are views which apply to one ore more entities.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   191
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   192
A view is defined by a python class which includes:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   193
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   194
  - an identifier: all objects used to build the user interface in |cubicweb| are
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   195
    recorded in a registry and this identifier will be used as a key in that
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   196
    registry. There may be multiple views for the same identifier.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   197
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   198
  - a *selector*, which is a kind of filter telling how well a view suit to a
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   199
    particular context. When looking for a particular view (e.g. given an
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   200
    identifier), |cubicweb| computes for each available view with that identifier
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   201
    a score which is returned by the selector. Then the view with the highest
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   202
    score is used. The standard library of selectors is in
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   203
    :mod:`cubicweb.selector`.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   204
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   205
A view has a set of methods inherited from the :class:`cubicweb.view.View` class,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   206
though you usually don't derive directly from this class but from one of its more
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   207
specific child class.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   208
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   209
Last but not least, |cubicweb| provides a set of default views accepting any kind
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   210
of entities.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   211
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   212
Want a proof? Create a community as you've already done for other entity types
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   213
through the index page, you'll then see something like that:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   214
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   215
.. image:: ../../images/tutos-base_myblog-community-default-primary_en.png
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
   :alt: the default primary view for our community entity type
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   217
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   218
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   219
If you notice the weird messages that appear in the page: those are messages
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   220
generated for the new data model, which have no translation yet. To fix that,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   221
we'll have to use dedicated `cubicweb-ctl` commands:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   222
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   223
.. sourcecode: bash
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   224
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   225
  cubicweb-ctl i18ncube myblog # build/update cube's message catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   226
  # then add translation into .po file into the cube's i18n directory
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   227
  cubicweb-ctl i18ninstance myblog # recompile instance's message catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   228
  cubicweb-ctl restart -D myblog # instance has to be restarted to consider new catalogs
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   229
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   230
You'll then be able to redefine each of them according to your needs and
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   231
preferences. So let's see how to do such thing.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   232
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   233
Changing the layout of the application
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   234
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   235
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   236
The layout is the general organization of the pages in the site. Views that generate
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   237
the layout are sometimes referred to as 'templates'. They are implemented in the
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   238
framework in the module :mod:`cubicweb.web.views.basetemplates`. By overriding
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   239
classes in this module, you can customize whatever part you wish of the default
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   240
layout.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   241
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   242
But notice that |cubicweb| provides many other ways to customize the
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   243
interface, thanks to actions and components (which you can individually
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   244
(de)activate, control their location, customize their look...) as well as
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   245
"simple" CSS customization. You should first try to achieve your goal using such
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   246
fine grained parametrization rather then overriding a whole template, which usually
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   247
embeds customisation access points that you may loose in the process.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   248
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   249
But for the sake of example, let's say we want to change the generic page
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   250
footer...  We can simply add to the module ``views`` of our cube,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   251
e.g. :file:`cubes/myblog/views.py`, the code below:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   252
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   253
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   254
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   255
  from cubicweb.web.views import basetemplates
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   256
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   257
  class MyHTMLPageFooter(basetemplates.HTMLPageFooter):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   258
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   259
      def footer_content(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   260
	  self.w(u'This website has been created with <a href="http://cubicweb.org">CubicWeb</a>.')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   261
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   262
  def registration_callback(vreg):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   263
      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
   264
      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
   265
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   266
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   267
* 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
   268
  but this is not mandatory.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   269
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   270
* When we want to write something to the output stream, we simply call `self.w`,
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   271
  with *must be passed an unicode string*.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   272
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   273
* 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
   274
  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
   275
  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
   276
  :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
   277
  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
   278
  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
   279
  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
   280
  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
   281
  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
   282
  registered blindly.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   283
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   284
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   285
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   286
  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
   287
  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
   288
  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
   289
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   290
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
   291
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   292
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   293
Primary view customization
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   294
~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   295
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   296
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
   297
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
   298
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
   299
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
   300
subclass it.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   301
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   302
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
   303
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
   304
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
   305
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
   306
details in :ref:`primary_view`).
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
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   309
So... Some code! That we'll put again in 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
   310
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   311
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   312
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   313
  from cubicweb.selectors import is_instance
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   314
  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
   315
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   316
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   317
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   318
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   319
      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
   320
          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
   321
          self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   322
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   323
              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
   324
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   325
What's going on here?
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   326
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   327
* 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
   328
  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
   329
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   330
* 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
   331
  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
   332
  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
   333
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   334
* View applying to entities usually have to define `cell_call` as entry point,
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   335
  and are given `row` and `col` arguments tell to which entity in the result set
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   336
  the view is applied. We can then get this entity from the result set
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   337
  (`self.cw_rset`) by using the `get_entity` method.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   338
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   339
* 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
   340
  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
   341
  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
   342
  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
   343
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
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
   346
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   347
.. 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
   348
   :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
   349
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   350
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
   351
|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
   352
build higher level layers.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   353
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   354
.. Note::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   355
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   356
  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
   357
  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
   358
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   359
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
   360
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   361
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   362
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
   363
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   364
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   365
|cubicweb| provides an ORM to easily programmaticaly manipulate
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   366
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
   367
`get_entity` on a result set). By default, entity
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   368
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
   369
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
   370
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
   371
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   372
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
   373
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
   374
entity:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   375
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   376
- 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
   377
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   378
- 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
   379
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   380
You may then want to add your own methods, override default implementation of some
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   381
method, etc...
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   382
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   383
.. sourcecode:: python
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
    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
   386
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
    class Community(AnyEntity):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   389
        """customized class for Community entities"""
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   390
        __regid__ = 'Community'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   391
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   392
        fetch_attrs, fetch_order = fetch_config(['name'])
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   393
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   394
        def dc_title(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   395
            return self.name
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   396
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   397
        def display_cw_logo(self):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   398
            return 'CubicWeb' in self.description
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   399
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   400
In this example:
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   401
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   402
* 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
   403
  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
   404
  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
   405
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   406
* 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
   407
  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
   408
  have had the same result)
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   409
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   410
* we implemented here a method :meth:`display_cw_logo` which tests if the blog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   411
  entry title contains 'CW'.  It can then be used when you're writing code
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   412
  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
   413
  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
   414
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   415
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   416
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   417
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   418
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   419
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   420
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   421
      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
   422
          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
   423
          self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   424
          if entity.display_cw_logo():
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   425
              self.w(u'<img src="http://www.cubicweb.org/doc/en/_static/cubicweb.png"/>')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   426
          if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   427
              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
   428
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   429
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
   430
logo in front of it.
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
.. Note::
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
  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
   435
  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
   436
  automatically reloaded.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   437
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   438
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   439
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
   440
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   441
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   442
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
   443
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
   444
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
   445
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
   446
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
   447
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
   448
template language provides.
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   449
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   450
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   451
A library of standard cubes is available from `CubicWeb Forge`_, to address a
6835
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   452
lot of common concerns such has manipulating people, files, things to do, etc. In
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   453
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
   454
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
   455
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
   456
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
   457
file:
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
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   460
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   461
   __depends__ =  {'cubicweb': '>= 3.10.7',
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   462
                   'cubicweb-blog': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   463
                   'cubicweb-comment': None,
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   464
                   'cubicweb-tag': None}
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   465
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   466
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
   467
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
   468
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
   469
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   470
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   471
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   472
  class comments(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   473
      subject = 'Comment'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   474
      object = 'BlogEntry'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   475
      cardinality = '1*'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   476
      composite = 'object'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   477
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   478
  class tags(RelationDefinition):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   479
      subject = 'Tag'
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   480
      object = ('Community', 'BlogEntry')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   481
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   482
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   483
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
   484
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
   485
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
   486
supported.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   487
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   488
Let's synchronize the data model as we've done earlier: ::
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   489
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   490
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   491
  $ cubicweb-ctl stop myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   492
  $ cubicweb-ctl shell myblog
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   493
  entering the migration python shell
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   494
  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
   495
  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
   496
  >>> add_cubes('comment', 'tag')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   497
  >>>
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   498
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   499
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
   500
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   501
.. 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
   502
   :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
   503
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   504
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
   505
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
   506
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
   507
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
   508
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
   509
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
   510
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
   511
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
   512
implementation.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   513
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   514
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   515
.. sourcecode:: python
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   516
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   517
  class CommunityPrimaryView(primary.PrimaryView):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   518
      __select__ = is_instance('Community')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   519
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   520
      def render_entity_title(self, entity):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   521
	  self.w(u'<h1>Welcome to the "%s" community</h1>' % entity.printable_value('name'))
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   522
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   523
      def render_entity_attributes(self, entity):
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   524
	  if entity.display_cw_logo():
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   525
	      self.w(u'<img src="http://www.cubicweb.org/doc/en/_static/cubicweb.png"/>')
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   526
	  if entity.description:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   527
	      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
   528
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   529
It appears now properly:
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   530
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   531
.. 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
   532
   :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
   533
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   534
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
   535
piece. Really.
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   536
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   537
87e2641d75f7 [doc] major rework of the simple blog tutorial
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   538
6837
7562418985ef tutorial proof reading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6835
diff changeset
   539
.. _`CubicWeb Forge`: http://www.cubicweb.org/project