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