<commit_before>import os

from django.db.models import get_model
from django.template import Library, Node, TemplateSyntaxError, resolve_variable
from django.conf import settings

from booki.account.models import UserProfile

register = Library()

class ProfileImageNode(Node):
    def __init__(self, user):
        self.user = user

    def render(self, context):
        user = resolve_variable(self.user, context)
        # should check if it exists and etc

        profile = UserProfile.objects.get(user=user)

        if not profile.image:
            return """<img src="%s/images/anonymous.jpg"/>""" % settings.SITE_STATIC_URL

        filename = profile.image.name
            
            
        return """<img src="%s/profile_images/%s"/>""" % (settings.DATA_URL, filename.split('/')[-1])

@register.tag
def profile_image(parser, token):
    """
    Django tag. Shows user profile image. If user does not have defined image it will show anonymous image.

    @type token: C{string}
    @param token: Variable name that points to C{User} object.
    """

    bits = token.contents.split()

    if len(bits) != 2:
        raise TemplateSyntaxError

    return ProfileImageNode(bits[1])

<commit_msg>Add default gravatar support.<commit_after>import os
import urllib
import hashlib

from django.db.models import get_model
from django.template import Library, Node, TemplateSyntaxError, resolve_variable
from django.conf import settings

from booki.account.models import UserProfile

register = Library()

class ProfileImageNode(Node):
    def __init__(self, user):
        self.user = user

    def render(self, context):
        user = resolve_variable(self.user, context)
        # should check if it exists and etc

        profile = UserProfile.objects.get(user=user)

        if not profile.image:
            return '<img src="http://www.gravatar.com/avatar/'+hashlib.md5(profile.user.email.lower()).hexdigest()+"?"+urllib.urlencode({"d": "%s/images/anonymous.jpg" % settings.SITE_STATIC_URL, "s": str(100)})+'">'

        filename = profile.image.name
            
            
        return """<img src="%s/profile_images/%s"/>""" % (settings.DATA_URL, filename.split('/')[-1])

@register.tag
def profile_image(parser, token):
    """
    Django tag. Shows user profile image. If user does not have defined image it will show anonymous image.

    @type token: C{string}
    @param token: Variable name that points to C{User} object.
    """

    bits = token.contents.split()

    if len(bits) != 2:
        raise TemplateSyntaxError

    return ProfileImageNode(bits[1])


{'fraction_non_alphanumeric': 0.11608175858079445, 'fraction_numerical': 0.0038565368299267257, 'mean_word_length': 3.1371610845295055, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941632', 'n_tokens_mistral': 760, 'n_tokens_neox': 732, 'n_words': 214}