doc/book/en/development/devweb/facets.rst
author Stephanie Marcu <stephanie.marcu@logilab.fr>
Thu, 15 Apr 2010 16:20:53 +0200
branchstable
changeset 5286 43d7044f8d0b
parent 4751 1a9d2c3c7f0c
permissions -rw-r--r--
[doc/book] complete the facet section
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
The facets system
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
-----------------
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     3
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     4
Facets allow to restrict searches according to some criteria. CubicWeb has a builtin `facet`_ system to define restrictions
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     5
`filters`_ really as easily as possible. A few base classes for facets
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     6
are provided in ``cubicweb.web.facet.py``. All classes inherits from
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     7
the base class ``AbstractFacet``. 
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     8
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
     9
Here is an overview of the facets rendering pick from the `tracker` cube:
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    10
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    11
.. image:: ../../images/facet_overview.png
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    12
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    13
Facets will appear on each page presenting more than one entity.
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    14
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    15
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    16
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    17
VocabularyFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    18
~~~~~~~~~~~~~~~~
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    19
The ``VocabularyFacet`` inherits from the ``AbstractFacet``.
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    20
A class which inherits from VocabularyFacets must redefine these methods:
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    21
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    22
.. automethod:: cubicweb.web.facet.VocabularyFacet.vocabulary
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    23
.. automethod:: cubicweb.web.facet.VocabularyFacet.possible_values
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    24
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    25
RelationFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    26
~~~~~~~~~~~~~~
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    27
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    28
The ``RelationFacet`` inherits from the ``VocabularyFacet``. It allows to filter entities according to certain relation's values. Generally, you just have to define some class attributes like:
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    29
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    30
- rtype: the name of the relation
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    31
- role: the default value is set to `subject`
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    32
- target_attr: needed if it is not the default attribute of the entity
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    33
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    34
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    35
To illustrate this facet, let's take for example an *excerpt* of the schema of an office location search application:
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    36
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    37
.. sourcecode:: python
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    38
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    39
  class Office(WorkflowableEntityType):
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    40
      price = Int(description='euros / m2 / HC / HT')
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    41
      surface = Int(description='m2')
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    42
      description = RichString(fulltextindexed=True)
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    43
      has_address = SubjectRelation('PostalAddress',
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    44
                                    cardinality='1?',
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    45
                                    composite='subject')
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    46
      proposed_by = SubjectRelation('Agency')
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    47
      comments = ObjectRelation('Comment',
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    48
                                cardinality='1*',
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    49
                                composite='object')
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    50
      screenshots = SubjectRelation(('File', 'Image'),
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    51
                                    cardinality='*1',
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    52
                                    composite='subject')
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    53
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    54
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    55
We define a facet to filter offices according to the attribute
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    56
`postalcode` of their associated `PostalAdress`.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    57
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    58
.. sourcecode:: python
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    59
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    60
  class PostalCodeFacet(RelationFacet):
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    61
      __regid__ = 'postalcode-facet'      # every registered class must have an id
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    62
      __select__ = implements('Office')   # this facet should only be selected when
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    63
                                          # visualizing offices
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    64
      rtype = 'has_address'               # this facet is a filter on the entity linked to
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    65
                                          # the office thrhough the relation
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    66
                                          # has_address
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    67
      target_attr = 'postalcode'          # the filter's key is the attribute "postal_code"
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    68
                                          # of the target PostalAddress entity
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    69
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    70
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    71
AttributeFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    72
~~~~~~~~~~~~~~
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    73
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    74
The ``AttributeFacet`` inherits from the ``RelationFacet``. It allows to filter entities according to certain attribute's values.
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    75
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    76
The example below resumes the former schema. We define now a filter based on the `surface` attribute of the
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    77
`Office`.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    78
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    79
.. sourcecode:: python
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    80
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    81
  class SurfaceFacet(AttributeFacet):
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
    82
      __regid__ = 'surface-facet'       # every registered class must have an id
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    83
      __select__ = implements('Office') # this facet should only be selected when
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    84
                                        # visualizing offices
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    85
      rtype = 'surface'                 # the filter's key is the attribute "surface"
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    86
      comparator = '>='                 # override the default value of operator since
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    87
                                        # we want to filter according to a
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    88
                                        # minimal
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    89
                                        # value, not an exact one
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    90
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    91
      def rset_vocabulary(self, ___):
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    92
          """override the default vocabulary method since we want to hard-code
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
    93
          our threshold values.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    94
          Not overriding would generate a filter box with all existing surfaces
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    95
          defined in the database.
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    96
          """
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    97
          return [('> 200', '200'), ('> 250', '250'),
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    98
                  ('> 275', '275'), ('> 300', '300')]
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
    99
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   100
RangeFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   101
~~~~~~~~~~
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   102
The ``RangeFacet`` inherits from the ``AttributeFacet``. It allows to filter entities according to certain attributes of numerical type.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   103
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   104
The ``RangeFacet`` displays a slider using `jquery`_ to choose a lower bound and an upper bound.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   105
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   106
The example below defines a facet to filter a selection of books according to their number of pages.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   107
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   108
.. sourcecode:: python
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   109
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   110
   class BookPagesFacet(RangeFacet):
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   111
       __regid__ = 'priority-facet'
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   112
       __select__ = RangeFacet.__select__ & implements('Book')
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   113
       rtype = 'pages'
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   114
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   115
The image below display the rendering of the ``RangeFacet``:
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   116
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   117
.. image:: ../../images/facet_range.png
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   118
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   119
DateRangeFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   120
~~~~~~~~~~~~~~
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   121
The ``DateRangeFacet`` inherits from the ``RangeFacet``. It allows to filter entities according to certain attributes of date type.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   122
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   123
Here is an example of code that defines a facet to filter
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   124
musical works according to their composition date:
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   125
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   126
.. sourcecode:: python
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   127
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   128
    class CompositionDateFacet(DateRangeFacet):
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   129
        # 1. make sure this facet is displayed only on Track selection
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   130
        __select__ = DateRangeFacet.__select__ & implements('Track')
4446
a413fac5ff5e damn me, more stupid sed fix...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4437
diff changeset
   131
        # 2. give the facet an id required by CubicWeb)
4751
1a9d2c3c7f0c [book] more id -> __regid__
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4446
diff changeset
   132
        __regid__ = 'compdate-facet'
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   133
        # 3. specify the attribute name that actually stores the date in the DB
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   134
        rtype = 'composition_date'
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   135
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   136
With this facet, on each page displaying tracks, you'll be able to filter them
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   137
according to their composition date with a jquery slider.
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   138
5286
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   139
The image below display the rendering of the ``DateRangeFacet``:
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   140
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   141
.. image:: ../../images/facet_date_range.png
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   142
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   143
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   144
HasRelationFacet
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   145
~~~~~~~~~~~~~~~~
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   146
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   147
The ``DateRangeFacet`` inherits from the ``AbstractFacet``. It will
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   148
display a simple checkbox and lets you refine your selection in order
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   149
to get only entities that actually use this relation.
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   150
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   151
Here is an example of the rendering of the ``HasRelationFacet`` to
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   152
filter entities with image and the corresponding code:
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   153
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   154
.. image:: ../../images/facet_has_image.png
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   155
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   156
.. sourcecode:: python
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   157
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   158
  class HasImageFacet(HasRelationFacet):
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   159
      __regid__ = 'hasimage-facet'
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   160
      __select__ = HasRelationFacet.__select__ & implements('Book')
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   161
      rtype = 'has_image'
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   162
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   163
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   164
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   165
To use ``HasRelationFacet`` on a reverse relation add ``role = 'object'`` in
43d7044f8d0b [doc/book] complete the facet section
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 4751
diff changeset
   166
it's definitions.
3991
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   167
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   168
.. _facet: http://en.wikipedia.org/wiki/Faceted_browser
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   169
.. _filters: http://www.cubicweb.org/blogentry/154152
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   170
.. _jquery: http://www.jqueryui.com/
a0aa9789c6bc Pasting blog about facet in the facet section of the doc book.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 1714
diff changeset
   171