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
.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()
17 def create_podcast_logo(podcast
, size
):
19 s
= '<img src="%s" alt="" />' % (get_logo_url(podcast
, size
),)
23 def podcast_logo(podcast
):
24 return create_podcast_logo(podcast
, PODCAST_LOGO_SIZE
)
27 def podcast_logo_big(podcast
):
28 return create_podcast_logo(podcast
, PODCAST_LOGO_BIG_SIZE
)
31 def podcast_logo_medium(podcast
):
32 return create_podcast_logo(podcast
, PODCAST_LOGO_MEDIUM_SIZE
)
36 def podcast_status_icon(action
):
37 if action
.action
== 'subscribe':
38 s
= '<img src="%s" />' % (staticfiles_storage
.url('subscribe.png'),)
39 elif action
.action
== 'unsubscribe':
40 s
= '<img src="%s" />' % (staticfiles_storage
.url('unsubscribe.png'),)
41 elif action
.action
== 'flattr':
42 s
= '<img src="https://flattr.com/_img/icons/flattr_logo_16.png" />'
48 def is_podcast(podcast
):
49 """ Returns True if the argument is a podcast (esp not a PodcastGroup) """
50 from mygpo
.podcasts
.models
import Podcast
51 return isinstance(podcast
, Podcast
)
56 class PodcastLinkTargetNode(template
.Node
):
57 """ Links to a (view of a) Podcast """
59 def __init__(self
, podcast
, view_name
, add_args
):
60 self
.podcast
= template
.Variable(podcast
)
61 self
.view_name
= view_name
.replace('"', '')
62 self
.add_args
= [template
.Variable(arg
) for arg
in add_args
]
65 def render(self
, context
):
66 podcast
= self
.podcast
.resolve(context
)
67 add_args
= [arg
.resolve(context
) for arg
in self
.add_args
]
68 return get_podcast_link_target(podcast
, self
.view_name
, add_args
)
72 def compile(parser
, token
):
74 contents
= token
.split_contents()
75 tag_name
= contents
[0]
77 view_name
= contents
[2] if len(contents
) > 2 else 'podcast'
78 add_args
= contents
[3:]
81 raise template
.TemplateSyntaxError("%r tag requires at least one argument" % token
.contents
.split()[0])
83 return PodcastLinkTargetNode(podcast
, view_name
, add_args
)
86 register
.tag('podcast_link_target', PodcastLinkTargetNode
.compile)
89 class PodcastGroupLinkTargetNode(template
.Node
):
90 """ Links to a (view of a) Podcast """
92 def __init__(self
, group
, view_name
, add_args
):
93 self
.group
= template
.Variable(group
)
94 self
.view_name
= view_name
.replace('"', '')
95 self
.add_args
= [template
.Variable(arg
) for arg
in add_args
]
98 def render(self
, context
):
99 group
= self
.group
.resolve(context
)
100 add_args
= [arg
.resolve(context
) for arg
in self
.add_args
]
101 return get_podcast_group_link_target(podcast
, self
.view_name
, add_args
)
105 def compile(parser
, token
):
107 contents
= token
.split_contents()
108 tag_name
= contents
[0]
109 podcast
= contents
[1]
110 view_name
= contents
[2] if len(contents
) > 2 else 'podcast'
111 add_args
= contents
[3:]
114 raise template
.TemplateSyntaxError("%r tag requires at least one argument" % token
.contents
.split()[0])
116 return PodcastLinkTargetNode(podcast
, view_name
, add_args
)
119 register
.tag('podcast_group_link_target', PodcastGroupLinkTargetNode
.compile)
124 def podcast_group_link(podcast
, title
=None):
125 """ Returns the link strings for Podcast and PodcastGroup objects
127 automatically distringuishes between relational Podcast/PodcastGroup
128 objects and CouchDB-based Podcast/PodcastGroup objects """
130 from mygpo
.podcasts
.models
import PodcastGroup
132 if isinstance(podcast
, PodcastGroup
):
133 podcasts
= list(podcast
.podcast_set
.all())
135 return podcast_link(podcast
, title
)
137 links
= (podcast_link(p
, p
.group_member_name
) for p
in podcasts
)
138 link_text
= ' '.join(links
)
139 return '%(title)s (%(links)s)' % dict(title
=podcast
.title
, links
=link_text
)
143 def podcast_link(podcast
, title
=None):
144 """ Returns the link for a single Podcast """
146 title
= title
or podcast
.display_title
148 title
= strip_tags(title
)
150 return '<a href="%(target)s" title="%(title)s">%(title)s</a>' % \
151 dict(target
=get_podcast_link_target(podcast
), title
=title
)