Example #1
0
def render_primary(request, extra_context={}, user=None, size=AVATAR_DEFAULT_SIZE, *args, **kwargs):
    size = int(size)
    if not isinstance(user, User):
        try:
            user = get_user(user)
        except User.DoesNotExist:
            return None
    avatar = get_primary_avatar(user, size=size)
    if avatar:
        # FIXME: later, add an option to render the resized avatar dynamically
        # instead of redirecting to an already created static file. This could
        # be useful in certain situations, particulary if there is a CDN and
        # we want to minimize the storage usage on our static server, letting
        # the CDN store those files instead
        return HttpResponseRedirect(avatar.avatar_url(size))
    else:
        if AVATAR_GRAVATAR_BACKUP:
            params = {'s': str(size)}
            if AVATAR_GRAVATAR_DEFAULT:
                params['d'] = AVATAR_GRAVATAR_DEFAULT
            path = "%s/?%s" % (hashlib.md5(user.email).hexdigest(),
                               urllib.urlencode(params))
            url = urlparse.urljoin(AVATAR_GRAVATAR_BASE_URL, path)
        else:
            url = get_default_avatar_url()
        return HttpResponseRedirect(url)
Example #2
0
def avatar_gallery(request, username, template_name="avatar/gallery.html"):
    try:
        user = get_user(username)
    except User.DoesNotExist:
        raise Http404
    return render_to_response(template_name, {
        "other_user": user,
        "avatars": user.avatar_set.all(),
    }, context_instance=RequestContext(request))
Example #3
0
def avatar_gallery(request, username, template_name="avatar/gallery.html"):
    try:
        user = get_user(username)
    except get_user_model().DoesNotExist:
        raise Http404
    return render(request, template_name, {
        "other_user": user,
        "avatars": user.avatar_set.all(),
    })
Example #4
0
def avatar_gallery(request, username, template_name="avatar/gallery.html"):
    try:
        user = get_user(username)
    except get_user_model().DoesNotExist:
        raise Http404
    return render(request, template_name, {
        "other_user": user,
        "avatars": user.avatar_set.all(),
    })
Example #5
0
def avatar_gallery(request, username, template_name="avatar/gallery.html"):
    try:
        user = get_user(username)
    except User.DoesNotExist:
        raise Http404
    return render_to_response(template_name, {
        "other_user": user,
        "avatars": user.avatar_set.all(),
    }, context_instance=RequestContext(request))
Example #6
0
def render_primary(request, user=None, size=settings.AVATAR_DEFAULT_SIZE):
    size = int(size)
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
        except get_user_model().DoesNotExist:
            return None    

    url = get_primary_avatar_url(user, size=size)
    
    return redirect(url)
Example #7
0
def render_primary(request, user=None, size=settings.AVATAR_DEFAULT_SIZE):
    size = int(size)
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
        except get_user_model().DoesNotExist:
            return None

    url = get_primary_avatar_url(user, size=size)

    return redirect(url)
def avatar(user, size=AVATAR_DEFAULT_SIZE, **kwargs):
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, size)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, size)
    context = dict(kwargs, **{"user": user, "url": url, "alt": alt, "size": size})
    return render_to_string("avatar/avatar_tag.html", context)
Example #9
0
def avatar(user, width=settings.AVATAR_DEFAULT_SIZE, height=False, **kwargs):
    if not height:
        height = width
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, width, height)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, width, height)
    context = dict(kwargs, **{"user": user, "url": url, "alt": alt, "width": width, "height": height})
    return render_to_string("avatar/avatar_tag.html", context)
Example #10
0
def avatar(request, username, id, template_name="avatar/avatar.html"):
    try:
        user = get_user(username)
    except get_user_model().DoesNotExist:
        raise Http404
    avatars = user.avatar_set.order_by("-date_uploaded")
    index = None
    avatar = None
    if avatars:
        avatar = avatars.get(pk=id)
        if not avatar:
            return Http404

        index = avatars.filter(date_uploaded__gt=avatar.date_uploaded).count()
        count = avatars.count()

        if index == 0:
            prev = avatars.reverse()[0]
            if count <= 1:
                next = avatars[0]
            else:
                next = avatars[1]
        else:
            prev = avatars[index - 1]

        if (index + 1) >= count:
            next = avatars[0]
            prev_index = index - 1
            if prev_index < 0:
                prev_index = 0
            prev = avatars[prev_index]
        else:
            next = avatars[index + 1]

    return render(
        request, template_name, {
            "other_user": user,
            "avatar": avatar,
            "index": index + 1,
            "avatars": avatars,
            "next": next,
            "prev": prev,
            "count": count,
        })
