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