author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 17 Feb 2009 21:46:46 +0100 | |
branch | tls-sprint |
changeset 697 | 06807984e610 |
parent 692 | 800592b8d39b |
child 742 | 99115e029dca |
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 |
||
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
13 |
from cubicweb.selectors import (chainfirst, chainall, non_final_entity, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
14 |
two_lines_rset, match_context_prop, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
15 |
yes, one_has_relation) |
0 | 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' |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
617
diff
changeset
|
30 |
__selectors__ = ((non_final_entity() & two_lines_rset) | contextview_selector, |
431 | 31 |
match_context_prop) |
0 | 32 |
context = 'left' |
33 |
title = _('boxes_filter_box') |
|
34 |
visible = True # functionality provided by the search box by default |
|
35 |
order = 1 |
|
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
36 |
roundcorners = True |
0 | 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') |
205
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
58 |
if self.roundcorners: |
8ea3294e7427
make round corners optional on facet boxes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
204
diff
changeset
|
59 |
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
|
60 |
rset, vid, divid, paginate=self._get_context(view) |
0 | 61 |
if rset.rowcount < 2: # XXX done by selectors, though maybe necessary when rset has been hijacked |
62 |
return |
|
63 |
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
|
64 |
vid = req.form.get('vid') |
0 | 65 |
rqlst = rset.syntax_tree() |
66 |
rqlst.save_state() |
|
67 |
try: |
|
68 |
mainvar, baserql = prepare_facets_rqlst(rqlst, rset.args) |
|
69 |
widgets = [] |
|
70 |
for facet in self.get_facets(rset, mainvar): |
|
71 |
if facet.propval('visible'): |
|
72 |
wdg = facet.get_widget() |
|
73 |
if wdg is not None: |
|
74 |
widgets.append(wdg) |
|
75 |
if not widgets: |
|
76 |
return |
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
req._('bookmark this search'))) |
0 | 89 |
w(u'<form method="post" id="%sForm" cubicweb:facetargs="%s" action="">' % ( |
90 |
divid, html_escape(dumps([divid, vid, paginate, self.facetargs()])))) |
|
91 |
w(u'<fieldset>') |
|
92 |
hiddens = {'facets': ','.join(wdg.facet.id for wdg in widgets), |
|
93 |
'baserql': baserql} |
|
94 |
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
|
95 |
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
|
96 |
hiddens[param] = req.form[param] |
0 | 97 |
filter_hiddens(w, **hiddens) |
98 |
for wdg in widgets: |
|
99 |
wdg.render(w=self.w) |
|
100 |
w(u'</fieldset>\n</form>\n') |
|
101 |
finally: |
|
102 |
rqlst.recover() |
|
47
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
103 |
import cubicweb |
54087a269bdd
fix tests (some where broken after ECache was added)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
16
diff
changeset
|
104 |
cubicweb.info('after facets with rql: %s' % repr(rqlst)) |
0 | 105 |
|
106 |
def get_facets(self, rset, mainvar): |
|
107 |
return self.vreg.possible_vobjects('facets', self.req, rset, |
|
108 |
context='facetbox', |
|
109 |
filtered_variable=mainvar) |
|
110 |
||
111 |
# facets ###################################################################### |
|
112 |
||
113 |
class CreatedByFacet(RelationFacet): |
|
114 |
id = 'created_by-facet' |
|
115 |
rtype = 'created_by' |
|
116 |
target_attr = 'login' |
|
117 |
||
118 |
class InGroupFacet(RelationFacet): |
|
119 |
id = 'in_group-facet' |
|
120 |
rtype = 'in_group' |
|
121 |
target_attr = 'name' |
|
122 |
||
123 |
class InStateFacet(RelationFacet): |
|
124 |
id = 'in_state-facet' |
|
125 |
rtype = 'in_state' |
|
126 |
target_attr = 'name' |
|
127 |
||
128 |
# inherit from RelationFacet to benefit from its possible_values implementation |
|
129 |
class ETypeFacet(RelationFacet): |
|
130 |
id = 'etype-facet' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
205
diff
changeset
|
131 |
__selectors__ = (yes,) |
0 | 132 |
order = 1 |
133 |
rtype = 'is' |
|
134 |
target_attr = 'name' |
|
135 |
||
136 |
@property |
|
137 |
def title(self): |
|
138 |
return self.req._('entity type') |
|
139 |
||
140 |
def vocabulary(self): |
|
141 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
142 |
""" |
|
143 |
etypes = self.rset.column_types(0) |
|
144 |
return sorted((self.req._(etype), etype) for etype in etypes) |
|
145 |
||
146 |
def add_rql_restrictions(self): |
|
147 |
"""add restriction for this facet into the rql syntax tree""" |
|
148 |
value = self.req.form.get(self.id) |
|
149 |
if not value: |
|
150 |
return |
|
151 |
self.rqlst.add_type_restriction(self.filtered_variable, value) |
|
152 |
||
153 |
||
154 |
class HasTextFacet(AbstractFacet): |
|
431 | 155 |
__selectors__ = (one_has_relation, match_context_prop) |
0 | 156 |
id = 'has_text-facet' |
157 |
rtype = 'has_text' |
|
158 |
role = 'subject' |
|
159 |
order = 0 |
|
160 |
@property |
|
161 |
def title(self): |
|
162 |
return self.req._('has_text') |
|
163 |
||
164 |
def get_widget(self): |
|
165 |
"""return the widget instance to use to display this facet |
|
166 |
||
167 |
default implentation expects a .vocabulary method on the facet and |
|
168 |
return a combobox displaying this vocabulary |
|
169 |
""" |
|
170 |
return FacetStringWidget(self) |
|
171 |
||
172 |
def add_rql_restrictions(self): |
|
173 |
"""add restriction for this facet into the rql syntax tree""" |
|
174 |
value = self.req.form.get(self.id) |
|
175 |
if not value: |
|
176 |
return |
|
177 |
self.rqlst.add_constant_restriction(self.filtered_variable, 'has_text', value, 'String') |