Handle None in ``podcast_logo`` templatetag
[mygpo.git] / mygpo / web / templatetags / podcasts.py
blob5200f892643abf240cf4e8a2c4f32b29ec558e53
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 def create_podcast_logo(podcast, size):
20 if not podcast:
21 return ''
23 size = int(size)
24 s = '<img src="%s" alt="" />' % (get_logo_url(podcast, size),)
25 return mark_safe(s)
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 def podcast_status_icon(action):
42 if action.action == 'subscribe':
43 s = '<img src="%s" />' % (staticfiles_storage.url('subscribe.png'),)
44 elif action.action == 'unsubscribe':
45 s = '<img src="%s" />' % (staticfiles_storage.url('unsubscribe.png'),)
46 elif action.action == 'flattr':
47 s = '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
49 return mark_safe(s)
52 @register.filter
53 def is_podcast(podcast):
54 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
55 from mygpo.podcasts.models import Podcast
56 return isinstance(podcast, Podcast)
61 class PodcastLinkTargetNode(template.Node):
62 """ Links to a (view of a) Podcast """
64 def __init__(self, podcast, view_name, add_args):
65 self.podcast = template.Variable(podcast)
66 self.view_name = view_name.replace('"', '')
67 self.add_args = [template.Variable(arg) for arg in add_args]
70 def render(self, context):
71 podcast = self.podcast.resolve(context)
72 add_args = [arg.resolve(context) for arg in self.add_args]
73 return get_podcast_link_target(podcast, self.view_name, add_args)
76 @staticmethod
77 def compile(parser, token):
78 try:
79 contents = token.split_contents()
80 tag_name = contents[0]
81 podcast = contents[1]
82 view_name = contents[2] if len(contents) > 2 else 'podcast'
83 add_args = contents[3:]
85 except ValueError:
86 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
88 return PodcastLinkTargetNode(podcast, view_name, add_args)
91 register.tag('podcast_link_target', PodcastLinkTargetNode.compile)
94 class PodcastGroupLinkTargetNode(template.Node):
95 """ Links to a (view of a) Podcast """
97 def __init__(self, group, view_name, add_args):
98 self.group = template.Variable(group)
99 self.view_name = view_name.replace('"', '')
100 self.add_args = [template.Variable(arg) for arg in add_args]
103 def render(self, context):
104 group = self.group.resolve(context)
105 add_args = [arg.resolve(context) for arg in self.add_args]
106 return get_podcast_group_link_target(podcast, self.view_name, add_args)
109 @staticmethod
110 def compile(parser, token):
111 try:
112 contents = token.split_contents()
113 tag_name = contents[0]
114 podcast = contents[1]
115 view_name = contents[2] if len(contents) > 2 else 'podcast'
116 add_args = contents[3:]
118 except ValueError:
119 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
121 return PodcastLinkTargetNode(podcast, view_name, add_args)
124 register.tag('podcast_group_link_target', PodcastGroupLinkTargetNode.compile)
128 @register.simple_tag
129 def podcast_group_link(podcast, title=None):
130 """ Returns the link strings for Podcast and PodcastGroup objects
132 automatically distinguishes between relational Podcast/PodcastGroup """
134 from mygpo.podcasts.models import PodcastGroup
136 if isinstance(podcast, PodcastGroup):
137 podcasts = list(podcast.podcast_set.all())
138 else:
139 return podcast_link(podcast, title)
141 links = (podcast_link(p, p.group_member_name) for p in podcasts)
142 link_text = ' '.join(links)
143 return '%(title)s (%(links)s)' % dict(title=podcast.title, links=link_text)
146 @register.simple_tag
147 def podcast_link(podcast, title=None):
148 """ Returns the link for a single Podcast """
150 title = title or podcast.display_title
152 title = strip_tags(title)
154 return format_html('<a href="{target}" title="{title}">{title}</a>',
155 target=get_podcast_link_target(podcast), title=title)