author | sylvain.thenault@logilab.fr |
Wed, 04 Mar 2009 17:12:28 +0100 | |
branch | tls-sprint |
changeset 1006 | 92a0601b2523 |
parent 985 | 6a25c58a1c23 |
parent 991 | 746fed01d1e0 |
child 1091 | b5e253c0dd13 |
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 |
|
988
b73f3cd23d43
remplacing tables by div for right (and left) box layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
978
diff
changeset
|
142 |
self.w(u'<div>') |
0 | 143 |
self.w(u'<div class="mainInfo">') |
144 |
self.render_entity_attributes(entity, siderelations) |
|
145 |
self.w(u'</div>') |
|
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
146 |
self.content_navigation_components('navcontenttop') |
0 | 147 |
if self.main_related_section: |
148 |
self.render_entity_relations(entity, siderelations) |
|
988
b73f3cd23d43
remplacing tables by div for right (and left) box layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
978
diff
changeset
|
149 |
self.w(u'</div>') |
0 | 150 |
# side boxes |
988
b73f3cd23d43
remplacing tables by div for right (and left) box layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
978
diff
changeset
|
151 |
self.w(u'<div class="primaryRight">') |
0 | 152 |
self.render_side_related(entity, siderelations) |
988
b73f3cd23d43
remplacing tables by div for right (and left) box layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
978
diff
changeset
|
153 |
self.w(u'</div>') |
991
746fed01d1e0
correction (removing comments)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
988
diff
changeset
|
154 |
self.w(u'<div class="clear"></div>') |
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
155 |
self.content_navigation_components('navcontentbottom') |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
156 |
|
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
157 |
def content_navigation_components(self, context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
158 |
self.w(u'<div class="%s">' % context) |
0 | 159 |
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
|
160 |
self.req, self.rset, row=self.row, |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
161 |
view=self, context=context): |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
162 |
try: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
163 |
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
|
164 |
except NotImplementedError: |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
165 |
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
|
166 |
% comp.__class__, DeprecationWarning) |
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
167 |
comp.dispatch(w=self.w, view=self) |
0 | 168 |
self.w(u'</div>') |
528
60bd171ecd04
give row when selecting/dispatching content navigation component
sylvain.thenault@logilab.fr
parents:
479
diff
changeset
|
169 |
|
0 | 170 |
def iter_attributes(self, entity): |
171 |
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
|
172 |
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
|
173 |
continue |
0 | 174 |
yield rschema, targetschema |
175 |
||
176 |
def iter_relations(self, entity): |
|
177 |
skip = set(self.skip_rels) |
|
178 |
skip.update(PRIMARY_SKIP_RELS) |
|
179 |
for rschema, targetschemas, x in entity.e_schema.relation_definitions(): |
|
180 |
if rschema.type in skip: |
|
181 |
continue |
|
182 |
yield rschema, targetschemas, x |
|
183 |
||
184 |
def render_entity_title(self, entity): |
|
185 |
title = self.content_title(entity) # deprecate content_title? |
|
186 |
if title: |
|
187 |
self.w(u'<h1><span class="etype">%s</span> %s</h1>' |
|
188 |
% (entity.dc_type().capitalize(), title)) |
|
189 |
||
190 |
def content_title(self, entity): |
|
191 |
"""default implementation return an empty string""" |
|
192 |
return u'' |
|
193 |
||
194 |
def render_entity_metadata(self, entity): |
|
195 |
entity.view('metadata', w=self.w) |
|
196 |
summary = self.summary(entity) # deprecate summary? |
|
197 |
if summary: |
|
198 |
self.w(u'<div class="summary">%s</div>' % summary) |
|
199 |
||
200 |
def summary(self, entity): |
|
201 |
"""default implementation return an empty string""" |
|
179 | 202 |
return u'' |
0 | 203 |
|
204 |
def render_entity_attributes(self, entity, siderelations): |
|
205 |
for rschema, targetschema in self.iter_attributes(entity): |
|
206 |
attr = rschema.type |
|
207 |
if targetschema.type in ('Password', 'Bytes'): |
|
208 |
continue |
|
209 |
try: |
|
210 |
wdg = entity.get_widget(attr) |
|
211 |
except Exception, ex: |
|
212 |
value = entity.printable_value(attr, entity[attr], targetschema.type) |
|
213 |
else: |
|
214 |
value = wdg.render(entity) |
|
215 |
if self.skip_none and (value is None or value == ''): |
|
216 |
continue |
|
217 |
if rschema.meta: |
|
218 |
continue |
|
219 |
self._render_related_entities(entity, rschema, value) |
|
220 |
||
221 |
def render_entity_relations(self, entity, siderelations): |
|
222 |
if hasattr(self, 'get_side_boxes_defs'): |
|
223 |
return |
|
224 |
eschema = entity.e_schema |
|
225 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
226 |
for rschema, targetschemas, x in self.iter_relations(entity): |
|
227 |
try: |
|
228 |
related = entity.related(rschema.type, x, limit=maxrelated+1) |
|
229 |
except Unauthorized: |
|
230 |
continue |
|
231 |
if not related: |
|
232 |
continue |
|
233 |
if self.is_side_related(rschema, eschema): |
|
234 |
siderelations.append((rschema, related, x)) |
|
235 |
continue |
|
236 |
self._render_related_entities(entity, rschema, related, x) |
|
237 |
||
238 |
def render_side_related(self, entity, siderelations): |
|
239 |
"""display side related relations: |
|
240 |
non-meta in a first step, meta in a second step |
|
241 |
""" |
|
242 |
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
|
243 |
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
|
244 |
if rset] |
6053622aae81
fix name error, filter out non empty rset
sylvain.thenault@logilab.fr
parents:
956
diff
changeset
|
245 |
if sideboxes: |
6053622aae81
fix name error, filter out non empty rset
sylvain.thenault@logilab.fr
parents:
956
diff
changeset
|
246 |
for label, rset in sideboxes: |
0 | 247 |
self.w(u'<div class="sideRelated">') |
248 |
self.wview('sidebox', rset, title=label) |
|
249 |
self.w(u'</div>') |
|
250 |
elif siderelations: |
|
251 |
self.w(u'<div class="sideRelated">') |
|
252 |
for relatedinfos in siderelations: |
|
253 |
# if not relatedinfos[0].meta: |
|
254 |
# continue |
|
255 |
self._render_related_entities(entity, *relatedinfos) |
|
256 |
self.w(u'</div>') |
|
953
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
context='incontext')) |
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
260 |
if boxes: |
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
261 |
for box in boxes: |
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
262 |
try: |
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
263 |
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
|
264 |
except NotImplementedError: |
ded89d49c0b4
xhtml fixes: table has no align attribute, avoid inserting empty table
sylvain.thenault@logilab.fr
parents:
925
diff
changeset
|
265 |
# 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
|
266 |
# .call() and not cell_call() |
991
746fed01d1e0
correction (removing comments)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
988
diff
changeset
|
267 |
box.dispatch(w=self.w) |
0 | 268 |
|
269 |
def is_side_related(self, rschema, eschema): |
|
270 |
return rschema.meta and \ |
|
271 |
not rschema.schema_relation() == eschema.schema_entity() |
|
272 |
||
273 |
def _render_related_entities(self, entity, rschema, related, |
|
274 |
role='subject'): |
|
275 |
if rschema.is_final(): |
|
276 |
value = related |
|
277 |
show_label = self.show_attr_label |
|
278 |
else: |
|
279 |
if not related: |
|
280 |
return |
|
281 |
show_label = self.show_rel_label |
|
282 |
# if not too many entities, show them all in a list |
|
283 |
maxrelated = self.req.property_value('navigation.related-limit') |
|
284 |
if related.rowcount <= maxrelated: |
|
285 |
if related.rowcount == 1: |
|
286 |
value = self.view('incontext', related, row=0) |
|
287 |
elif 1 < related.rowcount <= 5: |
|
288 |
value = self.view('csv', related) |
|
289 |
else: |
|
290 |
value = '<div>' + self.view('simplelist', related) + '</div>' |
|
291 |
# else show links to display related entities |
|
292 |
else: |
|
293 |
rql = related.printable_rql() |
|
294 |
related.limit(maxrelated) |
|
295 |
value = '<div>' + self.view('simplelist', related) |
|
296 |
value += '[<a href="%s">%s</a>]' % (self.build_url(rql=rql), |
|
297 |
self.req._('see them all')) |
|
298 |
value += '</div>' |
|
299 |
label = display_name(self.req, rschema.type, role) |
|
300 |
self.field(label, value, show_label=show_label, w=self.w, tr=False) |
|
301 |
||
302 |
||
303 |
class SecondaryView(EntityView): |
|
304 |
id = 'secondary' |
|
305 |
title = _('secondary') |
|
306 |
||
307 |
def cell_call(self, row, col): |
|
308 |
"""the secondary view for an entity |
|
309 |
secondary = icon + view(oneline) |
|
310 |
""" |
|
311 |
entity = self.entity(row, col) |
|
312 |
self.w(u' ') |
|
313 |
self.wview('oneline', self.rset, row=row, col=col) |
|
314 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
315 |
|
0 | 316 |
class OneLineView(EntityView): |
317 |
id = 'oneline' |
|
318 |
title = _('oneline') |
|
319 |
||
320 |
def cell_call(self, row, col): |
|
321 |
"""the one line view for an entity: linked text view |
|
322 |
""" |
|
323 |
entity = self.entity(row, col) |
|
324 |
self.w(u'<a href="%s">' % html_escape(entity.absolute_url())) |
|
325 |
self.w(html_escape(self.view('text', self.rset, row=row, col=col))) |
|
326 |
self.w(u'</a>') |
|
327 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
328 |
|
0 | 329 |
class TextView(EntityView): |
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
330 |
"""the simplest text view for an entity""" |
0 | 331 |
id = 'text' |
332 |
title = _('text') |
|
532
ce3e94cbb074
specify content type for text views
sylvain.thenault@logilab.fr
parents:
531
diff
changeset
|
333 |
content_type = 'text/plain' |
647
49bb57a9606b
some selectors update + drop EntityRelationView (only used by jpl)
sylvain.thenault@logilab.fr
parents:
632
diff
changeset
|
334 |
|
0 | 335 |
def call(self, **kwargs): |
336 |
"""the view is called for an entire result set, by default loop |
|
337 |
other rows of the result set and call the same view on the |
|
338 |
particular row |
|
339 |
||
340 |
Views applicable on None result sets have to override this method |
|
341 |
""" |
|
342 |
rset = self.rset |
|
343 |
if rset is None: |
|
344 |
raise NotImplementedError, self |
|
345 |
for i in xrange(len(rset)): |
|
346 |
self.wview(self.id, rset, row=i, **kwargs) |
|
347 |
if len(rset) > 1: |
|
348 |
self.w(u"\n") |
|
349 |
||
350 |
def cell_call(self, row, col=0, **kwargs): |
|
351 |
entity = self.entity(row, col) |
|
352 |
self.w(cut(entity.dc_title(), |
|
353 |
self.req.property_value('navigation.short-line-size'))) |
|
354 |
||
137
7e45cf48c2f1
don't display additional owners in metadata, this is confusing
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
355 |
|
0 | 356 |
class MetaDataView(EntityView): |
357 |
"""paragraph view of some metadata""" |
|
358 |
id = 'metadata' |
|
359 |
show_eid = True |
|
360 |
||
361 |
def cell_call(self, row, col): |
|
362 |
_ = self.req._ |
|
363 |
entity = self.entity(row, col) |
|
364 |
self.w(u'<div class="metadata">') |
|
365 |
if self.show_eid: |
|
366 |
self.w(u'#%s - ' % entity.eid) |
|
367 |
if entity.modification_date != entity.creation_date: |
|
368 |
self.w(u'<span>%s</span> ' % _('latest update on')) |
|
369 |
self.w(u'<span class="value">%s</span>, ' |
|
370 |
% self.format_date(entity.modification_date)) |
|
371 |
# entities from external source may not have a creation date (eg ldap) |
|
372 |
if entity.creation_date: |
|
373 |
self.w(u'<span>%s</span> ' % _('created on')) |
|
374 |
self.w(u'<span class="value">%s</span>' |
|
375 |
% self.format_date(entity.creation_date)) |
|
376 |
if entity.creator: |
|
377 |
self.w(u' <span>%s</span> ' % _('by')) |
|
378 |
self.w(u'<span class="value">%s</span>' % entity.creator.name()) |
|
379 |
self.w(u'</div>') |
|
380 |
||
381 |
||
382 |
# new default views for finner control in general views , to use instead of |
|
383 |
# oneline / secondary |
|
384 |
||
385 |
class InContextTextView(TextView): |
|
386 |
id = 'textincontext' |
|
387 |
title = None # not listed as a possible view |
|
388 |
def cell_call(self, row, col): |
|
389 |
entity = self.entity(row, col) |
|
390 |
self.w(entity.dc_title()) |
|
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
391 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
392 |
|
0 | 393 |
class OutOfContextTextView(InContextTextView): |
394 |
id = 'textoutofcontext' |
|
395 |
||
396 |
def cell_call(self, row, col): |
|
397 |
entity = self.entity(row, col) |
|
398 |
self.w(entity.dc_long_title()) |
|
399 |
||
400 |
||
401 |
class InContextView(EntityView): |
|
402 |
id = 'incontext' |
|
403 |
||
404 |
def cell_call(self, row, col): |
|
405 |
entity = self.entity(row, col) |
|
406 |
desc = cut(entity.dc_description(), 50) |
|
407 |
self.w(u'<a href="%s" title="%s">' % (html_escape(entity.absolute_url()), |
|
408 |
html_escape(desc))) |
|
409 |
self.w(html_escape(self.view('textincontext', self.rset, row=row, col=col))) |
|
410 |
self.w(u'</a>') |
|
411 |
||
412 |
||
413 |
class OutOfContextView(EntityView): |
|
414 |
id = 'outofcontext' |
|
415 |
||
416 |
def cell_call(self, row, col): |
|
417 |
self.w(u'<a href="%s">' % self.entity(row, col).absolute_url()) |
|
418 |
self.w(html_escape(self.view('textoutofcontext', self.rset, row=row, col=col))) |
|
419 |
self.w(u'</a>') |
|
420 |
||
421 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
422 |
# list views ################################################################## |
0 | 423 |
|
424 |
class ListView(EntityView): |
|
425 |
id = 'list' |
|
426 |
title = _('list') |
|
427 |
item_vid = 'listitem' |
|
428 |
||
429 |
def call(self, klass=None, title=None, subvid=None, listid=None, **kwargs): |
|
430 |
"""display a list of entities by calling their <item_vid> view |
|
431 |
|
|
432 |
:param listid: the DOM id to use for the root element |
|
433 |
""" |
|
434 |
if subvid is None and 'subvid' in self.req.form: |
|
435 |
subvid = self.req.form.pop('subvid') # consume it |
|
436 |
if listid: |
|
437 |
listid = u' id="%s"' % listid |
|
438 |
else: |
|
439 |
listid = u'' |
|
440 |
if title: |
|
441 |
self.w(u'<div%s class="%s"><h4>%s</h4>\n' % (listid, klass or 'section', title)) |
|
442 |
self.w(u'<ul>\n') |
|
443 |
else: |
|
444 |
self.w(u'<ul%s class="%s">\n' % (listid, klass or 'section')) |
|
445 |
for i in xrange(self.rset.rowcount): |
|
446 |
self.cell_call(row=i, col=0, vid=subvid, **kwargs) |
|
447 |
self.w(u'</ul>\n') |
|
448 |
if title: |
|
449 |
self.w(u'</div>\n') |
|
450 |
||
451 |
def cell_call(self, row, col=0, vid=None, **kwargs): |
|
452 |
self.w(u'<li>') |
|
453 |
self.wview(self.item_vid, self.rset, row=row, col=col, vid=vid, **kwargs) |
|
454 |
self.w(u'</li>\n') |
|
455 |
||
456 |
def url(self): |
|
457 |
"""overrides url method so that by default, the view list is called |
|
458 |
with sorted entities |
|
459 |
""" |
|
460 |
coltypes = self.rset.column_types(0) |
|
461 |
# don't want to generate the rql if there is some restriction on |
|
462 |
# something else than the entity type |
|
463 |
if len(coltypes) == 1: |
|
464 |
# XXX norestriction is not correct here. For instance, in cases like |
|
465 |
# Any P,N WHERE P is Project, P name N |
|
466 |
# norestriction should equal True |
|
467 |
restr = self.rset.syntax_tree().children[0].where |
|
468 |
norestriction = (isinstance(restr, nodes.Relation) and |
|
469 |
restr.is_types_restriction()) |
|
470 |
if norestriction: |
|
471 |
etype = iter(coltypes).next() |
|
472 |
return self.build_url(etype.lower(), vid=self.id) |
|
473 |
if len(self.rset) == 1: |
|
474 |
entity = self.rset.get_entity(0, 0) |
|
475 |
return self.build_url(entity.rest_path(), vid=self.id) |
|
476 |
return self.build_url(rql=self.rset.printable_rql(), vid=self.id) |
|
477 |
||
478 |
||
479 |
class ListItemView(EntityView): |
|
480 |
id = 'listitem' |
|
481 |
||
482 |
@property |
|
483 |
def redirect_vid(self): |
|
484 |
if self.req.search_state[0] == 'normal': |
|
485 |
return 'outofcontext' |
|
486 |
return 'outofcontext-search' |
|
487 |
||
488 |
def cell_call(self, row, col, vid=None, **kwargs): |
|
489 |
if not vid: |
|
490 |
vid = self.redirect_vid |
|
491 |
try: |
|
492 |
self.wview(vid, self.rset, row=row, col=col, **kwargs) |
|
493 |
except NoSelectableObject: |
|
494 |
if vid == self.redirect_vid: |
|
495 |
raise |
|
496 |
self.wview(self.redirect_vid, self.rset, row=row, col=col, **kwargs) |
|
497 |
||
498 |
||
499 |
class SimpleListView(ListItemView): |
|
500 |
"""list without bullets""" |
|
501 |
id = 'simplelist' |
|
502 |
redirect_vid = 'incontext' |
|
503 |
||
504 |
||
505 |
class CSVView(SimpleListView): |
|
506 |
id = 'csv' |
|
507 |
redirect_vid = 'incontext' |
|
508 |
||
509 |
def call(self, **kwargs): |
|
510 |
rset = self.rset |
|
511 |
for i in xrange(len(rset)): |
|
512 |
self.cell_call(i, 0, vid=kwargs.get('vid')) |
|
513 |
if i < rset.rowcount-1: |
|
514 |
self.w(u", ") |
|
515 |
||
516 |
||
517 |
class TreeItemView(ListItemView): |
|
518 |
id = 'treeitem' |
|
519 |
||
520 |
def cell_call(self, row, col): |
|
521 |
self.wview('incontext', self.rset, row=row, col=col) |
|
522 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
523 |
# context specific views ###################################################### |
0 | 524 |
|
525 |
class TextSearchResultView(EntityView): |
|
526 |
"""this view is used to display full-text search |
|
527 |
||
528 |
It tries to highlight part of data where the search word appears. |
|
529 |
||
530 |
XXX: finish me (fixed line width, fixed number of lines, CSS, etc.) |
|
531 |
""" |
|
532 |
id = 'tsearch' |
|
533 |
||
534 |
def cell_call(self, row, col, **kwargs): |
|
535 |
entity = self.complete_entity(row, col) |
|
536 |
self.w(entity.view('incontext')) |
|
537 |
searched = self.rset.searched_text() |
|
538 |
if searched is None: |
|
539 |
return |
|
540 |
searched = searched.lower() |
|
541 |
highlighted = '<b>%s</b>' % searched |
|
542 |
for attr in entity.e_schema.indexable_attributes(): |
|
543 |
try: |
|
544 |
value = html_escape(entity.printable_value(attr, format='text/plain').lower()) |
|
545 |
except TransformError, ex: |
|
546 |
continue |
|
547 |
except: |
|
548 |
continue |
|
549 |
if searched in value: |
|
550 |
contexts = [] |
|
551 |
for ctx in value.split(searched): |
|
552 |
if len(ctx) > 30: |
|
553 |
contexts.append(u'...' + ctx[-30:]) |
|
554 |
else: |
|
555 |
contexts.append(ctx) |
|
556 |
value = u'\n' + highlighted.join(contexts) |
|
557 |
self.w(value.replace('\n', '<br/>')) |
|
558 |
||
559 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
560 |
class TooltipView(EntityView): |
0 | 561 |
"""A entity view used in a tooltip""" |
562 |
id = 'tooltip' |
|
563 |
def cell_call(self, row, col): |
|
564 |
self.wview('oneline', self.rset, row=row, col=col) |
|
565 |
||
824
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
566 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
567 |
# XXX bw compat |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
568 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
569 |
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
|
570 |
|
0 | 571 |
try: |
572 |
from cubicweb.web.views.tableview import TableView |
|
573 |
TableView = class_moved(TableView) |
|
574 |
except ImportError: |
|
575 |
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
|
576 |
|
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
577 |
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
|
578 |
SideBoxView = class_moved(boxes.SideBoxView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
579 |
XmlView = class_moved(xmlrss.XmlView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
580 |
XmlItemView = class_moved(xmlrss.XmlItemView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
581 |
XmlRsetView = class_moved(xmlrss.XmlRsetView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
582 |
RssView = class_moved(xmlrss.RssView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
583 |
RssItemView = class_moved(xmlrss.RssItemView) |
a5e6acffde30
merge, split baseviews (new csvexport, xmlrss and editviews modules)
sylvain.thenault@logilab.fr
parents:
795
diff
changeset
|
584 |