1 from django
import template
2 from django
.utils
.safestring
import mark_safe
3 from django
.utils
.translation
import ugettext
as _
4 from django
.utils
.html
import strip_tags
5 from django
.contrib
.staticfiles
.storage
import staticfiles_storage
7 from mygpo
import utils
8 from mygpo
.data
.mimetype
import get_type
, get_mimetype
9 from mygpo
.web
.utils
import get_episode_link_target
12 register
= template
.Library()
15 def episode_status_text(episode
):
16 if not episode
or not episode
.action
:
19 if episode
.action
== 'new':
20 return _('New episode')
21 elif episode
.action
== 'download':
22 if episode
.device
.name
:
23 return _('Downloaded to %s') % episode
.device
.name
25 return _('Downloaded')
26 elif episode
.action
== 'play':
27 if episode
.device
.name
:
28 return _('Played on %s') % episode
.device
.name
31 elif episode
.action
== 'delete':
32 if episode
.device
.name
:
33 return _('Deleted on %s') % episode
.device
.name
37 return _('Unknown status')
40 def episode_status_icon(action
):
41 if not action
or not action
.action
:
42 s
= '<img src="%s" alt="nothing" title="%s" />' % \
43 (staticfiles_storage
.url('nothing.png'), _('Unplayed episode'))
46 date_string
= (_(' on %s') % (action
.timestamp
)) if action
.timestamp
else ''
47 device_string
= (_(' on %s') % (action
.client
.name
)) if action
.client
else ''
49 if action
.action
== 'flattr':
50 s
= '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" alt="flattr" title="%s" />' % (_("The episode has been flattr'd"),)
52 elif action
.action
== 'new':
53 s
= '<img src="%s" alt="new" title="%s" />' % (staticfiles_storage
.url('new.png'), '%s%s%s' % (_('This episode has been marked new'),date_string
, device_string
))
54 elif action
.action
== 'download':
55 s
= '<img src="%s" alt="downloaded" title="%s" />' % (staticfiles_storage
.url('download.png'), '%s%s%s' % (_('This episode has been downloaded'),date_string
, device_string
))
56 elif action
.action
== 'play':
57 if action
.playmark
is not None:
58 if getattr(action
, 'started', None) is not None:
59 playback_info
= _(' from %(start)s to %(end)s') % { \
60 'start': utils
.format_time(action
.started
), \
61 'end': utils
.format_time(action
.playmark
)}
63 playback_info
= _(' to position %s') % (\
64 utils
.format_time(action
.playmark
),)
67 s
= '<img src="%s" alt="played" title="%s" />' % (staticfiles_storage
.url('playback.png'), '%s%s%s%s' % (_('This episode has been played'),date_string
, device_string
, playback_info
))
68 elif action
.action
== 'delete':
69 s
= '<img src="%s" alt="deleted" title="%s"/>' % (staticfiles_storage
.url('delete.png'), '%s%s%s' % (_('This episode has been deleted'),date_string
, device_string
))
71 return action
.action
# this is not marked safe by intention
77 def is_image(episode
):
78 mimetypes
= episode
.mimetypes
.split(',')
79 return any(get_type(mimetype
) == 'image' for mimetype
in mimetypes
)
82 class EpisodeLinkTargetNode(template
.Node
):
83 """ Links to a (view of a) Podcast """
85 def __init__(self
, episode
, podcast
, view_name
='episode', add_args
=[]):
86 self
.episode
= template
.Variable(episode
)
87 self
.podcast
= template
.Variable(podcast
)
88 self
.view_name
= view_name
.replace('"', '')
89 self
.add_args
= [template
.Variable(arg
) for arg
in add_args
]
92 def render(self
, context
):
93 episode
= self
.episode
.resolve(context
)
94 podcast
= self
.podcast
.resolve(context
)
95 add_args
= [arg
.resolve(context
) for arg
in self
.add_args
]
96 return get_episode_link_target(episode
, podcast
, self
.view_name
, add_args
)
100 def compile(parser
, token
):
102 contents
= token
.split_contents()
103 tag_name
= contents
[0]
104 episode
= contents
[1]
105 podcast
= contents
[2]
106 view_name
= contents
[3] if len(contents
) > 3 else 'episode'
107 add_args
= contents
[4:]
110 raise template
.TemplateSyntaxError("%r tag requires at least one argument" % token
.contents
.split()[0])
112 return EpisodeLinkTargetNode(episode
, podcast
, view_name
, add_args
)
115 register
.tag('episode_link_target', EpisodeLinkTargetNode
.compile)
120 def episode_link(episode
, podcast
, title
=None):
121 """ Returns the link for a single Episode """
123 title
= title
or getattr(episode
, 'display_title', None) or \
124 episode
.get_short_title(podcast
.common_episode_title
) or \
128 title
= strip_tags(title
)
130 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
131 dict(target
=get_episode_link_target(episode
, podcast
), title
=title
)
140 def episode_number(episode
, podcast
):
141 num
= episode
.get_episode_number(podcast
.common_episode_title
)
145 def episode_short_title(episode
, podcast
):
146 title
= episode
.get_short_title(podcast
.common_episode_title
)