author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 18 Feb 2009 10:03:58 +0100 | |
branch | tls-sprint |
changeset 758 | 0c0dfd33a76d |
parent 742 | 99115e029dca |
child 760 | 95b3419e94c5 |
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 |
|
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
25 |
from cubicweb.selectors import (yes, empty_rset, nonempty_rset, one_line_rset, |
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
26 |
non_final_entity, match_search_state, match_form_params) |
0 | 27 |
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
|
28 |
ajax_replace_url, rql_for_eid, simple_sgml_tag) |
632 | 29 |
from cubicweb.common.view import EntityView, AnyRsetView, View |
0 | 30 |
from cubicweb.web.httpcache import MaxAgeHTTPCacheManager |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
608
diff
changeset
|
31 |
from cubicweb.web.views import vid_from_rset, linksearch_select_url |
0 | 32 |
|
33 |
_ = unicode |
|
34 |
||
35 |
class NullView(AnyRsetView): |
|
36 |
"""default view when no result has been found""" |
|
37 |
id = 'null' |
|
758
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
38 |
__select__ = yes() |
0 | 39 |
def call(self, **kwargs): |
40 |
pass |
|
41 |
cell_call = call |
|
42 |
||
43 |
||
632 | 44 |
class NoResultView(View): |
0 | 45 |
"""default view when no result has been found""" |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
647
diff
changeset
|
46 |
__select__ = empty_rset() |
0 | 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' |
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
361 |
|
0 | 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 |
show_eid = True |
|
387 |
||
388 |
def cell_call(self, row, col): |
|
389 |
_ = self.req._ |
|
390 |
entity = self.entity(row, col) |
|
391 |
self.w(u'<div class="metadata">') |
|
392 |
if self.show_eid: |
|
393 |
self.w(u'#%s - ' % entity.eid) |
|
394 |
if entity.modification_date != entity.creation_date: |
|
395 |
self.w(u'<span>%s</span> ' % _('latest update on')) |
|
396 |
self.w(u'<span class="value">%s</span>, ' |
|
397 |
% self.format_date(entity.modification_date)) |
|
398 |
# entities from external source may not have a creation date (eg ldap) |
|
399 |
if entity.creation_date: |
|
400 |
self.w(u'<span>%s</span> ' % _('created on')) |
|
401 |
self.w(u'<span class="value">%s</span>' |
|
402 |
% self.format_date(entity.creation_date)) |
|
403 |
if entity.creator: |
|
404 |
self.w(u' <span>%s</span> ' % _('by')) |
|
405 |
self.w(u'<span class="value">%s</span>' % entity.creator.name()) |
|
406 |
self.w(u'</div>') |
|
407 |
||
408 |
||
409 |
# new default views for finner control in general views , to use instead of |
|
410 |
# oneline / secondary |
|
411 |
||
412 |
class InContextTextView(TextView): |
|
413 |
id = 'textincontext' |
|
414 |
title = None # not listed as a possible view |
|
415 |
def cell_call(self, row, col): |
|
416 |
entity = self.entity(row, col) |
|
417 |
self.w(entity.dc_title()) |
|
418 |
||
419 |
class OutOfContextTextView(InContextTextView): |
|
420 |
id = 'textoutofcontext' |
|
421 |
||
422 |
def cell_call(self, row, col): |
|
423 |
entity = self.entity(row, col) |
|
424 |
self.w(entity.dc_long_title()) |
|
425 |
||
426 |
||
427 |
class InContextView(EntityView): |
|
428 |
id = 'incontext' |
|
429 |
||
430 |
def cell_call(self, row, col): |
|
431 |
entity = self.entity(row, col) |
|
432 |
desc = cut(entity.dc_description(), 50) |
|
433 |
self.w(u'<a href="%s" title="%s">' % (html_escape(entity.absolute_url()), |
|
434 |
html_escape(desc))) |
|
435 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
436 |
self.w(u'</a>') |
|
437 |
||
438 |
||
439 |
class OutOfContextView(EntityView): |
|
440 |
id = 'outofcontext' |
|
441 |
||
442 |
def cell_call(self, row, col): |
|
443 |
self.w(u'<a href="%s">' % self.entity(row, col).absolute_url()) |
|
444 |
self.w(html_escape(self.view('textoutofcontext', self.rset, row=row, col=col))) |
|
445 |
self.w(u'</a>') |
|
446 |
||
447 |
class NotClickableInContextView(EntityView): |
|
448 |
id = 'incontext' |
|
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
449 |
|
0 | 450 |
def cell_call(self, row, col): |
451 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
452 |
||
453 |
## class NotClickableOutOfContextView(EntityView): |
|
454 |
## id = 'outofcontext' |
|
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
455 |
|
0 | 456 |
## def cell_call(self, row, col): |
457 |
## self.w(html_escape(self.view('textoutofcontext', self.rset, row=row))) |
|
458 |
||
459 |
||
460 |
# list and table related views ################################################ |
|
461 |
||
462 |
class ListView(EntityView): |
|
463 |
id = 'list' |
|
464 |
title = _('list') |
|
465 |
item_vid = 'listitem' |
|
466 |
||
467 |
def call(self, klass=None, title=None, subvid=None, listid=None, **kwargs): |
|
468 |
"""display a list of entities by calling their <item_vid> view |
|
469 |
|
|
470 |
:param listid: the DOM id to use for the root element |
|
471 |
""" |
|
472 |
if subvid is None and 'subvid' in self.req.form: |
|
473 |
subvid = self.req.form.pop('subvid') # consume it |
|
474 |
if listid: |
|
475 |
listid = u' id="%s"' % listid |
|
476 |
else: |
|
477 |
listid = u'' |
|
478 |
if title: |
|
479 |
self.w(u'<div%s class="%s"><h4>%s</h4>\n' % (listid, klass or 'section', title)) |
|
480 |
self.w(u'<ul>\n') |
|
481 |
else: |
|
482 |
self.w(u'<ul%s class="%s">\n' % (listid, klass or 'section')) |
|
483 |
for i in xrange(self.rset.rowcount): |
|
484 |
self.cell_call(row=i, col=0, vid=subvid, **kwargs) |
|
485 |
self.w(u'</ul>\n') |
|
486 |
if title: |
|
487 |
self.w(u'</div>\n') |
|
488 |
||
489 |
def cell_call(self, row, col=0, vid=None, **kwargs): |
|
490 |
self.w(u'<li>') |
|
491 |
self.wview(self.item_vid, self.rset, row=row, col=col, vid=vid, **kwargs) |
|
492 |
self.w(u'</li>\n') |
|
493 |
||
494 |
def url(self): |
|
495 |
"""overrides url method so that by default, the view list is called |
|
496 |
with sorted entities |
|
497 |
""" |
|
498 |
coltypes = self.rset.column_types(0) |
|
499 |
# don't want to generate the rql if there is some restriction on |
|
500 |
# something else than the entity type |
|
501 |
if len(coltypes) == 1: |
|
502 |
# XXX norestriction is not correct here. For instance, in cases like |
|
503 |
# Any P,N WHERE P is Project, P name N |
|
504 |
# norestriction should equal True |
|
505 |
restr = self.rset.syntax_tree().children[0].where |
|
506 |
norestriction = (isinstance(restr, nodes.Relation) and |
|
507 |
restr.is_types_restriction()) |
|
508 |
if norestriction: |
|
509 |
etype = iter(coltypes).next() |
|
510 |
return self.build_url(etype.lower(), vid=self.id) |
|
511 |
if len(self.rset) == 1: |
|
512 |
entity = self.rset.get_entity(0, 0) |
|
513 |
return self.build_url(entity.rest_path(), vid=self.id) |
|
514 |
return self.build_url(rql=self.rset.printable_rql(), vid=self.id) |
|
515 |
||
516 |
||
517 |
class ListItemView(EntityView): |
|
518 |
id = 'listitem' |
|
519 |
||
520 |
@property |
|
521 |
def redirect_vid(self): |
|
522 |
if self.req.search_state[0] == 'normal': |
|
523 |
return 'outofcontext' |
|
524 |
return 'outofcontext-search' |
|
525 |
||
526 |
def cell_call(self, row, col, vid=None, **kwargs): |
|
527 |
if not vid: |
|
528 |
vid = self.redirect_vid |
|
529 |
try: |
|
530 |
self.wview(vid, self.rset, row=row, col=col, **kwargs) |
|
531 |
except NoSelectableObject: |
|
532 |
if vid == self.redirect_vid: |
|
533 |
raise |
|
534 |
self.wview(self.redirect_vid, self.rset, row=row, col=col, **kwargs) |
|
535 |
||
536 |
||
537 |
class SimpleListView(ListItemView): |
|
538 |
"""list without bullets""" |
|
539 |
id = 'simplelist' |
|
540 |
redirect_vid = 'incontext' |
|
541 |
||
542 |
||
543 |
class CSVView(SimpleListView): |
|
544 |
id = 'csv' |
|
545 |
redirect_vid = 'incontext' |
|
546 |
||
547 |
def call(self, **kwargs): |
|
548 |
rset = self.rset |
|
549 |
for i in xrange(len(rset)): |
|
550 |
self.cell_call(i, 0, vid=kwargs.get('vid')) |
|
551 |
if i < rset.rowcount-1: |
|
552 |
self.w(u", ") |
|
553 |
||
554 |
||
555 |
class TreeItemView(ListItemView): |
|
556 |
id = 'treeitem' |
|
557 |
||
558 |
def cell_call(self, row, col): |
|
559 |
self.wview('incontext', self.rset, row=row, col=col) |
|
560 |
||
561 |
||
562 |
# xml and xml/rss views ####################################################### |
|
563 |
||
564 |
class XmlView(EntityView): |
|
565 |
id = 'xml' |
|
566 |
title = _('xml') |
|
567 |
templatable = False |
|
568 |
content_type = 'text/xml' |
|
569 |
xml_root = 'rset' |
|
570 |
item_vid = 'xmlitem' |
|
571 |
||
572 |
def cell_call(self, row, col): |
|
573 |
self.wview(self.item_vid, self.rset, row=row, col=col) |
|
574 |
||
575 |
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
|
576 |
"""display a list of entities by calling their <item_vid> view""" |
0 | 577 |
self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding) |
578 |
self.w(u'<%s size="%s">\n' % (self.xml_root, len(self.rset))) |
|
579 |
for i in xrange(self.rset.rowcount): |
|
580 |
self.cell_call(i, 0) |
|
581 |
self.w(u'</%s>\n' % self.xml_root) |
|
582 |
||
583 |
||
584 |
class XmlItemView(EntityView): |
|
585 |
id = 'xmlitem' |
|
586 |
||
587 |
def cell_call(self, row, col): |
|
588 |
""" element as an item for an xml feed """ |
|
589 |
entity = self.complete_entity(row, col) |
|
590 |
self.w(u'<%s>\n' % (entity.e_schema)) |
|
591 |
for rschema, attrschema in entity.e_schema.attribute_definitions(): |
|
592 |
attr = rschema.type |
|
593 |
try: |
|
594 |
value = entity[attr] |
|
595 |
except KeyError: |
|
596 |
# Bytes |
|
597 |
continue |
|
598 |
if value is not None: |
|
599 |
if attrschema == 'Bytes': |
|
600 |
from base64 import b64encode |
|
601 |
value = '<![CDATA[%s]]>' % b64encode(value.getvalue()) |
|
602 |
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
|
603 |
value = xml_escape(value) |
0 | 604 |
self.w(u' <%s>%s</%s>\n' % (attr, value, attr)) |
605 |
self.w(u'</%s>\n' % (entity.e_schema)) |
|
606 |
||
607 |
||
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
608 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
609 |
class XMLRsetView(AnyRsetView): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
610 |
"""dumps xml in CSV""" |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
611 |
id = 'rsetxml' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
612 |
title = _('xml export') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
613 |
templatable = False |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
614 |
content_type = 'text/xml' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
615 |
xml_root = 'rset' |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
616 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
617 |
def call(self): |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
618 |
w = self.w |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
619 |
rset, descr = self.rset, self.rset.description |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
620 |
eschema = self.schema.eschema |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
621 |
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
|
622 |
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
|
623 |
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
|
624 |
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
|
625 |
w(u' <row>\n') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
626 |
for colindex, val in enumerate(row): |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
627 |
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
|
628 |
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
|
629 |
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
|
630 |
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
|
631 |
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
|
632 |
tag = 'funccall' |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
633 |
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
|
634 |
attrs['eid'] = val |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
635 |
# 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
|
636 |
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
|
637 |
row=rowindex, col=colindex) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
638 |
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
|
639 |
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
|
640 |
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
|
641 |
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
|
642 |
w(u' </row>\n') |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
643 |
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
|
644 |
|
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
645 |
|
0 | 646 |
class RssView(XmlView): |
647 |
id = 'rss' |
|
648 |
title = _('rss') |
|
649 |
templatable = False |
|
650 |
content_type = 'text/xml' |
|
651 |
http_cache_manager = MaxAgeHTTPCacheManager |
|
652 |
cache_max_age = 60*60*2 # stay in http cache for 2 hours by default |
|
653 |
||
654 |
def cell_call(self, row, col): |
|
655 |
self.wview('rssitem', self.rset, row=row, col=col) |
|
656 |
||
657 |
def call(self): |
|
658 |
"""display a list of entities by calling their <item_vid> view""" |
|
659 |
req = self.req |
|
660 |
self.w(u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding) |
|
661 |
self.w(u'''<rdf:RDF |
|
662 |
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
|
663 |
xmlns:dc="http://purl.org/dc/elements/1.1/" |
|
664 |
xmlns="http://purl.org/rss/1.0/" |
|
665 |
>''') |
|
666 |
self.w(u' <channel rdf:about="%s">\n' % html_escape(req.url())) |
|
667 |
self.w(u' <title>%s RSS Feed</title>\n' % html_escape(self.page_title())) |
|
668 |
self.w(u' <description>%s</description>\n' % html_escape(req.form.get('vtitle', ''))) |
|
669 |
params = req.form.copy() |
|
670 |
params.pop('vid', None) |
|
671 |
self.w(u' <link>%s</link>\n' % html_escape(self.build_url(**params))) |
|
672 |
self.w(u' <items>\n') |
|
673 |
self.w(u' <rdf:Seq>\n') |
|
674 |
for entity in self.rset.entities(): |
|
675 |
self.w(u' <rdf:li resource="%s" />\n' % html_escape(entity.absolute_url())) |
|
676 |
self.w(u' </rdf:Seq>\n') |
|
677 |
self.w(u' </items>\n') |
|
678 |
self.w(u' </channel>\n') |
|
679 |
for i in xrange(self.rset.rowcount): |
|
680 |
self.cell_call(i, 0) |
|
681 |
self.w(u'</rdf:RDF>') |
|
682 |
||
683 |
||
684 |
class RssItemView(EntityView): |
|
685 |
id = 'rssitem' |
|
686 |
date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600) |
|
687 |
||
688 |
def cell_call(self, row, col): |
|
689 |
entity = self.complete_entity(row, col) |
|
690 |
self.w(u'<item rdf:about="%s">\n' % html_escape(entity.absolute_url())) |
|
691 |
self._marker('title', entity.dc_long_title()) |
|
692 |
self._marker('link', entity.absolute_url()) |
|
693 |
self._marker('description', entity.dc_description()) |
|
694 |
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
|
695 |
if entity.creator: |
349 | 696 |
self.w(u'<author>') |
324
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
697 |
self._marker('name', entity.creator.name()) |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
698 |
email = entity.creator.get_email() |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
699 |
if email: |
9b51dac0bac2
fix author tag
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
323
diff
changeset
|
700 |
self._marker('email', email) |
349 | 701 |
self.w(u'</author>') |
0 | 702 |
self.w(u'</item>\n') |
703 |
||
704 |
def _marker(self, marker, value): |
|
705 |
if value: |
|
706 |
self.w(u' <%s>%s</%s>\n' % (marker, html_escape(value), marker)) |
|
707 |
||
708 |
||
709 |
class CSVMixIn(object): |
|
710 |
"""mixin class for CSV views""" |
|
711 |
templatable = False |
|
712 |
content_type = "text/comma-separated-values" |
|
713 |
binary = True # avoid unicode assertion |
|
714 |
csv_params = {'dialect': 'excel', |
|
715 |
'quotechar': '"', |
|
716 |
'delimiter': ';', |
|
717 |
'lineterminator': '\n'} |
|
718 |
||
719 |
def set_request_content_type(self): |
|
720 |
"""overriden to set a .csv filename""" |
|
721 |
self.req.set_content_type(self.content_type, filename='cubicwebexport.csv') |
|
722 |
||
723 |
def csvwriter(self, **kwargs): |
|
724 |
params = self.csv_params.copy() |
|
725 |
params.update(kwargs) |
|
726 |
return UnicodeCSVWriter(self.w, self.req.encoding, **params) |
|
727 |
||
728 |
||
729 |
class CSVRsetView(CSVMixIn, AnyRsetView): |
|
730 |
"""dumps rset in CSV""" |
|
731 |
id = 'csvexport' |
|
732 |
title = _('csv export') |
|
733 |
||
734 |
def call(self): |
|
735 |
writer = self.csvwriter() |
|
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
349
diff
changeset
|
736 |
writer.writerow(self.columns_labels()) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
737 |
rset, descr = self.rset, self.rset.description |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
738 |
eschema = self.schema.eschema |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
739 |
for rowindex, row in enumerate(rset): |
0 | 740 |
csvrow = [] |
741 |
for colindex, val in enumerate(row): |
|
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
742 |
etype = descr[rowindex][colindex] |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
743 |
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
|
744 |
# csvrow.append(val) # val is eid in that case |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
745 |
content = self.view('textincontext', rset, |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
746 |
row=rowindex, col=colindex) |
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
747 |
else: |
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
748 |
content = self.view('final', rset, |
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
749 |
displaytime=True, format='text/plain', |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
750 |
row=rowindex, col=colindex) |
0 | 751 |
csvrow.append(content) |
752 |
writer.writerow(csvrow) |
|
753 |
||
754 |
||
755 |
class CSVEntityView(CSVMixIn, EntityView): |
|
756 |
"""dumps rset's entities (with full set of attributes) in CSV""" |
|
757 |
id = 'ecsvexport' |
|
758 |
title = _('csv entities export') |
|
759 |
||
760 |
def call(self): |
|
761 |
""" |
|
762 |
the generated CSV file will have a table per entity type |
|
763 |
found in the resultset. ('table' here only means empty |
|
764 |
lines separation between table contents) |
|
765 |
""" |
|
766 |
req = self.req |
|
767 |
rows_by_type = {} |
|
768 |
writer = self.csvwriter() |
|
769 |
rowdef_by_type = {} |
|
770 |
for index in xrange(len(self.rset)): |
|
771 |
entity = self.complete_entity(index) |
|
772 |
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
|
773 |
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
|
774 |
if at != 'Bytes'] |
0 | 775 |
rows_by_type[entity.e_schema] = [[display_name(req, rschema.type) |
776 |
for rschema in rowdef_by_type[entity.e_schema]]] |
|
777 |
rows = rows_by_type[entity.e_schema] |
|
778 |
rows.append([entity.printable_value(rs.type, format='text/plain') |
|
779 |
for rs in rowdef_by_type[entity.e_schema]]) |
|
780 |
for etype, rows in rows_by_type.items(): |
|
781 |
writer.writerows(rows) |
|
782 |
# use two empty lines as separator |
|
783 |
writer.writerows([[], []]) |
|
784 |
||
785 |
||
786 |
## Work in progress ########################################################### |
|
787 |
||
788 |
class SearchForAssociationView(EntityView): |
|
789 |
"""view called by the edition view when the user asks |
|
790 |
to search for something to link to the edited eid |
|
791 |
""" |
|
792 |
id = 'search-associate' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
647
diff
changeset
|
793 |
__select__ = (one_line_rset() & match_search_state('linksearch') |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
647
diff
changeset
|
794 |
& non_final_entity()) |
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
795 |
|
0 | 796 |
title = _('search for association') |
797 |
||
798 |
def cell_call(self, row, col): |
|
799 |
rset, vid, divid, paginate = self.filter_box_context_info() |
|
800 |
self.w(u'<div id="%s">' % divid) |
|
801 |
self.pagination(self.req, rset, w=self.w) |
|
608 | 802 |
self.wview(vid, rset, 'noresult') |
0 | 803 |
self.w(u'</div>') |
804 |
||
805 |
@cached |
|
806 |
def filter_box_context_info(self): |
|
807 |
entity = self.entity(0, 0) |
|
808 |
role, eid, rtype, etype = self.req.search_state[1] |
|
809 |
assert entity.eid == typed_eid(eid) |
|
810 |
# the default behaviour is to fetch all unrelated entities and display |
|
811 |
# them. Use fetch_order and not fetch_unrelated_order as sort method |
|
812 |
# since the latter is mainly there to select relevant items in the combo |
|
813 |
# box, it doesn't give interesting result in this context |
|
814 |
rql = entity.unrelated_rql(rtype, etype, role, |
|
815 |
ordermethod='fetch_order', |
|
816 |
vocabconstraints=False) |
|
817 |
rset = self.req.execute(rql, {'x' : entity.eid}, 'x') |
|
818 |
#vid = vid_from_rset(self.req, rset, self.schema) |
|
819 |
return rset, 'list', "search-associate-content", True |
|
820 |
||
821 |
||
822 |
class OutOfContextSearch(EntityView): |
|
823 |
id = 'outofcontext-search' |
|
824 |
def cell_call(self, row, col): |
|
825 |
entity = self.entity(row, col) |
|
826 |
erset = entity.as_rset() |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
608
diff
changeset
|
827 |
if self.req.match_search_state(erset): |
0 | 828 |
self.w(u'<a href="%s" title="%s">%s</a> <a href="%s" title="%s">[...]</a>' % ( |
829 |
html_escape(linksearch_select_url(self.req, erset)), |
|
830 |
self.req._('select this entity'), |
|
831 |
html_escape(entity.view('textoutofcontext')), |
|
832 |
html_escape(entity.absolute_url(vid='primary')), |
|
833 |
self.req._('view detail for this entity'))) |
|
834 |
else: |
|
835 |
entity.view('outofcontext', w=self.w) |
|
836 |
||
837 |
||
838 |
class EditRelationView(EntityView): |
|
839 |
"""Note: This is work in progress |
|
840 |
||
841 |
This view is part of the edition view refactoring. |
|
842 |
It is still too big and cluttered with strange logic, but it's a start |
|
843 |
||
844 |
The main idea is to be able to call an edition view for a specific |
|
845 |
relation. For example : |
|
846 |
self.wview('editrelation', person_rset, rtype='firstname') |
|
847 |
self.wview('editrelation', person_rset, rtype='works_for') |
|
848 |
""" |
|
849 |
id = 'editrelation' |
|
850 |
||
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
647
diff
changeset
|
851 |
__select__ = match_form_params('rtype') |
0 | 852 |
|
853 |
# TODO: inlineview, multiple edit, (widget view ?) |
|
854 |
def cell_call(self, row, col, rtype=None, role='subject', targettype=None, |
|
855 |
showlabel=True): |
|
856 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.edition.js') ) |
|
857 |
entity = self.entity(row, col) |
|
858 |
rtype = self.req.form.get('rtype', rtype) |
|
859 |
showlabel = self.req.form.get('showlabel', showlabel) |
|
860 |
assert rtype is not None, "rtype is mandatory for 'edirelation' view" |
|
861 |
targettype = self.req.form.get('targettype', targettype) |
|
862 |
role = self.req.form.get('role', role) |
|
863 |
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
|
864 |
if category in ('primary', 'secondary') or self.schema.rschema(rtype).is_final(): |
0 | 865 |
if hasattr(entity, '%s_format' % rtype): |
866 |
formatwdg = entity.get_widget('%s_format' % rtype, role) |
|
867 |
self.w(formatwdg.edit_render(entity)) |
|
868 |
self.w(u'<br/>') |
|
869 |
wdg = entity.get_widget(rtype, role) |
|
870 |
if showlabel: |
|
871 |
self.w(u'%s' % wdg.render_label(entity)) |
|
872 |
self.w(u'%s %s %s' % |
|
873 |
(wdg.render_error(entity), wdg.edit_render(entity), |
|
874 |
wdg.render_help(entity),)) |
|
570
0c9dfc430200
fix test (this view is not used actually...)
sylvain.thenault@logilab.fr
parents:
532
diff
changeset
|
875 |
else: |
0 | 876 |
self._render_generic_relation(entity, rtype, role) |
877 |
||
878 |
def _render_generic_relation(self, entity, relname, role): |
|
879 |
text = self.req.__('add %s %s %s' % (entity.e_schema, relname, role)) |
|
880 |
# pending operations |
|
881 |
operations = self.req.get_pending_operations(entity, relname, role) |
|
882 |
if operations['insert'] or operations['delete'] or 'unfold' in self.req.form: |
|
883 |
self.w(u'<h3>%s</h3>' % text) |
|
884 |
self._render_generic_relation_form(operations, entity, relname, role) |
|
885 |
else: |
|
886 |
divid = "%s%sreledit" % (relname, role) |
|
887 |
url = ajax_replace_url(divid, rql_for_eid(entity.eid), 'editrelation', |
|
888 |
{'unfold' : 1, 'relname' : relname, 'role' : role}) |
|
889 |
self.w(u'<a href="%s">%s</a>' % (url, text)) |
|
890 |
self.w(u'<div id="%s"></div>' % divid) |
|
891 |
||
892 |
||
893 |
def _build_opvalue(self, entity, relname, target, role): |
|
894 |
if role == 'subject': |
|
895 |
return '%s:%s:%s' % (entity.eid, relname, target) |
|
896 |
else: |
|
897 |
return '%s:%s:%s' % (target, relname, entity.eid) |
|
898 |
||
899 |
||
900 |
def _render_generic_relation_form(self, operations, entity, relname, role): |
|
901 |
rqlexec = self.req.execute |
|
902 |
for optype, targets in operations.items(): |
|
903 |
for target in targets: |
|
904 |
self._render_pending(optype, entity, relname, target, role) |
|
905 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
906 |
self.w(u'<a href="javascript: addPendingDelete(\'%s\', %s);">-</a> ' |
|
907 |
% (opvalue, entity.eid)) |
|
908 |
rset = rqlexec('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
909 |
self.wview('oneline', rset) |
|
910 |
# now, unrelated ones |
|
911 |
self._render_unrelated_selection(entity, relname, role) |
|
912 |
||
913 |
def _render_pending(self, optype, entity, relname, target, role): |
|
914 |
opvalue = self._build_opvalue(entity, relname, target, role) |
|
915 |
self.w(u'<input type="hidden" name="__%s" value="%s" />' |
|
916 |
% (optype, opvalue)) |
|
917 |
if optype == 'insert': |
|
918 |
checktext = '-' |
|
919 |
else: |
|
920 |
checktext = '+' |
|
921 |
rset = self.req.execute('Any X WHERE X eid %(x)s', {'x': target}, 'x') |
|
922 |
self.w(u"""[<a href="javascript: cancelPending%s('%s:%s:%s')">%s</a>""" |
|
923 |
% (optype.capitalize(), relname, target, role, |
|
924 |
self.view('oneline', rset))) |
|
925 |
||
926 |
def _render_unrelated_selection(self, entity, relname, role): |
|
927 |
rschema = self.schema.rschema(relname) |
|
928 |
if role == 'subject': |
|
929 |
targettypes = rschema.objects(entity.e_schema) |
|
930 |
else: |
|
931 |
targettypes = rschema.subjects(entity.e_schema) |
|
932 |
self.w(u'<select onselect="addPendingInsert(this.selected.value);">') |
|
933 |
for targettype in targettypes: |
|
934 |
unrelated = entity.unrelated(relname, targettype, role) # XXX limit |
|
935 |
for rowindex, row in enumerate(unrelated): |
|
936 |
teid = row[0] |
|
937 |
opvalue = self._build_opvalue(entity, relname, teid, role) |
|
938 |
self.w(u'<option name="__insert" value="%s>%s</option>' |
|
939 |
% (opvalue, self.view('text', unrelated, row=rowindex))) |
|
940 |
self.w(u'</select>') |
|
941 |
||
942 |
||
943 |
class TextSearchResultView(EntityView): |
|
944 |
"""this view is used to display full-text search |
|
945 |
||
946 |
It tries to highlight part of data where the search word appears. |
|
947 |
||
948 |
XXX: finish me (fixed line width, fixed number of lines, CSS, etc.) |
|
949 |
""" |
|
950 |
id = 'tsearch' |
|
951 |
||
952 |
def cell_call(self, row, col, **kwargs): |
|
953 |
entity = self.complete_entity(row, col) |
|
954 |
self.w(entity.view('incontext')) |
|
955 |
searched = self.rset.searched_text() |
|
956 |
if searched is None: |
|
957 |
return |
|
958 |
searched = searched.lower() |
|
959 |
highlighted = '<b>%s</b>' % searched |
|
960 |
for attr in entity.e_schema.indexable_attributes(): |
|
961 |
try: |
|
962 |
value = html_escape(entity.printable_value(attr, format='text/plain').lower()) |
|
963 |
except TransformError, ex: |
|
964 |
continue |
|
965 |
except: |
|
966 |
continue |
|
967 |
if searched in value: |
|
968 |
contexts = [] |
|
969 |
for ctx in value.split(searched): |
|
970 |
if len(ctx) > 30: |
|
971 |
contexts.append(u'...' + ctx[-30:]) |
|
972 |
else: |
|
973 |
contexts.append(ctx) |
|
974 |
value = u'\n' + highlighted.join(contexts) |
|
975 |
self.w(value.replace('\n', '<br/>')) |
|
976 |
||
977 |
||
978 |
class TooltipView(OneLineView): |
|
979 |
"""A entity view used in a tooltip""" |
|
980 |
id = 'tooltip' |
|
981 |
title = None # don't display in possible views |
|
982 |
def cell_call(self, row, col): |
|
983 |
self.wview('oneline', self.rset, row=row, col=col) |
|
984 |
||
985 |
try: |
|
986 |
from cubicweb.web.views.tableview import TableView |
|
987 |
from logilab.common.deprecation import class_moved |
|
988 |
TableView = class_moved(TableView) |
|
989 |
except ImportError: |
|
990 |
pass # gae has no tableview module (yet) |