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
6 from mygpo
.core
.models
import Episode
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="/media/nothing.png" alt="nothing" title="%s" />' % _('Unplayed episode')
45 date_string
= (_(' on %s') % (action
.timestamp
)) if action
.timestamp
else ''
46 device_string
= (_(' on %s') % (action
.device
.name
)) if action
.device
else ''
48 if action
.action
== 'flattr':
49 s
= '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" alt="flattr" title="%s" />' % (_("The episode has been flattr'd"),)
51 elif action
.action
== 'new':
52 s
= '<img src="/media/new.png" alt="new" title="%s" />' % ('%s%s%s' % (_('This episode has been marked new'),date_string
, device_string
))
53 elif action
.action
== 'download':
54 s
= '<img src="/media/download.png" alt="downloaded" title="%s" />' % ('%s%s%s' % (_('This episode has been downloaded'),date_string
, device_string
))
55 elif action
.action
== 'play':
56 if action
.playmark
is not None:
57 if getattr(action
, 'started', None) is not None:
58 playback_info
= _(' from %(start)s to %(end)s') % { \
59 'start': utils
.format_time(action
.started
), \
60 'end': utils
.format_time(action
.playmark
)}
62 playback_info
= _(' to position %s') % (\
63 utils
.format_time(action
.playmark
),)
66 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
))
67 elif action
.action
== 'delete':
68 s
= '<img src="/media/delete.png" alt="deleted" title="%s"/>' % ('%s%s%s' % (_('This episode has been deleted'),date_string
, device_string
))
70 return action
.action
# this is not marked safe by intention
76 def is_image(episode
):
78 if isinstance(episode
, Episode
):
79 mimetypes
= episode
.mimetypes
82 mimetypes
= [get_mimetype(episode
.mimetype
, episode
.url
)]
84 return any(get_type(mimetype
) == 'image' for mimetype
in mimetypes
)
87 class EpisodeLinkTargetNode(template
.Node
):
88 """ Links to a (view of a) Podcast """
90 def __init__(self
, episode
, podcast
, view_name
='episode', add_args
=[]):
91 self
.episode
= template
.Variable(episode
)
92 self
.podcast
= template
.Variable(podcast
)
93 self
.view_name
= view_name
.replace('"', '')
94 self
.add_args
= [template
.Variable(arg
) for arg
in add_args
]
97 def render(self
, context
):
98 episode
= self
.episode
.resolve(context
)
99 podcast
= self
.podcast
.resolve(context
)
100 add_args
= [arg
.resolve(context
) for arg
in self
.add_args
]
101 return get_episode_link_target(episode
, podcast
, self
.view_name
, add_args
)
105 def compile(parser
, token
):
107 contents
= token
.split_contents()
108 tag_name
= contents
[0]
109 episode
= contents
[1]
110 podcast
= contents
[2]
111 view_name
= contents
[3] if len(contents
) > 3 else 'episode'
112 add_args
= contents
[4:]
115 raise template
.TemplateSyntaxError("%r tag requires at least one argument" % token
.contents
.split()[0])
117 return EpisodeLinkTargetNode(episode
, podcast
, view_name
, add_args
)
120 register
.tag('episode_link_target', EpisodeLinkTargetNode
.compile)
125 def episode_link(episode
, podcast
, title
=None):
126 """ Returns the link for a single Episode """
128 title
= title
or getattr(episode
, 'display_title', None) or \
129 episode
.title
or _('Unknown Episode')
131 title
= strip_tags(title
)
133 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
134 dict(target
=get_episode_link_target(episode
, podcast
), title
=title
)