author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 02 Jun 2010 15:29:18 +0200 | |
changeset 5633 | 879590c52679 |
parent 5556 | 9ab2b4c74baf |
child 5940 | 0e3ae19b181a |
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 |
0 | 26 |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
27 |
from cubicweb.selectors import adaptable |
984 | 28 |
from cubicweb.view import EntityView, StartupView |
5389
809d3b5b3d31
[python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5377
diff
changeset
|
29 |
from cubicweb.web import json |
0 | 30 |
|
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
31 |
_ = unicode |
0 | 32 |
|
33 |
class TimelineJsonView(EntityView): |
|
34 |
"""generates a json file to feed Timeline.loadJSON() |
|
35 |
NOTE: work in progress (image_url, bubbleUrl and so on |
|
36 |
should be properties of entity classes or subviews) |
|
37 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
38 |
__regid__ = 'timeline-json' |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
39 |
__select__ = adaptable('ICalendarable') |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
40 |
|
0 | 41 |
binary = True |
42 |
templatable = False |
|
43 |
content_type = 'application/json' |
|
44 |
||
45 |
date_fmt = '%Y/%m/%d' |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
46 |
|
0 | 47 |
def call(self): |
48 |
events = [] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
49 |
for entity in self.cw_rset.entities(): |
0 | 50 |
event = self.build_event(entity) |
51 |
if event is not None: |
|
52 |
events.append(event) |
|
53 |
timeline_data = {'dateTimeFormat': self.date_fmt, |
|
54 |
'events': events} |
|
5377
84d14ddfae13
[python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4252
diff
changeset
|
55 |
self.w(json.dumps(timeline_data)) |
0 | 56 |
|
57 |
# FIXME: those properties should be defined by the entity class |
|
58 |
def onclick_url(self, entity): |
|
59 |
return entity.absolute_url() |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
60 |
|
0 | 61 |
def onclick(self, entity): |
62 |
url = self.onclick_url(entity) |
|
63 |
if url: |
|
64 |
return u"javascript: document.location.href='%s'" % url |
|
65 |
return None |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
66 |
|
0 | 67 |
def build_event(self, entity): |
68 |
"""converts `entity` into a JSON object |
|
69 |
{'start': '1891', |
|
70 |
'end': '1915', |
|
71 |
'title': 'Portrait of Horace Brodsky', |
|
72 |
'description': 'by Henri Gaudier-Brzeska, French Sculptor, 1891-1915', |
|
73 |
'image': 'http://imagecache2.allposters.com/images/BRGPOD/102770_b.jpg', |
|
74 |
'link': 'http://www.allposters.com/-sp/Portrait-of-Horace-Brodsky-Posters_i1584413_.htm' |
|
75 |
} |
|
76 |
""" |
|
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
77 |
icalendarable = entity.cw_adapt_to('ICalendarable') |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
78 |
start = icalendarable.start |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
79 |
stop = icalendarable.stop |
0 | 80 |
start = start or stop |
81 |
if start is None and stop is None: |
|
82 |
return None |
|
83 |
event_data = {'start': start.strftime(self.date_fmt), |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
84 |
'title': xml_escape(entity.dc_title()), |
1881
75540944ae18
fix dc_description format arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1730
diff
changeset
|
85 |
'description': entity.dc_description(format='text/html'), |
0 | 86 |
'link': entity.absolute_url(), |
87 |
} |
|
88 |
onclick = self.onclick(entity) |
|
89 |
if onclick: |
|
90 |
event_data['onclick'] = onclick |
|
91 |
if stop: |
|
92 |
event_data['end'] = stop.strftime(self.date_fmt) |
|
93 |
return event_data |
|
94 |
||
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
95 |
|
0 | 96 |
class TimelineViewMixIn(object): |
97 |
widget_class = 'TimelineWidget' |
|
98 |
jsfiles = ('cubicweb.timeline-bundle.js', 'cubicweb.widgets.js', |
|
99 |
'cubicweb.timeline-ext.js', 'cubicweb.ajax.js') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
100 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
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
|
104 |
self._cw.add_css('timeline-bundle.css') |
0 | 105 |
if tlunit: |
106 |
additional = u' cubicweb:tlunit="%s"' % tlunit |
|
107 |
else: |
|
108 |
additional = u'' |
|
109 |
self.w(u'<div class="widget" cubicweb:wdgtype="%s" ' |
|
110 |
u'cubicweb:loadtype="auto" cubicweb:loadurl="%s" %s >' % |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
111 |
(self.widget_class, xml_escape(loadurl), |
0 | 112 |
additional)) |
113 |
self.w(u'</div>') |
|
114 |
||
115 |
||
116 |
class TimelineView(TimelineViewMixIn, EntityView): |
|
117 |
"""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
|
118 |
__regid__ = 'timeline' |
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
119 |
title = _('timeline') |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
120 |
__select__ = adaptable('ICalendarable') |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
121 |
paginable = False |
0 | 122 |
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
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
self.render_url(loadurl, tlunit) |
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
127 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
128 |
|
0 | 129 |
class StaticTimelineView(TimelineViewMixIn, StartupView): |
130 |
"""similar to `TimelineView` but loads data from a static |
|
131 |
JSON file instead of one after a RQL query. |
|
132 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
133 |
__regid__ = 'static-timeline' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
134 |
|
0 | 135 |
def call(self, loadurl, tlunit=None, wdgclass=None): |
1093 | 136 |
self.widget_class = wdgclass or self.widget_class |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
137 |
self.render_url(loadurl, tlunit) |