author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 01 Oct 2009 16:06:29 +0200 | |
branch | stable |
changeset 3544 | 188d86631c45 |
parent 3006 | 4d85386b06a5 |
child 3023 | 7864fee8b4ec |
child 3717 | a0cb4ed395f4 |
permissions | -rw-r--r-- |
0 | 1 |
"""the facets box and some basic facets |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from simplejson import dumps |
|
11 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2283
diff
changeset
|
12 |
from logilab.mtconverter import xml_escape |
0 | 13 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
14 |
from cubicweb.appobject import objectify_selector |
1132 | 15 |
from cubicweb.selectors import (non_final_entity, two_lines_rset, |
16 |
match_context_prop, yes, relation_possible) |
|
0 | 17 |
from cubicweb.web.box import BoxTemplate |
1132 | 18 |
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
|
19 |
prepare_facets_rqlst, filter_hiddens, _cleanup_rqlst, |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
20 |
_prepare_vocabulary_rqlst) |
0 | 21 |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
22 |
@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
|
23 |
def contextview_selector(cls, req, rset=None, row=None, col=None, view=None, |
0 | 24 |
**kwargs): |
25 |
if view and getattr(view, 'filter_box_context_info', lambda: None)(): |
|
26 |
return 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
27 |
return 0 |
0 | 28 |
|
29 |
||
30 |
class FilterBox(BoxTemplate): |
|
31 |
"""filter results of a query""" |
|
32 |
id = 'filter_box' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
33 |
__select__ = (((non_final_entity() & two_lines_rset()) |
758
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
34 |
| contextview_selector() |
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
35 |
) & match_context_prop()) |
0 | 36 |
context = 'left' |
37 |
title = _('boxes_filter_box') |
|
38 |
visible = True # functionality provided by the search box by default |
|
39 |
order = 1 |
|
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
40 |
roundcorners = True |
2214
a6b42cf05407
[facets] slightly better attr names
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2213
diff
changeset
|
41 |
|
2215
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
|
2325
363b66a15668
[cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2323
diff
changeset
|
45 |
bk_linkbox_template = u'<div class="facetTitle">%s</div>' |
2283 | 46 |
|
0 | 47 |
def facetargs(self): |
48 |
"""this method returns the list of extra arguments that should |
|
49 |
be used by the facet |
|
50 |
""" |
|
51 |
return {} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
52 |
|
0 | 53 |
def _get_context(self, view): |
54 |
context = getattr(view, 'filter_box_context_info', lambda: None)() |
|
55 |
if context: |
|
56 |
rset, vid, divid, paginate = context |
|
57 |
else: |
|
58 |
rset = self.rset |
|
59 |
vid, divid = None, 'pageContent' |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
60 |
paginate = view and view.need_navigation |
0 | 61 |
return rset, vid, divid, paginate |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
62 |
|
0 | 63 |
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
|
64 |
req = self.req |
2215
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
if self.roundcorners: |
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
68 |
req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");') |
1132 | 69 |
rset, vid, divid, paginate = self._get_context(view) |
0 | 70 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
71 |
return |
|
72 |
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
|
73 |
vid = req.form.get('vid') |
0 | 74 |
rqlst = rset.syntax_tree() |
75 |
rqlst.save_state() |
|
76 |
try: |
|
77 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
78 |
widgets = [] |
|
79 |
for facet in self.get_facets(rset, mainvar): |
|
80 |
if facet.propval('visible'): |
|
81 |
wdg = facet.get_widget() |
|
82 |
if wdg is not None: |
|
83 |
widgets.append(wdg) |
|
84 |
if not widgets: |
|
85 |
return |
|
2325
363b66a15668
[cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2323
diff
changeset
|
86 |
if self.bk_linkbox_template: |
363b66a15668
[cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2323
diff
changeset
|
87 |
self.display_bookmark_link(rset) |
0 | 88 |
w = self.w |
89 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2283
diff
changeset
|
90 |
divid, xml_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
0 | 91 |
w(u'<fieldset>') |
92 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
93 |
'baserql': baserql} |
|
94 |
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
|
95 |
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
|
96 |
hiddens[param] = req.form[param] |
0 | 97 |
filter_hiddens(w, **hiddens) |
98 |
for wdg in widgets: |
|
99 |
wdg.render(w=self.w) |
|
100 |
w(u'</fieldset>\n</form>\n') |
|
101 |
finally: |
|
102 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
103 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
104 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 105 |
|
2325
363b66a15668
[cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2323
diff
changeset
|
106 |
def display_bookmark_link(self, rset): |
2260
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
107 |
eschema = self.schema.eschema('Bookmark') |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
108 |
if eschema.has_perm(self.req, 'add'): |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
109 |
bk_path = 'view?rql=%s' % rset.printable_rql() |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
110 |
bk_title = self.req._('my custom search') |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
111 |
linkto = 'bookmarked_by:%s:subject' % self.req.user.eid |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
112 |
bk_add_url = self.build_url('add/Bookmark', path=bk_path, title=bk_title, __linkto=linkto) |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
113 |
bk_base_url = self.build_url('add/Bookmark', title=bk_title, __linkto=linkto) |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
114 |
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
|
115 |
xml_escape(bk_base_url), |
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2283
diff
changeset
|
116 |
xml_escape(bk_add_url), |
2260
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
117 |
self.req._('bookmark this search')) |
2325
363b66a15668
[cleaning] minor cleaning over last two checkins
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2323
diff
changeset
|
118 |
self.w(self.bk_linkbox_template % bk_link) |
2260
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
119 |
|
0 | 120 |
def get_facets(self, rset, mainvar): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
121 |
return self.vreg['facets'].possible_vobjects(self.req, rset=rset, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
122 |
context='facetbox', |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
123 |
filtered_variable=mainvar) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
124 |
|
0 | 125 |
# facets ###################################################################### |
126 |
||
127 |
class CreatedByFacet(RelationFacet): |
|
128 |
id = 'created_by-facet' |
|
129 |
rtype = 'created_by' |
|
130 |
target_attr = 'login' |
|
131 |
||
132 |
class InGroupFacet(RelationFacet): |
|
133 |
id = 'in_group-facet' |
|
134 |
rtype = 'in_group' |
|
135 |
target_attr = 'name' |
|
136 |
||
137 |
class InStateFacet(RelationFacet): |
|
138 |
id = 'in_state-facet' |
|
139 |
rtype = 'in_state' |
|
140 |
target_attr = 'name' |
|
141 |
||
142 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
143 |
class ETypeFacet(RelationFacet): |
|
144 |
id = 'etype-facet' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
145 |
__select__ = yes() |
0 | 146 |
order = 1 |
147 |
rtype = 'is' |
|
148 |
target_attr = 'name' |
|
149 |
||
150 |
@property |
|
151 |
def title(self): |
|
152 |
return self.req._('entity type') |
|
153 |
||
154 |
def vocabulary(self): |
|
155 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
156 |
""" |
|
157 |
etypes = self.rset.column_types(0) |
|
158 |
return sorted((self.req._(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
|
159 |
|
0 | 160 |
def add_rql_restrictions(self): |
161 |
"""add restriction for this facet into the rql syntax tree""" |
|
162 |
value = self.req.form.get(self.id) |
|
163 |
if not value: |
|
164 |
return |
|
165 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
166 |
||
2337
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
167 |
def possible_values(self): |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
168 |
"""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
|
169 |
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
|
170 |
""" |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
171 |
rqlst = self.rqlst |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
172 |
rqlst.save_state() |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
173 |
try: |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
174 |
_cleanup_rqlst(rqlst, self.filtered_variable) |
2340
65445ae67db0
optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2339
diff
changeset
|
175 |
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
|
176 |
attrvar = rqlst.make_variable() |
65445ae67db0
optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2339
diff
changeset
|
177 |
rqlst.add_selected(attrvar) |
65445ae67db0
optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2339
diff
changeset
|
178 |
rqlst.add_relation(etype_var, 'name', attrvar) |
65445ae67db0
optimize rql for ETypeFacet possible_values
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2339
diff
changeset
|
179 |
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
|
180 |
finally: |
6620e5abcd99
select right possible_values for ETypeFacet
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2323
diff
changeset
|
181 |
rqlst.recover() |
0 | 182 |
|
183 |
class HasTextFacet(AbstractFacet): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
184 |
__select__ = relation_possible('has_text', 'subject') & match_context_prop() |
0 | 185 |
id = 'has_text-facet' |
186 |
rtype = 'has_text' |
|
187 |
role = 'subject' |
|
188 |
order = 0 |
|
189 |
@property |
|
190 |
def title(self): |
|
191 |
return self.req._('has_text') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
192 |
|
0 | 193 |
def get_widget(self): |
194 |
"""return the widget instance to use to display this facet |
|
195 |
||
196 |
default implentation expects a .vocabulary method on the facet and |
|
197 |
return a combobox displaying this vocabulary |
|
198 |
""" |
|
199 |
return FacetStringWidget(self) |
|
200 |
||
201 |
def add_rql_restrictions(self): |
|
202 |
"""add restriction for this facet into the rql syntax tree""" |
|
203 |
value = self.req.form.get(self.id) |
|
204 |
if not value: |
|
205 |
return |
|
206 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |