author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 17 Feb 2010 11:25:12 +0100 | |
branch | stable |
changeset 4604 | 96cc3d296d75 |
parent 4252 | 6c4f109c2b03 |
child 5377 | 84d14ddfae13 |
permissions | -rw-r--r-- |
0 | 1 |
"""basic support for SIMILE's timline widgets |
2 |
||
3 |
cf. http://code.google.com/p/simile-widgets/ |
|
4 |
||
5 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2312
diff
changeset
|
6 |
:copyright: 2008-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 7 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1884
diff
changeset
|
8 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 9 |
""" |
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
import simplejson |
|
13 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
14 |
from logilab.mtconverter import xml_escape |
0 | 15 |
|
16 |
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
|
17 |
from cubicweb.selectors import implements |
984 | 18 |
from cubicweb.view import EntityView, StartupView |
0 | 19 |
|
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
20 |
_ = unicode |
0 | 21 |
|
22 |
class TimelineJsonView(EntityView): |
|
23 |
"""generates a json file to feed Timeline.loadJSON() |
|
24 |
NOTE: work in progress (image_url, bubbleUrl and so on |
|
25 |
should be properties of entity classes or subviews) |
|
26 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
27 |
__regid__ = 'timeline-json' |
0 | 28 |
binary = True |
29 |
templatable = False |
|
30 |
content_type = 'application/json' |
|
31 |
||
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
32 |
__select__ = implements(ICalendarable) |
0 | 33 |
date_fmt = '%Y/%m/%d' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
34 |
|
0 | 35 |
def call(self): |
36 |
events = [] |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
37 |
for entity in self.cw_rset.entities(): |
0 | 38 |
event = self.build_event(entity) |
39 |
if event is not None: |
|
40 |
events.append(event) |
|
41 |
timeline_data = {'dateTimeFormat': self.date_fmt, |
|
42 |
'events': events} |
|
43 |
self.w(simplejson.dumps(timeline_data)) |
|
44 |
||
45 |
# FIXME: those properties should be defined by the entity class |
|
46 |
def onclick_url(self, entity): |
|
47 |
return entity.absolute_url() |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
48 |
|
0 | 49 |
def onclick(self, entity): |
50 |
url = self.onclick_url(entity) |
|
51 |
if url: |
|
52 |
return u"javascript: document.location.href='%s'" % url |
|
53 |
return None |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
54 |
|
0 | 55 |
def build_event(self, entity): |
56 |
"""converts `entity` into a JSON object |
|
57 |
{'start': '1891', |
|
58 |
'end': '1915', |
|
59 |
'title': 'Portrait of Horace Brodsky', |
|
60 |
'description': 'by Henri Gaudier-Brzeska, French Sculptor, 1891-1915', |
|
61 |
'image': 'http://imagecache2.allposters.com/images/BRGPOD/102770_b.jpg', |
|
62 |
'link': 'http://www.allposters.com/-sp/Portrait-of-Horace-Brodsky-Posters_i1584413_.htm' |
|
63 |
} |
|
64 |
""" |
|
65 |
start = entity.start |
|
66 |
stop = entity.stop |
|
67 |
start = start or stop |
|
68 |
if start is None and stop is None: |
|
69 |
return None |
|
70 |
event_data = {'start': start.strftime(self.date_fmt), |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
71 |
'title': xml_escape(entity.dc_title()), |
1881
75540944ae18
fix dc_description format arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1730
diff
changeset
|
72 |
'description': entity.dc_description(format='text/html'), |
0 | 73 |
'link': entity.absolute_url(), |
74 |
} |
|
75 |
onclick = self.onclick(entity) |
|
76 |
if onclick: |
|
77 |
event_data['onclick'] = onclick |
|
78 |
if stop: |
|
79 |
event_data['end'] = stop.strftime(self.date_fmt) |
|
80 |
return event_data |
|
81 |
||
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
82 |
|
0 | 83 |
class TimelineViewMixIn(object): |
84 |
widget_class = 'TimelineWidget' |
|
85 |
jsfiles = ('cubicweb.timeline-bundle.js', 'cubicweb.widgets.js', |
|
86 |
'cubicweb.timeline-ext.js', 'cubicweb.ajax.js') |
|
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
87 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
self._cw.add_css('timeline-bundle.css') |
0 | 92 |
if tlunit: |
93 |
additional = u' cubicweb:tlunit="%s"' % tlunit |
|
94 |
else: |
|
95 |
additional = u'' |
|
96 |
self.w(u'<div class="widget" cubicweb:wdgtype="%s" ' |
|
97 |
u'cubicweb:loadtype="auto" cubicweb:loadurl="%s" %s >' % |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
(self.widget_class, xml_escape(loadurl), |
0 | 99 |
additional)) |
100 |
self.w(u'</div>') |
|
101 |
||
102 |
||
103 |
class TimelineView(TimelineViewMixIn, EntityView): |
|
104 |
"""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
|
105 |
__regid__ = 'timeline' |
1884
893119ce3fef
give a title to timeline generic view
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1881
diff
changeset
|
106 |
title = _('timeline') |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
107 |
__select__ = implements(ICalendarable) |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
108 |
paginable = False |
0 | 109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
self.render_url(loadurl, tlunit) |
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
114 |
|
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
115 |
|
0 | 116 |
class StaticTimelineView(TimelineViewMixIn, StartupView): |
117 |
"""similar to `TimelineView` but loads data from a static |
|
118 |
JSON file instead of one after a RQL query. |
|
119 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
120 |
__regid__ = 'static-timeline' |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
121 |
|
0 | 122 |
def call(self, loadurl, tlunit=None, wdgclass=None): |
1093 | 123 |
self.widget_class = wdgclass or self.widget_class |
1730
cec526a96535
fix 'render' method name conflict
sylvain.thenault@logilab.fr
parents:
1093
diff
changeset
|
124 |
self.render_url(loadurl, tlunit) |