use django.contrib.staticfiles everywhere
[mygpo.git] / mygpo / web / templatetags / episodes.py
blob2ab2ffcb78fc1fd688883eae3af59e2c3020a77a
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.core.models import Episode
8 from mygpo import utils
9 from mygpo.data.mimetype import get_type, get_mimetype
10 from mygpo.web.utils import get_episode_link_target
13 register = template.Library()
15 @register.filter
16 def episode_status_text(episode):
17 if not episode or not episode.action:
18 return ''
20 if episode.action == 'new':
21 return _('New episode')
22 elif episode.action == 'download':
23 if episode.device.name:
24 return _('Downloaded to %s') % episode.device.name
25 else:
26 return _('Downloaded')
27 elif episode.action == 'play':
28 if episode.device.name:
29 return _('Played on %s') % episode.device.name
30 else:
31 return _('Played')
32 elif episode.action == 'delete':
33 if episode.device.name:
34 return _('Deleted on %s') % episode.device.name
35 else:
36 return _('Deleted')
38 return _('Unknown status')
40 @register.filter
41 def episode_status_icon(action):
42 if not action or not action.action:
43 s = '<img src="%s" alt="nothing" title="%s" />' % \
44 (staticfiles_storage.url('nothing.png'), _('Unplayed episode'))
46 else:
47 date_string = (_(' on %s') % (action.timestamp)) if action.timestamp else ''
48 device_string = (_(' on %s') % (action.device.name)) if action.device else ''
50 if action.action == 'flattr':
51 s = '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" alt="flattr" title="%s" />' % (_("The episode has been flattr'd"),)
53 elif action.action == 'new':
54 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))
55 elif action.action == 'download':
56 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))
57 elif action.action == 'play':
58 if action.playmark is not None:
59 if getattr(action, 'started', None) is not None:
60 playback_info = _(' from %(start)s to %(end)s') % { \
61 'start': utils.format_time(action.started), \
62 'end': utils.format_time(action.playmark)}
63 else:
64 playback_info = _(' to position %s') % (\
65 utils.format_time(action.playmark),)
66 else:
67 playback_info = ''
68 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))
69 elif action.action == 'delete':
70 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 else:
72 return action.action # this is not marked safe by intention
74 return mark_safe(s)
77 @register.filter
78 def is_image(episode):
80 if isinstance(episode, Episode):
81 mimetypes = episode.mimetypes
83 else:
84 mimetypes = [get_mimetype(episode.mimetype, episode.url)]
86 return any(get_type(mimetype) == 'image' for mimetype in mimetypes)
89 class EpisodeLinkTargetNode(template.Node):
90 """ Links to a (view of a) Podcast """
92 def __init__(self, episode, podcast, view_name='episode', add_args=[]):
93 self.episode = template.Variable(episode)
94 self.podcast = template.Variable(podcast)
95 self.view_name = view_name.replace('"', '')
96 self.add_args = [template.Variable(arg) for arg in add_args]
99 def render(self, context):
100 episode = self.episode.resolve(context)
101 podcast = self.podcast.resolve(context)
102 add_args = [arg.resolve(context) for arg in self.add_args]
103 return get_episode_link_target(episode, podcast, self.view_name, add_args)
106 @staticmethod
107 def compile(parser, token):
108 try:
109 contents = token.split_contents()
110 tag_name = contents[0]
111 episode = contents[1]
112 podcast = contents[2]
113 view_name = contents[3] if len(contents) > 3 else 'episode'
114 add_args = contents[4:]
116 except ValueError:
117 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
119 return EpisodeLinkTargetNode(episode, podcast, view_name, add_args)
122 register.tag('episode_link_target', EpisodeLinkTargetNode.compile)
126 @register.simple_tag
127 def episode_link(episode, podcast, title=None):
128 """ Returns the link for a single Episode """
130 title = title or getattr(episode, 'display_title', None) or \
131 episode.title or _('Unknown Episode')
133 title = strip_tags(title)
135 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
136 dict(target=get_episode_link_target(episode, podcast), title=title)
139 @register.simple_tag
140 def get_id(obj):
141 return obj._id