Revert "remove under-used constants.py"
[mygpo.git] / mygpo / web / templatetags / podcasts.py
blob54001808c004dde278640ea88832a6cd7ff88856
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.constants import PODCAST_LOGO_SIZE, PODCAST_LOGO_BIG_SIZE, \
9 PODCAST_LOGO_MEDIUM_SIZE
10 from mygpo.web.utils import get_podcast_link_target, \
11 get_podcast_group_link_target
14 register = template.Library()
15 def create_podcast_logo(podcast, size):
16 size = int(size)
17 s = '<img src="%s" alt="" />' % (podcast.get_logo_url(size),)
18 return mark_safe(s)
20 @register.filter
21 def podcast_logo(podcast):
22 return create_podcast_logo(podcast, PODCAST_LOGO_SIZE)
24 @register.filter
25 def podcast_logo_big(podcast):
26 return create_podcast_logo(podcast, PODCAST_LOGO_BIG_SIZE)
28 @register.filter
29 def podcast_logo_medium(podcast):
30 return create_podcast_logo(podcast, PODCAST_LOGO_MEDIUM_SIZE)
33 @register.filter
34 def podcast_status_icon(action):
35 if action.action == 'subscribe':
36 s = '<img src="/media/subscribe.png" />'
37 elif action.action == 'unsubscribe':
38 s = '<img src="/media/unsubscribe.png" />'
39 elif action.action == 'flattr':
40 s = '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
42 return mark_safe(s)
45 @register.filter
46 def is_podcast(podcast):
47 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
48 from mygpo.core.models import Podcast
49 return isinstance(podcast, Podcast)
54 class PodcastLinkTargetNode(template.Node):
55 """ Links to a (view of a) Podcast """
57 def __init__(self, podcast, view_name, add_args):
58 self.podcast = template.Variable(podcast)
59 self.view_name = view_name.replace('"', '')
60 self.add_args = [template.Variable(arg) for arg in add_args]
63 def render(self, context):
64 podcast = self.podcast.resolve(context)
65 add_args = [arg.resolve(context) for arg in self.add_args]
66 return get_podcast_link_target(podcast, self.view_name, add_args)
69 @staticmethod
70 def compile(parser, token):
71 try:
72 contents = token.split_contents()
73 tag_name = contents[0]
74 podcast = contents[1]
75 view_name = contents[2] if len(contents) > 2 else 'podcast'
76 add_args = contents[3:]
78 except ValueError:
79 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
81 return PodcastLinkTargetNode(podcast, view_name, add_args)
84 register.tag('podcast_link_target', PodcastLinkTargetNode.compile)
87 class PodcastGroupLinkTargetNode(template.Node):
88 """ Links to a (view of a) Podcast """
90 def __init__(self, group, view_name, add_args):
91 self.group = template.Variable(group)
92 self.view_name = view_name.replace('"', '')
93 self.add_args = [template.Variable(arg) for arg in add_args]
96 def render(self, context):
97 group = self.group.resolve(context)
98 add_args = [arg.resolve(context) for arg in self.add_args]
99 return get_podcast_group_link_target(podcast, self.view_name, add_args)
102 @staticmethod
103 def compile(parser, token):
104 try:
105 contents = token.split_contents()
106 tag_name = contents[0]
107 podcast = contents[1]
108 view_name = contents[2] if len(contents) > 2 else 'podcast'
109 add_args = contents[3:]
111 except ValueError:
112 raise template.TemplateSyntaxError("%r tag requires at least one argument" % token.contents.split()[0])
114 return PodcastLinkTargetNode(podcast, view_name, add_args)
117 register.tag('podcast_group_link_target', PodcastGroupLinkTargetNode.compile)
121 @register.simple_tag
122 def podcast_group_link(podcast, title=None):
123 """ Returns the link strings for Podcast and PodcastGroup objects
125 automatically distringuishes between relational Podcast/PodcastGroup
126 objects and CouchDB-based Podcast/PodcastGroup objects """
128 from mygpo.core.models import PodcastGroup
130 if isinstance(podcast, PodcastGroup):
131 podcasts = list(podcast.podcasts)
132 else:
133 return podcast_link(podcast, title)
135 links = (podcast_link(p, p.group_member_name) for p in podcasts)
136 link_text = ' '.join(links)
137 return '%(title)s (%(links)s)' % dict(title=podcast.title, links=link_text)
140 @register.simple_tag
141 def podcast_link(podcast, title=None):
142 """ Returns the link for a single Podcast """
144 title = title or getattr(podcast, 'display_title', None) or podcast.title
146 title = strip_tags(title)
148 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
149 dict(target=get_podcast_link_target(podcast), title=title)