Use mark_safe as decorator where possible
[mygpo.git] / mygpo / web / templatetags / facebook.py
blob42ca7e3a0d93b6153452becdf7fea8ca9a5ae94c
1 import hashlib
3 from django import template
4 from django.utils.safestring import mark_safe
5 from django.utils.translation import ugettext as _
7 from mygpo.web.logo import get_logo_url
8 from mygpo.constants import PODCAST_LOGO_BIG_SIZE
9 from mygpo.web.templatetags.podcasts import create_podcast_logo
10 from mygpo.web.utils import get_episode_link_target, get_podcast_link_target
13 register = template.Library()
15 LIKE_BUTTON_STR = """<iframe class="fb_like" src="//www.facebook.com/plugins/like.php?href=%(url)s&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>"""
17 #FIXME: remove hardcoded URL
18 # we could convert the filter to a tag once the takes_context
19 # paramter to register.simple_tag() is included in a release, see
20 # http://stackoverflow.com/questions/2160261/access-request-in-django-custom-template-tags
21 # http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#shortcut-for-simple-tags
23 @register.simple_tag
24 @mark_safe
25 def fb_like_episode(episode, podcast):
26 url = 'http://gpodder.net/%s' % get_episode_link_target(episode, podcast)
27 s = LIKE_BUTTON_STR % dict(url=url)
28 return s
31 @register.filter
32 @mark_safe
33 def fb_like_podcast(podcast):
34 url = 'http://gpodder.net%s' % get_podcast_link_target(podcast)
35 s = LIKE_BUTTON_STR % dict(url=url)
36 return s
40 OPENGRAPH_STR = """
41 <meta property="og:title" content="%(title)s"/>
42 <meta property="og:type" content="%(type)s"/>
43 <meta property="og:image" content="%(image)s"/>
44 <meta property="og:url" content="%(url)s"/>
45 <meta property="og:site_name" content="%(site_name)s"/>
46 <meta property="og:admins" content="%(admins)s"/>
47 """
49 @register.simple_tag
50 def opengraph_episode(episode, podcast):
51 s = OPENGRAPH_STR % dict(
52 title = episode.title,
53 type = 'episode',
54 image = 'http://gpodder.net%s' % get_logo_url(podcast, PODCAST_LOGO_BIG_SIZE),
55 url = 'http://gpodder.net%s' % get_episode_link_target(episode, podcast),
56 site_name = 'gpodder.net',
57 admins = '0'
59 return s
61 @register.filter
62 @mark_safe
63 def opengraph_podcast(podcast):
64 s = OPENGRAPH_STR % dict(
65 title = podcast.title,
66 type = 'episode',
67 image = 'http://gpodder.net%s' % get_logo_url(podcast, PODCAST_LOGO_BIG_SIZE),
68 url = 'http://gpodder.net%s' % get_podcast_link_target(podcast),
69 site_name = 'gpodder.net',
70 admins = '0'
72 return s