mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-05-22 06:11:53 +00:00
Simplified the has_avatar filter.
This commit is contained in:
parent
7b0d9b4a9a
commit
66ab58631d
1 changed files with 10 additions and 12 deletions
|
|
@ -32,6 +32,7 @@ def avatar_url(user, size=AVATAR_DEFAULT_SIZE):
|
|||
else:
|
||||
return get_default_avatar_url()
|
||||
|
||||
|
||||
@cache_result
|
||||
@register.simple_tag
|
||||
def avatar(user, size=AVATAR_DEFAULT_SIZE):
|
||||
|
|
@ -48,18 +49,13 @@ def avatar(user, size=AVATAR_DEFAULT_SIZE):
|
|||
url = avatar_url(user, size)
|
||||
return """<img src="%s" alt="%s" width="%s" height="%s" />""" % (url, alt,
|
||||
size, size)
|
||||
|
||||
|
||||
|
||||
|
||||
@register.filter
|
||||
def has_avatar(user):
|
||||
if not isinstance(user, User):
|
||||
return False
|
||||
else:
|
||||
avatar = Avatar.objects.filter(user=user, primary=True)
|
||||
if avatar:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return Avatar.objects.filter(user=user, primary=True).exists()
|
||||
|
||||
|
||||
@cache_result
|
||||
|
|
@ -67,15 +63,16 @@ def has_avatar(user):
|
|||
def primary_avatar(user, size=AVATAR_DEFAULT_SIZE):
|
||||
"""
|
||||
This tag tries to get the default avatar for a user without doing any db
|
||||
requests. It achieve this by linking to a special view that will do all the
|
||||
requests. It achieve this by linking to a special view that will do all the
|
||||
work for us. If that special view is then cached by a CDN for instance,
|
||||
we will avoid many db calls.
|
||||
"""
|
||||
alt = unicode(user)
|
||||
url = reverse('avatar_render_primary', kwargs={'user' : user, 'size' : size})
|
||||
url = reverse('avatar_render_primary', kwargs={'user': user, 'size': size})
|
||||
return """<img src="%s" alt="%s" width="%s" height="%s" />""" % (url, alt,
|
||||
size, size)
|
||||
|
||||
|
||||
@cache_result
|
||||
@register.simple_tag
|
||||
def render_avatar(avatar, size=AVATAR_DEFAULT_SIZE):
|
||||
|
|
@ -92,11 +89,12 @@ def primary_avatar_object(parser, token):
|
|||
else:
|
||||
raise template.TemplateSyntaxError('%r tag takes three arguments.' % split[0])
|
||||
|
||||
|
||||
class UsersAvatarObjectNode(template.Node):
|
||||
def __init__(self, user, key):
|
||||
self.user = template.Variable(user)
|
||||
self.key = key
|
||||
|
||||
self.key = key
|
||||
|
||||
def render(self, context):
|
||||
user = self.user.resolve(context)
|
||||
key = self.key
|
||||
|
|
|
|||
Loading…
Reference in a new issue