author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 10 Dec 2008 10:38:23 +0100 | |
changeset 204 | 4b01608c5397 |
parent 47 | 54087a269bdd |
child 205 | 8ea3294e7427 |
permissions | -rw-r--r-- |
0 | 1 |
"""the facets box and some basic facets |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
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 |
||
13 |
from cubicweb.common.selectors import (chainfirst, chainall, nfentity_selector, |
|
14 |
twolinerset_selector, contextprop_selector, |
|
15 |
yes_selector, one_has_relation_selector) |
|
16 |
from cubicweb.web.box import BoxTemplate |
|
17 |
from cubicweb.web.facet import (AbstractFacet, VocabularyFacet, FacetStringWidget, |
|
18 |
RelationFacet, prepare_facets_rqlst, filter_hiddens) |
|
19 |
||
20 |
def contextview_selector(cls, req, rset, row=None, col=None, view=None, |
|
21 |
**kwargs): |
|
22 |
if view and getattr(view, 'filter_box_context_info', lambda: None)(): |
|
23 |
return 1 |
|
24 |
return 0 |
|
25 |
||
26 |
||
27 |
class FilterBox(BoxTemplate): |
|
28 |
"""filter results of a query""" |
|
29 |
id = 'filter_box' |
|
30 |
__selectors__ = (chainfirst(contextview_selector, |
|
31 |
chainall(nfentity_selector, twolinerset_selector)), |
|
32 |
contextprop_selector) |
|
33 |
context = 'left' |
|
34 |
title = _('boxes_filter_box') |
|
35 |
visible = True # functionality provided by the search box by default |
|
36 |
order = 1 |
|
37 |
||
38 |
def facetargs(self): |
|
39 |
"""this method returns the list of extra arguments that should |
|
40 |
be used by the facet |
|
41 |
""" |
|
42 |
return {} |
|
43 |
||
44 |
def _get_context(self, view): |
|
45 |
context = getattr(view, 'filter_box_context_info', lambda: None)() |
|
46 |
if context: |
|
47 |
rset, vid, divid, paginate = context |
|
48 |
else: |
|
49 |
rset = self.rset |
|
50 |
vid, divid = None, 'pageContent' |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
51 |
paginate = view and view.need_navigation |
0 | 52 |
return rset, vid, divid, paginate |
53 |
||
54 |
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
|
55 |
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
|
56 |
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
|
57 |
req.add_css('cubicweb.facets.css') |
4b01608c5397
include cubicbeb.facet.css when displaying filterbox
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
47
diff
changeset
|
58 |
req.html_headers.add_onload('jQuery(".facet").corner("tl br 10px");') |
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
59 |
rset, vid, divid, paginate=self._get_context(view) |
0 | 60 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
61 |
return |
|
62 |
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
|
63 |
vid = req.form.get('vid') |
0 | 64 |
rqlst = rset.syntax_tree() |
65 |
rqlst.save_state() |
|
66 |
try: |
|
67 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
68 |
widgets = [] |
|
69 |
for facet in self.get_facets(rset, mainvar): |
|
70 |
if facet.propval('visible'): |
|
71 |
wdg = facet.get_widget() |
|
72 |
if wdg is not None: |
|
73 |
widgets.append(wdg) |
|
74 |
if not widgets: |
|
75 |
return |
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
req._('bookmark this search'))) |
0 | 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 |
|
105 |
def get_facets(self, rset, mainvar): |
|
106 |
return self.vreg.possible_vobjects('facets', self.req, rset, |
|
107 |
context='facetbox', |
|
108 |
filtered_variable=mainvar) |
|
109 |
||
110 |
# facets ###################################################################### |
|
111 |
||
112 |
class CreatedByFacet(RelationFacet): |
|
113 |
id = 'created_by-facet' |
|
114 |
rtype = 'created_by' |
|
115 |
target_attr = 'login' |
|
116 |
||
117 |
class InGroupFacet(RelationFacet): |
|
118 |
id = 'in_group-facet' |
|
119 |
rtype = 'in_group' |
|
120 |
target_attr = 'name' |
|
121 |
||
122 |
class InStateFacet(RelationFacet): |
|
123 |
id = 'in_state-facet' |
|
124 |
rtype = 'in_state' |
|
125 |
target_attr = 'name' |
|
126 |
||
127 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
128 |
class ETypeFacet(RelationFacet): |
|
129 |
id = 'etype-facet' |
|
130 |
__selectors__ = (yes_selector,) |
|
131 |
order = 1 |
|
132 |
rtype = 'is' |
|
133 |
target_attr = 'name' |
|
134 |
||
135 |
@property |
|
136 |
def title(self): |
|
137 |
return self.req._('entity type') |
|
138 |
||
139 |
def vocabulary(self): |
|
140 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
141 |
""" |
|
142 |
etypes = self.rset.column_types(0) |
|
143 |
return sorted((self.req._(etype), etype) for etype in etypes) |
|
144 |
||
145 |
def add_rql_restrictions(self): |
|
146 |
"""add restriction for this facet into the rql syntax tree""" |
|
147 |
value = self.req.form.get(self.id) |
|
148 |
if not value: |
|
149 |
return |
|
150 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
151 |
||
152 |
||
153 |
class HasTextFacet(AbstractFacet): |
|
154 |
__selectors__ = (one_has_relation_selector, contextprop_selector) |
|
155 |
id = 'has_text-facet' |
|
156 |
rtype = 'has_text' |
|
157 |
role = 'subject' |
|
158 |
order = 0 |
|
159 |
@property |
|
160 |
def title(self): |
|
161 |
return self.req._('has_text') |
|
162 |
||
163 |
def get_widget(self): |
|
164 |
"""return the widget instance to use to display this facet |
|
165 |
||
166 |
default implentation expects a .vocabulary method on the facet and |
|
167 |
return a combobox displaying this vocabulary |
|
168 |
""" |
|
169 |
return FacetStringWidget(self) |
|
170 |
||
171 |
def add_rql_restrictions(self): |
|
172 |
"""add restriction for this facet into the rql syntax tree""" |
|
173 |
value = self.req.form.get(self.id) |
|
174 |
if not value: |
|
175 |
return |
|
176 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |