author | sylvain.thenault@logilab.fr |
Wed, 18 Feb 2009 17:22:10 +0100 | |
branch | tls-sprint |
changeset 819 | 7fd66f389ef7 |
parent 811 | c6ebabe9db91 |
child 824 | a5e6acffde30 |
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 |
663 | 23 |
from cubicweb.web.box import BoxTemplate |
0 | 24 |
|
25 |
_ = unicode |
|
26 |
||
27 |
||
28 |
class EditBox(BoxTemplate): |
|
29 |
""" |
|
30 |
box with all actions impacting the entity displayed: edit, copy, delete |
|
31 |
change state, add related entities |
|
32 |
""" |
|
809 | 33 |
__select__ = BoxTemplate.__select__ & non_final_entity() |
0 | 34 |
id = 'edit_box' |
35 |
title = _('actions') |
|
36 |
order = 2 |
|
37 |
||
38 |
def call(self, **kwargs): |
|
39 |
_ = self.req._ |
|
40 |
title = _(self.title) |
|
41 |
if self.rset: |
|
42 |
etypes = self.rset.column_types(0) |
|
43 |
if len(etypes) == 1: |
|
44 |
plural = self.rset.rowcount > 1 and 'plural' or '' |
|
45 |
etypelabel = display_name(self.req, iter(etypes).next(), plural) |
|
46 |
title = u'%s - %s' % (title, etypelabel.lower()) |
|
47 |
box = BoxWidget(title, self.id, _class="greyBoxFrame") |
|
48 |
# build list of actions |
|
49 |
actions = self.vreg.possible_actions(self.req, self.rset) |
|
50 |
add_menu = BoxMenu(_('add')) # 'addrelated' category |
|
51 |
other_menu = BoxMenu(_('more actions')) # 'moreactions' category |
|
52 |
searchstate = self.req.search_state[0] |
|
53 |
for category, menu in (('mainactions', box), |
|
54 |
('addrelated', add_menu), |
|
55 |
('moreactions', other_menu)): |
|
56 |
for action in actions.get(category, ()): |
|
57 |
menu.append(self.box_action(action)) |
|
58 |
if self.rset and self.rset.rowcount == 1 and \ |
|
59 |
not self.schema[self.rset.description[0][0]].is_final() and \ |
|
60 |
searchstate == 'normal': |
|
61 |
entity = self.rset.get_entity(0, 0) |
|
62 |
#entity.complete() |
|
63 |
if add_menu.items: |
|
64 |
self.info('explicit actions defined, ignoring potential rtags for %s', |
|
65 |
entity.e_schema) |
|
66 |
else: |
|
67 |
# some addrelated actions may be specified but no one is selectable |
|
68 |
# in which case we should not fallback to schema_actions. The proper |
|
69 |
# way to avoid this is to override add_related_schemas() on the |
|
70 |
# entity class to return an empty list |
|
71 |
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
|
72 |
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
|
73 |
self.workflow_actions(entity, box) |
0 | 74 |
if box.is_empty() and not other_menu.is_empty(): |
75 |
box.items = other_menu.items |
|
76 |
other_menu.items = [] |
|
77 |
self.add_submenu(box, add_menu, _('add')) |
|
78 |
self.add_submenu(box, other_menu) |
|
79 |
if not box.is_empty(): |
|
80 |
box.render(self.w) |
|
81 |
||
82 |
def add_submenu(self, box, submenu, label_prefix=None): |
|
83 |
if len(submenu.items) == 1: |
|
84 |
boxlink = submenu.items[0] |
|
85 |
if label_prefix: |
|
86 |
boxlink.label = u'%s %s' % (label_prefix, boxlink.label) |
|
87 |
box.append(boxlink) |
|
88 |
elif submenu.items: |
|
89 |
box.append(submenu) |
|
90 |
||
91 |
def schema_actions(self, entity): |
|
92 |
user = self.req.user |
|
93 |
actions = [] |
|
94 |
_ = self.req._ |
|
95 |
eschema = entity.e_schema |
|
96 |
for rschema, teschema, x in entity.add_related_schemas(): |
|
97 |
if x == 'subject': |
|
98 |
label = 'add %s %s %s %s' % (eschema, rschema, teschema, x) |
|
99 |
url = self.linkto_url(entity, rschema, teschema, 'object') |
|
100 |
else: |
|
101 |
label = 'add %s %s %s %s' % (teschema, rschema, eschema, x) |
|
102 |
url = self.linkto_url(entity, rschema, teschema, 'subject') |
|
103 |
actions.append(self.mk_action(_(label), url)) |
|
104 |
return actions |
|
105 |
||
106 |
||
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
107 |
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
|
108 |
if 'in_state' in entity.e_schema.subject_relations() and entity.in_state: |
399 | 109 |
_ = self.req._ |
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
if transitions: |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
113 |
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
|
114 |
menu_items = [] |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
# 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
|
120 |
# 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
|
121 |
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
|
122 |
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
|
123 |
_('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
|
124 |
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
|
125 |
return None |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
126 |
|
0 | 127 |
def linkto_url(self, entity, rtype, etype, target): |
128 |
return self.build_url(vid='creation', etype=etype, |
|
129 |
__linkto='%s:%s:%s' % (rtype, entity.eid, target), |
|
130 |
__redirectpath=entity.rest_path(), # should not be url quoted! |
|
131 |
__redirectvid=self.req.form.get('vid', '')) |
|
132 |
||
133 |
||
134 |
class SearchBox(BoxTemplate): |
|
135 |
"""display a box with a simple search form""" |
|
136 |
id = 'search_box' |
|
809 | 137 |
|
0 | 138 |
visible = True # enabled by default |
139 |
title = _('search') |
|
140 |
order = 0 |
|
141 |
formdef = u"""<form action="%s"> |
|
142 |
<table id="tsearch"><tr><td> |
|
143 |
<input id="norql" type="text" accesskey="q" tabindex="%s" title="search text" value="%s" name="rql" /> |
|
144 |
<input type="hidden" name="__fromsearchbox" value="1" /> |
|
145 |
<input type="hidden" name="subvid" value="tsearch" /> |
|
146 |
</td><td> |
|
147 |
<input tabindex="%s" type="submit" id="rqlboxsubmit" value="" /> |
|
148 |
</td></tr></table> |
|
149 |
</form>""" |
|
150 |
||
151 |
def call(self, view=None, **kwargs): |
|
152 |
req = self.req |
|
153 |
if req.form.pop('__fromsearchbox', None): |
|
154 |
rql = req.form.get('rql', '') |
|
155 |
else: |
|
156 |
rql = '' |
|
157 |
form = self.formdef % (req.build_url('view'), req.next_tabindex(), |
|
158 |
html_escape(rql), req.next_tabindex()) |
|
159 |
title = u"""<span onclick="javascript: toggleVisibility('rqlinput')">%s</span>""" % req._(self.title) |
|
160 |
box = BoxWidget(title, self.id, _class="searchBoxFrame", islist=False, escape=False) |
|
161 |
box.append(BoxHtml(form)) |
|
162 |
box.render(self.w) |
|
163 |
||
164 |
||
165 |
# boxes disabled by default ################################################### |
|
166 |
||
167 |
class PossibleViewsBox(BoxTemplate): |
|
168 |
"""display a box containing links to all possible views""" |
|
169 |
id = 'possible_views_box' |
|
809 | 170 |
__select__ = BoxTemplate.__select__ & match_user_groups('users', 'managers') |
0 | 171 |
|
809 | 172 |
visible = False |
0 | 173 |
title = _('possible views') |
174 |
order = 10 |
|
175 |
||
176 |
def call(self, **kwargs): |
|
177 |
box = BoxWidget(self.req._(self.title), self.id) |
|
613 | 178 |
views = [v for v in self.vreg.possible_views(self.req, self.rset) |
179 |
if v.category != 'startupview'] |
|
180 |
for category, views in self.sort_actions(views): |
|
0 | 181 |
menu = BoxMenu(category) |
613 | 182 |
for view in views: |
183 |
menu.append(self.box_action(view)) |
|
0 | 184 |
box.append(menu) |
185 |
if not box.is_empty(): |
|
186 |
box.render(self.w) |
|
187 |
||
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
188 |
|
663 | 189 |
class RSSIconBox(BoxTemplate): |
0 | 190 |
"""just display the RSS icon on uniform result set""" |
663 | 191 |
id = 'rss' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
663
diff
changeset
|
192 |
__select__ = (BoxTemplate.__select__ |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
663
diff
changeset
|
193 |
& appobject_selectable('components', 'rss_feed_url')) |
0 | 194 |
|
809 | 195 |
visible = False |
0 | 196 |
order = 999 |
197 |
||
198 |
def call(self, **kwargs): |
|
663 | 199 |
try: |
200 |
rss = self.req.external_resource('RSS_LOGO') |
|
201 |
except KeyError: |
|
202 |
self.error('missing RSS_LOGO external resource') |
|
203 |
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
|
204 |
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
|
205 |
url = urlgetter.feed_url() |
536 | 206 |
self.w(u'<a href="%s"><img src="%s" alt="rss"/></a>\n' % (html_escape(url), rss)) |
0 | 207 |
|
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
107
diff
changeset
|
208 |
|
0 | 209 |
class StartupViewsBox(BoxTemplate): |
210 |
"""display a box containing links to all startup views""" |
|
211 |
id = 'startup_views_box' |
|
809 | 212 |
|
0 | 213 |
visible = False # disabled by default |
214 |
title = _('startup views') |
|
215 |
order = 70 |
|
216 |
||
217 |
def call(self, **kwargs): |
|
218 |
box = BoxWidget(self.req._(self.title), self.id) |
|
219 |
for view in self.vreg.possible_views(self.req, None): |
|
220 |
if view.category == 'startupview': |
|
221 |
box.append(self.box_action(view)) |
|
222 |
||
223 |
if not box.is_empty(): |
|
224 |
box.render(self.w) |
|
225 |