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