doc/tutorials/advanced/part05_ui-advanced.rst
author Laurent Peuch <cortex@worlddomination.be>
Tue, 26 Feb 2019 16:29:44 +0100
branch3.26
changeset 12479 26542c8244d3
parent 12472 4ae2b13017ef
child 12415 6c23631b887e
permissions -rw-r--r--
[doc] add a note because uiprops.py is not generated anymore by default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
     1
Building my photos web site with |cubicweb| part V: let's make it even more user friendly
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
=========================================================================================
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
7529
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6880
diff changeset
     4
.. _uiprops:
2fdc310be7cd [book] add autoload section from code and fix sphinx warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6880
diff changeset
     5
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
     6
Step 1: tired of the default look?
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
     9
OK... Now our site has its most desired features. But... I would like to make it look
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
    10
somewhat like *my* website. It is not www.cubicweb.org after all. Let's tackle this
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
first!
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
The first thing we can to is to change the logo. There are various way to achieve
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    14
this. The easiest way is to put a :file:`logo.png` file into the cube's :file:`data`
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
    15
directory. As data files are looked at according to cubes order (CubicWeb
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
    16
resources coming last), that file will be selected instead of CubicWeb's one.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
.. Note::
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
   As the location for static resources are cached, you'll have to restart
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
   your instance for this to be taken into account.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    22
Though there are some cases where you don't want to use a :file:`logo.png` file.
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    23
For instance if it's a JPEG file. You can still change the logo by defining in
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    24
the cube's :file:`uiprops.py` file:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
   LOGO = data('logo.jpg')
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
12479
26542c8244d3 [doc] add a note because uiprops.py is not generated anymore by default
Laurent Peuch <cortex@worlddomination.be>
parents: 12472
diff changeset
    30
.. Note::
26542c8244d3 [doc] add a note because uiprops.py is not generated anymore by default
Laurent Peuch <cortex@worlddomination.be>
parents: 12472
diff changeset
    31
   If the file :file:`uiprops.py` doesn't exist in your cube, simply create it.
26542c8244d3 [doc] add a note because uiprops.py is not generated anymore by default
Laurent Peuch <cortex@worlddomination.be>
parents: 12472
diff changeset
    32
9256
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8665
diff changeset
    33
The uiprops machinery is used to define some static file resources,
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8665
diff changeset
    34
such as the logo, default Javascript / CSS files, as well as CSS
697a8181ba30 remove 3.9 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8665
diff changeset
    35
properties (we'll see that later).
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
.. Note::
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
   This file is imported specifically by |cubicweb|, with a predefined name space,
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
   containing for instance the `data` function, telling the file is somewhere
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
    40
   in a cube or CubicWeb's data directory.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
   One side effect of this is that it can't be imported as a regular python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
   module.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
The nice thing is that in debug mode, change to a :file:`uiprops.py` file are detected
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
and then automatically reloaded.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
Now, as it's a photos web-site, I would like to have a photo of mine as background...
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
After some trials I won't detail here, I've found a working recipe explained `here`_.
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
    50
All I've to do is to override some stuff of the default CubicWeb user interface to
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
apply it as explained.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    53
The first thing to to get the ``<img/>`` tag as first element after the
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    54
``<body>`` tag.  If you know a way to avoid this by simply specifying the image
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    55
in the CSS, tell me!  The easiest way to do so is to override the
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    56
:class:`HTMLPageHeader` view, since that's the one that is directly called once
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    57
the ``<body>`` has been written. How did I find this?  By looking in the
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    58
:mod:`cubiweb.web.views.basetemplates` module, since I know that global page
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    59
layouts sits there. I could also have grep the "body" tag in
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    60
:mod:`cubicweb.web.views`... Finding this was the hardest part. Now all I need is
12472
4ae2b13017ef [doc] uses :file:`name` everywhere + indicate file when it's not present
Laurent Peuch <cortex@worlddomination.be>
parents: 12470
diff changeset
    61
to customize it to write that ``img`` tag, as below in :file:`views.py`:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
    class HTMLPageHeader(basetemplates.HTMLPageHeader):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    66
        # override this since it's the easier way to have our bg image
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    67
        # as the first element following <body>
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    68
        def call(self, **kwargs):
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
            self.w(u'<img id="bg-image" src="%sbackground.jpg" alt="background image"/>'
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
                   % self._cw.datadir_url)
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    71
            super(HTMLPageHeader, self).call(**kwargs)
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
    def registration_callback(vreg):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    75
        vreg.register_all(globals().values(), __name__, (HTMLPageHeader))
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
    76
        vreg.register_and_replace(HTMLPageHeader, basetemplates.HTMLPageHeader)
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    79
As you may have guessed, my background image is in a :file:`background.jpg` file
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    80
in the cube's :file:`data` directory, but there are still some things to explain
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    81
to newcomers here:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    83
* The :meth:`call` method is there the main access point of the view. It's called by
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    84
  the view's :meth:`render` method. It is not the only access point for a view, but
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
    85
  this will be detailed later.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
    87
* Calling `self.w` writes something to the output stream. Except for binary views
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
    88
  (which do not generate text), it *must* be passed an Unicode string.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    90
* The proper way to get a file in :file:`data` directory is to use the `datadir_url`
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
  attribute of the incoming request (e.g. `self._cw`).
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    93
I won't explain again the :func:`registration_callback` stuff, you should understand it
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
    94
now!  If not, go back to previous posts in the series :)
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    96
Fine. Now all I've to do is to add a bit of CSS to get it to behave nicely (which
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    97
is not the case at all for now). I'll put all this in a :file:`cubes.sytweb.css`
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
    98
file, stored as usual in our :file:`data` directory:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
.. sourcecode:: css
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
    /* fixed full screen background image
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
     * as explained on http://webdesign.about.com/od/css3/f/blfaqbgsize.htm
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
     *
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   106
     * syt update: set z-index=0 on the img instead of z-index=1 on div#page & co to
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
     * avoid pb with the user actions menu
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   108
     */
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
    img#bg-image {
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   110
        position: fixed;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   111
        top: 0;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   112
        left: 0;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   113
        width: 100%;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   114
        height: 100%;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   115
        z-index: 0;
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
    }
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
    div#page, table#header, div#footer {
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   119
        background: transparent;
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   120
        position: relative;
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
    }
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
    /* add some space around the logo
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
     */
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
    img#logo {
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   126
        padding: 5px 15px 0px 15px;
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
    }
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
    /* more dark font for metadata to have a chance to see them with the background
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
     *  image
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
     */
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
    div.metadata {
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   133
        color: black;
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
    }
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   136
You can see here stuff explained in the cited page, with only a slight modification
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   137
explained in the comments, plus some additional rules to make things somewhat cleaner:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   139
* a bit of padding around the logo
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   140
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
* darker metadata which appears by default below the content (the white frame in the page)
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   143
To get this CSS file used everywhere in the site, I have to modify the :file:`uiprops.py` file
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   144
introduced above:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
   STYLESHEETS = sheet['STYLESHEETS'] + [data('cubes.sytweb.css')]
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   150
.. Note::
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
   `sheet` is another predefined variable containing values defined by
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   152
   already process `:file:`uiprops.py`` file, notably the CubicWeb's one.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   154
Here we simply want our CSS in addition to CubicWeb's base CSS files, so we
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
redefine the `STYLESHEETS` variable to existing CSS (accessed through the `sheet`
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
variable) with our one added. I could also have done:
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
   sheet['STYLESHEETS'].append(data('cubes.sytweb.css'))
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
But this is less interesting since we don't see the overriding mechanism...
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
At this point, the site should start looking good, the background image being
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
resized to fit the screen.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   166
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
.. image:: ../../images/tutos-photowebsite_background-image.png
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   169
The final touch: let's customize CubicWeb's CSS to get less orange... By simply adding
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
  contextualBoxTitleBg = incontextBoxTitleBg = '#AAAAAA'
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   174
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   175
and reloading the page we've just seen, we know have a nice greyed box instead of
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
the orange one:
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
.. image:: ../../images/tutos-photowebsite_grey-box.png
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   180
This is because CubicWeb's CSS include some variables which are
12472
4ae2b13017ef [doc] uses :file:`name` everywhere + indicate file when it's not present
Laurent Peuch <cortex@worlddomination.be>
parents: 12470
diff changeset
   181
