author | Julien Jehannet <julien.jehannet@logilab.fr> |
Thu, 19 Feb 2009 20:21:10 +0100 | |
changeset 859 | 17555e9b9854 |
parent 852 | 105893288777 |
parent 613 | e059279de6cf |
child 939 | ad72e06320e2 |
child 1113 | 5608c79c789b |
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 |
||
613 | 20 |
from cubicweb.common.selectors import any_rset, appobject_selectable |
0 | 21 |
from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem |
22 |
from cubicweb.web.box import BoxTemplate, ExtResourcesBoxTemplate |
|
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 |
need_resources = 'SEARCH_GO' |
|
140 |
formdef = u"""<form action="%s"> |
|
141 |
<table id="tsearch"><tr><td> |
|
142 |
<input id="norql" type="text" accesskey="q" tabindex="%s" title="search text" value="%s" name="rql" /> |
|
143 |
<input type="hidden" name="__fromsearchbox" value="1" /> |
|
144 |
<input type="hidden" name="subvid" value="tsearch" /> |
|
145 |
</td><td> |
|
852
105893288777
simplify css style
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
536
diff
changeset
|
146 |
<input tabindex="%s" type="submit" id="rqlboxsubmit" class="rqlsubmit" value="" /> |
0 | 147 |
</td></tr></table> |
148 |
</form>""" |
|
149 |
||
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' |
|
170 |
||
171 |
title = _('possible views') |
|
172 |
order = 10 |
|
173 |
require_groups = ('users', 'managers') |
|
174 |
visible = False |
|
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 |
|
0 | 189 |
class RSSIconBox(ExtResourcesBoxTemplate): |
190 |
"""just display the RSS icon on uniform result set""" |
|
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
|
191 |
__selectors__ = ExtResourcesBoxTemplate.__selectors__ + (appobject_selectable('components', 'rss_feed_url'),) |
0 | 192 |
|
193 |
id = 'rss' |
|
194 |
order = 999 |
|
195 |
need_resources = 'RSS_LOGO', |
|
196 |
visible = False |
|
197 |
||
198 |
def call(self, **kwargs): |
|
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
|
199 |
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
|
200 |
url = urlgetter.feed_url() |
0 | 201 |
rss = self.req.external_resource('RSS_LOGO') |
536 | 202 |
self.w(u'<a href="%s"><img src="%s" alt="rss"/></a>\n' % (html_escape(url), rss)) |
0 | 203 |
|
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
107
diff
changeset
|
204 |
|
0 | 205 |
class StartupViewsBox(BoxTemplate): |
206 |
"""display a box containing links to all startup views""" |
|
207 |
id = 'startup_views_box' |
|
208 |
visible = False # disabled by default |
|
209 |
title = _('startup views') |
|
210 |
order = 70 |
|
211 |
||
212 |
def call(self, **kwargs): |
|
213 |
box = BoxWidget(self.req._(self.title), self.id) |
|
214 |
for view in self.vreg.possible_views(self.req, None): |
|
215 |
if view.category == 'startupview': |
|
216 |
box.append(self.box_action(view)) |
|
217 |
||
218 |
if not box.is_empty(): |
|
219 |
box.render(self.w) |
|
220 |