author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 02 Jul 2009 10:35:03 +0200 | |
changeset 2234 | 1fbcf202882d |
parent 2181 | 94ca417b9b07 |
parent 2215 | b1977f5263ac |
child 2293 | 7ded2a1416e4 |
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 |
||
12 |
from logilab.mtconverter import html_escape |
|
13 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
14 |
from cubicweb.vregistry 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, |
19 |
prepare_facets_rqlst, filter_hiddens) |
|
0 | 20 |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
21 |
@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
|
22 |
def contextview_selector(cls, req, rset=None, row=None, col=None, view=None, |
0 | 23 |
**kwargs): |
24 |
if view and getattr(view, 'filter_box_context_info', lambda: None)(): |
|
25 |
return 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
26 |
return 0 |
0 | 27 |
|
28 |
||
29 |
class FilterBox(BoxTemplate): |
|
30 |
"""filter results of a query""" |
|
31 |
id = 'filter_box' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
32 |
__select__ = (((non_final_entity() & two_lines_rset()) |
758
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
33 |
| contextview_selector() |
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
34 |
) & match_context_prop()) |
0 | 35 |
context = 'left' |
36 |
title = _('boxes_filter_box') |
|
37 |
visible = True # functionality provided by the search box by default |
|
38 |
order = 1 |
|
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
39 |
roundcorners = True |
2214
a6b42cf05407
[facets] slightly better attr names
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2213
diff
changeset
|
40 |
|
2215
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
41 |
needs_css = 'cubicweb.facets.css' |
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
42 |
needs_js = ('cubicweb.ajax.js', 'cubicweb.formfilter.js') |
2214
a6b42cf05407
[facets] slightly better attr names
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2213
diff
changeset
|
43 |
|
0 | 44 |
def facetargs(self): |
45 |
"""this method returns the list of extra arguments that should |
|
46 |
be used by the facet |
|
47 |
""" |
|
48 |
return {} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
49 |
|
0 | 50 |
def _get_context(self, view): |
51 |
context = getattr(view, 'filter_box_context_info', lambda: None)() |
|
52 |
if context: |
|
53 |
rset, vid, divid, paginate = context |
|
54 |
else: |
|
55 |
rset = self.rset |
|
56 |
vid, divid = None, 'pageContent' |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
57 |
paginate = view and view.need_navigation |
0 | 58 |
return rset, vid, divid, paginate |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
59 |
|
0 | 60 |
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
|
61 |
req = self.req |
2215
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
if self.roundcorners: |
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
65 |
req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");') |
1132 | 66 |
rset, vid, divid, paginate = self._get_context(view) |
0 | 67 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
68 |
return |
|
69 |
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
|
70 |
vid = req.form.get('vid') |
0 | 71 |
rqlst = rset.syntax_tree() |
72 |
rqlst.save_state() |
|
73 |
try: |
|
74 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
75 |
widgets = [] |
|
76 |
for facet in self.get_facets(rset, mainvar): |
|
77 |
if facet.propval('visible'): |
|
78 |
wdg = facet.get_widget() |
|
79 |
if wdg is not None: |
|
80 |
widgets.append(wdg) |
|
81 |
if not widgets: |
|
82 |
return |
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
req._('bookmark this search'))) |
0 | 95 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
96 |
divid, html_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
|
97 |
w(u'<fieldset>') |
|
98 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
99 |
'baserql': baserql} |
|
100 |
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
|
101 |
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
|
102 |
hiddens[param] = req.form[param] |
0 | 103 |
filter_hiddens(w, **hiddens) |
104 |
for wdg in widgets: |
|
105 |
wdg.render(w=self.w) |
|
106 |
w(u'</fieldset>\n</form>\n') |
|
107 |
finally: |
|
108 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
109 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
110 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 111 |
|
112 |
def get_facets(self, rset, mainvar): |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
113 |
return self.vreg.possible_vobjects('facets', self.req, rset=rset, |
0 | 114 |
context='facetbox', |
115 |
filtered_variable=mainvar) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
116 |
|
0 | 117 |
# facets ###################################################################### |
118 |
||
119 |
class CreatedByFacet(RelationFacet): |
|
120 |
id = 'created_by-facet' |
|
121 |
rtype = 'created_by' |
|
122 |
target_attr = 'login' |
|
123 |
||
124 |
class InGroupFacet(RelationFacet): |
|
125 |
id = 'in_group-facet' |
|
126 |
rtype = 'in_group' |
|
127 |
target_attr = 'name' |
|
128 |
||
129 |
class InStateFacet(RelationFacet): |
|
130 |
id = 'in_state-facet' |
|
131 |
rtype = 'in_state' |
|
132 |
target_attr = 'name' |
|
133 |
||
134 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
135 |
class ETypeFacet(RelationFacet): |
|
136 |
id = 'etype-facet' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
137 |
__select__ = yes() |
0 | 138 |
order = 1 |
139 |
rtype = 'is' |
|
140 |
target_attr = 'name' |
|
141 |
||
142 |
@property |
|
143 |
def title(self): |
|
144 |
return self.req._('entity type') |
|
145 |
||
146 |
def vocabulary(self): |
|
147 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
148 |
""" |
|
149 |
etypes = self.rset.column_types(0) |
|
150 |
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
|
151 |
|
0 | 152 |
def add_rql_restrictions(self): |
153 |
"""add restriction for this facet into the rql syntax tree""" |
|
154 |
value = self.req.form.get(self.id) |
|
155 |
if not value: |
|
156 |
return |
|
157 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
158 |
||
159 |
||
160 |
class HasTextFacet(AbstractFacet): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
161 |
__select__ = relation_possible('has_text', 'subject') & match_context_prop() |
0 | 162 |
id = 'has_text-facet' |
163 |
rtype = 'has_text' |
|
164 |
role = 'subject' |
|
165 |
order = 0 |
|
166 |
@property |
|
167 |
def title(self): |
|
168 |
return self.req._('has_text') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
169 |
|
0 | 170 |
def get_widget(self): |
171 |
"""return the widget instance to use to display this facet |
|
172 |
||
173 |
default implentation expects a .vocabulary method on the facet and |
|
174 |
return a combobox displaying this vocabulary |
|
175 |
""" |
|
176 |
return FacetStringWidget(self) |
|
177 |
||
178 |
def add_rql_restrictions(self): |
|
179 |
"""add restriction for this facet into the rql syntax tree""" |
|
180 |
value = self.req.form.get(self.id) |
|
181 |
if not value: |
|
182 |
return |
|
183 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |