remove under-used constants.py
[mygpo.git] / mygpo / web / templatetags / podcasts.py
blob853234c15a17b611797b6e93d0f1ed5441e98c53
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
8 from mygpo.web.utils import get_podcast_link_target, \
9 get_podcast_group_link_target
12 PODCAST_LOGO_SIZE = 32
13 PODCAST_LOGO_MEDIUM_SIZE=64
14 PODCAST_LOGO_BIG_SIZE = 128
17 register = template.Library()
18 def create_podcast_logo(podcast, size):
19 size = int(size)
20 s = '<img src="%s" alt="" />' % (podcast.get_logo_url(size),)
21 return mark_safe(s)
23 @register.filter
24 def podcast_logo(podcast):
25 return create_podcast_logo(podcast, PODCAST_LOGO_SIZE)
27 @register.filter
28 def podcast_logo_big(podcast):
29 return create_podcast_logo(podcast, PODCAST_LOGO_BIG_SIZE)
31 @register.filter
32 def podcast_logo_medium(podcast):
33 return create_podcast_logo(podcast, PODCAST_LOGO_MEDIUM_SIZE)
36 @register.filter
37 def podcast_status_icon(action):
38 if action.action == 'subscribe':
39 s = '<img src="/media/subscribe.png" />'
40 elif action.action == 'unsubscribe':
41 s = '<img src="/media/unsubscribe.png" />'
42 elif action.action == 'flattr':
43 s = '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
45 return mark_safe(s)
48 @register.filter
49 def is_podcast(podcast):
50 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
51 from mygpo.core.models import Podcast
52 return isinstance(podcast, Podcast)
57 class PodcastLinkTargetNode(template.Node):
58 """ Links to a (view of a) Podcast """
60 def __init__(self, podcast, view_name, add_args):
61 self.podcast = template.Variable(podcast)
62 self.view_name = view_name.replace('"', '')
63 self.add_args = [template.Variable(arg) for arg in add_args]
66 def render(self, context):
67 podcast = self.podcast.resolve(context)
68 add_args = [arg.resolve(context) for arg in self.add_args]
69 return get_podcast_link_target(podcast, self.view_name, add_args)
72 @staticmethod
73 def compile(parser, token):
74 try:
75 contents = token.split_contents()
76 tag_name = contents[0]
77 podcast = contents[1]
78 view_name = contents[2] if len(contents) > 2 else 'podcast'
79 add_args = contents[3:]
81 except ValueError:
82 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
84 return PodcastLinkTargetNode(podcast, view_name, add_args)
87 register.tag('podcast_link_target', PodcastLinkTargetNode.compile)
90 class PodcastGroupLinkTargetNode(template.Node):
91 """ Links to a (view of a) Podcast """
93 def __init__(self, group, view_name, add_args):
94 self.group = template.Variable(group)
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 group = self.group.resolve(context)
101 add_args = [arg.resolve(context) for arg in self.add_args]
102 return get_podcast_group_link_target(podcast, self.view_name, add_args)
105 @staticmethod
106 def compile(parser, token):
107 try:
108 contents = token.split_contents()
109 tag_name = contents[0]
110 podcast = contents[1]
111 view_name = contents[2] if len(contents) > 2 else 'podcast'
112 add_args = contents[3:]
114 except ValueError:
115 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
117 return PodcastLinkTargetNode(podcast, view_name, add_args)
120 register.tag('podcast_group_link_target', PodcastGroupLinkTargetNode.compile)
124 @register.simple_tag
125 def podcast_group_link(podcast, title=None):
126 """ Returns the link strings for Podcast and PodcastGroup objects
128 automatically distringuishes between relational Podcast/PodcastGroup
129 objects and CouchDB-based Podcast/PodcastGroup objects """
131 from mygpo.core.models import PodcastGroup
133 if isinstance(podcast, PodcastGroup):
134 podcasts = list(podcast.podcasts)
135 else:
136 return podcast_link(podcast, title)
138 links = (podcast_link(p, p.group_member_name) for p in podcasts)
139 link_text = ' '.join(links)
140 return '%(title)s (%(links)s)' % dict(title=podcast.title, links=link_text)
143 @register.simple_tag
144 def podcast_link(podcast, title=None):
145 """ Returns the link for a single Podcast """
147 title = title or getattr(podcast, 'display_title', None) or podcast.title
149 title = strip_tags(title)
151 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
152 dict(target=get_podcast_link_target(podcast), title=title)