author | sylvain.thenault@logilab.fr |
Thu, 19 Feb 2009 22:59:02 +0100 | |
branch | tls-sprint |
changeset 870 | 0b31dbac6459 |
parent 824 | a5e6acffde30 |
child 985 | 6a25c58a1c23 |
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 |
|
18 |
from rql import nodes |
|
19 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
20 |
from logilab.mtconverter import TransformError, html_escape |
0 | 21 |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
22 |
from cubicweb import Unauthorized, NoSelectableObject |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
23 |
from cubicweb.selectors import yes, empty_rset |
756 | 24 |
from cubicweb.view import EntityView, AnyRsetView, View |
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
25 |
from cubicweb.common.uilib import cut, printable_value |
0 | 26 |
|
27 |
_ = unicode |
|
28 |
||
29 |
class NullView(AnyRsetView): |
|
30 |
"""default view when no result has been found""" |
|
31 |
id = 'null' |
|
758
0c0dfd33a76d
instantiate selectors wherever needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
32 |
__select__ = yes() |
0 | 33 |
def call(self, **kwargs): |
34 |
pass |
|
35 |
cell_call = call |
|
36 |
||
37 |
||
632 | 38 |
class NoResultView(View): |
0 | 39 |
"""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
|
40 |
__select__ = empty_rset() |
0 | 41 |
id = 'noresult' |
42 |
||
43 |
def call(self, **kwargs): |
|
44 |
self.w(u'<div class="searchMessage"><strong>%s</strong></div>\n' |
|
45 |
% self.req._('No result matching query')) |
|
46 |
||
47 |
||
48 |
class FinalView(AnyRsetView): |
|
49 |
"""display values without any transformation (i.e. get a number for |
|
50 |
entities) |
|
51 |
""" |
|
52 |
id = 'final' |
|
53 |
||
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
54 |
def cell_call(self, row, col, props=None, displaytime=False, format='text/html'): |
0 | 55 |
etype = self.rset.description[row][col] |
56 |
value = self.rset.rows[row][col] |
|
57 |
if etype == 'String': |
|
58 |
entity, rtype = self.rset.related_entity(row, col) |
|
59 |
if entity is not None: |
|
60 |
# yes ! |
|
573
9c8fd72ba6c1
try to get raw text values in rset csv view
sylvain.thenault@logilab.fr
parents:
570
diff
changeset
|
61 |
self.w(entity.printable_value(rtype, value, format=format)) |
0 | 62 |
return |
63 |
if etype in ('Time', 'Interval'): |
|
64 |
_ = self.req._ |
|
65 |
# value is DateTimeDelta but we have no idea about what is the |
|
66 |
# reference date here, so we can only approximate years and months |
|
67 |
if value.days > 730: # 2 years |
|
68 |
self.w(_('%d years') % (value.days // 365)) |
|
69 |
elif value.days > 60: # 2 months |
|
70 |
self.w(_('%d months') % (value.days // 30)) |
|
71 |
elif value.days > 14: # 2 weeks |
|
72 |
self.w(_('%d weeks') % (value.days // 7)) |
|
73 |
elif value.days > 2: |
|
74 |
self.w(_('%s days') % int(value.days)) |
|
75 |
elif value.hours > 2: |
|
76 |
self.w(_('%s hours') % int(value.hours)) |
|
77 |
elif value.minutes >= 2: |
|
78 |
self.w(_('%s minutes') % int(value.minutes)) |
|
79 |
else: |
|
80 |
self.w(_('%s seconds') % int(value.seconds)) |
|
81 |
return |
|
82 |
self.wdata(printable_value(self.req, etype, value, props, displaytime=displaytime)) |
|
83 |
||
84 |
||
85 |
PRIMARY_SKIP_RELS = set(['is', 'is_instance_of', 'identity', |
|
86 |
'owned_by', 'created_by', |
|
87 |
'in_state', 'wf_info_for', 'require_permission', |
|
88 |
'from_entity', 'to_entity', |
|
89 |
'see_also']) |
|
90 |
||
91 |
class PrimaryView(EntityView): |
|
92 |
"""the full view of an non final entity""" |
|
93 |
id = 'primary' |
|
94 |
title = _('primary') |
|
95 |
show_attr_label = True |
|
96 |
show_rel_label = True |
|
97 |
skip_none = True |
|
98 |
skip_attrs = ('eid', 'creation_date', 'modification_date') |
|
99 |
skip_rels = () |
|
100 |
main_related_section = True |
|
101 |
||
102 |
def html_headers(self): |
|
103 |
"""return a list of html headers (eg something to be inserted between |
|
104 |
<head> and </head> of the returned page |
|
105 |
||
106 |
by default primary views are indexed |
|
107 |
""" |
|
108 |
return [] |
|
109 |
||
110 |
def cell_call(self, row, col): |
|
111 |
self.row = row |
|
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
179
diff
changeset
|
112 |
# XXX move render_entity implementation here |
0 | 113 |
self.render_entity(self.complete_entity(row, col)) |
114 |
||
115 |
def render_entity(self, entity): |
|
116 |
"""return html to display the given entity""" |
|
117 |
siderelations = [] |
|
118 |
self.render_entity_title(entity) |
|
119 |
self.render_entity_metadata(entity) |
|
120 |
# entity's attributes and relations, excluding meta data |
|
121 |
# if the entity isn't meta itself |
|
122 |
self.w(u'<table border="0" width="100%">') |
|
123 |
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
|
124 |
self.w(u'<td valign="top">') |
0 | 125 |
self.w(u'<div class="mainInfo">') |
126 |
self.render_entity_attributes(entity, siderelations) |
|
127 |
self.w(u'</div>') |
|
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
128 |
self.content_navigation_components('navcontenttop') |
0 | 129 |
if self.main_related_section: |
130 |
self.render_entity_relations(entity, siderelations) |
|
131 |
self.w(u'</td>') |
|
132 |
# side boxes |
|
133 |
self.w(u'<td valign="top">') |
|
134 |
self.render_side_related(entity, siderelations) |
|
135 |
self.w(u'</td>') |
|
136 |
self.w(u'</tr>') |
|
137 |
self.w(u'</table>') |
|
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
138 |
self.content_navigation_components('navcontentbottom') |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
139 |
|
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
140 |
def content_navigation_components(self, context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
141 |
self.w(u'<div class="%s">' % context) |
0 | 142 |
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
|
143 |
self.req, self.rset, row=self.row, |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
144 |
view=self, context=context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
145 |
try: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
146 |
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
|
147 |
except NotImplementedError: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
148 |
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
|
149 |
% comp.__class__, DeprecationWarning) |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
150 |
comp.dispatch(w=self.w, view=self) |
0 | 151 |
self.w(u'</div>') |
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
152 |
|
0 | 153 |
def iter_attributes(self, entity): |
154 |
for rschema, targetschema in entity.e_schema.attribute_definitions(): |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
155 |
if rschema.type in self.skip_attrs: |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
156 |
continue |
0 | 157 |
yield rschema, targetschema |
158 |
||
159 |
def iter_relations(self, entity): |
|
160 |
skip = set(self.skip_rels) |
|
161 |
skip.update(PRIMARY_SKIP_RELS) |
|
162 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
163 |
if rschema.type in skip: |
|
164 |
continue |
|
165 |
yield rschema, targetschemas, x |
|
166 |
||
167 |
def render_entity_title(self, entity): |
|
168 |
title = self.content_title(entity) # deprecate content_title? |
|
169 |
if title: |
|
170 |
self.w(u'<h1><span class="etype">%s</span> %s</h1>' |
|
171 |
% (entity.dc_type().capitalize(), title)) |
|
172 |
||
173 |
def content_title(self, entity): |
|
174 |
"""default implementation return an empty string""" |
|
175 |
return u'' |
|
176 |
||
177 |
def render_entity_metadata(self, entity): |
|
178 |
entity.view('metadata', w=self.w) |
|
179 |
summary = self.summary(entity) # deprecate summary? |
|
180 |
if summary: |
|
181 |
self.w(u'<div class="summary">%s</div>' % summary) |
|
182 |
||
183 |
def summary(self, entity): |
|
184 |
"""default implementation return an empty string""" |
|
179 | 185 |
return u'' |
0 | 186 |
|
187 |
def render_entity_attributes(self, entity, siderelations): |
|
188 |
for rschema, targetschema in self.iter_attributes(entity): |
|
189 |
attr = rschema.type |
|
190 |
if targetschema.type in ('Password', 'Bytes'): |
|
191 |
continue |
|
192 |
try: |
|
193 |
wdg = entity.get_widget(attr) |
|
194 |
except Exception, ex: |
|
195 |
value = entity.printable_value(attr, entity[attr], targetschema.type) |
|
196 |
else: |
|
197 |
value = wdg.render(entity) |
|
198 |
if self.skip_none and (value is None or value == ''): |
|
199 |
continue |
|
200 |
if rschema.meta: |
|
201 |
continue |
|
202 |
self._render_related_entities(entity, rschema, value) |
|
203 |
||
204 |
def render_entity_relations(self, entity, siderelations): |
|
205 |
if hasattr(self, 'get_side_boxes_defs'): |
|
206 |
return |
|
207 |
eschema = entity.e_schema |
|
208 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
209 |
for rschema, targetschemas, x in self.iter_relations(entity): |
|
210 |
try: |
|
211 |
related = entity.related(rschema.type, x, limit=maxrelated+1) |
|
212 |
except Unauthorized: |
|
213 |
continue |
|
214 |
if not related: |
|
215 |
continue |
|
216 |
if self.is_side_related(rschema, eschema): |
|
217 |
siderelations.append((rschema, related, x)) |
|
218 |
continue |
|
219 |
self._render_related_entities(entity, rschema, related, x) |
|
220 |
||
221 |
def render_side_related(self, entity, siderelations): |
|
222 |
"""display side related relations: |
|
223 |
non-meta in a first step, meta in a second step |
|
224 |
""" |
|
225 |
if hasattr(self, 'get_side_boxes_defs'): |
|
226 |
for label, rset in self.get_side_boxes_defs(entity): |
|
227 |
if rset: |
|
228 |
self.w(u'<div class="sideRelated">') |
|
229 |
self.wview('sidebox', rset, title=label) |
|
230 |
self.w(u'</div>') |
|
231 |
elif siderelations: |
|
232 |
self.w(u'<div class="sideRelated">') |
|
233 |
for relatedinfos in siderelations: |
|
234 |
# if not relatedinfos[0].meta: |
|
235 |
# continue |
|
236 |
self._render_related_entities(entity, *relatedinfos) |
|
237 |
self.w(u'</div>') |
|
531 | 238 |
for box in self.vreg.possible_vobjects('boxes', self.req, self.rset, |
239 |
row=self.row, view=self, |
|
240 |
context='incontext'): |
|
0 | 241 |
try: |
531 | 242 |
box.dispatch(w=self.w, row=self.row) |
0 | 243 |
except NotImplementedError: |
244 |
# much probably a context insensitive box, which only implements |
|
245 |
# .call() and not cell_call() |
|
246 |
box.dispatch(w=self.w) |
|
247 |
||
248 |
def is_side_related(self, rschema, eschema): |
|
249 |
return rschema.meta and \ |
|
250 |
not rschema.schema_relation() == eschema.schema_entity() |
|
251 |
||
252 |
def _render_related_entities(self, entity, rschema, related, |
|
253 |
role='subject'): |
|
254 |
if rschema.is_final(): |
|
255 |
value = related |
|
256 |
show_label = self.show_attr_label |
|
257 |
else: |
|
258 |
if not related: |
|
259 |
return |
|
260 |
show_label = self.show_rel_label |
|
261 |
# if not too many entities, show them all in a list |
|
262 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
263 |
if related.rowcount <= maxrelated: |
|
264 |
if related.rowcount == 1: |
|
265 |
value = self.view('incontext', related, row=0) |
|
266 |
elif 1 < related.rowcount <= 5: |
|
267 |
value = self.view('csv', related) |
|
268 |
else: |
|
269 |
value = '<div>' + self.view('simplelist', related) + '</div>' |
|
270 |
# else show links to display related entities |
|
271 |
else: |
|
272 |
rql = related.printable_rql() |
|
273 |
related.limit(maxrelated) |
|
274 |
value = '<div>' + self.view('simplelist', related) |
|
275 |
value += '[<a href="%s">%s</a>]' % (self.build_url(rql=rql), |
|
276 |
self.req._('see them all')) |
|
277 |
value += '</div>' |
|
278 |
label = display_name(self.req, rschema.type, role) |
|
279 |
self.field(label, value, show_label=show_label, w=self.w, tr=False) |
|
280 |
||
281 |
||
282 |
class SecondaryView(EntityView): |
|
283 |
id = 'secondary' |
|
284 |
title = _('secondary') |
|
285 |
||
286 |
def cell_call(self, row, col): |
|
287 |
"""the secondary view for an entity |
|
288 |
secondary = icon + view(oneline) |
|
289 |
""" |
|
290 |
entity = self.entity(row, col) |
|
291 |
self.w(u' ') |
|
292 |
self.wview('oneline', self.rset, row=row, col=col) |
|
293 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
294 |
|
0 | 295 |
class OneLineView(EntityView): |
296 |
id = 'oneline' |
|
297 |
title = _('oneline') |
|
298 |
||
299 |
def cell_call(self, row, col): |
|
300 |
"""the one line view for an entity: linked text view |
|
301 |
""" |
|
302 |
entity = self.entity(row, col) |
|
303 |
self.w(u'<a href="%s">' % html_escape(entity.absolute_url())) |
|
304 |
self.w(html_escape(self.view('text', self.rset, row=row, col=col))) |
|
305 |
self.w(u'</a>') |
|
306 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
307 |
|
0 | 308 |
class TextView(EntityView): |
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
309 |
"""the simplest text view for an entity""" |
0 | 310 |
id = 'text' |
311 |
title = _('text') |
|
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
312 |
content_type = 'text/plain' |
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
313 |
|
0 | 314 |
def call(self, **kwargs): |
315 |
"""the view is called for an entire result set, by default loop |
|
316 |
other rows of the result set and call the same view on the |
|
317 |
particular row |
|
318 |
||
319 |
Views applicable on None result sets have to override this method |
|
320 |
""" |
|
321 |
rset = self.rset |
|
322 |
if rset is None: |
|
323 |
raise NotImplementedError, self |
|
324 |
for i in xrange(len(rset)): |
|
325 |
self.wview(self.id, rset, row=i, **kwargs) |
|
326 |
if len(rset) > 1: |
|
327 |
self.w(u"\n") |
|
328 |
||
329 |
def cell_call(self, row, col=0, **kwargs): |
|
330 |
entity = self.entity(row, col) |
|
331 |
self.w(cut(entity.dc_title(), |
|
332 |
self.req.property_value('navigation.short-line-size'))) |
|
333 |
||
137
7e45cf48c2f1
don't display additional owners in metadata, this is confusing
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
334 |
|
0 | 335 |
class MetaDataView(EntityView): |
336 |
"""paragraph view of some metadata""" |
|
337 |
id = 'metadata' |
|
338 |
show_eid = True |
|
339 |
||
340 |
def cell_call(self, row, col): |
|
341 |
_ = self.req._ |
|
342 |
entity = self.entity(row, col) |
|
343 |
self.w(u'<div class="metadata">') |
|
344 |
if self.show_eid: |
|
345 |
self.w(u'#%s - ' % entity.eid) |
|
346 |
if entity.modification_date != entity.creation_date: |
|
347 |
self.w(u'<span>%s</span> ' % _('latest update on')) |
|
348 |
self.w(u'<span class="value">%s</span>, ' |
|
349 |
% self.format_date(entity.modification_date)) |
|
350 |
# entities from external source may not have a creation date (eg ldap) |
|
351 |
if entity.creation_date: |
|
352 |
self.w(u'<span>%s</span> ' % _('created on')) |
|
353 |
self.w(u'<span class="value">%s</span>' |
|
354 |
% self.format_date(entity.creation_date)) |
|
355 |
if entity.creator: |
|
356 |
self.w(u' <span>%s</span> ' % _('by')) |
|
357 |
self.w(u'<span class="value">%s</span>' % entity.creator.name()) |
|
358 |
self.w(u'</div>') |
|
359 |
||
360 |
||
361 |
# new default views for finner control in general views , to use instead of |
|
362 |
# oneline / secondary |
|
363 |
||
364 |
class InContextTextView(TextView): |
|
365 |
id = 'textincontext' |
|
366 |
title = None # not listed as a possible view |
|
367 |
def cell_call(self, row, col): |
|
368 |
entity = self.entity(row, col) |
|
369 |
self.w(entity.dc_title()) |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
370 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
371 |
|
0 | 372 |
class OutOfContextTextView(InContextTextView): |
373 |
id = 'textoutofcontext' |
|
374 |
||
375 |
def cell_call(self, row, col): |
|
376 |
entity = self.entity(row, col) |
|
377 |
self.w(entity.dc_long_title()) |
|
378 |
||
379 |
||
380 |
class InContextView(EntityView): |
|
381 |
id = 'incontext' |
|
382 |
||
383 |
def cell_call(self, row, col): |
|
384 |
entity = self.entity(row, col) |
|
385 |
desc = cut(entity.dc_description(), 50) |
|
386 |
self.w(u'<a href="%s" title="%s">' % (html_escape(entity.absolute_url()), |
|
387 |
html_escape(desc))) |
|
388 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
389 |
self.w(u'</a>') |
|
390 |
||
391 |
||
392 |
class OutOfContextView(EntityView): |
|
393 |
id = 'outofcontext' |
|
394 |
||
395 |
def cell_call(self, row, col): |
|
396 |
self.w(u'<a href="%s">' % self.entity(row, col).absolute_url()) |
|
397 |
self.w(html_escape(self.view('textoutofcontext', self.rset, row=row, col=col))) |
|
398 |
self.w(u'</a>') |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
399 |
|
0 | 400 |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
401 |
# list views ################################################################## |
0 | 402 |
|
403 |
class ListView(EntityView): |
|
404 |
id = 'list' |
|
405 |
title = _('list') |
|
406 |
item_vid = 'listitem' |
|
407 |
||
408 |
def call(self, klass=None, title=None, subvid=None, listid=None, **kwargs): |
|
409 |
"""display a list of entities by calling their <item_vid> view |
|
410 |
|
|
411 |
:param listid: the DOM id to use for the root element |
|
412 |
""" |
|
413 |
if subvid is None and 'subvid' in self.req.form: |
|
414 |
subvid = self.req.form.pop('subvid') # consume it |
|
415 |
if listid: |
|
416 |
listid = u' id="%s"' % listid |
|
417 |
else: |
|
418 |
listid = u'' |
|
419 |
if title: |
|
420 |
self.w(u'<div%s class="%s"><h4>%s</h4>\n' % (listid, klass or 'section', title)) |
|
421 |
self.w(u'<ul>\n') |
|
422 |
else: |
|
423 |
self.w(u'<ul%s class="%s">\n' % (listid, klass or 'section')) |
|
424 |
for i in xrange(self.rset.rowcount): |
|
425 |
self.cell_call(row=i, col=0, vid=subvid, **kwargs) |
|
426 |
self.w(u'</ul>\n') |
|
427 |
if title: |
|
428 |
self.w(u'</div>\n') |
|
429 |
||
430 |
def cell_call(self, row, col=0, vid=None, **kwargs): |
|
431 |
self.w(u'<li>') |
|
432 |
self.wview(self.item_vid, self.rset, row=row, col=col, vid=vid, **kwargs) |
|
433 |
self.w(u'</li>\n') |
|
434 |
||
435 |
def url(self): |
|
436 |
"""overrides url method so that by default, the view list is called |
|
437 |
with sorted entities |
|
438 |
""" |
|
439 |
coltypes = self.rset.column_types(0) |
|
440 |
# don't want to generate the rql if there is some restriction on |
|
441 |
# something else than the entity type |
|
442 |
if len(coltypes) == 1: |
|
443 |
# XXX norestriction is not correct here. For instance, in cases like |
|
444 |
# Any P,N WHERE P is Project, P name N |
|
445 |
# norestriction should equal True |
|
446 |
restr = self.rset.syntax_tree().children[0].where |
|
447 |
norestriction = (isinstance(restr, nodes.Relation) and |
|
448 |
restr.is_types_restriction()) |
|
449 |
if norestriction: |
|
450 |
etype = iter(coltypes).next() |
|
451 |
return self.build_url(etype.lower(), vid=self.id) |
|
452 |
if len(self.rset) == 1: |
|
453 |
entity = self.rset.get_entity(0, 0) |
|
454 |
return self.build_url(entity.rest_path(), vid=self.id) |
|
455 |
return self.build_url(rql=self.rset.printable_rql(), vid=self.id) |
|
456 |
||
457 |
||
458 |
class ListItemView(EntityView): |
|
459 |
id = 'listitem' |
|
460 |
||
461 |
@property |
|
462 |
def redirect_vid(self): |
|
463 |
if self.req.search_state[0] == 'normal': |
|
464 |
return 'outofcontext' |
|
465 |
return 'outofcontext-search' |
|
466 |
||
467 |
def cell_call(self, row, col, vid=None, **kwargs): |
|
468 |
if not vid: |
|
469 |
vid = self.redirect_vid |
|
470 |
try: |
|
471 |
self.wview(vid, self.rset, row=row, col=col, **kwargs) |
|
472 |
except NoSelectableObject: |
|
473 |
if vid == self.redirect_vid: |
|
474 |
raise |
|
475 |
self.wview(self.redirect_vid, self.rset, row=row, col=col, **kwargs) |
|
476 |
||
477 |
||
478 |
class SimpleListView(ListItemView): |
|
479 |
"""list without bullets""" |
|
480 |
id = 'simplelist' |
|
481 |
redirect_vid = 'incontext' |
|
482 |
||
483 |
||
484 |
class CSVView(SimpleListView): |
|
485 |
id = 'csv' |
|
486 |
redirect_vid = 'incontext' |
|
487 |
||
488 |
def call(self, **kwargs): |
|
489 |
rset = self.rset |
|
490 |
for i in xrange(len(rset)): |
|
491 |
self.cell_call(i, 0, vid=kwargs.get('vid')) |
|
492 |
if i < rset.rowcount-1: |
|
493 |
self.w(u", ") |
|
494 |
||
495 |
||
496 |
class TreeItemView(ListItemView): |
|
497 |
id = 'treeitem' |
|
498 |
||
499 |
def cell_call(self, row, col): |
|
500 |
self.wview('incontext', self.rset, row=row, col=col) |
|
501 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
502 |
# context specific views ###################################################### |
0 | 503 |
|
504 |
class TextSearchResultView(EntityView): |
|
505 |
"""this view is used to display full-text search |
|
506 |
||
507 |
It tries to highlight part of data where the search word appears. |
|
508 |
||
509 |
XXX: finish me (fixed line width, fixed number of lines, CSS, etc.) |
|
510 |
""" |
|
511 |
id = 'tsearch' |
|
512 |
||
513 |
def cell_call(self, row, col, **kwargs): |
|
514 |
entity = self.complete_entity(row, col) |
|
515 |
self.w(entity.view('incontext')) |
|
516 |
searched = self.rset.searched_text() |
|
517 |
if searched is None: |
|
518 |
return |
|
519 |
searched = searched.lower() |
|
520 |
highlighted = '<b>%s</b>' % searched |
|
521 |
for attr in entity.e_schema.indexable_attributes(): |
|
522 |
try: |
|
523 |
value = html_escape(entity.printable_value(attr, format='text/plain').lower()) |
|
524 |
except TransformError, ex: |
|
525 |
continue |
|
526 |
except: |
|
527 |
continue |
|
528 |
if searched in value: |
|
529 |
contexts = [] |
|
530 |
for ctx in value.split(searched): |
|
531 |
if len(ctx) > 30: |
|
532 |
contexts.append(u'...' + ctx[-30:]) |
|
533 |
else: |
|
534 |
contexts.append(ctx) |
|
535 |
value = u'\n' + highlighted.join(contexts) |
|
536 |
self.w(value.replace('\n', '<br/>')) |
|
537 |
||
538 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
539 |
class TooltipView(EntityView): |
0 | 540 |
"""A entity view used in a tooltip""" |
541 |
id = 'tooltip' |
|
542 |
def cell_call(self, row, col): |
|
543 |
self.wview('oneline', self.rset, row=row, col=col) |
|
544 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
545 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
546 |
# XXX bw compat |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
547 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
548 |
from logilab.common.deprecation import class_moved |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
549 |
|
0 | 550 |
try: |
551 |
from cubicweb.web.views.tableview import TableView |
|
552 |
TableView = class_moved(TableView) |
|
553 |
except ImportError: |
|
554 |
pass # gae has no tableview module (yet) |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
555 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
556 |
from cubicweb.web.views import boxes, xmlrss |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
557 |
SideBoxView = class_moved(boxes.SideBoxView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
558 |
XmlView = class_moved(xmlrss.XmlView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
559 |
XmlItemView = class_moved(xmlrss.XmlItemView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
560 |
XmlRsetView = class_moved(xmlrss.XmlRsetView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
561 |
RssView = class_moved(xmlrss.RssView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
562 |
RssItemView = class_moved(xmlrss.RssItemView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
563 |