expanded by values defined in :file:`uiprops.py` file. In our case we controlled the
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   182
properties of the CSS `background` property of boxes with CSS class
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   183
`contextualBoxTitleBg` and `incontextBoxTitleBg`.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   184
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   185
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   186
Step 2: configuring boxes
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   187
~~~~~~~~~~~~~~~~~~~~~~~~~
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   188
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   189
Boxes present to the user some ways to use the application. Let's first do a few
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   190
user interface tweaks in our :file:`views.py` file:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   191
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   192
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   193
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7529
diff changeset
   194
  from cubicweb.predicates import none_rset
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   195
  from cubicweb.web.views import bookmark
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   196
  from cubes.zone import views as zone
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   197
  from cubes.tag import views as tag
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   198
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   199
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   200
  # change bookmarks box selector so it's only displayed on startup views
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   201
  bookmark.BookmarksBox.__select__ = bookmark.BookmarksBox.__select__ & none_rset()
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   202
  # move zone box to the left instead of in the context frame and tweak its order
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   203
  zone.ZoneBox.context = 'left'
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   204
  zone.ZoneBox.order = 100
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   205
  # move tags box to the left instead of in the context frame and tweak its order
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   206
  tag.TagsBox.context = 'left'
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   207
  tag.TagsBox.order = 102
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   208
  # hide similarity box, not interested
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   209
  tag.SimilarityBox.visible = False
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   210
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   211
The idea is to move all boxes in the left column, so we get more space for the
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   212
photos.  Now, serious things: I want a box similar to the tags box but to handle
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   213
the `Person displayed_on File` relation. We can do this simply by adding a
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   214
:class:`AjaxEditRelationCtxComponent` subclass to our views, as below:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   215
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   217
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   218
    from logilab.common.decorators import monkeypatch
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   219
    from cubicweb import ValidationError
8665
e65af61bde7d [uicfg] uicfg.py moves from web/ to web/views/ (prepares #2406609)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8190
diff changeset
   220
    from cubicweb.web.views import uicfg, component
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   221
    from cubicweb.web.views import basecontrollers
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   222
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   223
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   224
    # hide displayed_on relation using uicfg since it will be displayed by the box below
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   225
    uicfg.primaryview_section.tag_object_of(('*', 'displayed_on', '*'), 'hidden')
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   226
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   227
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   228
    class PersonBox(component.AjaxEditRelationCtxComponent):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   229
        __regid__ = 'sytweb.displayed-on-box'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   230
        # box position
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   231
        order = 101
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   232
        context = 'left'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   233
        # define relation to be handled
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   234
        rtype = 'displayed_on'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   235
        role = 'object'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   236
        target_etype = 'Person'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   237
        # messages
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   238
        added_msg = _('person has been added')
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   239
        removed_msg = _('person has been removed')
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   240
        # bind to js_* methods of the json controller
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   241
        fname_vocabulary = 'unrelated_persons'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   242
        fname_validate = 'link_to_person'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   243
        fname_remove = 'unlink_person'
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   244
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   245
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   246
    @monkeypatch(basecontrollers.JSonController)
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   247
    @basecontrollers.jsonize
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   248
    def js_unrelated_persons(self, eid):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   249
        """return tag unrelated to an entity"""
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   250
        rql = "Any F + ' ' + S WHERE P surname S, P firstname F, X eid %(x)s, NOT P displayed_on X"
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   251
        return [name for (name,) in self._cw.execute(rql, {'x' : eid})]
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   252
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   253
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   254
    @monkeypatch(basecontrollers.JSonController)
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   255
    def js_link_to_person(self, eid, people):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   256
        req = self._cw
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   257
        for name in people:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   258
            name = name.strip().title()
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   259
            if not name:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   260
                continue
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   261
            try:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   262
                firstname, surname = name.split(None, 1)
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   263
            except:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   264
                raise ValidationError(eid, {('displayed_on', 'object'): 'provide <first name> <surname>'})
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   265
            rset = req.execute('Person P WHERE '
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   266
                               'P firstname %(firstname)s, P surname %(surname)s',
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   267
                               locals())
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   268
            if rset:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   269
                person = rset.get_entity(0, 0)
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   270
            else:
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   271
                person = req.create_entity('Person', firstname=firstname,
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   272
                                                surname=surname)
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   273
            req.execute('SET P displayed_on X WHERE '
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   274
                        'P eid %(p)s, X eid %(x)s, NOT P displayed_on X',
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   275
                        {'p': person.eid, 'x' : eid})
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   276
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   277
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   278
    @monkeypatch(basecontrollers.JSonController)
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   279
    def js_unlink_person(self, eid, personeid):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   280
        self._cw.execute('DELETE P displayed_on X WHERE P eid %(p)s, X eid %(x)s',
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   281
                         {'p': personeid, 'x': eid})
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   282
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   283
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   284
You basically subclass to configure with some class attributes. The `fname_*`
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   285
attributes give the name of methods that should be defined on the json control to
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   286
make the AJAX part of the widget work: one to get the vocabulary, one to add a
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   287
relation and another to delete a relation. These methods must start by a `js_`
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   288
prefix and are added to the controller using the `@monkeypatch` decorator. In my
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   289
case, the most complicated method is the one which adds a relation, since it
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   290
tries to see if the person already exists, and else automatically create it,
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   291
assuming the user entered "firstname surname".
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   292
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   293
Let's see how it looks like on a file primary view:
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   294
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   295
.. image:: ../../images/tutos-photowebsite_boxes.png
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   296
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   297
Great, it's now as easy for me to link my pictures to people than to tag them.
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   298
Also, visitors get a consistent display of these two pieces of information.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   299
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   300
.. Note::
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   301
  The ui component system has been refactored in `CubicWeb 3.10`_, which also
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   302
  introduced the :class:`AjaxEditRelationCtxComponent` class.
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   303
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   304
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   305
Step 3: configuring facets
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   306
~~~~~~~~~~~~~~~~~~~~~~~~~~
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   307
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   308
The last feature we'll add today is facet configuration. If you access to the
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   309
'/file' url, you'll see a set of 'facets' appearing in the left column. Facets
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   310
provide an intuitive way to build a query incrementally, by proposing to the user
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   311
various way to restrict the result set. For instance CubicWeb proposes a facet to
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   312
restrict based on who created an entity; the tag cube proposes a facet to
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   313
restrict based on tags; the zoe cube a facet to restrict based on geographical
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   314
location, and so on. In that gist, I want to propose a facet to restrict based on
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   315
the people displayed on the picture. To do so, there are various classes in the
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   316
:mod:`cubicweb.web.facet` module which simply have to be configured using class
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   317
attributes as we've done for the box. In our case, we'll define a subclass of
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   318
:class:`RelationFacet`.
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   319
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   320
.. Note::
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   321
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   322
   Since that's ui stuff, we'll continue to add code below to our
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   323
   :file:`views.py` file. Though we begin to have a lot of various code their, so
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   324
   it's may be a good time to split our views module into submodules of a `view`
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   325
   package. In our case of a simple application (glue) cube, we could start using
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   326
   for instance the layout below: ::
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   327
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   328
     views/__init__.py   # uicfg configuration, facets
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   329
     views/layout.py     # header/footer/background stuff
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   330
     views/components.py # boxes, adapters
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   331
     views/pages.py      # index view, 404 view
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   332
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   333
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   334
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   335
    from cubicweb.web import facet
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   336
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   337
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   338
    class DisplayedOnFacet(facet.RelationFacet):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   339
        __regid__ = 'displayed_on-facet'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   340
        # relation to be displayed
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   341
        rtype = 'displayed_on'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   342
        role = 'object'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   343
        # view to use to display persons
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   344
        label_vid = 'combobox'
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   345
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   346
Let's say we also want to filter according to the `visibility` attribute. This is
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   347
even simpler as we just have to derive from the :class:`AttributeFacet` class:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   348
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   349
.. sourcecode:: python
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   350
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   351
    class VisibilityFacet(facet.AttributeFacet):
12470
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   352
        __regid__ = 'visibility-facet'
4ed60412d02d [doc] pep8/retab in code examples
Laurent Peuch <cortex@worlddomination.be>
parents: 10491
diff changeset
   353
        rtype = 'visibility'
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   354
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   355
Now if I search for some pictures on my site, I get the following facets available:
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   356
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   357
.. image:: ../../images/tutos-photowebsite_facets.png
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   358
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   359
.. Note::
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   360
6879
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   361
  By default a facet must be applyable to every entity in the result set and
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   362
  provide at leat two elements of vocabulary to be displayed (for instance you
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   363
  won't see the `created_by` facet if the same user has created all
54944faf9b61 [book] advanced tutorial part 5 improvments: add some roles, talk about cube's layout, strip image
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6877
diff changeset
   364
  entities). This may explain why you don't see yours...
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   365
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   366
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   367
Conclusion
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   368
~~~~~~~~~~
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   369
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   370
We started to see the power behind the infrastructure provided by the
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   371
framework, both on the pure ui (CSS, Javascript) side and on the Python side
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   372
(high level generic classes for components, including boxes and facets). We now
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   373
have, with a few lines of code, a full-featured web site with a personalized look.
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   374
6877
0e8fc441b38b blog proofreading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6876
diff changeset
   375
Of course we'll probably want more as time goes, but we can now
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   376
concentrate on making good pictures, publishing albums and sharing them with
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   377
friends...
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   378
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   379
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   380
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6879
diff changeset
   381
.. _`CubicWeb 3.10`: http://www.cubicweb.org/blogentry/1330518
6876
4b0b9d8207c5 [doc] backport part 3 & 4 of the sytweb's tutorial + to be published part 5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   382
.. _`here`: http://webdesign.about.com/od/css3/f/blfaqbgsize.htm