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