author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 24 Feb 2009 11:59:53 +0100 | |
branch | tls-sprint |
changeset 940 | 15dcdc863965 |
parent 939 | ad72e06320e2 |
child 1091 | b5e253c0dd13 |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2 |
generic boxes for CubicWeb web client: |
|
3 |
||
4 |
* actions box |
|
5 |
* possible views box |
|
6 |
* rss icon |
|
7 |
||
8 |
additional (disabled by default) boxes |
|
9 |
* schema box |
|
10 |
* startup views box |
|
11 |
||
12 |
:organization: Logilab |
|
612 | 13 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 14 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
15 |
""" |
|
16 |
__docformat__ = "restructuredtext en" |
|
17 |
||
18 |
from logilab.mtconverter import html_escape |
|
19 |
||
811 | 20 |
from cubicweb.selectors import (any_rset, appobject_selectable, |
21 |
match_user_groups, non_final_entity) |
|
0 | 22 |
from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem |
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
23 |
from cubicweb.view import EntityView |
663 | 24 |
from cubicweb.web.box import BoxTemplate |
0 | 25 |
|
26 |
_ = unicode |
|
27 |
||
28 |
||
29 |
class EditBox(BoxTemplate): |
|
30 |
""" |
|
31 |
box with all actions impacting the entity displayed: edit, copy, delete |
|
32 |
change state, add related entities |
|
33 |
""" |
|
809 | 34 |
__select__ = BoxTemplate.__select__ & non_final_entity() |
0 | 35 |
id = 'edit_box' |
36 |
title = _('actions') |
|
37 |
order = 2 |
|
38 |
||
39 |
def call(self, **kwargs): |
|
40 |
_ = self.req._ |
|
41 |
title = _(self.title) |
|
42 |
if self.rset: |
|
43 |
etypes = self.rset.column_types(0) |
|
44 |
if len(etypes) == 1: |
|
45 |
plural = self.rset.rowcount > 1 and 'plural' or '' |
|
46 |
etypelabel = display_name(self.req, iter(etypes).next(), plural) |
|
47 |
title = u'%s - %s' % (title, etypelabel.lower()) |
|
48 |
box = BoxWidget(title, self.id, _class="greyBoxFrame") |
|
49 |
# build list of actions |
|
50 |
actions = self.vreg.possible_actions(self.req, self.rset) |
|
51 |
add_menu = BoxMenu(_('add')) # 'addrelated' category |
|
52 |
other_menu = BoxMenu(_('more actions')) # 'moreactions' category |
|
53 |
searchstate = self.req.search_state[0] |
|
54 |
for category, menu in (('mainactions', box), |
|
55 |
('addrelated', add_menu), |
|
56 |
('moreactions', other_menu)): |
|
57 |
for action in actions.get(category, ()): |
|
58 |
menu.append(self.box_action(action)) |
|
59 |
if self.rset and self.rset.rowcount == 1 and \ |
|
60 |
not self.schema[self.rset.description[0][0]].is_final() and \ |
|
61 |
searchstate == 'normal': |
|
62 |
entity = self.rset.get_entity(0, 0) |
|
63 |
#entity.complete() |
|
64 |
if add_menu.items: |
|
65 |
self.info('explicit actions defined, ignoring potential rtags for %s', |
|
66 |
entity.e_schema) |
|
67 |
else: |
|
68 |
# some addrelated actions may be specified but no one is selectable |
|
69 |
# in which case we should not fallback to schema_actions. The proper |
|
70 |
# way to avoid this is to override add_related_schemas() on the |
|
71 |
# entity class to return an empty list |
|
72 |
for action in self.schema_actions(entity): |
|
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
73 |
add_menu.append(action) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
74 |
self.workflow_actions(entity, box) |
0 | 75 |
if box.is_empty() and not other_menu.is_empty(): |
76 |
box.items = other_menu.items |
|
77 |
other_menu.items = [] |
|
78 |
self.add_submenu(box, add_menu, _('add')) |
|
79 |
self.add_submenu(box, other_menu) |
|
80 |
if not box.is_empty(): |
|
81 |
box.render(self.w) |
|
82 |
||
83 |
def add_submenu(self, box, submenu, label_prefix=None): |
|
84 |
if len(submenu.items) == 1: |
|
85 |
boxlink = submenu.items[0] |
|
86 |
if label_prefix: |
|
87 |
boxlink.label = u'%s %s' % (label_prefix, boxlink.label) |
|
88 |
box.append(boxlink) |
|
89 |
elif submenu.items: |
|
90 |
box.append(submenu) |
|
91 |
||
92 |
def schema_actions(self, entity): |
|
93 |
user = self.req.user |
|
94 |
actions = [] |
|
95 |
_ = self.req._ |
|
96 |
eschema = entity.e_schema |
|
97 |
for rschema, teschema, x in entity.add_related_schemas(): |
|
98 |
if x == 'subject': |
|
99 |
label = 'add %s %s %s %s' % (eschema, rschema, teschema, x) |
|
100 |
url = self.linkto_url(entity, rschema, teschema, 'object') |
|
101 |
else: |
|
102 |
label = 'add %s %s %s %s' % (teschema, rschema, eschema, x) |
|
103 |
url = self.linkto_url(entity, rschema, teschema, 'subject') |
|
104 |
actions.append(self.mk_action(_(label), url)) |
|
105 |
return actions |
|
106 |
||
107 |
||
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
108 |
def workflow_actions(self, entity, box): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
109 |
if 'in_state' in entity.e_schema.subject_relations() and entity.in_state: |
399 | 110 |
_ = self.req._ |
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
111 |
state = entity.in_state[0] |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
112 |
transitions = list(state.transitions(entity)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
113 |
if transitions: |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
114 |
menu_title = u'%s: %s' % (_('state'), state.view('text')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
115 |
menu_items = [] |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
116 |
for tr in state.transitions(entity): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
117 |
url = entity.absolute_url(vid='statuschange', treid=tr.eid) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
118 |
menu_items.append(self.mk_action(_(tr.name), url)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
119 |
box.append(BoxMenu(menu_title, menu_items)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
120 |
# when there are no possible transition, put state if the menu if |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
121 |
# there are some other actions |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
122 |
elif not box.is_empty(): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
123 |
menu_title = u'<a title="%s">%s: <i>%s</i></a>' % ( |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
124 |
_('no possible transition'), _('state'), state.view('text')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
125 |
box.append(RawBoxItem(menu_title, 'boxMainactions')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
126 |
return None |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
127 |
|
0 | 128 |
def linkto_url(self, entity, rtype, etype, target): |
129 |
return self.build_url(vid='creation', etype=etype, |
|
130 |
__linkto='%s:%s:%s' % (rtype, entity.eid, target), |
|
131 |
__redirectpath=entity.rest_path(), # should not be url quoted! |
|
132 |
__redirectvid=self.req.form.get('vid', '')) |
|
133 |
||
134 |
||
135 |
class SearchBox(BoxTemplate): |
|
136 |
"""display a box with a simple search form""" |
|
137 |
id = 'search_box' |
|
809 | 138 |
|
0 | 139 |
visible = True # enabled by default |
140 |
title = _('search') |
|
141 |
order = 0 |
|
142 |
formdef = u"""<form action="%s"> |
|
143 |
<table id="tsearch"><tr><td> |
|
144 |
<input id="norql" type="text" accesskey="q" tabindex="%s" title="search text" value="%s" name="rql" /> |
|
145 |
<input type="hidden" name="__fromsearchbox" value="1" /> |
|
146 |
<input type="hidden" name="subvid" value="tsearch" /> |
|
147 |
</td><td> |
|
852
105893288777
simplify css style
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
536
diff
changeset
|
148 |
<input tabindex="%s" type="submit" id="rqlboxsubmit" class="rqlsubmit" value="" /> |
0 | 149 |
</td></tr></table> |
150 |
</form>""" |
|
151 |
||
152 |
def call(self, view=None, **kwargs): |
|
153 |
req = self.req |
|
154 |
if req.form.pop('__fromsearchbox', None): |
|
155 |
rql = req.form.get('rql', '') |
|
156 |
else: |
|
157 |
rql = '' |
|
158 |
form = self.formdef % (req.build_url('view'), req.next_tabindex(), |
|
159 |
html_escape(rql), req.next_tabindex()) |
|
160 |
title = u"""<span onclick="javascript: toggleVisibility('rqlinput')">%s</span>""" % req._(self.title) |
|
161 |
box = BoxWidget(title, self.id, _class="searchBoxFrame", islist=False, escape=False) |
|
162 |
box.append(BoxHtml(form)) |
|
163 |
box.render(self.w) |
|
164 |
||
165 |
||
166 |
# boxes disabled by default ################################################### |
|
167 |
||
168 |
class PossibleViewsBox(BoxTemplate): |
|
169 |
"""display a box containing links to all possible views""" |
|
170 |
id = 'possible_views_box' |
|
809 | 171 |
__select__ = BoxTemplate.__select__ & match_user_groups('users', 'managers') |
0 | 172 |
|
809 | 173 |
visible = False |
0 | 174 |
title = _('possible views') |
175 |
order = 10 |
|
176 |
||
177 |
def call(self, **kwargs): |
|
178 |
box = BoxWidget(self.req._(self.title), self.id) |
|
613 | 179 |
views = [v for v in self.vreg.possible_views(self.req, self.rset) |
180 |
if v.category != 'startupview'] |
|
181 |
for category, views in self.sort_actions(views): |
|
0 | 182 |
menu = BoxMenu(category) |
613 | 183 |
for view in views: |
184 |
menu.append(self.box_action(view)) |
|
0 | 185 |
box.append(menu) |
186 |
if not box.is_empty(): |
|
187 |
box.render(self.w) |
|
188 |
||
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
189 |
|
663 | 190 |
class RSSIconBox(BoxTemplate): |
0 | 191 |
"""just display the RSS icon on uniform result set""" |
663 | 192 |
id = 'rss' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
663
diff
changeset
|
193 |
__select__ = (BoxTemplate.__select__ |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
663
diff
changeset
|
194 |
& appobject_selectable('components', 'rss_feed_url')) |
0 | 195 |
|
809 | 196 |
visible = False |
0 | 197 |
order = 999 |
198 |
||
199 |
def call(self, **kwargs): |
|
663 | 200 |
try: |
201 |
rss = self.req.external_resource('RSS_LOGO') |
|
202 |
except KeyError: |
|
203 |
self.error('missing RSS_LOGO external resource') |
|
204 |
return |
|
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
205 |
urlgetter = self.vreg.select_component('rss_feed_url', self.req, self.rset) |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
206 |
url = urlgetter.feed_url() |
536 | 207 |
self.w(u'<a href="%s"><img src="%s" alt="rss"/></a>\n' % (html_escape(url), rss)) |
0 | 208 |
|
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
107
diff
changeset
|
209 |
|
0 | 210 |
class StartupViewsBox(BoxTemplate): |
211 |
"""display a box containing links to all startup views""" |
|
212 |
id = 'startup_views_box' |
|
809 | 213 |
|
0 | 214 |
visible = False # disabled by default |
215 |
title = _('startup views') |
|
216 |
order = 70 |
|
217 |
||
218 |
def call(self, **kwargs): |
|
219 |
box = BoxWidget(self.req._(self.title), self.id) |
|
220 |
for view in self.vreg.possible_views(self.req, None): |
|
221 |
if view.category == 'startupview': |
|
222 |
box.append(self.box_action(view)) |
|
223 |
||
224 |
if not box.is_empty(): |
|
225 |
box.render(self.w) |
|
226 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
227 |
# helper classes ############################################################## |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
228 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
229 |
class SideBoxView(EntityView): |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
230 |
"""helper view class to display some entities in a sidebox""" |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
231 |
id = 'sidebox' |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
232 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
233 |
def call(self, boxclass='sideBox', title=u''): |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
234 |
"""display a list of entities by calling their <item_vid> view""" |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
235 |
if title: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
236 |
self.w(u'<div class="sideBoxTitle"><span>%s</span></div>' % title) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
237 |
self.w(u'<div class="%s"><div class="sideBoxBody">' % boxclass) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
238 |
# if not too much entities, show them all in a list |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
239 |
maxrelated = self.req.property_value('navigation.related-limit') |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
240 |
if self.rset.rowcount <= maxrelated: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
241 |
if len(self.rset) == 1: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
242 |
self.wview('incontext', self.rset, row=0) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
243 |
elif 1 < len(self.rset) < 5: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
244 |
self.wview('csv', self.rset) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
245 |
else: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
246 |
self.wview('simplelist', self.rset) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
247 |
# else show links to display related entities |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
248 |
else: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
249 |
self.rset.limit(maxrelated) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
250 |
rql = self.rset.printable_rql(encoded=False) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
251 |
self.wview('simplelist', self.rset) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
252 |
self.w(u'[<a href="%s">%s</a>]' % (self.build_url(rql=rql), |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
253 |
self.req._('see them all'))) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
811
diff
changeset
|
254 |
self.w(u'</div>\n</div>\n') |