author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 25 Nov 2008 10:58:56 +0100 | |
changeset 134 | dca5a995c16a |
parent 47 | 54087a269bdd |
child 204 | 4b01608c5397 |
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') ) |
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8
diff
changeset
|
57 |
rset, vid, divid, paginate=self._get_context(view) |
0 | 58 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
59 |
return |
|
60 |
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
|
61 |
vid = req.form.get('vid') |
0 | 62 |
rqlst = rset.syntax_tree() |
63 |
rqlst.save_state() |
|
64 |
try: |
|
65 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
66 |
widgets = [] |
|
67 |
for facet in self.get_facets(rset, mainvar): |
|
68 |
if facet.propval('visible'): |
|
69 |
wdg = facet.get_widget() |
|
70 |
if wdg is not None: |
|
71 |
widgets.append(wdg) |
|
72 |
if not widgets: |
|
73 |
return |
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
req._('bookmark this search'))) |
0 | 86 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
87 |
divid, html_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
|
88 |
w(u'<fieldset>') |
|
89 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
90 |
'baserql': baserql} |
|
91 |
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
|
92 |
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
|
93 |
hiddens[param] = req.form[param] |
0 | 94 |
filter_hiddens(w, **hiddens) |
95 |
for wdg in widgets: |
|
96 |
wdg.render(w=self.w) |
|
97 |
w(u'</fieldset>\n</form>\n') |
|
98 |
finally: |
|
99 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
100 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
101 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 102 |
|
103 |
def get_facets(self, rset, mainvar): |
|
104 |
return self.vreg.possible_vobjects('facets', self.req, rset, |
|
105 |
context='facetbox', |
|
106 |
filtered_variable=mainvar) |
|
107 |
||
108 |
# facets ###################################################################### |
|
109 |
||
110 |
class CreatedByFacet(RelationFacet): |
|
111 |
id = 'created_by-facet' |
|
112 |
rtype = 'created_by' |
|
113 |
target_attr = 'login' |
|
114 |
||
115 |
class InGroupFacet(RelationFacet): |
|
116 |
id = 'in_group-facet' |
|
117 |
rtype = 'in_group' |
|
118 |
target_attr = 'name' |
|
119 |
||
120 |
class InStateFacet(RelationFacet): |
|
121 |
id = 'in_state-facet' |
|
122 |
rtype = 'in_state' |
|
123 |
target_attr = 'name' |
|
124 |
||
125 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
126 |
class ETypeFacet(RelationFacet): |
|
127 |
id = 'etype-facet' |
|
128 |
__selectors__ = (yes_selector,) |
|
129 |
order = 1 |
|
130 |
rtype = 'is' |
|
131 |
target_attr = 'name' |
|
132 |
||
133 |
@property |
|
134 |
def title(self): |
|
135 |
return self.req._('entity type') |
|
136 |
||
137 |
def vocabulary(self): |
|
138 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
139 |
""" |
|
140 |
etypes = self.rset.column_types(0) |
|
141 |
return sorted((self.req._(etype), etype) for etype in etypes) |
|
142 |
||
143 |
def add_rql_restrictions(self): |
|
144 |
"""add restriction for this facet into the rql syntax tree""" |
|
145 |
value = self.req.form.get(self.id) |
|
146 |
if not value: |
|
147 |
return |
|
148 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
149 |
||
150 |
||
151 |
class HasTextFacet(AbstractFacet): |
|
152 |
__selectors__ = (one_has_relation_selector, contextprop_selector) |
|
153 |
id = 'has_text-facet' |
|
154 |
rtype = 'has_text' |
|
155 |
role = 'subject' |
|
156 |
order = 0 |
|
157 |
@property |
|
158 |
def title(self): |
|
159 |
return self.req._('has_text') |
|
160 |
||
161 |
def get_widget(self): |
|
162 |
"""return the widget instance to use to display this facet |
|
163 |
||
164 |
default implentation expects a .vocabulary method on the facet and |
|
165 |
return a combobox displaying this vocabulary |
|
166 |
""" |
|
167 |
return FacetStringWidget(self) |
|
168 |
||
169 |
def add_rql_restrictions(self): |
|
170 |
"""add restriction for this facet into the rql syntax tree""" |
|
171 |
value = self.req.form.get(self.id) |
|
172 |
if not value: |
|
173 |
return |
|
174 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |