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
):
18 s
= '<img src="%s" alt="" />' % (podcast
.get_logo_url(size
),)
22 def podcast_logo(podcast
):
23 return create_podcast_logo(podcast
, PODCAST_LOGO_SIZE
)
26 def podcast_logo_big(podcast
):
27 return create_podcast_logo(podcast
, PODCAST_LOGO_BIG_SIZE
)
30 def podcast_logo_medium(podcast
):
31 return create_podcast_logo(podcast
, PODCAST_LOGO_MEDIUM_SIZE
)
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" />'
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
)
71 def compile(parser
, token
):
73 contents
= token
.split_contents()
74 tag_name
= contents
[0]
76 view_name
= contents
[2] if len(contents
) > 2 else 'podcast'
77 add_args
= contents
[3:]
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
)
104 def compile(parser
, token
):
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:]
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)
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
)
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
)
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
)