author | sylvain.thenault@logilab.fr |
Fri, 20 Feb 2009 01:49:00 +0100 | |
branch | tls-sprint |
changeset 884 | 969c16600fb3 |
parent 796 | 62253c7fe5ba |
child 939 | ad72e06320e2 |
permissions | -rw-r--r-- |
0 | 1 |
"""Bases HTML components: |
2 |
||
3 |
* the rql input form |
|
4 |
* the logged user link |
|
5 |
* the workflow history section for workflowable objects |
|
6 |
||
7 |
:organization: Logilab |
|
658
b5c73b5cdc68
only kill Singleton[V]Component, keep [V]Component classes
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
8 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 9 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
10 |
""" |
|
11 |
__docformat__ = "restructuredtext en" |
|
12 |
||
13 |
from rql import parse |
|
14 |
||
15 |
from cubicweb import Unauthorized |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
688
diff
changeset
|
16 |
from cubicweb.selectors import (yes, non_final_entity, one_line_rset, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
688
diff
changeset
|
17 |
chainfirst, two_etypes_rset, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
688
diff
changeset
|
18 |
match_form_params, relation_possible) |
0 | 19 |
from cubicweb.common.uilib import html_escape, toggle_action |
20 |
from cubicweb.schema import display_name |
|
21 |
||
22 |
from cubicweb.web.htmlwidgets import MenuWidget, PopupBoxMenu, BoxSeparator, BoxLink |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
23 |
from cubicweb.web.component import (Component, EntityVComponent, |
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
29
diff
changeset
|
24 |
RelatedObjectsVComponent) |
0 | 25 |
|
26 |
_ = unicode |
|
27 |
||
28 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
29 |
class RQLInputForm(Component): |
0 | 30 |
"""build the rql input form, usually displayed in the header""" |
31 |
id = 'rqlinput' |
|
32 |
visible = False |
|
33 |
||
34 |
def call(self, view=None): |
|
35 |
if hasattr(view, 'filter_box_context_info'): |
|
36 |
rset = view.filter_box_context_info()[0] |
|
37 |
else: |
|
38 |
rset = self.rset |
|
39 |
# display multilines query as one line |
|
40 |
rql = rset is not None and rset.printable_rql(encoded=False) or self.req.form.get('rql', '') |
|
41 |
rql = rql.replace(u"\n", u" ") |
|
42 |
req = self.req |
|
43 |
self.w(u'''<div id="rqlinput" class="%s"> |
|
44 |
<form action="%s"> |
|
45 |
<fieldset> |
|
46 |
<input type="text" id="rql" name="rql" value="%s" title="%s" tabindex="%s" accesskey="q" class="searchField" /> |
|
47 |
<input type="submit" value="%s" class="searchButton" tabindex="%s" /> |
|
48 |
</fieldset> |
|
49 |
''' % (not self.propval('visible') and 'hidden' or '', |
|
50 |
self.build_url('view'), html_escape(rql), req._('full text or RQL query'), req.next_tabindex(), |
|
51 |
req._('search'), req.next_tabindex())) |
|
52 |
if self.req.search_state[0] != 'normal': |
|
53 |
self.w(u'<input type="hidden" name="__mode" value="%s"/>' |
|
54 |
% ':'.join(req.search_state[1])) |
|
55 |
self.w(u'</form></div>') |
|
56 |
||
57 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
58 |
class ApplLogo(Component): |
0 | 59 |
"""build the application logo, usually displayed in the header""" |
60 |
id = 'logo' |
|
61 |
site_wide = True # don't want user to hide this component using an eproperty |
|
62 |
def call(self): |
|
63 |
self.w(u'<a href="%s"><img class="logo" src="%s" alt="logo"/></a>' |
|
64 |
% (self.req.base_url(), self.req.external_resource('LOGO'))) |
|
65 |
||
66 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
67 |
class ApplHelp(Component): |
0 | 68 |
"""build the help button, usually displayed in the header""" |
69 |
id = 'help' |
|
70 |
def call(self): |
|
71 |
self.w(u'<a href="%s" class="help" title="%s"> </a>' |
|
72 |
% (self.build_url(_restpath='doc/main'), |
|
73 |
self.req._(u'help'),)) |
|
74 |
||
75 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
76 |
class UserLink(Component): |
0 | 77 |
"""if the user is the anonymous user, build a link to login |
78 |
else a link to the connected user object with a loggout link |
|
79 |
""" |
|
80 |
id = 'loggeduserlink' |
|
81 |
site_wide = True # don't want user to hide this component using an eproperty |
|
82 |
||
83 |
def call(self): |
|
84 |
if not self.req.cnx.anonymous_connection: |
|
85 |
# display useractions and siteactions |
|
86 |
actions = self.vreg.possible_actions(self.req, self.rset) |
|
87 |
box = MenuWidget('', 'userActionsBox', _class='', islist=False) |
|
88 |
menu = PopupBoxMenu(self.req.user.login, isitem=False) |
|
89 |
box.append(menu) |
|
90 |
for action in actions.get('useractions', ()): |
|
91 |
menu.append(BoxLink(action.url(), self.req._(action.title), |
|
92 |
action.html_class())) |
|
93 |
if actions.get('useractions') and actions.get('siteactions'): |
|
94 |
menu.append(BoxSeparator()) |
|
95 |
for action in actions.get('siteactions', ()): |
|
96 |
menu.append(BoxLink(action.url(), self.req._(action.title), |
|
97 |
action.html_class())) |
|
98 |
box.render(w=self.w) |
|
99 |
else: |
|
100 |
self.anon_user_link() |
|
101 |
||
102 |
def anon_user_link(self): |
|
103 |
if self.config['auth-mode'] == 'cookie': |
|
104 |
self.w(self.req._('anonymous')) |
|
29
7d14f1eadded
fix focus problems on login inputs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
105 |
self.w(u''' [<a class="logout" href="javascript: popupLoginBox();">%s</a>]''' |
0 | 106 |
% (self.req._('i18n_login_popup'))) |
638 | 107 |
# FIXME maybe have an other option to explicitely authorise registration |
108 |
if self.config['anonymous-user']: |
|
109 |
self.w(u''' [<a class="logout" href="?vid=register">%s</a>]''' |
|
110 |
% (self.req._('i18n_register_user'))) |
|
0 | 111 |
else: |
112 |
self.w(self.req._('anonymous')) |
|
113 |
self.w(u' [<a class="logout" href="%s">%s</a>]' |
|
114 |
% (self.build_url('login'), self.req._('login'))) |
|
115 |
||
116 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
117 |
class ApplicationMessage(Component): |
0 | 118 |
"""display application's messages given using the __message parameter |
119 |
into a special div section |
|
120 |
""" |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
121 |
__select__ = yes() |
0 | 122 |
id = 'applmessages' |
123 |
site_wide = True # don't want user to hide this component using an eproperty |
|
124 |
||
125 |
def call(self): |
|
126 |
msgs = [msg for msg in (self.req.get_shared_data('sources_error', pop=True), |
|
127 |
self.req.message) if msg] |
|
128 |
self.w(u'<div id="appMsg" onclick="%s" class="%s">\n' % |
|
129 |
(toggle_action('appMsg'), (msgs and ' ' or 'hidden'))) |
|
130 |
for msg in msgs: |
|
131 |
self.w(u'<div class="message" id="%s">%s</div>' % ( |
|
132 |
self.div_id(), msg)) |
|
133 |
self.w(u'</div>') |
|
134 |
||
135 |
||
136 |
class WFHistoryVComponent(EntityVComponent): |
|
137 |
"""display the workflow history for entities supporting it""" |
|
138 |
id = 'wfhistory' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
139 |
__select__ = (EntityVComponent.__select__ |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
140 |
& relation_possible('wf_info_for', role='object')) |
0 | 141 |
context = 'navcontentbottom' |
142 |
title = _('Workflow history') |
|
143 |
||
533 | 144 |
def cell_call(self, row, col, view=None): |
0 | 145 |
_ = self.req._ |
533 | 146 |
eid = self.rset[row][col] |
0 | 147 |
sel = 'Any FS,TS,WF,D' |
148 |
rql = ' ORDERBY D DESC WHERE WF wf_info_for X,'\ |
|
149 |
'WF from_state FS, WF to_state TS, WF comment C,'\ |
|
150 |
'WF creation_date D' |
|
151 |
if self.vreg.schema.eschema('EUser').has_perm(self.req, 'read'): |
|
152 |
sel += ',U,C' |
|
153 |
rql += ', WF owned_by U?' |
|
154 |
displaycols = range(5) |
|
155 |
headers = (_('from_state'), _('to_state'), _('comment'), _('date'), |
|
156 |
_('EUser')) |
|
157 |
else: |
|
158 |
sel += ',C' |
|
159 |
displaycols = range(4) |
|
160 |
headers = (_('from_state'), _('to_state'), _('comment'), _('date')) |
|
161 |
rql = '%s %s, X eid %%(x)s' % (sel, rql) |
|
162 |
try: |
|
163 |
rset = self.req.execute(rql, {'x': eid}, 'x') |
|
164 |
except Unauthorized: |
|
165 |
return |
|
166 |
if rset: |
|
167 |
self.wview('table', rset, title=_(self.title), displayactions=False, |
|
168 |
displaycols=displaycols, headers=headers) |
|
169 |
||
170 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
171 |
class ApplicationName(Component): |
0 | 172 |
"""display the application name""" |
173 |
id = 'appliname' |
|
174 |
||
175 |
def call(self): |
|
176 |
self.w(u'<span id="appliName"><a href="%s">%s</a></span>' % (self.req.base_url(), |
|
177 |
self.req.property_value('ui.site-title'))) |
|
178 |
||
179 |
||
180 |
class SeeAlsoVComponent(RelatedObjectsVComponent): |
|
181 |
"""display any entity's see also""" |
|
182 |
id = 'seealso' |
|
183 |
context = 'navcontentbottom' |
|
184 |
rtype = 'see_also' |
|
185 |
target = 'object' |
|
186 |
order = 40 |
|
187 |
# register msg not generated since no entity use see_also in cubicweb itself |
|
188 |
title = _('contentnavigation_seealso') |
|
189 |
help = _('contentnavigation_seealso_description') |
|
190 |
||
191 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
192 |
class EtypeRestrictionComponent(Component): |
0 | 193 |
"""displays the list of entity types contained in the resultset |
194 |
to be able to filter accordingly. |
|
195 |
""" |
|
196 |
id = 'etypenavigation' |
|
758
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
197 |
__select__ = two_etypes_rset() | match_form_params('__restrtype', '__restrtypes', |
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
198 |
'__restrrql') |
0 | 199 |
visible = False # disabled by default |
200 |
||
201 |
def call(self): |
|
202 |
_ = self.req._ |
|
203 |
self.w(u'<div id="etyperestriction">') |
|
204 |
restrtype = self.req.form.get('__restrtype') |
|
205 |
restrtypes = self.req.form.get('__restrtypes', '').split(',') |
|
206 |
restrrql = self.req.form.get('__restrrql') |
|
207 |
if not restrrql: |
|
208 |
rqlst = self.rset.syntax_tree() |
|
209 |
restrrql = rqlst.as_string(self.req.encoding, self.rset.args) |
|
210 |
restrtypes = self.rset.column_types(0) |
|
211 |
else: |
|
212 |
rqlst = parse(restrrql) |
|
213 |
html = [] |
|
214 |
on_etype = False |
|
215 |
etypes = sorted((display_name(self.req, etype).capitalize(), etype) |
|
216 |
for etype in restrtypes) |
|
217 |
for elabel, etype in etypes: |
|
218 |
if etype == restrtype: |
|
219 |
html.append(u'<span class="selected">%s</span>' % elabel) |
|
220 |
on_etype = True |
|
221 |
else: |
|
222 |
rqlst.save_state() |
|
223 |
for select in rqlst.children: |
|
224 |
select.add_type_restriction(select.selection[0], etype) |
|
225 |
newrql = rqlst.as_string(self.req.encoding, self.rset.args) |
|
226 |
url = self.build_url(rql=newrql, __restrrql=restrrql, |
|
227 |
__restrtype=etype, __restrtypes=','.join(restrtypes)) |
|
228 |
html.append(u'<span><a href="%s">%s</a></span>' % ( |
|
229 |
html_escape(url), elabel)) |
|
230 |
rqlst.recover() |
|
231 |
if on_etype: |
|
232 |
url = self.build_url(rql=restrrql) |
|
233 |
html.insert(0, u'<span><a href="%s">%s</a></span>' % ( |
|
234 |
url, _('Any'))) |
|
235 |
else: |
|
236 |
html.insert(0, u'<span class="selected">%s</span>' % _('Any')) |
|
237 |
self.w(u' | '.join(html)) |
|
238 |
self.w(u'</div>') |
|
239 |
||
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
240 |
|
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
241 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
242 |
class RSSFeedURL(Component): |
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
243 |
id = 'rss_feed_url' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
244 |
__select__ = non_final_entity() |
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
245 |
|
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
246 |
def feed_url(self): |
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
247 |
return self.build_url(rql=self.limited_rql(), vid='rss') |
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
248 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
658
diff
changeset
|
249 |
class RSSEntityFeedURL(Component): |
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
250 |
id = 'rss_feed_url' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
251 |
__select__ = non_final_entity() & one_line_rset() |
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
252 |
|
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
253 |
def feed_url(self): |
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
254 |
return self.entity(0, 0).rss_feed_url() |
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
255 |
|
796
62253c7fe5ba
require explicit registration control
sylvain.thenault@logilab.fr
parents:
758
diff
changeset
|
256 |
|
62253c7fe5ba
require explicit registration control
sylvain.thenault@logilab.fr
parents:
758
diff
changeset
|
257 |
def registration_callback(vreg): |
62253c7fe5ba
require explicit registration control
sylvain.thenault@logilab.fr
parents:
758
diff
changeset
|
258 |
vreg.register_all(globals().values(), __name__, (SeeAlsoVComponent,)) |
62253c7fe5ba
require explicit registration control
sylvain.thenault@logilab.fr
parents:
758
diff
changeset
|
259 |
if 'see_also' in vreg.schema: |
62253c7fe5ba
require explicit registration control
sylvain.thenault@logilab.fr
parents:
758
diff
changeset
|
260 |
vreg.register(SeeAlsoVComponent) |