author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Tue, 07 Jul 2009 11:13:09 +0200 | |
branch | stable |
changeset 2323 | 017bc7d7552d |
parent 2283 | dc13ec07a70a |
child 2325 | 363b66a15668 |
child 2337 | 6620e5abcd99 |
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 |
|
2260
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
44 |
bkLinkBox_template = u'<div class="facetTitle">%s</div>' |
2283 | 45 |
|
0 | 46 |
def facetargs(self): |
47 |
"""this method returns the list of extra arguments that should |
|
48 |
be used by the facet |
|
49 |
""" |
|
50 |
return {} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
51 |
|
0 | 52 |
def _get_context(self, view): |
53 |
context = getattr(view, 'filter_box_context_info', lambda: None)() |
|
54 |
if context: |
|
55 |
rset, vid, divid, paginate = context |
|
56 |
else: |
|
57 |
rset = self.rset |
|
58 |
vid, divid = None, 'pageContent' |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
59 |
paginate = view and view.need_navigation |
0 | 60 |
return rset, vid, divid, paginate |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
61 |
|
0 | 62 |
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
|
63 |
req = self.req |
2215
b1977f5263ac
by popular demand, let us setle on needs_xxx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2214
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
if self.roundcorners: |
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
67 |
req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");') |
1132 | 68 |
rset, vid, divid, paginate = self._get_context(view) |
0 | 69 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
70 |
return |
|
71 |
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
|
72 |
vid = req.form.get('vid') |
0 | 73 |
rqlst = rset.syntax_tree() |
74 |
rqlst.save_state() |
|
75 |
try: |
|
76 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
77 |
widgets = [] |
|
78 |
for facet in self.get_facets(rset, mainvar): |
|
79 |
if facet.propval('visible'): |
|
80 |
wdg = facet.get_widget() |
|
81 |
if wdg is not None: |
|
82 |
widgets.append(wdg) |
|
83 |
if not widgets: |
|
84 |
return |
|
2323
017bc7d7552d
test self.bkLinkBox_template
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2283
diff
changeset
|
85 |
if self.bkLinkBox_template: |
017bc7d7552d
test self.bkLinkBox_template
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2283
diff
changeset
|
86 |
self.displayBookmarkLink(rset) |
0 | 87 |
w = self.w |
88 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
|
89 |
divid, html_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
|
90 |
w(u'<fieldset>') |
|
91 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
92 |
'baserql': baserql} |
|
93 |
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
|
94 |
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
|
95 |
hiddens[param] = req.form[param] |
0 | 96 |
filter_hiddens(w, **hiddens) |
97 |
for wdg in widgets: |
|
98 |
wdg.render(w=self.w) |
|
99 |
w(u'</fieldset>\n</form>\n') |
|
100 |
finally: |
|
101 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
102 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
103 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 104 |
|
2260
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
105 |
def displayBookmarkLink(self, rset): |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
106 |
eschema = self.schema.eschema('Bookmark') |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
107 |
if eschema.has_perm(self.req, 'add'): |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
108 |
bk_path = 'view?rql=%s' % rset.printable_rql() |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
109 |
bk_title = self.req._('my custom search') |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
110 |
linkto = 'bookmarked_by:%s:subject' % self.req.user.eid |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
bk_link = u'<a cubicweb:target="%s" id="facetBkLink" href="%s">%s</a>' % ( |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
114 |
html_escape(bk_base_url), |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
115 |
html_escape(bk_add_url), |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
116 |
self.req._('bookmark this search')) |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
117 |
self.w(self.bkLinkBox_template % bk_link) |
080167dad19e
split FilterBox call function
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2215
diff
changeset
|
118 |
|
0 | 119 |
def get_facets(self, rset, mainvar): |
120 |
return self.vreg.possible_vobjects('facets', self.req, rset, |
|
121 |
context='facetbox', |
|
122 |
filtered_variable=mainvar) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
123 |
|
0 | 124 |
# facets ###################################################################### |
125 |
||
126 |
class CreatedByFacet(RelationFacet): |
|
127 |
id = 'created_by-facet' |
|
128 |
rtype = 'created_by' |
|
129 |
target_attr = 'login' |
|
130 |
||
131 |
class InGroupFacet(RelationFacet): |
|
132 |
id = 'in_group-facet' |
|
133 |
rtype = 'in_group' |
|
134 |
target_attr = 'name' |
|
135 |
||
136 |
class InStateFacet(RelationFacet): |
|
137 |
id = 'in_state-facet' |
|
138 |
rtype = 'in_state' |
|
139 |
target_attr = 'name' |
|
140 |
||
141 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
142 |
class ETypeFacet(RelationFacet): |
|
143 |
id = 'etype-facet' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
144 |
__select__ = yes() |
0 | 145 |
order = 1 |
146 |
rtype = 'is' |
|
147 |
target_attr = 'name' |
|
148 |
||
149 |
@property |
|
150 |
def title(self): |
|
151 |
return self.req._('entity type') |
|
152 |
||
153 |
def vocabulary(self): |
|
154 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
155 |
""" |
|
156 |
etypes = self.rset.column_types(0) |
|
157 |
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
|
158 |
|
0 | 159 |
def add_rql_restrictions(self): |
160 |
"""add restriction for this facet into the rql syntax tree""" |
|
161 |
value = self.req.form.get(self.id) |
|
162 |
if not value: |
|
163 |
return |
|
164 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
165 |
||
166 |
||
167 |
class HasTextFacet(AbstractFacet): |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
168 |
__select__ = relation_possible('has_text', 'subject') & match_context_prop() |
0 | 169 |
id = 'has_text-facet' |
170 |
rtype = 'has_text' |
|
171 |
role = 'subject' |
|
172 |
order = 0 |
|
173 |
@property |
|
174 |
def title(self): |
|
175 |
return self.req._('has_text') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
176 |
|
0 | 177 |
def get_widget(self): |
178 |
"""return the widget instance to use to display this facet |
|
179 |
||
180 |
default implentation expects a .vocabulary method on the facet and |
|
181 |
return a combobox displaying this vocabulary |
|
182 |
""" |
|
183 |
return FacetStringWidget(self) |
|
184 |
||
185 |
def add_rql_restrictions(self): |
|
186 |
"""add restriction for this facet into the rql syntax tree""" |
|
187 |
value = self.req.form.get(self.id) |
|
188 |
if not value: |
|
189 |
return |
|
190 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |