web/views/facets.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 07 Oct 2010 17:25:24 +0200
branchstable
changeset 6406 39663630ca3c
parent 5940 0e3ae19b181a
child 6040 9ae260b2d1c4
permissions -rw-r--r--
xml-escape <script> tags generated by HTMLHead
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""the facets box and some basic facets"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2283
diff changeset
    22
from logilab.mtconverter import xml_escape
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
    24
from cubicweb.appobject import objectify_selector
4320
4efd4c306746 closes #345410: two_lines_rset, two_cols_rset, two_etypes_rset are badly named
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    25
from cubicweb.selectors import (non_final_entity, multi_lines_rset,
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 758
diff changeset
    26
                                match_context_prop, yes, relation_possible)
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    27
from cubicweb.utils import json_dumps
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
from cubicweb.web.box import BoxTemplate
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 758
diff changeset
    29
from cubicweb.web.facet import (AbstractFacet, FacetStringWidget, RelationFacet,
2337
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
    30
                                prepare_facets_rqlst, filter_hiddens, _cleanup_rqlst,
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
    31
                                _prepare_vocabulary_rqlst)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
    33
@objectify_selector
2161
200481e7b156 prepare time where it won't be mandatory to give rset to select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    34
def contextview_selector(cls, req, rset=None, row=None, col=None, view=None,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
                         **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    if view and getattr(view, 'filter_box_context_info', lambda: None)():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        return 1
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    38
    return 0
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
class FilterBox(BoxTemplate):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    """filter results of a query"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
    43
    __regid__ = 'filter_box'
4320
4efd4c306746 closes #345410: two_lines_rset, two_cols_rset, two_etypes_rset are badly named
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    44
    __select__ = (((non_final_entity() & multi_lines_rset())
758
0c0dfd33a76d instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 742
diff changeset
    45
                   | contextview_selector()
0c0dfd33a76d instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 742
diff changeset
    46
                   ) & match_context_prop())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    context = 'left'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    title = _('boxes_filter_box')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    visible = True # functionality provided by the search box by default
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    order = 1
205
8ea3294e7427 make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 204
diff changeset
    51
    roundcorners = True
2214
a6b42cf05407 [facets] slightly better attr names
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2213
diff changeset
    52
2215
b1977f5263ac by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2214
diff changeset
    53
    needs_css = 'cubicweb.facets.css'
3006
4d85386b06a5 rename cubicweb.formfilter.js into cubicweb.facets.js
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
    54
    needs_js = ('cubicweb.ajax.js', 'cubicweb.facets.js')
2214
a6b42cf05407 [facets] slightly better attr names
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2213
diff changeset
    55
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2323
diff changeset
    56
    bk_linkbox_template = u'<div class="facetTitle">%s</div>'
2283
dc13ec07a70a cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2260
diff changeset
    57
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    def facetargs(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        """this method returns the list of extra arguments that should
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        be used by the facet
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        return {}
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    63
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    def _get_context(self, view):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        context = getattr(view, 'filter_box_context_info', lambda: None)()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        if context:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
            rset, vid, divid, paginate = context
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        else:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    69
            rset = self.cw_rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            vid, divid = None, 'pageContent'
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    71
            paginate = view and view.paginable
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        return rset, vid, divid, paginate
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
    73
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    def call(self, view=None):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    75
        req = self._cw
2215
b1977f5263ac by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2214
diff changeset
    76
        req.add_js( self.needs_js )
b1977f5263ac by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2214
diff changeset
    77
        req.add_css( self.needs_css)
205
8ea3294e7427 make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 204
diff changeset
    78
        if self.roundcorners:
8ea3294e7427 make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 204
diff changeset
    79
            req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");')
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 758
diff changeset
    80
        rset, vid, divid, paginate = self._get_context(view)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            return
5282
d7f72547208e [facet] fix #873742: erroenously use self.cw_rset instead of context rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5112
diff changeset
    83
        rqlst = rset.syntax_tree()
4616
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    84
        # union not yet supported
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    85
        if len(rqlst.children) != 1:
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    86
            return ()
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    87
        rqlst = rqlst.copy()
4614
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    88
        req.vreg.rqlhelper.annotate(rqlst)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    89
        mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    90
        widgets = []
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    91
        for facet in self.get_facets(rset, rqlst.children[0], mainvar):
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    92
            if facet.cw_propval('visible'):
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    93
                wdg = facet.get_widget()
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    94
                if wdg is not None:
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    95
                    widgets.append(wdg)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    96
        if not widgets:
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
    97
            return
4616
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    98
        if vid is None:
2f296c34aec4 fix table view facet retreival, necessary since 95ccd84c892b. Also some cleanup in facet box
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4614
diff changeset
    99
            vid = req.form.get('vid')
4614
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   100
        if self.bk_linkbox_template:
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   101
            self.display_bookmark_link(rset)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   102
        w = self.w
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   103
        w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">'  % (
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   104
            divid, xml_escape(json_dumps([divid, vid, paginate, self.facetargs()]))))
