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