forked from jazzband/django-avatar
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Cleanup, reorganization, moved the default avatar url to conform to c…
…ommunity standards, and added a test to ensure that it's correct.
- Loading branch information
Showing
6 changed files
with
63 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.conf import settings | ||
|
||
from django.contrib.auth.models import User | ||
|
||
from avatar import AVATAR_DEFAULT_URL | ||
|
||
def get_default_avatar_url(): | ||
base_url = getattr(settings, 'STATIC_URL', None) | ||
if not base_url: | ||
base_url = getattr(settings, 'MEDIA_URL', '') | ||
# We'll be nice and make sure there are no duplicated forward slashes | ||
ends = base_url.endswith('/') | ||
begins = AVATAR_DEFAULT_URL.startswith('/') | ||
if ends and begins: | ||
base_url = base_url[:-1] | ||
elif not ends and not begins: | ||
return '%s/%s' % (base_url, AVATAR_DEFAULT_URL) | ||
return '%s%s' % (base_url, AVATAR_DEFAULT_URL) | ||
|
||
def get_primary_avatar(user, size=80): | ||
if not isinstance(user, User): | ||
try: | ||
user = User.objects.get(username=user) | ||
except User.DoesNotExist: | ||
return get_default_avatar_url() | ||
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: | ||
avatar = None | ||
if avatar: | ||
if not avatar.thumbnail_exists(size): | ||
avatar.create_thumbnail(size) | ||
return avatar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters