author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 04 May 2011 14:55:27 +0200 | |
branch | stable |
changeset 7305 | 1f97c05cfbab |
parent 6372 | 4c3e2a92e340 |
child 8190 | 2a3c1b787688 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""basic support for SIMILE's timline widgets |
19 |
||
20 |
cf. http://code.google.com/p/simile-widgets/ |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
21 |
""" |
0 | 22 |
|
23 |
__docformat__ = "restructuredtext en" |
|
24 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
25 |
from logilab.mtconverter import xml_escape |
6372
4c3e2a92e340
timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5940
diff
changeset
|
26 |
from logilab.common.date import ustrftime |
0 | 27 |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
28 |
from cubicweb.selectors import adaptable |
984 | 29 |
from cubicweb.view import EntityView, StartupView |
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
30 |
from cubicweb.utils import json_dumps |
0 | 31 |
|
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
32 |
_ = unicode |
0 | 33 |
|
34 |
class TimelineJsonView(EntityView): |
|
35 |
"""generates a json file to feed Timeline.loadJSON() |
|
36 |
NOTE: work in progress (image_url, bubbleUrl and so on |
|
37 |
should be properties of entity classes or subviews) |
|
38 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
39 |
__regid__ = 'timeline-json' |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
40 |
__select__ = adaptable('ICalendarable') |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
41 |
|
0 | 42 |
binary = True |
43 |
templatable = False |
|
44 |
content_type = 'application/json' |
|
45 |
||
46 |
date_fmt = '%Y/%m/%d' |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
47 |
|
0 | 48 |
def call(self): |
49 |
events = [] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
50 |
for entity in self.cw_rset.entities(): |
0 | 51 |
event = self.build_event(entity) |
52 |
if event is not None: |
|
53 |
events.append(event) |
|
54 |
timeline_data = {'dateTimeFormat': self.date_fmt, |
|
55 |
'events': events} |
|
5940
0e3ae19b181a
[uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
56 |
self.w(json_dumps(timeline_data)) |
0 | 57 |
|
58 |
# FIXME: those properties should be defined by the entity class |
|
59 |
def onclick_url(self, entity): |
|
60 |
return entity.absolute_url() |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
61 |
|
0 | 62 |
def onclick(self, entity): |
63 |
url = self.onclick_url(entity) |
|
64 |
if url: |
|
65 |
return u"javascript: document.location.href='%s'" % url |
|
66 |
return None |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
67 |
|
0 | 68 |
def build_event(self, entity): |
69 |
"""converts `entity` into a JSON object |
|
70 |
{'start': '1891', |
|
71 |
'end': '1915', |
|
72 |
'title': 'Portrait of Horace Brodsky', |
|
73 |
'description': 'by Henri Gaudier-Brzeska, French Sculptor, 1891-1915', |
|
74 |
'image': 'http://imagecache2.allposters.com/images/BRGPOD/102770_b.jpg', |
|
75 |
'link': 'http://www.allposters.com/-sp/Portrait-of-Horace-Brodsky-Posters_i1584413_.htm' |
|
76 |
} |
|
77 |
""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
78 |
icalendarable = entity.cw_adapt_to('ICalendarable') |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
79 |
start = icalendarable.start |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
80 |
stop = icalendarable.stop |
0 | 81 |
start = start or stop |
82 |
if start is None and stop is None: |
|
83 |
return None |
|
6372
4c3e2a92e340
timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5940
diff
changeset
|
84 |
event_data = {'start': ustrftime(start, self.date_fmt), |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
85 |
'title': xml_escape(entity.dc_title()), |
1881
75540944ae18
fix dc_description format arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1730
diff
changeset
|
86 |
'description': entity.dc_description(format='text/html'), |
0 | 87 |
'link': entity.absolute_url(), |
88 |
} |
|
89 |
onclick = self.onclick(entity) |
|
90 |
if onclick: |
|
91 |
event_data['onclick'] = onclick |
|
92 |
if stop: |
|
6372
4c3e2a92e340
timeline view minor enhancements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5940
diff
changeset
|
93 |
event_data['end'] = ustrftime(stop, self.date_fmt) |
0 | 94 |
return event_data |
95 |
||
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
96 |
|
0 | 97 |
class TimelineViewMixIn(object): |
98 |
widget_class = 'TimelineWidget' |
|
99 |
jsfiles = ('cubicweb.timeline-bundle.js', 'cubicweb.widgets.js', |
|
100 |
'cubicweb.timeline-ext.js', 'cubicweb.ajax.js') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
101 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
102 |
def render_url(self, loadurl, tlunit=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
103 |
tlunit = tlunit or self._cw.form.get('tlunit') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
104 |
self._cw.add_js(self.jsfiles) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
105 |
self._cw.add_css('timeline-bundle.css') |
0 | 106 |
if tlunit: |
107 |
additional = u' cubicweb:tlunit="%s"' % tlunit |
|
108 |
else: |
|
109 |
additional = u'' |
|
110 |
self.w(u'<div class="widget" cubicweb:wdgtype="%s" ' |
|
111 |
u'cubicweb:loadtype="auto" cubicweb:loadurl="%s" %s >' % |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
(self.widget_class, xml_escape(loadurl), |
0 | 113 |
additional)) |
114 |
self.w(u'</div>') |
|
115 |
||
116 |
||
117 |
class TimelineView(TimelineViewMixIn, EntityView): |
|
118 |
"""builds a cubicweb timeline widget node""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
119 |
__regid__ = 'timeline' |
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
120 |
title = _('timeline') |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
121 |
__select__ = adaptable('ICalendarable') |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
122 |
paginable = False |
0 | 123 |
def call(self, tlunit=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
124 |
self._cw.html_headers.define_var('Timeline_urlPrefix', self._cw.datadir_url) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
125 |
rql = self.cw_rset.printable_rql() |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
126 |
loadurl = self._cw.build_url(rql=rql, vid='timeline-json') |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
127 |
self.render_url(loadurl, tlunit) |
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
128 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
129 |
|
0 | 130 |
class StaticTimelineView(TimelineViewMixIn, StartupView): |
131 |
"""similar to `TimelineView` but loads data from a static |
|
132 |
JSON file instead of one after a RQL query. |
|
133 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
134 |
__regid__ = 'static-timeline' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
135 |
|
0 | 136 |
def call(self, loadurl, tlunit=None, wdgclass=None): |
1093 | 137 |
self.widget_class = wdgclass or self.widget_class |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
138 |
self.render_url(loadurl, tlunit) |