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