4614
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   105
        w(u'<fieldset>')
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   106
        hiddens = {'facets': ','.join(wdg.facet.__regid__ for wdg in widgets),
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   107
                   'baserql': baserql}
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   108
        for param in ('subvid', 'vtitle'):
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   109
            if param in req.form:
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   110
                hiddens[param] = req.form[param]
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   111
        filter_hiddens(w, **hiddens)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   112
        for wdg in widgets:
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   113
            wdg.render(w=self.w)
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   114
        w(u'</fieldset>\n</form>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2323
diff changeset
   116
    def display_bookmark_link(self, rset):
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3720
diff changeset
   117
        eschema = self._cw.vreg.schema.eschema('Bookmark')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   118
        if eschema.has_perm(self._cw, 'add'):
5112
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4616
diff changeset
   119
            bk_path = 'rql=%s' % self._cw.url_quote(rset.printable_rql())
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4616
diff changeset
   120
            if self._cw.form.get('vid'):
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4616
diff changeset
   121
                bk_path += '&vid=%s' % self._cw.url_quote(self._cw.form['vid'])
5bf8baecfaf8 [facet] fix 'bookmark this search':
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4616
diff changeset
   122
            bk_path = 'view?' + bk_path
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   123
            bk_title = self._cw._('my custom search')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   124
            linkto = 'bookmarked_by:%s:subject' % self._cw.user.eid
3460
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   125
            bk_add_url = self._cw.build_url('add/Bookmark', path=bk_path, title=bk_title, __linkto=linkto)
e4843535db25 [api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   126
            bk_base_url = self._cw.build_url('add/Bookmark', title=bk_title, __linkto=linkto)
2260
080167dad19e split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2215
diff changeset
   127
            bk_link = u'<a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a>' % (
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2283
diff changeset
   128
                    xml_escape(bk_base_url),
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2283
diff changeset
   129
                    xml_escape(bk_add_url),
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   130
                    self._cw._('bookmark this search'))
2325
363b66a15668 [cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2323
diff changeset
   131
            self.w(self.bk_linkbox_template % bk_link)
2260
080167dad19e split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2215
diff changeset
   132
4614
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   133
    def get_facets(self, rset, rqlst, mainvar):
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   134
        return self._cw.vreg['facets'].poss_visible_objects(
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   135
            self._cw, rset=rset, rqlst=rqlst,
95ccd84c892b fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents: 4320
diff changeset
   136
            context='facetbox', filtered_variable=mainvar)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   137
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
# facets ######################################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
class CreatedByFacet(RelationFacet):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   141
    __regid__ = 'created_by-facet'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    rtype = 'created_by'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    target_attr = 'login'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
class InGroupFacet(RelationFacet):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   146
    __regid__ = 'in_group-facet'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    rtype = 'in_group'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
    target_attr = 'name'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
class InStateFacet(RelationFacet):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   151
    __regid__ = 'in_state-facet'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
    rtype = 'in_state'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    target_attr = 'name'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
# inherit from RelationFacet to benefit from its possible_values implementation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
class ETypeFacet(RelationFacet):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   157
    __regid__ = 'etype-facet'
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
   158
    __select__ = yes()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
    order = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
    rtype = 'is'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    target_attr = 'name'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    def title(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   165
        return self._cw._('entity type')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    def vocabulary(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
        """return vocabulary for this facet, eg a list of 2-uple (label, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        """
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   170
        etypes = self.cw_rset.column_types(0)
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   171
        return sorted((self._cw._(etype), etype) for etype in etypes)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   172
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
    def add_rql_restrictions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        """add restriction for this facet into the rql syntax tree"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   175
        value = self._cw.form.get(self.__regid__)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        if not value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        self.rqlst.add_type_restriction(self.filtered_variable, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
2337
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   180
    def possible_values(self):
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   181
        """return a list of possible values (as string since it's used to
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   182
        compare to a form value in javascript) for this facet
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   183
        """
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   184
        rqlst = self.rqlst
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   185
        rqlst.save_state()
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   186
        try:
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   187
            _cleanup_rqlst(rqlst, self.filtered_variable)
2340
65445ae67db0 optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2339
diff changeset
   188
            etype_var = _prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, self.role)
65445ae67db0 optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2339
diff changeset
   189
            attrvar = rqlst.make_variable()
65445ae67db0 optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2339
diff changeset
   190
            rqlst.add_selected(attrvar)
65445ae67db0 optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2339
diff changeset
   191
            rqlst.add_relation(etype_var, 'name', attrvar)
65445ae67db0 optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2339
diff changeset
   192
            return [etype for _, etype in self.rqlexec(rqlst.as_string())]
2337
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   193
        finally:
6620e5abcd99 select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 2323
diff changeset
   194
            rqlst.recover()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
class HasTextFacet(AbstractFacet):
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
   197
    __select__ = relation_possible('has_text', 'subject') & match_context_prop()
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   198
    __regid__ = 'has_text-facet'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
    rtype = 'has_text'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    role = 'subject'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    order = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
    def title(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   204
        return self._cw._('has_text')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1132
diff changeset
   205
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
    def get_widget(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
        """return the widget instance to use to display this facet
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        default implentation expects a .vocabulary method on the facet and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        return a combobox displaying this vocabulary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        return FacetStringWidget(self)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
    def add_rql_restrictions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
        """add restriction for this facet into the rql syntax tree"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   216
        value = self._cw.form.get(self.__regid__)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
        if not value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
        self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String')