author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Sat, 29 May 2010 10:33:40 +0200 | |
branch | stable |
changeset 5607 | 38d43dc5ee57 |
parent 5424 | 8ecbcbff9777 |
child 5556 | 9ab2b4c74baf |
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/ |
|
21 |
||
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 |
|
27 |
from cubicweb.interfaces import ICalendarable |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
28 |
from cubicweb.selectors import implements |
984 | 29 |
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
|
30 |
from cubicweb.web import json |
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' |
0 | 40 |
binary = True |
41 |
templatable = False |
|
42 |
content_type = 'application/json' |
|
43 |
||
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
44 |
__select__ = implements(ICalendarable) |
0 | 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 |
""" |
|
77 |
start = entity.start |
|
78 |
stop = entity.stop |
|
79 |
start = start or stop |
|
80 |
if start is None and stop is None: |
|
81 |
return None |
|
82 |
event_data = {'start': start.strftime(self.date_fmt), |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
83 |
'title': xml_escape(entity.dc_title()), |
1881
75540944ae18
fix dc_description format arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1730
diff
changeset
|
84 |
'description': entity.dc_description(format='text/html'), |
0 | 85 |
'link': entity.absolute_url(), |
86 |
} |
|
87 |
onclick = self.onclick(entity) |
|
88 |
if onclick: |
|
89 |
event_data['onclick'] = onclick |
|
90 |
if stop: |
|
91 |
event_data['end'] = stop.strftime(self.date_fmt) |
|
92 |
return event_data |
|
93 |
||
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
94 |
|
0 | 95 |
class TimelineViewMixIn(object): |
96 |
widget_class = 'TimelineWidget' |
|
97 |
jsfiles = ('cubicweb.timeline-bundle.js', 'cubicweb.widgets.js', |
|
98 |
'cubicweb.timeline-ext.js', 'cubicweb.ajax.js') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
99 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
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
|
103 |
self._cw.add_css('timeline-bundle.css') |
0 | 104 |
if tlunit: |
105 |
additional = u' cubicweb:tlunit="%s"' % tlunit |
|
106 |
else: |
|
107 |
additional = u'' |
|
108 |
self.w(u'<div class="widget" cubicweb:wdgtype="%s" ' |
|
109 |
u'cubicweb:loadtype="auto" cubicweb:loadurl="%s" %s >' % |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
110 |
(self.widget_class, xml_escape(loadurl), |
0 | 111 |
additional)) |
112 |
self.w(u'</div>') |
|
113 |
||
114 |
||
115 |
class TimelineView(TimelineViewMixIn, EntityView): |
|
116 |
"""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
|
117 |
__regid__ = 'timeline' |
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
118 |
title = _('timeline') |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
119 |
__select__ = implements(ICalendarable) |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
120 |
paginable = False |
0 | 121 |
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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
self.render_url(loadurl, tlunit) |
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
126 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
127 |
|
0 | 128 |
class StaticTimelineView(TimelineViewMixIn, StartupView): |
129 |
"""similar to `TimelineView` but loads data from a static |
|
130 |
JSON file instead of one after a RQL query. |
|
131 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
132 |
__regid__ = 'static-timeline' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
133 |
|
0 | 134 |
def call(self, loadurl, tlunit=None, wdgclass=None): |
1093 | 135 |
self.widget_class = wdgclass or self.widget_class |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
136 |
self.render_url(loadurl, tlunit) |