Example #11
0
def avatar(request, username, id, template_name="avatar/avatar.html"):
    try:
        user = get_user(username)
    except get_user_model().DoesNotExist:
        raise Http404
    avatars = user.avatar_set.order_by("-date_uploaded")
    index = None
    avatar = None
    if avatars:
        avatar = avatars.get(pk=id)
        if not avatar:
            return Http404

        index = avatars.filter(date_uploaded__gt=avatar.date_uploaded).count()
        count = avatars.count()

        if index == 0:
            prev = avatars.reverse()[0]
            if count <= 1:
                next = avatars[0]
            else:
                next = avatars[1]
        else:
            prev = avatars[index - 1]

        if (index + 1) >= count:
            next = avatars[0]
            prev_index = index - 1
            if prev_index < 0:
                prev_index = 0
            prev = avatars[prev_index]
        else:
            next = avatars[index + 1]

    return render(request, template_name, {
        "other_user": user,
        "avatar": avatar,
        "index": index + 1,
        "avatars": avatars,
        "next": next,
        "prev": prev,
        "count": count,
    })
Example #12
0
def avatar(user, size=AVATAR_DEFAULT_SIZE, **kwargs):
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, size)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, size)
    context = dict(kwargs, **{
        'user': user,
        'url': url,
        'alt': alt,
        'size': size,
    })
    return render_to_string('avatar/avatar_tag.html', context)
Example #13
0
def avatar(user, size=AVATAR_DEFAULT_SIZE, **kwargs):
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, size)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, size)
    context = dict(kwargs, **{
        'user': user,
        'url': url,
        'alt': alt,
        'size': size,
    })
    return render_to_string('avatar/avatar_tag.html', context)
Example #14
0
def get_primary_avatar(user, size=settings.AVATAR_DEFAULT_SIZE):
    User = get_user_model()
    if not isinstance(user, User):
        try:
            user = get_user(user)
        except User.DoesNotExist:
            return None
    try:
        # Order by -primary first; this means if a primary=True avatar exists
        # it will be first, and then ordered by date uploaded, otherwise a
        # primary=False avatar will be first.  Exactly the fallback behavior we
        # want.
        avatar = user.avatar_set.order_by("-primary", "-date_uploaded")[0]
    except IndexError:
        avatar = None
    if avatar:
        if not avatar.thumbnail_exists(size):
            avatar.create_thumbnail(size)
    return avatar
Example #15
0
def user_avatar(user, size=avatar_settings.AVATAR_DEFAULT_SIZE, **kwargs):
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, size)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, size)
    context = dict(kwargs, **{
        'user': user,
        'url': url,
        'alt': alt,
        'size': size,
    })
    ret = jinja_env.get_template('avatar/avatar_tag.html').render(**context)
    return jinja2.Markup(ret)
Example #16
0
def avatar(user, width=settings.AVATAR_DEFAULT_SIZE, height = False, **kwargs):
    if height == False: height = width
    if not isinstance(user, get_user_model()):
        try:
            user = get_user(user)
            alt = six.text_type(user)
            url = avatar_url(user, width, height)
        except get_user_model().DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = six.text_type(user)
        url = avatar_url(user, width, height)
    context = dict(kwargs, **{
        'user': user,
        'url': url,
        'alt': alt,
        'width': width,
        'height': height
    })
    return render_to_string('avatar/avatar_tag.html', context)
Example #17
0
def avatar(user, size=AVATAR_DEFAULT_SIZE, template=None, **kwargs):
    if not isinstance(user, User):
        try:
            user = get_user(user)
            alt = unicode(user)
            url = avatar_url(user, size)
        except User.DoesNotExist:
            url = get_default_avatar_url()
            alt = _("Default Avatar")
    else:
        alt = unicode(user)
        url = avatar_url(user, size)
    context = dict(kwargs, **{
        'user': user,
        'url': url,
        'alt': alt,
        'size': size,
    })
    if template == 'img':
        return """<img src="%s" alt="%s" width="%s" height="%s" />""" % (
            url, alt, size, size)
    if not template:
        template = 'avatar/avatar_tag.html'
    return render_to_string(template, context)