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
.mimetype
import get_type
, get_mimetype
8 register
= template
.Library()
11 def episode_status_text(episode
):
12 if not episode
or not episode
.action
:
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
21 return _('Downloaded')
22 elif episode
.action
== 'play':
23 if episode
.device
.name
:
24 return _('Played on %s') % episode
.device
.name
27 elif episode
.action
== 'delete':
28 if episode
.device
.name
:
29 return _('Deleted on %s') % episode
.device
.name
33 return _('Unknown status')
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')
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
)}
55 playback_info
= _(' to position %s') % (\
56 utils
.format_time(action
.playmark
),)
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
))
63 return action
.action
#this is not marked safe by intention
69 def played_visualization(played_parts
, length
):
71 displays a visualization of played parts of an episode using the
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()
82 for p
in played_parts
:
84 if p
< narrow_pos_limit
:
86 axis_pos
.append(last_p
)
87 axis_label
.append(utils
.format_time(last_p
))
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
)
108 def is_image(episode
):
110 mimetype
= get_mimetype(episode
.mimetype
, episode
.url
)
111 return get_type(mimetype
) == 'image'