author | sylvain.thenault@logilab.fr |
Wed, 11 Feb 2009 13:52:01 +0100 | |
changeset 573 | 9c8fd72ba6c1 |
parent 570 | 0c9dfc430200 |
child 608 | 7433011bb811 |
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 |
||
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
60 |
def cell_call(self, row, col, props=None, displaytime=False, format='text/html'): |
0 | 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 ! |
|
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
67 |
self.w(entity.printable_value(rtype, value, format=format)) |
0 | 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, |
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
642 |
row=rowindex, col=colindex, format='text/plain') |
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
|
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: |
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
750 |
content = self.view('final', rset, |
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
751 |
displaytime=True, format='text/plain', |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
752 |
row=rowindex, col=colindex) |
0 | 753 |
csvrow.append(content) |
754 |
writer.writerow(csvrow) |
|
755 |
||
756 |
||
757 |
class CSVEntityView(CSVMixIn, EntityView): |
|
758 |
"""dumps rset's entities (with full set of attributes) in CSV""" |
|
759 |
id = 'ecsvexport' |
|
760 |
title = _('csv entities export') |
|
761 |
||
762 |
def call(self): |
|
763 |
""" |
|
764 |
the generated CSV file will have a table per entity type |
|
765 |
found in the resultset. ('table' here only means empty |
|
766 |
lines separation between table contents) |
|
767 |
""" |
|
768 |
req = self.req |
|
769 |
rows_by_type = {} |
|
770 |
writer = self.csvwriter() |
|
771 |
rowdef_by_type = {} |
|
772 |
for index in xrange(len(self.rset)): |
|
773 |
entity = self.complete_entity(index) |
|
774 |
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
|
775 |
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
|
776 |
if at != 'Bytes'] |
0 | 777 |
rows_by_type[entity.e_schema] = [[display_name(req, rschema.type) |
778 |
for rschema in rowdef_by_type[entity.e_schema]]] |
|
779 |
rows = rows_by_type[entity.e_schema] |
|
780 |
rows.append([entity.printable_value(rs.type, format='text/plain') |
|
781 |
for rs in rowdef_by_type[entity.e_schema]]) |
|
782 |
for etype, rows in rows_by_type.items(): |
|
783 |
writer.writerows(rows) |
|
784 |
# use two empty lines as separator |
|
785 |
writer.writerows([[], []]) |
|
786 |
||
787 |
||
788 |
## Work in progress ########################################################### |
|
789 |
||
790 |
class SearchForAssociationView(EntityView): |
|
791 |
"""view called by the edition view when the user asks |
|
792 |
to search for something to link to the edited eid |
|
793 |
""" |
|
794 |
id = 'search-associate' |
|
795 |
title = _('search for association') |
|
431 | 796 |
__selectors__ = (one_line_rset, match_search_state, accept) |
0 | 797 |
accepts = ('Any',) |
798 |
search_states = ('linksearch',) |
|
799 |
||
800 |
def cell_call(self, row, col): |
|
801 |
rset, vid, divid, paginate = self.filter_box_context_info() |
|
802 |
self.w(u'<div id="%s">' % divid) |
|
803 |
self.pagination(self.req, rset, w=self.w) |
|
804 |
self.wview(vid, rset) |
|
805 |
self.w(u'</div>') |
|
806 |
||
807 |
@cached |
|
808 |
def filter_box_context_info(self): |
|
809 |
entity = self.entity(0, 0) |
|
810 |
role, eid, rtype, etype = self.req.search_state[1] |
|
811 |
assert entity.eid == typed_eid(eid) |
|
812 |
# the default behaviour is to fetch all unrelated entities and display |
|
813 |
# them. Use fetch_order and not fetch_unrelated_order as sort method |
|
814 |
# since the latter is mainly there to select relevant items in the combo |
|
815 |
# box, it doesn't give interesting result in this context |
|
816 |
rql = entity.unrelated_rql(rtype, etype, role, |
|
817 |
ordermethod='fetch_order', |
|
818 |
vocabconstraints=False) |
|
819 |
rset = self.req.execute(rql, {'x' : entity.eid}, 'x') |
|
820 |
#vid = vid_from_rset(self.req, rset, self.schema) |
|
821 |
return rset, 'list', "search-associate-content", True |
|
822 |
||
823 |
||
824 |
class OutOfContextSearch(EntityView): |
|
825 |
id = 'outofcontext-search' |
|
826 |
def cell_call(self, row, col): |
|
827 |
entity = self.entity(row, col) |
|
828 |
erset = entity.as_rset() |
|
829 |
if linksearch_match(self.req, erset): |
|
830 |
self.w(u'<a href="%s" title="%s">%s</a> <a href="%s" title="%s">[...]</a>' % ( |
|
831 |
html_escape(linksearch_select_url(self.req, erset)), |
|
832 |
self.req._('select this entity'), |
|
833 |
html_escape(entity.view('textoutofcontext')), |
|
834 |
html_escape(entity.absolute_url(vid='primary')), |
|
835 |
self.req._('view detail for this entity'))) |
|
836 |
else: |
|
837 |
entity.view('outofcontext', w=self.w) |
|
838 |
||
839 |
||
840 |
class EditRelationView(EntityView): |
|
841 |
"""Note: This is work in progress |
|
842 |
||
843 |
This view is part of the edition view refactoring. |
|
844 |
It is still too big and cluttered with strange logic, but it's a start |
|
845 |
||
846 |
The main idea is to be able to call an edition view for a specific |
|
847 |
relation. For example : |
|
848 |
self.wview('editrelation', person_rset, rtype='firstname') |
|
849 |
self.wview('editrelation', person_rset, rtype='works_for') |
|
850 |
""" |
|
851 |
id = 'editrelation' |
|
852 |
||
431 | 853 |
__selectors__ = (match_form_params,) |
0 | 854 |
form_params = ('rtype',) |
855 |
||
856 |
# TODO: inlineview, multiple edit, (widget view ?) |
|
857 |
def cell_call(self, row, col, rtype=None, role='subject', targettype=None, |
|
858 |
showlabel=True): |
|
859 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
860 |
entity = self.entity(row, col) |
|
861 |
rtype = self.req.form.get('rtype', rtype) |
|
862 |
showlabel = self.req.form.get('showlabel', showlabel) |
|
863 |
assert rtype is not None, "rtype is mandatory for 'edirelation' view" |
|
864 |
targettype = self.req.form.get('targettype', targettype) |
|
865 |
role = self.req.form.get('role', role) |
|
866 |
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
|
867 |
if category in ('primary', 'secondary') or self.schema.rschema(rtype).is_final(): |
0 | 868 |
if hasattr(entity, '%s_format' % rtype): |
869 |
formatwdg = entity.get_widget('%s_format' % rtype, role) |
|
870 |
self.w(formatwdg.edit_render(entity)) |
|
871 |
self.w(u'<br/>') |
|
872 |
wdg = entity.get_widget(rtype, role) |
|
873 |
if showlabel: |
|
874 |
self.w(u'%s' % wdg.render_label(entity)) |
|
875 |
self.w(u'%s %s %s' % |
|
876 |
(wdg.render_error(entity), wdg.edit_render(entity), |
|
877 |
wdg.render_help(entity),)) |
|
570
0c9dfc430200
fix test (this view is not used actually...)
sylvain.thenault@logilab.fr
parents:
532
diff
changeset
|
878 |
else: |
0 | 879 |
self._render_generic_relation(entity, rtype, role) |
880 |
||
881 |
def _render_generic_relation(self, entity, relname, role): |
|
882 |
text = self.req.__('add %s %s %s' % (entity.e_schema, relname, role)) |
|
883 |
# pending operations |
|
884 |
operations = self.req.get_pending_operations(entity, relname, role) |
|
885 |
if operations['insert'] or operations['delete'] or 'unfold' in self.req.form: |
|
886 |
self.w(u'<h3>%s</h3>' % text) |
|
887 |
self._render_generic_relation_form(operations, entity, relname, role) |
|
888 |
else: |
|
889 |
divid = "%s%sreledit" % (relname, role) |
|
890 |
url = ajax_replace_url(divid, rql_for_eid(entity.eid), 'editrelation', |
|
891 |
{'unfold' : 1, 'relname' : relname, 'role' : role}) |
|
892 |
self.w(u'<a href="%s">%s</a>' % (url, text)) |
|
893 |
self.w(u'<div id="%s"></div>' % divid) |
|
894 |
||
895 |
||
896 |
def _build_opvalue(self, entity, relname, target, role): |
|
897 |
if role == 'subject': |
|
898 |
return '%s:%s:%s' % (entity.eid, relname, target) |
|
899 |
else: |
|
900 |
return '%s:%s:%s' % (target, relname, entity.eid) |
|
901 |
||
902 |
||
903 |
def _render_generic_relation_form(self, operations, entity, relname, role): |
|
904 |
rqlexec = self.req.execute |
|
905 |
for optype, targets in operations.items(): |
|
906 |
for target in targets: |
|
907 |
self._render_pending(optype, entity, relname, target, role) |
|
908 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
909 |
self.w(u'<a href="javascript: addPendingDelete(\'%s\', %s);">-</a> ' |
|
910 |
% (opvalue, entity.eid)) |
|
911 |
rset = rqlexec('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
912 |
self.wview('oneline', rset) |
|
913 |
# now, unrelated ones |
|
914 |
self._render_unrelated_selection(entity, relname, role) |
|
915 |
||
916 |
def _render_pending(self, optype, entity, relname, target, role): |
|
917 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
918 |
self.w(u'<input type="hidden" name="__%s" value="%s" />' |
|
919 |
% (optype, opvalue)) |
|
920 |
if optype == 'insert': |
|
921 |
checktext = '-' |
|
922 |
else: |
|
923 |
checktext = '+' |
|
924 |
rset = self.req.execute('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
925 |
self.w(u"""[<a href="javascript: cancelPending%s('%s:%s:%s')">%s</a>""" |
|
926 |
% (optype.capitalize(), relname, target, role, |
|
927 |
self.view('oneline', rset))) |
|
928 |
||
929 |
def _render_unrelated_selection(self, entity, relname, role): |
|
930 |
rschema = self.schema.rschema(relname) |
|
931 |
if role == 'subject': |
|
932 |
targettypes = rschema.objects(entity.e_schema) |
|
933 |
else: |
|
934 |
targettypes = rschema.subjects(entity.e_schema) |
|
935 |
self.w(u'<select onselect="addPendingInsert(this.selected.value);">') |
|
936 |
for targettype in targettypes: |
|
937 |
unrelated = entity.unrelated(relname, targettype, role) # XXX limit |
|
938 |
for rowindex, row in enumerate(unrelated): |
|
939 |
teid = row[0] |
|
940 |
opvalue = self._build_opvalue(entity, relname, teid, role) |
|
941 |
self.w(u'<option name="__insert" value="%s>%s</option>' |
|
942 |
% (opvalue, self.view('text', unrelated, row=rowindex))) |
|
943 |
self.w(u'</select>') |
|
944 |
||
945 |
||
946 |
class TextSearchResultView(EntityView): |
|
947 |
"""this view is used to display full-text search |
|
948 |
||
949 |
It tries to highlight part of data where the search word appears. |
|
950 |
||
951 |
XXX: finish me (fixed line width, fixed number of lines, CSS, etc.) |
|
952 |
""" |
|
953 |
id = 'tsearch' |
|
954 |
||
955 |
||
956 |
def cell_call(self, row, col, **kwargs): |
|
957 |
entity = self.complete_entity(row, col) |
|
958 |
self.w(entity.view('incontext')) |
|
959 |
searched = self.rset.searched_text() |
|
960 |
if searched is None: |
|
961 |
return |
|
962 |
searched = searched.lower() |
|
963 |
highlighted = '<b>%s</b>' % searched |
|
964 |
for attr in entity.e_schema.indexable_attributes(): |
|
965 |
try: |
|
966 |
value = html_escape(entity.printable_value(attr, format='text/plain').lower()) |
|
967 |
except TransformError, ex: |
|
968 |
continue |
|
969 |
except: |
|
970 |
continue |
|
971 |
if searched in value: |
|
972 |
contexts = [] |
|
973 |
for ctx in value.split(searched): |
|
974 |
if len(ctx) > 30: |
|
975 |
contexts.append(u'...' + ctx[-30:]) |
|
976 |
else: |
|
977 |
contexts.append(ctx) |
|
978 |
value = u'\n' + highlighted.join(contexts) |
|
979 |
self.w(value.replace('\n', '<br/>')) |
|
980 |
||
981 |
||
982 |
class EntityRelationView(EntityView): |
|
983 |
accepts = () |
|
984 |
vid = 'list' |
|
985 |
||
986 |
def cell_call(self, row, col): |
|
987 |
if self.target == 'object': |
|
988 |
role = 'subject' |
|
989 |
else: |
|
990 |
role = 'object' |
|
991 |
rset = self.rset.get_entity(row, col).related(self.rtype, role) |
|
992 |
self.w(u'<h1>%s</h1>' % self.req._(self.title).capitalize()) |
|
993 |
self.w(u'<div class="mainInfo">') |
|
994 |
self.wview(self.vid, rset, 'noresult') |
|
995 |
self.w(u'</div>') |
|
996 |
||
997 |
||
998 |
class TooltipView(OneLineView): |
|
999 |
"""A entity view used in a tooltip""" |
|
1000 |
id = 'tooltip' |
|
1001 |
title = None # don't display in possible views |
|
1002 |
def cell_call(self, row, col): |
|
1003 |
self.wview('oneline', self.rset, row=row, col=col) |
|
1004 |
||
1005 |
try: |
|
1006 |
from cubicweb.web.views.tableview import TableView |
|
1007 |
from logilab.common.deprecation import class_moved |
|
1008 |
TableView = class_moved(TableView) |
|
1009 |
except ImportError: |
|
1010 |
pass # gae has no tableview module (yet) |