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