embed episodes with image mimetype
[mygpo.git] / mygpo / web / templatetags / episodes.py
blob0f06ca2d057fdcacc578867dfca2a4f0971c5068
1 from django import template
2 from django.utils.safestring import mark_safe
3 from django.utils.translation import ugettext as _
5 from mygpo import utils
6 from mygpo.data.mimetypes import get_type, get_mimetype
8 register = template.Library()
10 @register.filter
11 def episode_status_text(episode):
12 if not episode or not episode.action:
13 return ''
15 if episode.action == 'new':
16 return _('New episode')
17 elif episode.action == 'download':
18 if episode.device.name:
19 return _('Downloaded to %s') % episode.device.name
20 else:
21 return _('Downloaded')
22 elif episode.action == 'play':
23 if episode.device.name:
24 return _('Played on %s') % episode.device.name
25 else:
26 return _('Played')
27 elif episode.action == 'delete':
28 if episode.device.name:
29 return _('Deleted on %s') % episode.device.name
30 else:
31 return _('Deleted')
33 return _('Unknown status')
35 @register.filter
36 def episode_status_icon(action):
37 if not action or not action.action:
38 s = '<img src="/media/nothing.png" alt="nothing" title="%s" />' % _('Unplayed episode')
40 else:
41 date_string = (_(' on %s') % (action.timestamp)) if action.timestamp else ''
42 device_string = (_(' on %s') % (action.device.name)) if action.device else ''
44 if action.action == 'new':
45 s = '<img src="/media/new.png" alt="new" title="%s" />' % ('%s%s%s' % (_('This episode has been marked new'),date_string, device_string))
46 elif action.action == 'download':
47 s = '<img src="/media/download.png" alt="downloaded" title="%s" />' % ('%s%s%s' % (_('This episode has been downloaded'),date_string, device_string))
48 elif action.action == 'play':
49 if action.playmark is not None:
50 if action.started is not None:
51 playback_info = _(' from %(start)s to %(end)s') % { \
52 'start': utils.format_time(action.started), \
53 'end': utils.format_time(action.playmark)}
54 else:
55 playback_info = _(' to position %s') % (\
56 utils.format_time(action.playmark),)
57 else:
58 playback_info = ''
59 s = '<img src="/media/playback.png" alt="played" title="%s" />' % ('%s%s%s%s' % (_('This episode has been played'),date_string, device_string, playback_info))
60 elif action.action == 'delete':
61 s = '<img src="/media/delete.png" alt="deleted" title="%s"/>' % ('%s%s%s' % (_('This episode has been deleted'),date_string, device_string))
62 else:
63 return action.action #this is not marked safe by intention
65 return mark_safe(s)
68 @register.filter
69 def played_visualization(played_parts, length):
70 """
71 displays a visualization of played parts of an episode using the
72 Google Chart API.
74 played_parts is expected as a list of the length of the alternativing
75 unplayed and played parts as generated by mygpo.web.utils.get_played_parts()
76 """
77 narrow_pos_limit = 15
79 axis_pos = []
80 axis_label = []
81 last_p = 0
82 for p in played_parts:
83 last_p += p
84 if p < narrow_pos_limit:
85 continue
86 axis_pos.append(last_p)
87 axis_label.append(utils.format_time(last_p))
90 parts = [
91 'cht=bhs', #bar chart
92 'chco=C6D9FD,4D89F9', #colors alternating between light (unplayed) and dark (played) blue
93 'chs=760x50', #width corresponds to length, arbitrary height
94 'chds=0,%s' % length, #axis scaling from 0 to maximum duration
95 'chd=t:%s' % '|'.join([repr(x) for x in played_parts]), #alternating blocks; value corresponds to length
96 'chxt=x', #visible axes
97 'chxr=0,0,%s' % length, #axis range for axis 0 (x): 0 - length
98 'chxl=0:|%s' % '|'.join(axis_label), #axis labels
99 'chxp=0,%s' % ','.join([repr(x) for x in axis_pos]), #axis label positions
102 s = '<img src="http://chart.apis.google.com/chart?%s"' % '&'.join(parts)
104 return mark_safe(s)
107 @register.filter
108 def is_image(episode):
110 mimetype = get_mimetype(episode.mimetype, episode.url)
111 return get_type(mimetype) == 'image'