use django.contrib.staticfiles everywhere
[mygpo.git] / mygpo / web / templatetags / podcasts.py
blob8d33d6ad6563c88229c372ca9b0ecac3057207e9
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
7 from django.contrib.staticfiles.storage import staticfiles_storage
9 from mygpo.constants import PODCAST_LOGO_SIZE, PODCAST_LOGO_BIG_SIZE, \
10 PODCAST_LOGO_MEDIUM_SIZE
11 from mygpo.web.utils import get_podcast_link_target, \
12 get_podcast_group_link_target
15 register = template.Library()
16 def create_podcast_logo(podcast, size):
17 size = int(size)
18 s = '<img src="%s" alt="" />' % (podcast.get_logo_url(size),)
19 return mark_safe(s)
21 @register.filter
22 def podcast_logo(podcast):
23 return create_podcast_logo(podcast, PODCAST_LOGO_SIZE)
25 @register.filter
26 def podcast_logo_big(podcast):
27 return create_podcast_logo(podcast, PODCAST_LOGO_BIG_SIZE)
29 @register.filter
30 def podcast_logo_medium(podcast):
31 return create_podcast_logo(podcast, PODCAST_LOGO_MEDIUM_SIZE)
34 @register.filter
35 def podcast_status_icon(action):
36 if action.action == 'subscribe':
37 s = '<img src="%s" />' % (staticfiles_storage.url('subscribe.png'),)
38 elif action.action == 'unsubscribe':
39 s = '<img src="%s" />' % (staticfiles_storage.url('unsubscribe.png'),)
40 elif action.action == 'flattr':
41 s = '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
43 return mark_safe(s)
46 @register.filter
47 def is_podcast(podcast):
48 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
49 from mygpo.core.models import Podcast
50 return isinstance(podcast, Podcast)
55 class PodcastLinkTargetNode(template.Node):
56 """ Links to a (view of a) Podcast """
58 def __init__(self, podcast, view_name, add_args):
59 self.podcast = template.Variable(podcast)
60 self.view_name = view_name.replace('"', '')
61 self.add_args = [template.Variable(arg) for arg in add_args]
64 def render(self, context):
65 podcast = self.podcast.resolve(context)
66 add_args = [arg.resolve(context) for arg in self.add_args]
67 return get_podcast_link_target(podcast, self.view_name, add_args)
70 @staticmethod
71 def compile(parser, token):
72 try:
73 contents = token.split_contents()
74 tag_name = contents[0]
75 podcast = contents[1]
76 view_name = contents[2] if len(contents) > 2 else 'podcast'
77 add_args = contents[3:]
79 except ValueError:
80 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
82 return PodcastLinkTargetNode(podcast, view_name, add_args)
85 register.tag('podcast_link_target', PodcastLinkTargetNode.compile)
88 class PodcastGroupLinkTargetNode(template.Node):
89 """ Links to a (view of a) Podcast """
91 def __init__(self, group, view_name, add_args):
92 self.group = template.Variable(group)
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 group = self.group.resolve(context)
99 add_args = [arg.resolve(context) for arg in self.add_args]
100 return get_podcast_group_link_target(podcast, self.view_name, add_args)
103 @staticmethod
104 def compile(parser, token):
105 try:
106 contents = token.split_contents()
107 tag_name = contents[0]
108 podcast = contents[1]
109 view_name = contents[2] if len(contents) > 2 else 'podcast'
110 add_args = contents[3:]
112 except ValueError:
113 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
115 return PodcastLinkTargetNode(podcast, view_name, add_args)
118 register.tag('podcast_group_link_target', PodcastGroupLinkTargetNode.compile)
122 @register.simple_tag
123 def podcast_group_link(podcast, title=None):
124 """ Returns the link strings for Podcast and PodcastGroup objects
126 automatically distringuishes between relational Podcast/PodcastGroup
127 objects and CouchDB-based Podcast/PodcastGroup objects """
129 from mygpo.core.models import PodcastGroup
131 if isinstance(podcast, PodcastGroup):
132 podcasts = list(podcast.podcasts)
133 else:
134 return podcast_link(podcast, title)
136 links = (podcast_link(p, p.group_member_name) for p in podcasts)
137 link_text = ' '.join(links)
138 return '%(title)s (%(links)s)' % dict(title=podcast.title, links=link_text)
141 @register.simple_tag
142 def podcast_link(podcast, title=None):
143 """ Returns the link for a single Podcast """
145 title = title or getattr(podcast, 'display_title', None) or podcast.title
147 title = strip_tags(title)
149 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
150 dict(target=get_podcast_link_target(podcast), title=title)