Created
March 24, 2012 08:33
-
-
Save allanlei/2180128 to your computer and use it in GitHub Desktop.
Gravatar Template Tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
import urllib, hashlib | |
register = template.Library() | |
@register.simple_tag(takes_context=True) | |
def gravatar_image_url(context, email, default='mm', size=40, extension='jpg', force_default=False, rating='g'): | |
return '{host}/avatar/{hash}.{extension}?{params}'.format( | |
host='https://secure.gravatar.com' or '//www.gravatar.com', | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
extension=extension, | |
params=urllib.urlencode({ | |
'd': default, | |
's': size, | |
'f': force_default and 'y' or '', | |
'r': rating.lower(), | |
}) | |
) | |
@register.simple_tag | |
def gravatar_profile_url(email): | |
return '//www.gravatar.com/avatar/{hash}'.format( | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
) | |
@register.simple_tag | |
def gravatar_profile_qr(email, size=50): | |
return '//www.gravatar.com/{hash}.qr?{params}'.format( | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
params=urllib.urlencode({ | |
's': size, | |
}) | |
) | |
@register.simple_tag | |
def gravatar_profile_json(email): | |
return '//www.gravatar.com/{hash}.json'.format( | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
) | |
@register.simple_tag | |
def gravatar_profile_xml(email): | |
return '//www.gravatar.com/{hash}.xml'.format( | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
) | |
@register.simple_tag | |
def gravatar_profile_json(email): | |
return '//www.gravatar.com/{hash}.vcf?'.format( | |
hash=hashlib.md5(email.strip().lower()).hexdigest(), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment