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