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