.. -*- coding: utf-8 -*-
Building my photos web site with |cubicweb| part V: let's make it even more user friendly
=========================================================================================
.. _uiprops:
Step 1: tired of the default look?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OK... Now our site has its most desired features. But... I would like to make it look
somewhat like *my* website. It is not www.cubicweb.org after all. Let's tackle this
first!
The first thing we can to is to change the logo. There are various way to achieve
this. The easiest way is to put a :file:`logo.png` file into the cube's :file:`data`
directory. As data files are looked at according to cubes order (CubicWeb
resources coming last), that file will be selected instead of CubicWeb's one.
.. Note::
As the location for static resources are cached, you'll have to restart
your instance for this to be taken into account.
Though there are some cases where you don't want to use a :file:`logo.png` file.
For instance if it's a JPEG file. You can still change the logo by defining in
the cube's :file:`uiprops.py` file:
.. sourcecode:: python
LOGO = data('logo.jpg')
.. Note::
If the file :file:`uiprops.py` doesn't exist in your cube, simply create it.
The uiprops machinery is used to define some static file resources,
such as the logo, default Javascript / CSS files, as well as CSS
properties (we'll see that later).
.. Note::
This file is imported specifically by |cubicweb|, with a predefined name space,
containing for instance the `data` function, telling the file is somewhere
in a cube or CubicWeb's data directory.
One side effect of this is that it can't be imported as a regular python
module.
The nice thing is that in debug mode, change to a :file:`uiprops.py` file are detected
and then automatically reloaded.
Now, as it's a photos web-site, I would like to have a photo of mine as background...
After some trials I won't detail here, I've found a working recipe explained `here`_.
All I've to do is to override some stuff of the default CubicWeb user interface to
apply it as explained.
The first thing to to get the ```` tag as first element after the
``
'
% self._cw.datadir_url)
super(HTMLPageHeader, self).call(**kwargs)
def registration_callback(vreg):
vreg.register_all(globals().values(), __name__, (HTMLPageHeader))
vreg.register_and_replace(HTMLPageHeader, basetemplates.HTMLPageHeader)
As you may have guessed, my background image is in a :file:`background.jpg` file
in the cube's :file:`data` directory, but there are still some things to explain
to newcomers here:
* The :meth:`call` method is there the main access point of the view. It's called by
the view's :meth:`render` method. It is not the only access point for a view, but
this will be detailed later.
* Calling `self.w` writes something to the output stream. Except for binary views
(which do not generate text), it *must* be passed an Unicode string.
* The proper way to get a file in :file:`data` directory is to use the `datadir_url`
attribute of the incoming request (e.g. `self._cw`).
I won't explain again the :func:`registration_callback` stuff, you should understand it
now! If not, go back to `previous post in the series`_ :)
Fine. Now all I've to do is to add a bit of CSS to get it to behave nicely (which
is not the case at all for now). I'll put all this in a :file:`cubes.sytweb.css`
file, stored as usual in our :file:`data` directory:
.. sourcecode:: css
/* fixed full screen background image
* as explained on http://webdesign.about.com/od/css3/f/blfaqbgsize.htm
*
* syt update: set z-index=0 on the img instead of z-index=1 on div#page & co to
* avoid pb with the user actions menu
*/
img#bg-image {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
}
div#page, table#header, div#footer {
background: transparent;
position: relative;
}
/* add some space around the logo
*/
img#logo {
padding: 5px 15px 0px 15px;
}
/* more dark font for metadata to have a chance to see them with the background
* image
*/
div.metadata {
color: black;
}
You can see here stuff explained in the cited page, with only a slight modification
explained in the comments, plus some additional rules to make things somewhat cleaner:
* a bit of padding around the logo
* darker metadata which appears by default below the content (the white frame in the page)
To get this CSS file used everywhere in the site, I have to modify the :file:`uiprops.py` file
introduced above:
.. sourcecode:: python
STYLESHEETS = sheet['STYLESHEETS'] + [data('cubes.sytweb.css')]
.. Note::
`sheet` is another predefined variable containing values defined by
already process `:file:`uiprops.py`` file, notably the CubicWeb's one.
Here we simply want our CSS in addition to CubicWeb's base CSS files, so we
redefine the `STYLESHEETS` variable to existing CSS (accessed through the `sheet`
variable) with our one added. I could also have done:
.. sourcecode:: python
sheet['STYLESHEETS'].append(data('cubes.sytweb.css'))
But this is less interesting since we don't see the overriding mechanism...
At this point, the site should start looking good, the background image being
resized to fit the screen.
.. image:: ../../images/tutos-photowebsite_background-image.png
The final touch: let's customize CubicWeb's CSS to get less orange... By simply adding
.. sourcecode:: python
contextualBoxTitleBg = incontextBoxTitleBg = '#AAAAAA'
and reloading the page we've just seen, we know have a nice greyed box instead of
the orange one:
.. image:: ../../images/tutos-photowebsite_grey-box.png
This is because CubicWeb's CSS include some variables which are
expanded by values defined in :file:`uiprops.py` file. In our case we controlled the
properties of the CSS `background` property of boxes with CSS class
`contextualBoxTitleBg` and `incontextBoxTitleBg`.
Step 2: configuring boxes
~~~~~~~~~~~~~~~~~~~~~~~~~
Boxes present to the user some ways to use the application. Let's first do a few
user interface tweaks in our :file:`views.py` file:
.. sourcecode:: python
from cubicweb.predicates import none_rset
from cubicweb.web.views import bookmark
from cubicweb_zone import views as zone
from cubicweb_tag import views as tag
# change bookmarks box selector so it's only displayed on startup views
bookmark.BookmarksBox.__select__ = bookmark.BookmarksBox.__select__ & none_rset()
# move zone box to the left instead of in the context frame and tweak its order
zone.ZoneBox.context = 'left'
zone.ZoneBox.order = 100
# move tags box to the left instead of in the context frame and tweak its order
tag.TagsBox.context = 'left'
tag.TagsBox.order = 102
# hide similarity box, not interested
tag.SimilarityBox.visible = False
The idea is to move all boxes in the left column, so we get more space for the
photos. Now, serious things: I want a box similar to the tags box but to handle
the `Person displayed_on File` relation. We can do this simply by adding a
:class:`AjaxEditRelationCtxComponent` subclass to our views, as below:
.. sourcecode:: python
from cubicweb import _
from logilab.common.decorators import monkeypatch
from cubicweb import ValidationError
from cubicweb.web.views import uicfg, component
from cubicweb.web.views import basecontrollers
# hide displayed_on relation using uicfg since it will be displayed by the box below
uicfg.primaryview_section.tag_object_of(('*', 'displayed_on', '*'), 'hidden')
class PersonBox(component.AjaxEditRelationCtxComponent):
__regid__ = 'sytweb.displayed-on-box'
# box position
order = 101
context = 'left'
# define relation to be handled
rtype = 'displayed_on'
role = 'object'
target_etype = 'Person'
# messages
added_msg = _('person has been added')
removed_msg = _('person has been removed')
# bind to js_* methods of the json controller
fname_vocabulary = 'unrelated_persons'
fname_validate = 'link_to_person'
fname_remove = 'unlink_person'
@monkeypatch(basecontrollers.JSonController)
@basecontrollers.jsonize
def js_unrelated_persons(self, eid):
"""return tag unrelated to an entity"""
rql = "Any F + ' ' + S WHERE P surname S, P firstname F, X eid %(x)s, NOT P displayed_on X"
return [name for (name,) in self._cw.execute(rql, {'x' : eid})]
@monkeypatch(basecontrollers.JSonController)
def js_link_to_person(self, eid, people):
req = self._cw
for name in people:
name = name.strip().title()
if not name:
continue
try:
firstname, surname = name.split(None, 1)
except:
raise ValidationError(eid, {('displayed_on', 'object'): 'provide