mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-03-16 22:20:30 +00:00
Merge branch 'master' of git://github.com/alex/django-avatar
This commit is contained in:
commit
92f9794701
2 changed files with 13 additions and 10 deletions
|
|
@ -26,6 +26,7 @@ from avatar import AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD, \
|
|||
AVATAR_HASH_USERDIRNAMES, AVATAR_HASH_FILENAMES, \
|
||||
AVATAR_THUMB_QUALITY
|
||||
|
||||
|
||||
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
|
||||
tmppath = [AVATAR_STORAGE_DIR]
|
||||
if AVATAR_HASH_USERDIRNAMES:
|
||||
|
|
@ -71,15 +72,17 @@ class Avatar(models.Model):
|
|||
def __unicode__(self):
|
||||
return _(u'Avatar for %s') % self.user
|
||||
|
||||
def save(self, force_insert=False, force_update=False):
|
||||
avatars = Avatar.objects.filter(user=self.user).exclude(id=self.id)
|
||||
def save(self, *args, **kwargs):
|
||||
avatars = Avatar.objects.filter(user=self.user)
|
||||
if self.pk:
|
||||
avatars = avatars.exclude(pk=self.pk)
|
||||
if AVATAR_MAX_AVATARS_PER_USER > 1:
|
||||
if self.primary:
|
||||
avatars = avatars.filter(primary=True)
|
||||
avatars.update(primary=False)
|
||||
else:
|
||||
avatars.delete()
|
||||
super(Avatar, self).save(force_insert, force_update)
|
||||
super(Avatar, self).save(*args, **kwargs)
|
||||
|
||||
def thumbnail_exists(self, size):
|
||||
return self.avatar.storage.exists(self.avatar_name(size))
|
||||
|
|
|
|||
|
|
@ -26,13 +26,13 @@ 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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue