[extensions-web] Rewrite gravatar template tag to use Library.simple_tag and SSL
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] Rewrite gravatar template tag to use Library.simple_tag and SSL
- Date: Wed, 26 Oct 2011 20:07:30 +0000 (UTC)
commit baf3f21ac99a6894b591cce48e01d9a11d7bab8a
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Wed Oct 26 13:45:28 2011 -0400
Rewrite gravatar template tag to use Library.simple_tag and SSL
sweettooth/extensions/templatetags/gravatar.py | 43 +++--------------------
1 files changed, 6 insertions(+), 37 deletions(-)
---
diff --git a/sweettooth/extensions/templatetags/gravatar.py b/sweettooth/extensions/templatetags/gravatar.py
index 71346b5..6014513 100644
--- a/sweettooth/extensions/templatetags/gravatar.py
+++ b/sweettooth/extensions/templatetags/gravatar.py
@@ -1,43 +1,12 @@
-### gravatar.py ###############
-### place inside a 'templatetags' directory inside the top level of a Django app (not project, must be inside an app)
-### at the top of your page template include this:
-### {% load gravatar %}
-### and to use the url do this:
-### <img src="{% gravatar_url someone somewhere com %}">
-### or
-### <img src="{% gravatar_url some_template_variable %}">
-### just make sure to update the "default" image path below
-
from django import template
import urllib, hashlib
register = template.Library()
-class GravatarUrlNode(template.Node):
- def __init__(self, email, size):
- self.email = template.Variable(email)
- self.size = size
-
- def render(self, context):
- try:
- email = self.email.resolve(context)
- except template.VariableDoesNotExist:
- return ''
-
- default = "http://planet.gnome.org/heads/nobody.png"
-
- gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?"
- gravatar_url += urllib.urlencode({'d':default, 's':str(self.size)})
-
- return gravatar_url
-
- register tag
-def gravatar_url(parser, token):
- try:
- # Default of 70 px
- tag_name, email, size = (token.split_contents() + [70])[:3]
-
- except ValueError:
- raise template.TemplateSyntaxError, "%r tag requires one or two arguments" % token.contents.split()[0]
+GRAVATAR_BASE = "https://secure.gravatar.com/avatar/%s?%s"
- return GravatarUrlNode(email, size)
+ register simple_tag
+def gravatar_url(email, size=70, default="http://planet.gnome.org/heads/nobody.png"):
+ email_md5 = hashlib.md5(email.lower()).hexdigest()
+ options = urllib.urlencode(dict(d=default, s=size))
+ return GRAVATAR_BASE % (email_md5, options)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]