author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Fri, 30 Jan 2009 17:15:07 +0100 | |
changeset 545 | 73aab1d2a27a |
parent 431 | 18b4dd650ef8 |
child 617 | 9bac1d158bdc |
permissions | -rw-r--r-- |
0 | 1 |
"""the facets box and some basic facets |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from simplejson import dumps |
|
10 |
||
11 |
from logilab.mtconverter import html_escape |
|
12 |
||
431 | 13 |
from cubicweb.common.selectors import (chainfirst, chainall, non_final_entity, |
14 |
two_lines_rset, match_context_prop, |
|
15 |
yes, one_has_relation) |
|
0 | 16 |
from cubicweb.web.box import BoxTemplate |
17 |
from cubicweb.web.facet import (AbstractFacet, VocabularyFacet, FacetStringWidget, |
|
18 |
RelationFacet, prepare_facets_rqlst, filter_hiddens) |
|
19 |
||
20 |
def contextview_selector(cls, req, rset, row=None, col=None, view=None, |
|
21 |
**kwargs): |
|
22 |
if view and getattr(view, 'filter_box_context_info', lambda: None)(): |
|
23 |
return 1 |
|
24 |
return 0 |
|
25 |
||
26 |
||
27 |
class FilterBox(BoxTemplate): |
|
28 |
"""filter results of a query""" |
|
29 |
id = 'filter_box' |
|
30 |
__selectors__ = (chainfirst(contextview_selector, |
|
431 | 31 |
chainall(non_final_entity, two_lines_rset)), |
32 |
match_context_prop) |
|
0 | 33 |
context = 'left' |
34 |
title = _('boxes_filter_box') |
|
35 |
visible = True # functionality provided by the search box by default |
|
36 |
order = 1 |
|
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
37 |
roundcorners = True |
0 | 38 |
|
39 |
def facetargs(self): |
|
40 |
"""this method returns the list of extra arguments that should |
|
41 |
be used by the facet |
|
42 |
""" |
|
43 |
return {} |
|
44 |
||
45 |
def _get_context(self, view): |
|
46 |
context = getattr(view, 'filter_box_context_info', lambda: None)() |
|
47 |
if context: |
|
48 |
rset, vid, divid, paginate = context |
|
49 |
else: |
|
50 |
rset = self.rset |
|
51 |
vid, divid = None, 'pageContent' |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
52 |
paginate = view and view.need_navigation |
0 | 53 |
return rset, vid, divid, paginate |
54 |
||
55 |
def call(self, view=None): |
|
5
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
req = self.req |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
57 |
req.add_js( ('cubicweb.ajax.js', 'cubicweb.formfilter.js') ) |
204
4b01608c5397
include cubicbeb.facet.css when displaying filterbox
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
47
diff
changeset
|
58 |
req.add_css('cubicweb.facets.css') |
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
59 |
if self.roundcorners: |
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
60 |
req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");') |
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
61 |
rset, vid, divid, paginate=self._get_context(view) |
0 | 62 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
63 |
return |
|
64 |
if vid is None: |
|
5
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
65 |
vid = req.form.get('vid') |
0 | 66 |
rqlst = rset.syntax_tree() |
67 |
rqlst.save_state() |
|
68 |
try: |
|
69 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
70 |
widgets = [] |
|
71 |
for facet in self.get_facets(rset, mainvar): |
|
72 |
if facet.propval('visible'): |
|
73 |
wdg = facet.get_widget() |
|
74 |
if wdg is not None: |
|
75 |
widgets.append(wdg) |
|
76 |
if not widgets: |
|
77 |
return |
|
78 |
w = self.w |
|
5
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
79 |
eschema = self.schema.eschema('Bookmark') |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
80 |
if eschema.has_perm(req, 'add'): |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
81 |
bk_path = 'view?rql=%s' % rset.printable_rql() |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
82 |
bk_title = req._('my custom search') |
8
8cdf3ed28e64
set bookmarked_by relation on facet bookmarking
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5
diff
changeset
|
83 |
linkto = 'bookmarked_by:%s:subject' % self.req.user.eid |
8cdf3ed28e64
set bookmarked_by relation on facet bookmarking
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5
diff
changeset
|
84 |
bk_add_url = self.build_url('add/Bookmark', path=bk_path, title=bk_title, __linkto=linkto) |
8cdf3ed28e64
set bookmarked_by relation on facet bookmarking
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5
diff
changeset
|
85 |
bk_base_url = self.build_url('add/Bookmark', title=bk_title, __linkto=linkto) |
8cdf3ed28e64
set bookmarked_by relation on facet bookmarking
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
5
diff
changeset
|
86 |
w(u'<div class="facetTitle"><a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a></div>' % ( |
5
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
87 |
html_escape(bk_base_url), |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
88 |
html_escape(bk_add_url), |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
89 |
req._('bookmark this search'))) |
0 | 90 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
91 |
divid, html_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
|
92 |
w(u'<fieldset>') |
|
93 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
94 |
'baserql': baserql} |
|
95 |
for param in ('subvid', 'vtitle'): |
|
5
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
96 |
if param in req.form: |
64072193bd48
simple and naive implementation of 'bookmark this search' while using facets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
97 |
hiddens[param] = req.form[param] |
0 | 98 |
filter_hiddens(w, **hiddens) |
99 |
for wdg in widgets: |
|
100 |
wdg.render(w=self.w) |
|
101 |
w(u'</fieldset>\n</form>\n') |
|
102 |
finally: |
|
103 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
104 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
105 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 106 |
|
107 |
def get_facets(self, rset, mainvar): |
|
108 |
return self.vreg.possible_vobjects('facets', self.req, rset, |
|
109 |
context='facetbox', |
|
110 |
filtered_variable=mainvar) |
|
111 |
||
112 |
# facets ###################################################################### |
|
113 |
||
114 |
class CreatedByFacet(RelationFacet): |
|
115 |
id = 'created_by-facet' |
|
116 |
rtype = 'created_by' |
|
117 |
target_attr = 'login' |
|
118 |
||
119 |
class InGroupFacet(RelationFacet): |
|
120 |
id = 'in_group-facet' |
|
121 |
rtype = 'in_group' |
|
122 |
target_attr = 'name' |
|
123 |
||
124 |
class InStateFacet(RelationFacet): |
|
125 |
id = 'in_state-facet' |
|
126 |
rtype = 'in_state' |
|
127 |
target_attr = 'name' |
|
128 |
||
129 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
130 |
class ETypeFacet(RelationFacet): |
|
131 |
id = 'etype-facet' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
205
diff
changeset
|
132 |
__selectors__ = (yes,) |
0 | 133 |
order = 1 |
134 |
rtype = 'is' |
|
135 |
target_attr = 'name' |
|
136 |
||
137 |
@property |
|
138 |
def title(self): |
|
139 |
return self.req._('entity type') |
|
140 |
||
141 |
def vocabulary(self): |
|
142 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
143 |
""" |
|
144 |
etypes = self.rset.column_types(0) |
|
145 |
return sorted((self.req._(etype), etype) for etype in etypes) |
|
146 |
||
147 |
def add_rql_restrictions(self): |
|
148 |
"""add restriction for this facet into the rql syntax tree""" |
|
149 |
value = self.req.form.get(self.id) |
|
150 |
if not value: |
|
151 |
return |
|
152 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
153 |
||
154 |
||
155 |
class HasTextFacet(AbstractFacet): |
|
431 | 156 |
__selectors__ = (one_has_relation, match_context_prop) |
0 | 157 |
id = 'has_text-facet' |
158 |
rtype = 'has_text' |
|
159 |
role = 'subject' |
|
160 |
order = 0 |
|
161 |
@property |
|
162 |
def title(self): |
|
163 |
return self.req._('has_text') |
|
164 |
||
165 |
def get_widget(self): |
|
166 |
"""return the widget instance to use to display this facet |
|
167 |
||
168 |
default implentation expects a .vocabulary method on the facet and |
|
169 |
return a combobox displaying this vocabulary |
|
170 |
""" |
|
171 |
return FacetStringWidget(self) |
|
172 |
||
173 |
def add_rql_restrictions(self): |
|
174 |
"""add restriction for this facet into the rql syntax tree""" |
|
175 |
value = self.req.form.get(self.id) |
|
176 |
if not value: |
|
177 |
return |
|
178 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |