Login

Full Featured Gravatar Tag

Author:
ashcrow
Posted:
December 13, 2008
Language:
Python
Version:
1.0
Tags:
tag simple_tag avatar gravatar
Score:
1 (after 1 ratings)

Simply returns a created gravatar url based on input. Creates the url utilizing the full gravatar url inputs as defined at http://en.gravatar.com/site/implement/url.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from hashlib import md5

from django import template


# We call it avatar_url instead of gravatar_url so if we change services in the
# future it's just a change of the tag.
@register.simple_tag
def avatar_url(email, size=50, rating='g', default=None):
    """
    Returns a gravatar url.

    Example tag usage: {% avatar user.email 80 "g" %}

    :Parameters:
       - `email`: the email to send to gravatar.
       - `size`: optional YxY size for the image.
       - `rating`: optional rating (g, pg, r, or x) of the image.
       - `default`: optional default image url or hosted image like wavatar.
    """
    # Verify the rating actually is a rating accepted by gravatar
    rating = rating.lower()
    ratings = ['g', 'pg', 'r', 'x']
    if rating not in ratings:
        raise template.TemplateSyntaxError('rating must be %s' % (
            ", ".join(ratings)))
    # Create and return the url
    hash = md5(email).hexdigest()
    url = 'http://www.gravatar.com/avatar/%s?s=%s&r=%s' % (
        hash, size, rating)
    if default:
        url = "%s&d=%s" % (url, default)
    return url

More like this

  1. Gravatar Images by jtauber 6 years, 11 months ago
  2. Photologue wiki-syntax templatetag by yeago 6 years, 6 months ago
  3. Yet another query string template tag by atms 4 years ago
  4. Gravatar support in model save override by JoeLinux 2 years, 2 months ago
  5. Add site info to request context by bthomas 6 years, 5 months ago

Comments

Please login first before commenting.