author | sylvain.thenault@logilab.fr |
Wed, 11 Feb 2009 10:43:02 +0100 | |
changeset 570 | 0c9dfc430200 |
parent 532 | ce3e94cbb074 |
child 573 | 9c8fd72ba6c1 |
permissions | -rw-r--r-- |
0 | 1 |
"""Set of HTML generic base views: |
2 |
||
3 |
* noresult, final |
|
4 |
* primary, sidebox |
|
5 |
* secondary, oneline, incontext, outofcontext, text |
|
6 |
* list |
|
7 |
* xml, rss |
|
8 |
||
9 |
||
10 |
:organization: Logilab |
|
479 | 11 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 12 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
13 |
""" |
|
14 |
__docformat__ = "restructuredtext en" |
|
15 |
||
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
16 |
from warnings import warn |
0 | 17 |
from time import timezone |
18 |
||
19 |
from rql import nodes |
|
20 |
||
21 |
from logilab.common.decorators import cached |
|
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
22 |
from logilab.mtconverter import TransformError, html_escape, xml_escape |
0 | 23 |
|
24 |
from cubicweb import Unauthorized, NoSelectableObject, typed_eid |
|
431 | 25 |
from cubicweb.common.selectors import (yes, nonempty_rset, accept, |
323 | 26 |
one_line_rset, match_search_state, |
431 | 27 |
match_form_params, accept_rset) |
0 | 28 |
from cubicweb.common.uilib import (cut, printable_value, UnicodeCSVWriter, |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
29 |
ajax_replace_url, rql_for_eid, simple_sgml_tag) |
0 | 30 |
from cubicweb.common.view import EntityView, AnyRsetView, EmptyRsetView |
31 |
from cubicweb.web.httpcache import MaxAgeHTTPCacheManager |
|
32 |
from cubicweb.web.views import vid_from_rset, linksearch_select_url, linksearch_match |
|
33 |
||
34 |
_ = unicode |
|
35 |
||
36 |
class NullView(AnyRsetView): |
|
37 |
"""default view when no result has been found""" |
|
38 |
id = 'null' |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
213
diff
changeset
|
39 |
__select__ = classmethod(yes) |
0 | 40 |
def call(self, **kwargs): |
41 |
pass |
|
42 |
cell_call = call |
|
43 |
||
44 |
||
45 |
class NoResultView(EmptyRsetView): |
|
46 |
"""default view when no result has been found""" |
|
47 |
id = 'noresult' |
|
48 |
||
49 |
def call(self, **kwargs): |
|
50 |
self.w(u'<div class="searchMessage"><strong>%s</strong></div>\n' |
|
51 |
% self.req._('No result matching query')) |
|
52 |
||
53 |
||
54 |
class FinalView(AnyRsetView): |
|
55 |
"""display values without any transformation (i.e. get a number for |
|
56 |
entities) |
|
57 |
""" |
|
58 |
id = 'final' |
|
59 |
||
60 |
def cell_call(self, row, col, props=None, displaytime=False): |
|
61 |
etype = self.rset.description[row][col] |
|
62 |
value = self.rset.rows[row][col] |
|
63 |
if etype == 'String': |
|
64 |
entity, rtype = self.rset.related_entity(row, col) |
|
65 |
if entity is not None: |
|
66 |
# yes ! |
|
67 |
self.w(entity.printable_value(rtype, value)) |
|
68 |
return |
|
69 |
if etype in ('Time', 'Interval'): |
|
70 |
_ = self.req._ |
|
71 |
# value is DateTimeDelta but we have no idea about what is the |
|
72 |
# reference date here, so we can only approximate years and months |
|
73 |
if value.days > 730: # 2 years |
|
74 |
self.w(_('%d years') % (value.days // 365)) |
|
75 |
elif value.days > 60: # 2 months |
|
76 |
self.w(_('%d months') % (value.days // 30)) |
|
77 |
elif value.days > 14: # 2 weeks |
|
78 |
self.w(_('%d weeks') % (value.days // 7)) |
|
79 |
elif value.days > 2: |
|
80 |
self.w(_('%s days') % int(value.days)) |
|
81 |
elif value.hours > 2: |
|
82 |
self.w(_('%s hours') % int(value.hours)) |
|
83 |
elif value.minutes >= 2: |
|
84 |
self.w(_('%s minutes') % int(value.minutes)) |
|
85 |
else: |
|
86 |
self.w(_('%s seconds') % int(value.seconds)) |
|
87 |
return |
|
88 |
self.wdata(printable_value(self.req, etype, value, props, displaytime=displaytime)) |
|
89 |
||
90 |
||
91 |
class EditableFinalView(FinalView): |
|
92 |
"""same as FinalView but enables inplace-edition when possible""" |
|
93 |
id = 'editable-final' |
|
94 |
||
95 |
def cell_call(self, row, col, props=None, displaytime=False): |
|
96 |
etype = self.rset.description[row][col] |
|
97 |
value = self.rset.rows[row][col] |
|
98 |
entity, rtype = self.rset.related_entity(row, col) |
|
99 |
if entity is not None: |
|
100 |
self.w(entity.view('reledit', rtype=rtype)) |
|
101 |
else: |
|
102 |
super(EditableFinalView, self).cell_call(row, col, props, displaytime) |
|
103 |
||
104 |
PRIMARY_SKIP_RELS = set(['is', 'is_instance_of', 'identity', |
|
105 |
'owned_by', 'created_by', |
|
106 |
'in_state', 'wf_info_for', 'require_permission', |
|
107 |
'from_entity', 'to_entity', |
|
108 |
'see_also']) |
|
109 |
||
110 |
class PrimaryView(EntityView): |
|
111 |
"""the full view of an non final entity""" |
|
112 |
id = 'primary' |
|
113 |
title = _('primary') |
|
114 |
show_attr_label = True |
|
115 |
show_rel_label = True |
|
116 |
skip_none = True |
|
117 |
skip_attrs = ('eid', 'creation_date', 'modification_date') |
|
118 |
skip_rels = () |
|
119 |
main_related_section = True |
|
120 |
||
121 |
def html_headers(self): |
|
122 |
"""return a list of html headers (eg something to be inserted between |
|
123 |
<head> and </head> of the returned page |
|
124 |
||
125 |
by default primary views are indexed |
|
126 |
""" |
|
127 |
return [] |
|
128 |
||
129 |
def cell_call(self, row, col): |
|
130 |
self.row = row |
|
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
179
diff
changeset
|
131 |
# XXX move render_entity implementation here |
0 | 132 |
self.render_entity(self.complete_entity(row, col)) |
133 |
||
134 |
def render_entity(self, entity): |
|
135 |
"""return html to display the given entity""" |
|
136 |
siderelations = [] |
|
137 |
self.render_entity_title(entity) |
|
138 |
self.render_entity_metadata(entity) |
|
139 |
# entity's attributes and relations, excluding meta data |
|
140 |
# if the entity isn't meta itself |
|
141 |
self.w(u'<table border="0" width="100%">') |
|
142 |
self.w(u'<tr>') |
|
371
4e849b424aaa
get extra space available in primary view when no right boxes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
370
diff
changeset
|
143 |
self.w(u'<td valign="top">') |
0 | 144 |
self.w(u'<div class="mainInfo">') |
145 |
self.render_entity_attributes(entity, siderelations) |
|
146 |
self.w(u'</div>') |
|
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
147 |
self.content_navigation_components('navcontenttop') |
0 | 148 |
if self.main_related_section: |
149 |
self.render_entity_relations(entity, siderelations) |
|
150 |
self.w(u'</td>') |
|
151 |
# side boxes |
|
152 |
self.w(u'<td valign="top">') |
|
153 |
self.render_side_related(entity, siderelations) |
|
154 |
self.w(u'</td>') |
|
155 |
self.w(u'</tr>') |
|
156 |
self.w(u'</table>') |
|
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
157 |
self.content_navigation_components('navcontentbottom') |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
158 |
|
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
159 |
def content_navigation_components(self, context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
160 |
self.w(u'<div class="%s">' % context) |
0 | 161 |
for comp in self.vreg.possible_vobjects('contentnavigation', |
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
162 |
self.req, self.rset, row=self.row, |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
163 |
view=self, context=context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
164 |
try: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
165 |
comp.dispatch(w=self.w, row=self.row, view=self) |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
166 |
except NotImplementedError: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
167 |
warn('component %s doesnt implement cell_call, please update' |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
168 |
% comp.__class__, DeprecationWarning) |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
169 |
comp.dispatch(w=self.w, view=self) |
0 | 170 |
self.w(u'</div>') |
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
171 |
|
0 | 172 |
def iter_attributes(self, entity): |
173 |
for rschema, targetschema in entity.e_schema.attribute_definitions(): |
|
174 |
attr = rschema.type |
|
175 |
if attr in self.skip_attrs: |
|
176 |
continue |
|
177 |
yield rschema, targetschema |
|
178 |
||
179 |
def iter_relations(self, entity): |
|
180 |
skip = set(self.skip_rels) |
|
181 |
skip.update(PRIMARY_SKIP_RELS) |
|
182 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
183 |
if rschema.type in skip: |
|
184 |
continue |
|
185 |
yield rschema, targetschemas, x |
|
186 |
||
187 |
def render_entity_title(self, entity): |
|
188 |
title = self.content_title(entity) # deprecate content_title? |
|
189 |
if title: |
|
190 |
self.w(u'<h1><span class="etype">%s</span> %s</h1>' |
|
191 |
% (entity.dc_type().capitalize(), title)) |
|
192 |
||
193 |
def content_title(self, entity): |
|
194 |
"""default implementation return an empty string""" |
|
195 |
return u'' |
|
196 |
||
197 |
def render_entity_metadata(self, entity): |
|
198 |
entity.view('metadata', w=self.w) |
|
199 |
summary = self.summary(entity) # deprecate summary? |
|
200 |
if summary: |
|
201 |
self.w(u'<div class="summary">%s</div>' % summary) |
|
202 |
||
203 |
def summary(self, entity): |
|
204 |
"""default implementation return an empty string""" |
|
179 | 205 |
return u'' |
0 | 206 |
|
207 |
def render_entity_attributes(self, entity, siderelations): |
|
208 |
for rschema, targetschema in self.iter_attributes(entity): |
|
209 |
attr = rschema.type |
|
210 |
if targetschema.type in ('Password', 'Bytes'): |
|
211 |
continue |
|
212 |
try: |
|
213 |
wdg = entity.get_widget(attr) |
|
214 |
except Exception, ex: |
|
215 |
value = entity.printable_value(attr, entity[attr], targetschema.type) |
|
216 |
else: |
|
217 |
value = wdg.render(entity) |
|
218 |
if self.skip_none and (value is None or value == ''): |
|
219 |
continue |
|
220 |
if rschema.meta: |
|
221 |
continue |
|
222 |
self._render_related_entities(entity, rschema, value) |
|
223 |
||
224 |
def render_entity_relations(self, entity, siderelations): |
|
225 |
if hasattr(self, 'get_side_boxes_defs'): |
|
226 |
return |
|
227 |
eschema = entity.e_schema |
|
228 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
229 |
for rschema, targetschemas, x in self.iter_relations(entity): |
|
230 |
try: |
|
231 |
related = entity.related(rschema.type, x, limit=maxrelated+1) |
|
232 |
except Unauthorized: |
|
233 |
continue |
|
234 |
if not related: |
|
235 |
continue |
|
236 |
if self.is_side_related(rschema, eschema): |
|
237 |
siderelations.append((rschema, related, x)) |
|
238 |
continue |
|
239 |
self._render_related_entities(entity, rschema, related, x) |
|
240 |
||
241 |
def render_side_related(self, entity, siderelations): |
|
242 |
"""display side related relations: |
|
243 |
non-meta in a first step, meta in a second step |
|
244 |
""" |
|
245 |
if hasattr(self, 'get_side_boxes_defs'): |
|
246 |
for label, rset in self.get_side_boxes_defs(entity): |
|
247 |
if rset: |
|
248 |
self.w(u'<div class="sideRelated">') |
|
249 |
self.wview('sidebox', rset, title=label) |
|
250 |
self.w(u'</div>') |
|
251 |
elif siderelations: |
|
252 |
self.w(u'<div class="sideRelated">') |
|
253 |
for relatedinfos in siderelations: |
|
254 |
# if not relatedinfos[0].meta: |
|
255 |
# continue |
|
256 |
self._render_related_entities(entity, *relatedinfos) |
|
257 |
self.w(u'</div>') |
|
531 | 258 |
for box in self.vreg.possible_vobjects('boxes', self.req, self.rset, |
259 |
row=self.row, view=self, |
|
260 |
context='incontext'): |
|
0 | 261 |
try: |
531 | 262 |
box.dispatch(w=self.w, row=self.row) |
0 | 263 |
except NotImplementedError: |
264 |
# much probably a context insensitive box, which only implements |
|
265 |
# .call() and not cell_call() |
|
266 |
box.dispatch(w=self.w) |
|
267 |
||
268 |
def is_side_related(self, rschema, eschema): |
|
269 |
return rschema.meta and \ |
|
270 |
not rschema.schema_relation() == eschema.schema_entity() |
|
271 |
||
272 |
def _render_related_entities(self, entity, rschema, related, |
|
273 |
role='subject'): |
|
274 |
if rschema.is_final(): |
|
275 |
value = related |
|
276 |
show_label = self.show_attr_label |
|
277 |
else: |
|
278 |
if not related: |
|
279 |
return |
|
280 |
show_label = self.show_rel_label |
|
281 |
# if not too many entities, show them all in a list |
|
282 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
283 |
if related.rowcount <= maxrelated: |
|
284 |
if related.rowcount == 1: |
|
285 |
value = self.view('incontext', related, row=0) |
|
286 |
elif 1 < related.rowcount <= 5: |
|
287 |
value = self.view('csv', related) |
|
288 |
else: |
|
289 |
value = '<div>' + self.view('simplelist', related) + '</div>' |
|
290 |
# else show links to display related entities |
|
291 |
else: |
|
292 |
rql = related.printable_rql() |
|
293 |
related.limit(maxrelated) |
|
294 |
value = '<div>' + self.view('simplelist', related) |
|
295 |
value += '[<a href="%s">%s</a>]' % (self.build_url(rql=rql), |
|
296 |
self.req._('see them all')) |
|
297 |
value += '</div>' |
|
298 |
label = display_name(self.req, rschema.type, role) |
|
299 |
self.field(label, value, show_label=show_label, w=self.w, tr=False) |
|
300 |
||
301 |
||
302 |
class SideBoxView(EntityView): |
|
303 |
"""side box usually displaying some related entities in a primary view""" |
|
304 |
id = 'sidebox' |
|
305 |
||
306 |
def call(self, boxclass='sideBox', title=u''): |
|
307 |
"""display a list of entities by calling their <item_vid> view |
|
308 |
""" |
|
309 |
if title: |
|
310 |
self.w(u'<div class="sideBoxTitle"><span>%s</span></div>' % title) |
|
311 |
self.w(u'<div class="%s"><div class="sideBoxBody">' % boxclass) |
|
312 |
# if not too much entities, show them all in a list |
|
313 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
314 |
if self.rset.rowcount <= maxrelated: |
|
315 |
if len(self.rset) == 1: |
|
316 |
self.wview('incontext', self.rset, row=0) |
|
317 |
elif 1 < len(self.rset) < 5: |
|
318 |
self.wview('csv', self.rset) |
|
319 |
else: |
|
320 |
self.wview('simplelist', self.rset) |
|
321 |
# else show links to display related entities |
|
322 |
else: |
|
323 |
self.rset.limit(maxrelated) |
|
324 |
rql = self.rset.printable_rql(encoded=False) |
|
325 |
self.wview('simplelist', self.rset) |
|
326 |
self.w(u'[<a href="%s">%s</a>]' % (self.build_url(rql=rql), |
|
327 |
self.req._('see them all'))) |
|
328 |
self.w(u'</div>\n</div>\n') |
|
329 |
||
330 |
||
331 |
||
332 |
class SecondaryView(EntityView): |
|
333 |
id = 'secondary' |
|
334 |
title = _('secondary') |
|
335 |
||
336 |
def cell_call(self, row, col): |
|
337 |
"""the secondary view for an entity |
|
338 |
secondary = icon + view(oneline) |
|
339 |
""" |
|
340 |
entity = self.entity(row, col) |
|
341 |
self.w(u' ') |
|
342 |
self.wview('oneline', self.rset, row=row, col=col) |
|
343 |
||
344 |
class OneLineView(EntityView): |
|
345 |
id = 'oneline' |
|
346 |
title = _('oneline') |
|
347 |
||
348 |
def cell_call(self, row, col): |
|
349 |
"""the one line view for an entity: linked text view |
|
350 |
""" |
|
351 |
entity = self.entity(row, col) |
|
352 |
self.w(u'<a href="%s">' % html_escape(entity.absolute_url())) |
|
353 |
self.w(html_escape(self.view('text', self.rset, row=row, col=col))) |
|
354 |
self.w(u'</a>') |
|
355 |
||
356 |
class TextView(EntityView): |
|
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
357 |
"""the simplest text view for an entity""" |
0 | 358 |
id = 'text' |
359 |
title = _('text') |
|
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
360 |
content_type = 'text/plain' |
0 | 361 |
accepts = 'Any', |
362 |
def call(self, **kwargs): |
|
363 |
"""the view is called for an entire result set, by default loop |
|
364 |
other rows of the result set and call the same view on the |
|
365 |
particular row |
|
366 |
||
367 |
Views applicable on None result sets have to override this method |
|
368 |
""" |
|
369 |
rset = self.rset |
|
370 |
if rset is None: |
|
371 |
raise NotImplementedError, self |
|
372 |
for i in xrange(len(rset)): |
|
373 |
self.wview(self.id, rset, row=i, **kwargs) |
|
374 |
if len(rset) > 1: |
|
375 |
self.w(u"\n") |
|
376 |
||
377 |
def cell_call(self, row, col=0, **kwargs): |
|
378 |
entity = self.entity(row, col) |
|
379 |
self.w(cut(entity.dc_title(), |
|
380 |
self.req.property_value('navigation.short-line-size'))) |
|
381 |
||
137
7e45cf48c2f1
don't display additional owners in metadata, this is confusing
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
382 |
|
0 | 383 |
class MetaDataView(EntityView): |
384 |
"""paragraph view of some metadata""" |
|
385 |
id = 'metadata' |
|
386 |
accepts = 'Any', |
|
387 |
show_eid = True |
|
388 |
||
389 |
def cell_call(self, row, col): |
|
390 |
_ = self.req._ |
|
391 |
entity = self.entity(row, col) |
|
392 |
self.w(u'<div class="metadata">') |
|
393 |
if self.show_eid: |
|
394 |
self.w(u'#%s - ' % entity.eid) |
|
395 |
if entity.modification_date != entity.creation_date: |
|
396 |
self.w(u'<span>%s</span> ' % _('latest update on')) |
|
397 |
self.w(u'<span class="value">%s</span>, ' |
|
398 |
% self.format_date(entity.modification_date)) |
|
399 |
# entities from external source may not have a creation date (eg ldap) |
|
400 |
if entity.creation_date: |
|
401 |
self.w(u'<span>%s</span> ' % _('created on')) |
|
402 |
self.w(u'<span class="value">%s</span>' |
|
403 |
% self.format_date(entity.creation_date)) |
|
404 |
if entity.creator: |
|
405 |
self.w(u' <span>%s</span> ' % _('by')) |
|
406 |
self.w(u'<span class="value">%s</span>' % entity.creator.name()) |
|
407 |
self.w(u'</div>') |
|
408 |
||
409 |
||
410 |
# new default views for finner control in general views , to use instead of |
|
411 |
# oneline / secondary |
|
412 |
||
413 |
class InContextTextView(TextView): |
|
414 |
id = 'textincontext' |
|
415 |
title = None # not listed as a possible view |
|
416 |
def cell_call(self, row, col): |
|
417 |
entity = self.entity(row, col) |
|
418 |
self.w(entity.dc_title()) |
|
419 |
||
420 |
class OutOfContextTextView(InContextTextView): |
|
421 |
id = 'textoutofcontext' |
|
422 |
||
423 |
def cell_call(self, row, col): |
|
424 |
entity = self.entity(row, col) |
|
425 |
self.w(entity.dc_long_title()) |
|
426 |
||
427 |
||
428 |
class InContextView(EntityView): |
|
429 |
id = 'incontext' |
|
430 |
||
431 |
def cell_call(self, row, col): |
|
432 |
entity = self.entity(row, col) |
|
433 |
desc = cut(entity.dc_description(), 50) |
|
434 |
self.w(u'<a href="%s" title="%s">' % (html_escape(entity.absolute_url()), |
|
435 |
html_escape(desc))) |
|
436 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
437 |
self.w(u'</a>') |
|
438 |
||
439 |
||
440 |
class OutOfContextView(EntityView): |
|
441 |
id = 'outofcontext' |
|
442 |
||
443 |
def cell_call(self, row, col): |
|
444 |
self.w(u'<a href="%s">' % self.entity(row, col).absolute_url()) |
|
445 |
self.w(html_escape(self.view('textoutofcontext', self.rset, row=row, col=col))) |
|
446 |
self.w(u'</a>') |
|
447 |
||
448 |
class NotClickableInContextView(EntityView): |
|
449 |
id = 'incontext' |
|
450 |
accepts = ('State',) |
|
451 |
def cell_call(self, row, col): |
|
452 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
453 |
||
454 |
## class NotClickableOutOfContextView(EntityView): |
|
455 |
## id = 'outofcontext' |
|
456 |
## accepts = ('State',) |
|
457 |
## def cell_call(self, row, col): |
|
458 |
## self.w(html_escape(self.view('textoutofcontext', self.rset, row=row))) |
|
459 |
||
460 |
||
461 |
# list and table related views ################################################ |
|
462 |
||
463 |
class ListView(EntityView): |
|
464 |
id = 'list' |
|
465 |
title = _('list') |
|
466 |
item_vid = 'listitem' |
|
467 |
||
468 |
def call(self, klass=None, title=None, subvid=None, listid=None, **kwargs): |
|
469 |
"""display a list of entities by calling their <item_vid> view |
|
470 |
|
|
471 |
:param listid: the DOM id to use for the root element |
|
472 |
""" |
|
473 |
if subvid is None and 'subvid' in self.req.form: |
|
474 |
subvid = self.req.form.pop('subvid') # consume it |
|
475 |
if listid: |
|
476 |
listid = u' id="%s"' % listid |
|
477 |
else: |
|
478 |
listid = u'' |
|
479 |
if title: |
|
480 |
self.w(u'<div%s class="%s"><h4>%s</h4>\n' % (listid, klass or 'section', title)) |
|
481 |
self.w(u'<ul>\n') |
|
482 |
else: |
|
483 |
self.w(u'<ul%s class="%s">\n' % (listid, klass or 'section')) |
|
484 |
for i in xrange(self.rset.rowcount): |
|
485 |
self.cell_call(row=i, col=0, vid=subvid, **kwargs) |
|
486 |
self.w(u'</ul>\n') |
|
487 |
if title: |
|
488 |
self.w(u'</div>\n') |
|
489 |
||
490 |
def cell_call(self, row, col=0, vid=None, **kwargs): |
|
491 |
self.w(u'<li>') |
|
492 |
self.wview(self.item_vid, self.rset, row=row, col=col, vid=vid, **kwargs) |
|
493 |
self.w(u'</li>\n') |
|
494 |
||
495 |
def url(self): |
|
496 |
"""overrides url method so that by default, the view list is called |
|
497 |
with sorted entities |
|
498 |
""" |
|
499 |
coltypes = self.rset.column_types(0) |
|
500 |
# don't want to generate the rql if there is some restriction on |
|
501 |
# something else than the entity type |
|
502 |
if len(coltypes) == 1: |
|
503 |
# XXX norestriction is not correct here. For instance, in cases like |
|
504 |
# Any P,N WHERE P is Project, P name N |
|
505 |
# norestriction should equal True |
|
506 |
restr = self.rset.syntax_tree().children[0].where |
|
507 |
norestriction = (isinstance(restr, nodes.Relation) and |
|
508 |
restr.is_types_restriction()) |
|
509 |
if norestriction: |
|
510 |
etype = iter(coltypes).next() |
|
511 |
return self.build_url(etype.lower(), vid=self.id) |
|
512 |
if len(self.rset) == 1: |
|
513 |
entity = self.rset.get_entity(0, 0) |
|
514 |
return self.build_url(entity.rest_path(), vid=self.id) |
|
515 |
return self.build_url(rql=self.rset.printable_rql(), vid=self.id) |
|
516 |
||
517 |
||
518 |
class ListItemView(EntityView): |
|
519 |
id = 'listitem' |
|
520 |
||
521 |
@property |
|
522 |
def redirect_vid(self): |
|
523 |
if self.req.search_state[0] == 'normal': |
|
524 |
return 'outofcontext' |
|
525 |
return 'outofcontext-search' |
|
526 |
||
527 |
def cell_call(self, row, col, vid=None, **kwargs): |
|
528 |
if not vid: |
|
529 |
vid = self.redirect_vid |
|
530 |
try: |
|
531 |
self.wview(vid, self.rset, row=row, col=col, **kwargs) |
|
532 |
except NoSelectableObject: |
|
533 |
if vid == self.redirect_vid: |
|
534 |
raise |
|
535 |
self.wview(self.redirect_vid, self.rset, row=row, col=col, **kwargs) |
|
536 |
||
537 |
||
538 |
class SimpleListView(ListItemView): |
|
539 |
"""list without bullets""" |
|
540 |
id = 'simplelist' |
|
541 |
redirect_vid = 'incontext' |
|
542 |
||
543 |
||
544 |
class CSVView(SimpleListView): |
|
545 |
id = 'csv' |
|
546 |
redirect_vid = 'incontext' |
|
547 |
||
548 |
def call(self, **kwargs): |
|
549 |
rset = self.rset |
|
550 |
for i in xrange(len(rset)): |
|
551 |
self.cell_call(i, 0, vid=kwargs.get('vid')) |
|
552 |
if i < rset.rowcount-1: |
|
553 |
self.w(u", ") |
|
554 |
||
555 |
||
556 |
class TreeItemView(ListItemView): |
|
557 |
accepts = ('Any',) |
|
558 |
id = 'treeitem' |
|
559 |
||
560 |
def cell_call(self, row, col): |
|
561 |
self.wview('incontext', self.rset, row=row, col=col) |
|
562 |
||
563 |
||
564 |
# xml and xml/rss views ####################################################### |
|
565 |
||
566 |
class XmlView(EntityView): |
|
567 |
id = 'xml' |
|
568 |
title = _('xml') |
|
569 |
templatable = False |
|
570 |
content_type = 'text/xml' |
|
571 |
xml_root = 'rset' |
|
572 |
item_vid = 'xmlitem' |
|
573 |
||
574 |
def cell_call(self, row, col): |
|
575 |
self.wview(self.item_vid, self.rset, row=row, col=col) |
|
576 |
||
577 |
def call(self): |
|
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
578 |
"""display a list of entities by calling their <item_vid> view""" |
0 | 579 |
self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding) |
580 |
self.w(u'<%s size="%s">\n' % (self.xml_root, len(self.rset))) |
|
581 |
for i in xrange(self.rset.rowcount): |
|
582 |
self.cell_call(i, 0) |
|
583 |
self.w(u'</%s>\n' % self.xml_root) |
|
584 |
||
585 |
||
586 |
class XmlItemView(EntityView): |
|
587 |
id = 'xmlitem' |
|
588 |
||
589 |
def cell_call(self, row, col): |
|
590 |
""" element as an item for an xml feed """ |
|
591 |
entity = self.complete_entity(row, col) |
|
592 |
self.w(u'<%s>\n' % (entity.e_schema)) |
|
593 |
for rschema, attrschema in entity.e_schema.attribute_definitions(): |
|
594 |
attr = rschema.type |
|
595 |
try: |
|
596 |
value = entity[attr] |
|
597 |
except KeyError: |
|
598 |
# Bytes |
|
599 |
continue |
|
600 |
if value is not None: |
|
601 |
if attrschema == 'Bytes': |
|
602 |
from base64 import b64encode |
|
603 |
value = '<![CDATA[%s]]>' % b64encode(value.getvalue()) |
|
604 |
elif isinstance(value, basestring): |
|
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
605 |
value = xml_escape(value) |
0 | 606 |
self.w(u' <%s>%s</%s>\n' % (attr, value, attr)) |
607 |
self.w(u'</%s>\n' % (entity.e_schema)) |
|
608 |
||
609 |
||
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
610 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
611 |
class XMLRsetView(AnyRsetView): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
612 |
"""dumps xml in CSV""" |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
613 |
id = 'rsetxml' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
614 |
title = _('xml export') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
615 |
templatable = False |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
616 |
content_type = 'text/xml' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
617 |
xml_root = 'rset' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
618 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
619 |
def call(self): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
620 |
w = self.w |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
621 |
rset, descr = self.rset, self.rset.description |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
622 |
eschema = self.schema.eschema |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
623 |
labels = self.columns_labels(False) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
624 |
w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding) |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
625 |
w(u'<%s query="%s">\n' % (self.xml_root, html_escape(rset.printable_rql()))) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
626 |
for rowindex, row in enumerate(self.rset): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
627 |
w(u' <row>\n') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
628 |
for colindex, val in enumerate(row): |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
629 |
etype = descr[rowindex][colindex] |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
630 |
tag = labels[colindex] |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
631 |
attrs = {} |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
632 |
if '(' in tag: |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
633 |
attrs['expr'] = tag |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
634 |
tag = 'funccall' |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
635 |
if val is not None and not eschema(etype).is_final(): |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
636 |
attrs['eid'] = val |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
637 |
# csvrow.append(val) # val is eid in that case |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
638 |
val = self.view('textincontext', rset, |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
639 |
row=rowindex, col=colindex) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
640 |
else: |
530
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
641 |
val = self.view('final', rset, displaytime=True, |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
642 |
row=rowindex, col=colindex) |
9b56df97ec5f
fix xml views: ensure we're generating valide tag names (rsetxml), protect against control characters in xml view (should probably be generalized...)
sylvain.thenault@logilab.fr
parents:
528
diff
changeset
|
643 |
w(simple_sgml_tag(tag, val, **attrs)) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
644 |
w(u' </row>\n') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
645 |
w(u'</%s>\n' % self.xml_root) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
646 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
647 |
|
0 | 648 |
class RssView(XmlView): |
649 |
id = 'rss' |
|
650 |
title = _('rss') |
|
651 |
templatable = False |
|
652 |
content_type = 'text/xml' |
|
653 |
http_cache_manager = MaxAgeHTTPCacheManager |
|
654 |
cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |
|
655 |
||
656 |
def cell_call(self, row, col): |
|
657 |
self.wview('rssitem', self.rset, row=row, col=col) |
|
658 |
||
659 |
def call(self): |
|
660 |
"""display a list of entities by calling their <item_vid> view""" |
|
661 |
req = self.req |
|
662 |
self.w(u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding) |
|
663 |
self.w(u'''<rdf:RDF |
|
664 |
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
|
665 |
xmlns:dc="http://purl.org/dc/elements/1.1/" |
|
666 |
xmlns="http://purl.org/rss/1.0/" |
|
667 |
>''') |
|
668 |
self.w(u' <channel rdf:about="%s">\n' % html_escape(req.url())) |
|
669 |
self.w(u' <title>%s RSS Feed</title>\n' % html_escape(self.page_title())) |
|
670 |
self.w(u' <description>%s</description>\n' % html_escape(req.form.get('vtitle', ''))) |
|
671 |
params = req.form.copy() |
|
672 |
params.pop('vid', None) |
|
673 |
self.w(u' <link>%s</link>\n' % html_escape(self.build_url(**params))) |
|
674 |
self.w(u' <items>\n') |
|
675 |
self.w(u' <rdf:Seq>\n') |
|
676 |
for entity in self.rset.entities(): |
|
677 |
self.w(u' <rdf:li resource="%s" />\n' % html_escape(entity.absolute_url())) |
|
678 |
self.w(u' </rdf:Seq>\n') |
|
679 |
self.w(u' </items>\n') |
|
680 |
self.w(u' </channel>\n') |
|
681 |
for i in xrange(self.rset.rowcount): |
|
682 |
self.cell_call(i, 0) |
|
683 |
self.w(u'</rdf:RDF>') |
|
684 |
||
685 |
||
686 |
class RssItemView(EntityView): |
|
687 |
id = 'rssitem' |
|
688 |
date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600) |
|
689 |
||
690 |
def cell_call(self, row, col): |
|
691 |
entity = self.complete_entity(row, col) |
|
692 |
self.w(u'<item rdf:about="%s">\n' % html_escape(entity.absolute_url())) |
|
693 |
self._marker('title', entity.dc_long_title()) |
|
694 |
self._marker('link', entity.absolute_url()) |
|
695 |
self._marker('description', entity.dc_description()) |
|
696 |
self._marker('dc:date', entity.dc_date(self.date_format)) |
|
324
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
697 |
if entity.creator: |
349 | 698 |
self.w(u'<author>') |
324
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
699 |
self._marker('name', entity.creator.name()) |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
700 |
email = entity.creator.get_email() |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
701 |
if email: |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
702 |
self._marker('email', email) |
349 | 703 |
self.w(u'</author>') |
0 | 704 |
self.w(u'</item>\n') |
705 |
||
706 |
def _marker(self, marker, value): |
|
707 |
if value: |
|
708 |
self.w(u' <%s>%s</%s>\n' % (marker, html_escape(value), marker)) |
|
709 |
||
710 |
||
711 |
class CSVMixIn(object): |
|
712 |
"""mixin class for CSV views""" |
|
713 |
templatable = False |
|
714 |
content_type = "text/comma-separated-values" |
|
715 |
binary = True # avoid unicode assertion |
|
716 |
csv_params = {'dialect': 'excel', |
|
717 |
'quotechar': '"', |
|
718 |
'delimiter': ';', |
|
719 |
'lineterminator': '\n'} |
|
720 |
||
721 |
def set_request_content_type(self): |
|
722 |
"""overriden to set a .csv filename""" |
|
723 |
self.req.set_content_type(self.content_type, filename='cubicwebexport.csv') |
|
724 |
||
725 |
def csvwriter(self, **kwargs): |
|
726 |
params = self.csv_params.copy() |
|
727 |
params.update(kwargs) |
|
728 |
return UnicodeCSVWriter(self.w, self.req.encoding, **params) |
|
729 |
||
730 |
||
731 |
class CSVRsetView(CSVMixIn, AnyRsetView): |
|
732 |
"""dumps rset in CSV""" |
|
733 |
id = 'csvexport' |
|
734 |
title = _('csv export') |
|
735 |
||
736 |
def call(self): |
|
737 |
writer = self.csvwriter() |
|
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
738 |
writer.writerow(self.columns_labels()) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
739 |
rset, descr = self.rset, self.rset.description |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
740 |
eschema = self.schema.eschema |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
741 |
for rowindex, row in enumerate(rset): |
0 | 742 |
csvrow = [] |
743 |
for colindex, val in enumerate(row): |
|
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
744 |
etype = descr[rowindex][colindex] |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
745 |
if val is not None and not eschema(etype).is_final(): |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
746 |
# csvrow.append(val) # val is eid in that case |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
747 |
content = self.view('textincontext', rset, |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
748 |
row=rowindex, col=colindex) |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
749 |
else: |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
750 |
content = self.view('final', rset, displaytime=True, |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
751 |
row=rowindex, col=colindex) |
0 | 752 |
csvrow.append(content) |
753 |
writer.writerow(csvrow) |
|
754 |
||
755 |
||
756 |
class CSVEntityView(CSVMixIn, EntityView): |
|
757 |
"""dumps rset's entities (with full set of attributes) in CSV""" |
|
758 |
id = 'ecsvexport' |
|
759 |
title = _('csv entities export') |
|
760 |
||
761 |
def call(self): |
|
762 |
""" |
|
763 |
the generated CSV file will have a table per entity type |
|
764 |
found in the resultset. ('table' here only means empty |
|
765 |
lines separation between table contents) |
|
766 |
""" |
|
767 |
req = self.req |
|
768 |
rows_by_type = {} |
|
769 |
writer = self.csvwriter() |
|
770 |
rowdef_by_type = {} |
|
771 |
for index in xrange(len(self.rset)): |
|
772 |
entity = self.complete_entity(index) |
|
773 |
if entity.e_schema not in rows_by_type: |
|
400
b05a36678265
'as' becomes a keyword when with statement activated
sylvain.thenault@logilab.fr
parents:
371
diff
changeset
|
774 |
rowdef_by_type[entity.e_schema] = [rs for rs, at in entity.e_schema.attribute_definitions() |
b05a36678265
'as' becomes a keyword when with statement activated
sylvain.thenault@logilab.fr
parents:
371
diff
changeset
|
775 |
if at != 'Bytes'] |
0 | 776 |
rows_by_type[entity.e_schema] = [[display_name(req, rschema.type) |
777 |
for rschema in rowdef_by_type[entity.e_schema]]] |
|
778 |
rows = rows_by_type[entity.e_schema] |
|
779 |
rows.append([entity.printable_value(rs.type, format='text/plain') |
|
780 |
for rs in rowdef_by_type[entity.e_schema]]) |
|
781 |
for etype, rows in rows_by_type.items(): |
|
782 |
writer.writerows(rows) |
|
783 |
# use two empty lines as separator |
|
784 |
writer.writerows([[], []]) |
|
785 |
||
786 |
||
787 |
## Work in progress ########################################################### |
|
788 |
||
789 |
class SearchForAssociationView(EntityView): |
|
790 |
"""view called by the edition view when the user asks |
|
791 |
to search for something to link to the edited eid |
|
792 |
""" |
|
793 |
id = 'search-associate' |
|
794 |
title = _('search for association') |
|
431 | 795 |
__selectors__ = (one_line_rset, match_search_state, accept) |
0 | 796 |
accepts = ('Any',) |
797 |
search_states = ('linksearch',) |
|
798 |
||
799 |
def cell_call(self, row, col): |
|
800 |
rset, vid, divid, paginate = self.filter_box_context_info() |
|
801 |
self.w(u'<div id="%s">' % divid) |
|
802 |
self.pagination(self.req, rset, w=self.w) |
|
803 |
self.wview(vid, rset) |
|
804 |
self.w(u'</div>') |
|
805 |
||
806 |
@cached |
|
807 |
def filter_box_context_info(self): |
|
808 |
entity = self.entity(0, 0) |
|
809 |
role, eid, rtype, etype = self.req.search_state[1] |
|
810 |
assert entity.eid == typed_eid(eid) |
|
811 |
# the default behaviour is to fetch all unrelated entities and display |
|
812 |
# them. Use fetch_order and not fetch_unrelated_order as sort method |
|
813 |
# since the latter is mainly there to select relevant items in the combo |
|
814 |
# box, it doesn't give interesting result in this context |
|
815 |
rql = entity.unrelated_rql(rtype, etype, role, |
|
816 |
ordermethod='fetch_order', |
|
817 |
vocabconstraints=False) |
|
818 |
rset = self.req.execute(rql, {'x' : entity.eid}, 'x') |
|
819 |
#vid = vid_from_rset(self.req, rset, self.schema) |
|
820 |
return rset, 'list', "search-associate-content", True |
|
821 |
||
822 |
||
823 |
class OutOfContextSearch(EntityView): |
|
824 |
id = 'outofcontext-search' |
|
825 |
def cell_call(self, row, col): |
|
826 |
entity = self.entity(row, col) |
|
827 |
erset = entity.as_rset() |
|
828 |
if linksearch_match(self.req, erset): |
|
829 |
self.w(u'<a href="%s" title="%s">%s</a> <a href="%s" title="%s">[...]</a>' % ( |
|
830 |
html_escape(linksearch_select_url(self.req, erset)), |
|
831 |
self.req._('select this entity'), |
|
832 |
html_escape(entity.view('textoutofcontext')), |
|
833 |
html_escape(entity.absolute_url(vid='primary')), |
|
834 |
self.req._('view detail for this entity'))) |
|
835 |
else: |
|
836 |
entity.view('outofcontext', w=self.w) |
|
837 |
||
838 |
||
839 |
class EditRelationView(EntityView): |
|
840 |
"""Note: This is work in progress |
|
841 |
||
842 |
This view is part of the edition view refactoring. |
|
843 |
It is still too big and cluttered with strange logic, but it's a start |
|
844 |
||
845 |
The main idea is to be able to call an edition view for a specific |
|
846 |
relation. For example : |
|
847 |
self.wview('editrelation', person_rset, rtype='firstname') |
|
848 |
self.wview('editrelation', person_rset, rtype='works_for') |
|
849 |
""" |
|
850 |
id = 'editrelation' |
|
851 |
||
431 | 852 |
__selectors__ = (match_form_params,) |
0 | 853 |
form_params = ('rtype',) |
854 |
||
855 |
# TODO: inlineview, multiple edit, (widget view ?) |
|
856 |
def cell_call(self, row, col, rtype=None, role='subject', targettype=None, |
|
857 |
showlabel=True): |
|
858 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
859 |
entity = self.entity(row, col) |
|
860 |
rtype = self.req.form.get('rtype', rtype) |
|
861 |
showlabel = self.req.form.get('showlabel', showlabel) |
|
862 |
assert rtype is not None, "rtype is mandatory for 'edirelation' view" |
|
863 |
targettype = self.req.form.get('targettype', targettype) |
|
864 |
role = self.req.form.get('role', role) |
|
865 |
category = entity.rtags.get_category(rtype, targettype, role) |
|
570
0c9dfc430200
fix test (this view is not used actually...)
sylvain.thenault@logilab.fr
parents:
532
diff
changeset
|
866 |
if category in ('primary', 'secondary') or self.schema.rschema(rtype).is_final(): |
0 | 867 |
if hasattr(entity, '%s_format' % rtype): |
868 |
formatwdg = entity.get_widget('%s_format' % rtype, role) |
|
869 |
self.w(formatwdg.edit_render(entity)) |
|
870 |
self.w(u'<br/>') |
|
871 |
wdg = entity.get_widget(rtype, role) |
|
872 |
if showlabel: |
|
873 |
self.w(u'%s' % wdg.render_label(entity)) |
|
874 |
self.w(u'%s %s %s' % |
|
875 |
(wdg.render_error(entity), wdg.edit_render(entity), |
|
876 |
wdg.render_help(entity),)) |
|
570
0c9dfc430200
fix test (this view is not used actually...)
sylvain.thenault@logilab.fr
parents:
532
diff
changeset
|
877 |
else: |
0 | 878 |
self._render_generic_relation(entity, rtype, role) |
879 |
||
880 |
def _render_generic_relation(self, entity, relname, role): |
|
881 |
text = self.req.__('add %s %s %s' % (entity.e_schema, relname, role)) |
|
882 |
# pending operations |
|
883 |
operations = self.req.get_pending_operations(entity, relname, role) |
|
884 |
if operations['insert'] or operations['delete'] or 'unfold' in self.req.form: |
|
885 |
self.w(u'<h3>%s</h3>' % text) |
|
886 |
self._render_generic_relation_form(operations, entity, relname, role) |
|
887 |
else: |
|
888 |
divid = "%s%sreledit" % (relname, role) |
|
889 |
url = ajax_replace_url(divid, rql_for_eid(entity.eid), 'editrelation', |
|
890 |
{'unfold' : 1, 'relname' : relname, 'role' : role}) |
|
891 |
self.w(u'<a href="%s">%s</a>' % (url, text)) |
|
892 |
self.w(u'<div id="%s"></div>' % divid) |
|
893 |
||
894 |
||
895 |
def _build_opvalue(self, entity, relname, target, role): |
|
896 |
if role == 'subject': |
|
897 |
return '%s:%s:%s' % (entity.eid, relname, target) |
|
898 |
else: |
|
899 |
return '%s:%s:%s' % (target, relname, entity.eid) |
|
900 |
||
901 |
||
902 |
def _render_generic_relation_form(self, operations, entity, relname, role): |
|
903 |
rqlexec = self.req.execute |
|
904 |
for optype, targets in operations.items(): |
|
905 |
for target in targets: |
|
906 |
self._render_pending(optype, entity, relname, target, role) |
|
907 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
908 |
self.w(u'<a href="javascript: addPendingDelete(\'%s\', %s);">-</a> ' |
|
909 |
% (opvalue, entity.eid)) |
|
910 |
rset = rqlexec('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
911 |
self.wview('oneline', rset) |
|
912 |
# now, unrelated ones |
|
913 |
self._render_unrelated_selection(entity, relname, role) |
|
914 |
||
915 |
def _render_pending(self, optype, entity, relname, target, role): |
|
916 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
917 |
self.w(u'<input type="hidden" name="__%s" value="%s" />' |
|
918 |
% (optype, opvalue)) |
|
919 |
if optype == 'insert': |
|
920 |
checktext = '-' |
|
921 |
else: |
|
922 |
checktext = '+' |
|
923 |
rset = self.req.execute('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
924 |
self.w(u"""[<a href="javascript: cancelPending%s('%s:%s:%s')">%s</a>""" |
|
925 |
% (optype.capitalize(), relname, target, role, |
|
926 |
self.view('oneline', rset))) |
|
927 |
||
928 |
def _render_unrelated_selection(self, entity, relname, role): |
|
929 |
rschema = self.schema.rschema(relname) |
|
930 |
if role == 'subject': |
|
931 |
targettypes = rschema.objects(entity.e_schema) |
|
932 |
else: |
|
933 |
targettypes = rschema.subjects(entity.e_schema) |
|
934 |
self.w(u'<select onselect="addPendingInsert(this.selected.value);">') |
|
935 |
for targettype in targettypes: |
|
936 |
unrelated = entity.unrelated(relname, targettype, role) # XXX limit |
|
937 |
for rowindex, row in enumerate(unrelated): |
|
938 |
teid = row[0] |
|
939 |
opvalue = self._build_opvalue(entity, relname, teid, role) |
|
940 |
self.w(u'<option name="__insert" value="%s>%s</option>' |
|
941 |
% (opvalue, self.view('text', unrelated, row=rowindex))) |
|
942 |
self.w(u'</select>') |
|
943 |
||
944 |
||
945 |
class TextSearchResultView(EntityView): |
|
946 |
"""this view is used to display full-text search |
|
947 |
||
948 |
It tries to highlight part of data where the search word appears. |
|
949 |
||
950 |
XXX: finish me (fixed line width, fixed number of lines, CSS, etc.) |
|
951 |
""" |
|
952 |
id = 'tsearch' |
|
953 |
||
954 |
||
955 |
def cell_call(self, row, col, **kwargs): |
|
956 |
entity = self.complete_entity(row, col) |
|
957 |
self.w(entity.view('incontext')) |
|
958 |
searched = self.rset.searched_text() |
|
959 |
if searched is None: |
|
960 |
return |
|
961 |
searched = searched.lower() |
|
962 |
highlighted = '<b>%s</b>' % searched |
|
963 |
for attr in entity.e_schema.indexable_attributes(): |
|
964 |
try: |
|
965 |
value = html_escape(entity.printable_value(attr, format='text/plain').lower()) |
|
966 |
except TransformError, ex: |
|
967 |
continue |
|
968 |
except: |
|
969 |
continue |
|
970 |
if searched in value: |
|
971 |
contexts = [] |
|
972 |
for ctx in value.split(searched): |
|
973 |
if len(ctx) > 30: |
|
974 |
contexts.append(u'...' + ctx[-30:]) |
|
975 |
else: |
|
976 |
contexts.append(ctx) |
|
977 |
value = u'\n' + highlighted.join(contexts) |
|
978 |
self.w(value.replace('\n', '<br/>')) |
|
979 |
||
980 |
||
981 |
class EntityRelationView(EntityView): |
|
982 |
accepts = () |
|
983 |
vid = 'list' |
|
984 |
||
985 |
def cell_call(self, row, col): |
|
986 |
if self.target == 'object': |
|
987 |
role = 'subject' |
|
988 |
else: |
|
989 |
role = 'object' |
|
990 |
rset = self.rset.get_entity(row, col).related(self.rtype, role) |
|
991 |
self.w(u'<h1>%s</h1>' % self.req._(self.title).capitalize()) |
|
992 |
self.w(u'<div class="mainInfo">') |
|
993 |
self.wview(self.vid, rset, 'noresult') |
|
994 |
self.w(u'</div>') |
|
995 |
||
996 |
||
997 |
class TooltipView(OneLineView): |
|
998 |
"""A entity view used in a tooltip""" |
|
999 |
id = 'tooltip' |
|
1000 |
title = None # don't display in possible views |
|
1001 |
def cell_call(self, row, col): |
|
1002 |
self.wview('oneline', self.rset, row=row, col=col) |
|
1003 |
||
1004 |
try: |
|
1005 |
from cubicweb.web.views.tableview import TableView |
|
1006 |
from logilab.common.deprecation import class_moved |
|
1007 |
TableView = class_moved(TableView) |
|
1008 |
except ImportError: |
|
1009 |
pass # gae has no tableview module (yet) |