Permalink
Browse files

PEP8ify.

  • Loading branch information...
1 parent 44f3776 commit cdbc5f1197ff7f507bbe26f130568e73e51c0b27 @nvie committed Dec 20, 2010
Showing with 61 additions and 50 deletions.
  1. +61 −50 gravatar/templatetags/gravatar.py
@@ -7,48 +7,55 @@
from django.utils.html import escape
from django.utils import simplejson
-GRAVATAR_URL_PREFIX = getattr(settings, "GRAVATAR_URL_PREFIX", "http://www.gravatar.com/")
-GRAVATAR_DEFAULT_IMAGE = getattr(settings, "GRAVATAR_DEFAULT_IMAGE", "")
+GRAVATAR_URL_PREFIX = getattr(settings, "GRAVATAR_URL_PREFIX",
+ "http://www.gravatar.com/")
+GRAVATAR_DEFAULT_IMAGE = getattr(settings, "GRAVATAR_DEFAULT_IMAGE", "")
GRAVATAR_DEFAULT_RATING = getattr(settings, "GRAVATAR_DEFAULT_RATING", "g")
-GRAVATAR_DEFAULT_SIZE = getattr(settings, "GRAVATAR_DEFAULT_SIZE", 80)
-GRAVATAR_IMG_CLASS = getattr(settings, "GRAVATAR_IMG_CLASS", "gravatar")
+GRAVATAR_DEFAULT_SIZE = getattr(settings, "GRAVATAR_DEFAULT_SIZE", 80)
+GRAVATAR_IMG_CLASS = getattr(settings, "GRAVATAR_IMG_CLASS", "gravatar")
register = template.Library()
+
def _imgclass_attr():
if GRAVATAR_IMG_CLASS:
return ' class="%s"' % (GRAVATAR_IMG_CLASS,)
return ''
+
def _wrap_img_tag(url, info, size):
- return """<img src="%s"%s alt="Avatar for %s" height="%s" width="%s"/>""" % \
+ return '<img src="%s"%s alt="Avatar for %s" height="%s" width="%s"/>' % \
(escape(url), _imgclass_attr(), info, size, size)
+
def _get_user(user):
if not isinstance(user, User):
try:
user = User.objects.get(username=user)
except User.DoesNotExist:
- raise Exception, "Bad user for gravatar."
+ raise Exception("Bad user for gravatar.")
return user
+
def _get_gravatar_id(email):
- return md5_constructor(email).hexdigest()
+ return md5_constructor(email).hexdigest()
+
@register.simple_tag
def gravatar_for_email(email, size=None, rating=None):
"""
Generates a Gravatar URL for the given email address.
-
+
Syntax::
-
- {% gravatar_for_email <email> [size] [rating] %}
-
+
+ {% gravatar_for_email <email> [size] [rating] %}
+
Example::
-
- {% gravatar_for_email someone@example.com 48 pg %}
+
+ {% gravatar_for_email someone@example.com 48 pg %}
"""
- gravatar_url = "%savatar/%s" % (GRAVATAR_URL_PREFIX,_get_gravatar_id(email))
+ gravatar_url = "%savatar/%s" % (GRAVATAR_URL_PREFIX,
+ _get_gravatar_id(email))
parameters = [p for p in (
('d', GRAVATAR_DEFAULT_IMAGE),
@@ -61,87 +68,91 @@ def gravatar_for_email(email, size=None, rating=None):
return escape(gravatar_url)
+
@register.simple_tag
def gravatar_for_user(user, size=None, rating=None):
"""
Generates a Gravatar URL for the given user object or username.
-
+
Syntax::
-
- {% gravatar_for_user <user> [size] [rating] %}
-
+
+ {% gravatar_for_user <user> [size] [rating] %}
+
Example::
-
- {% gravatar_for_user request.user 48 pg %}
- {% gravatar_for_user 'jtauber' 48 pg %}
+
+ {% gravatar_for_user request.user 48 pg %}
+ {% gravatar_for_user 'jtauber' 48 pg %}
"""
user = _get_user(user)
return gravatar_for_email(user.email, size, rating)
+
@register.simple_tag
def gravatar_img_for_email(email, size=None, rating=None):
"""
Generates a Gravatar img for the given email address.
-
+
Syntax::
-
- {% gravatar_img_for_email <email> [size] [rating] %}
-
+
+ {% gravatar_img_for_email <email> [size] [rating] %}
+
Example::
-
- {% gravatar_img_for_email someone@example.com 48 pg %}
+
+ {% gravatar_img_for_email someone@example.com 48 pg %}
"""
gravatar_url = gravatar_for_email(email, size, rating)
return _wrap_img_tag(gravatar_url, email, size)
+
@register.simple_tag
def gravatar_img_for_user(user, size=None, rating=None):
"""
Generates a Gravatar img for the given user object or username.
-
+
Syntax::
-
- {% gravatar_img_for_user <user> [size] [rating] %}
-
+
+ {% gravatar_img_for_user <user> [size] [rating] %}
+
Example::
-
- {% gravatar_img_for_user request.user 48 pg %}
- {% gravatar_img_for_user 'jtauber' 48 pg %}
+
+ {% gravatar_img_for_user request.user 48 pg %}
+ {% gravatar_img_for_user 'jtauber' 48 pg %}
"""
gravatar_url = gravatar_for_user(user, size, rating)
return _wrap_img_tag(gravatar_url, user.username, size)
+
@register.simple_tag
def gravatar_profile_for_email(email):
"""
Generates the gravatar profile in json format for the given email address.
-
+
Syntax::
-
- {% gravatar_profile_for_email <email> %}
-
+
+ {% gravatar_profile_for_email <email> %}
+
Example::
-
- {% gravatar_profile_for_email someone@example.com %}
+
+ {% gravatar_profile_for_email someone@example.com %}
"""
- gravatar_url = "%s%s.json" % (GRAVATAR_URL_PREFIX,_get_gravatar_id(email))
+ gravatar_url = "%s%s.json" % (GRAVATAR_URL_PREFIX, _get_gravatar_id(email))
return simplejson.load(urllib.urlopen(gravatar_url))
+
@register.simple_tag
def gravatar_profile_for_user(user):
"""
Generates the gravatar profile in json format for the given user object or
- username.
-
+ username.
+
Syntax::
-
- {% gravatar_profile_for_user <user> %}
-
+
+ {% gravatar_profile_for_user <user> %}
+
Example::
-
- {% gravatar_profile_for_user request.user %}
- {% gravatar_profile_for_user 'jtauber' %}
+
+ {% gravatar_profile_for_user request.user %}
+ {% gravatar_profile_for_user 'jtauber' %}
"""
user = _get_user(user)
return gravatar_profile_for_email(user.email)
-

0 comments on commit cdbc5f1

Please sign in to comment.