Use mark_safe as decorator where possible
[mygpo.git] / mygpo / web / templatetags / podcasts.py
blob96ae6a1fd45940f0e52771d4e1a4982722ff7a86
1 import hashlib
3 from django import template
4 from django.utils.safestring import mark_safe
5 from django.utils.translation import ugettext as _
6 from django.utils.html import strip_tags, format_html
7 from django.contrib.staticfiles.storage import staticfiles_storage
9 from mygpo.web.logo import get_logo_url
10 from mygpo.constants import PODCAST_LOGO_SIZE, PODCAST_LOGO_BIG_SIZE, \
11 PODCAST_LOGO_MEDIUM_SIZE
12 from mygpo.web.utils import get_podcast_link_target, \
13 get_podcast_group_link_target
16 register = template.Library()
19 @mark_safe
20 def create_podcast_logo(podcast, size):
21 if not podcast:
22 return ''
24 size = int(size)
25 return '<img src="%s" alt="" />' % (get_logo_url(podcast, size),)
27 @register.filter
28 def podcast_logo(podcast):
29 return create_podcast_logo(podcast, PODCAST_LOGO_SIZE)
31 @register.filter
32 def podcast_logo_big(podcast):
33 return create_podcast_logo(podcast, PODCAST_LOGO_BIG_SIZE)
35 @register.filter
36 def podcast_logo_medium(podcast):
37 return create_podcast_logo(podcast, PODCAST_LOGO_MEDIUM_SIZE)
40 @register.filter
41 @mark_safe
42 def podcast_status_icon(action):
43 if action.action == 'subscribe':
44 return '<img src="%s" />' % (staticfiles_storage.url('subscribe.png'),)
45 elif action.action == 'unsubscribe':
46 return '<img src="%s" />' % (staticfiles_storage.url('unsubscribe.png'),)
47 elif action.action == 'flattr':
48 return '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
50 return ''
53 @register.filter
54 def is_podcast(podcast):
55 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
56 from mygpo.podcasts.models import Podcast
57 return isinstance(podcast, Podcast)
62 class PodcastLinkTargetNode(template.Node):
63 """ Links to a (view of a) Podcast """
65 def __init__(self, podcast, view_name, add_args):
66 self.podcast = template.Variable(podcast)
67 self.view_name = view_name.replace('"', '')
68 self.add_args = [template.Variable(arg) for arg in add_args]
71 def render(self, context):
72 podcast = self.podcast.resolve(context)
73 add_args = [arg.resolve(context) for arg in self.add_args]
74 return get_podcast_link_target(podcast, self.view_name, add_args)
77 @staticmethod
78 def compile(parser, token):
79 try:
80 contents = token.split_contents()
81 tag_name = contents[0]
82 podcast = contents[1]
83 view_name = contents[2] if len(contents) > 2 else 'podcast'
84 add_args = contents[3:]
86 except ValueError:
87 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
89 return PodcastLinkTargetNode(podcast, view_name, add_args)
92 register.tag('podcast_link_target', PodcastLinkTargetNode.compile)
95 class PodcastGroupLinkTargetNode(template.Node):
96 """ Links to a (view of a) Podcast """
98 def __init__(self, group, view_name, add_args):
99 self.group = template.Variable(group)
100 self.view_name = view_name.replace('"', '')
101 self.add_args = [template.Variable(arg) for arg in add_args]
104 def render(self, context):
105 group = self.group.resolve(context)
106 add_args = [arg.resolve(context) for arg in self.add_args]
107 return get_podcast_group_link_target(podcast, self.view_name, add_args)
110 @staticmethod
111 def compile(parser, token):
112 try:
113 contents = token.split_contents()
114 tag_name = contents[0]
115 podcast = contents[1]
116 view_name = contents[2] if len(contents) > 2 else 'podcast'
117 add_args = contents[3:]
119 except ValueError:
120 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
122 return PodcastLinkTargetNode(podcast, view_name, add_args)
125 register.tag('podcast_group_link_target', PodcastGroupLinkTargetNode.compile)
129 @register.simple_tag
130 def podcast_group_link(podcast, title=None):
131 """ Returns the link strings for Podcast and PodcastGroup objects
133 automatically distinguishes between relational Podcast/PodcastGroup """
135 from mygpo.podcasts.models import PodcastGroup
137 if isinstance(podcast, PodcastGroup):
138 podcasts = list(podcast.podcast_set.all())
139 else:
140 return podcast_link(podcast, title)
142 links = (podcast_link(p, p.group_member_name) for p in podcasts)
143 link_text = ' '.join(links)
144 return '%(title)s (%(links)s)' % dict(title=podcast.title, links=link_text)
147 @register.simple_tag
148 def podcast_link(podcast, title=None):
149 """ Returns the link for a single Podcast """
151 title = title or podcast.display_title
153 title = strip_tags(title)
155 return format_html('<a href="{target}" title="{title}">{title}</a>',
156 target=get_podcast_link_target(podcast), title=title)