mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-03-16 22:20:30 +00:00
Do significantly fewer SQL queries here.
This commit is contained in:
parent
4180a7a101
commit
c57a7499fb
1 changed files with 8 additions and 8 deletions
|
|
@ -23,15 +23,15 @@ def get_primary_avatar(user, size=80):
|
|||
user = User.objects.get(username=user)
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
avatars = user.avatar_set.order_by('-date_uploaded')
|
||||
primary = avatars.filter(primary=True)
|
||||
if primary.count() > 0:
|
||||
avatar = primary[0]
|
||||
elif avatars.count() > 0:
|
||||
avatar = avatars[0]
|
||||
else:
|
||||
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
|
||||
return avatar
|
||||
|
|
|
|||
Loading…
Reference in a new issue