mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-11 08:43:10 +00:00
Ditch user_profile.get_avatar_url and ignore avatar_choice when locating avatar
Avatars are now sourced from the uploaded file if available, falling back on gravatar (if enabled and email address is non-empty), falling back on the default mystery-man icon. This restores the old behaviour of using gravatar by default for new users.
This commit is contained in:
parent
bcbfe226ee
commit
b25bf0c6bd
6 changed files with 59 additions and 34 deletions
|
|
@ -19,6 +19,7 @@ from wagtail.core.models import (
|
|||
CollectionViewRestriction, Page, PageViewRestriction, UserPagePermissionsProxy)
|
||||
from wagtail.core.utils import cautious_slugify as _cautious_slugify
|
||||
from wagtail.core.utils import camelcase_to_underscore, escape_script
|
||||
from wagtail.users.utils import get_gravatar_url
|
||||
from wagtail.utils.pagination import DEFAULT_PAGE_KEY, replace_page_in_query
|
||||
|
||||
register = template.Library()
|
||||
|
|
@ -374,14 +375,20 @@ def admin_urlquote(value):
|
|||
return quote(value)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def avatar_url(context, user, size=50):
|
||||
@register.simple_tag
|
||||
def avatar_url(user, size=50):
|
||||
"""
|
||||
A template tag that receives a user and size and return
|
||||
the appropiate avatar url for that user.
|
||||
Example usage: {% avatar_url request.user 50 %}
|
||||
"""
|
||||
|
||||
if hasattr(user, 'wagtail_userprofile'): # A user could not have profile yet, so this is necessary
|
||||
return user.wagtail_userprofile.get_avatar_url(size=size)
|
||||
if hasattr(user, 'wagtail_userprofile') and user.wagtail_userprofile.avatar:
|
||||
return user.wagtail_userprofile.avatar.url
|
||||
|
||||
if hasattr(user, 'email'):
|
||||
gravatar_url = get_gravatar_url(user.email, size=size)
|
||||
if gravatar_url is not None:
|
||||
return gravatar_url
|
||||
|
||||
return static('wagtailadmin/images/default-user-avatar.png')
|
||||
|
|
|
|||
|
|
@ -515,11 +515,10 @@ class TestAvatarSection(TestCase, WagtailTestUtils):
|
|||
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk))
|
||||
self.assertEqual('default', profile.avatar_choice)
|
||||
|
||||
def test_get_avatar_returns_default_if_not_changed(self):
|
||||
def test_avatar_choice_returns_default_if_not_changed(self):
|
||||
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk))
|
||||
|
||||
self.assertEqual(profile.avatar_choice, 'default')
|
||||
self.assertIn('default-user-avatar', profile.get_avatar_url())
|
||||
|
||||
def test_set_gravatar_returns_gravatar(self):
|
||||
post_data = {
|
||||
|
|
@ -530,7 +529,6 @@ class TestAvatarSection(TestCase, WagtailTestUtils):
|
|||
|
||||
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk))
|
||||
self.assertEqual(profile.avatar_choice, 'gravatar')
|
||||
self.assertIn('www.gravatar.com', profile.get_avatar_url())
|
||||
|
||||
@override_settings(MEDIA_ROOT=TMP_MEDIA_ROOT)
|
||||
def test_set_custom_avatar_stores_and_get_custom_avatar(self):
|
||||
|
|
@ -543,7 +541,7 @@ class TestAvatarSection(TestCase, WagtailTestUtils):
|
|||
|
||||
profile = UserProfile.get_for_user(get_user_model().objects.get(pk=self.user.pk))
|
||||
self.assertEqual('custom', profile.avatar_choice)
|
||||
self.assertIn(os.path.basename(self.avatar.name), profile.get_avatar_url())
|
||||
self.assertIn(os.path.basename(self.avatar.name), profile.avatar.url)
|
||||
|
||||
@override_settings(MEDIA_ROOT=TMP_MEDIA_ROOT)
|
||||
def test_user_upload_another_image_removes_previous_one(self):
|
||||
|
|
|
|||
44
wagtail/admin/tests/test_templatetags.py
Normal file
44
wagtail/admin/tests/test_templatetags.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from wagtail.admin.templatetags.wagtailadmin_tags import avatar_url
|
||||
from wagtail.images.tests.utils import get_test_image_file
|
||||
from wagtail.users.models import UserProfile
|
||||
|
||||
|
||||
class TestAvatarTemplateTag(TestCase):
|
||||
def setUp(self):
|
||||
# Create a user
|
||||
self.test_user = get_user_model().objects.create_user(
|
||||
username='testuser',
|
||||
email='testuser@email.com',
|
||||
password='password',
|
||||
)
|
||||
|
||||
def test_use_gravatar_by_default(self):
|
||||
url = avatar_url(self.test_user)
|
||||
self.assertIn('www.gravatar.com', url)
|
||||
|
||||
def test_skip_gravatar_if_no_email(self):
|
||||
self.test_user.email = ''
|
||||
url = avatar_url(self.test_user)
|
||||
self.assertIn('default-user-avatar', url)
|
||||
|
||||
@override_settings(WAGTAIL_GRAVATAR_PROVIDER_URL='https://robohash.org')
|
||||
def test_custom_gravatar_provider(self):
|
||||
url = avatar_url(self.test_user)
|
||||
self.assertIn('robohash.org', url)
|
||||
|
||||
@override_settings(WAGTAIL_GRAVATAR_PROVIDER_URL=None)
|
||||
def test_disable_gravatar(self):
|
||||
url = avatar_url(self.test_user)
|
||||
self.assertIn('default-user-avatar', url)
|
||||
|
||||
def test_uploaded_avatar(self):
|
||||
user_profile = UserProfile.get_for_user(self.test_user)
|
||||
user_profile.avatar = get_test_image_file(filename='custom-avatar.png')
|
||||
user_profile.save()
|
||||
|
||||
url = avatar_url(self.test_user)
|
||||
self.assertIn('custom-avatar', url)
|
||||
|
|
@ -7,8 +7,6 @@ from django.db import models
|
|||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from wagtail.users.utils import get_gravatar_url
|
||||
|
||||
|
||||
def upload_avatar_to(instance, filename):
|
||||
filename, ext = os.path.splitext(filename)
|
||||
|
|
@ -95,21 +93,6 @@ class UserProfile(models.Model):
|
|||
def default_avatar(self):
|
||||
return static('wagtailadmin/images/default-user-avatar.png')
|
||||
|
||||
def get_avatar_url(self, size=50):
|
||||
if self.avatar_choice == self.DEFAULT:
|
||||
return self.default_avatar
|
||||
|
||||
if self.avatar_choice == self.CUSTOM:
|
||||
try:
|
||||
return self.avatar.url
|
||||
except ValueError:
|
||||
return self.default_avatar
|
||||
|
||||
if self.avatar_choice == self.GRAVATAR and self.user.email:
|
||||
return get_gravatar_url(self.user.email, size=50)
|
||||
|
||||
return self.default_avatar
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.avatar:
|
||||
this = UserProfile.objects.get(pk=self.pk)
|
||||
|
|
|
|||
|
|
@ -949,12 +949,6 @@ class TestUserProfileCreation(TestCase, WagtailTestUtils):
|
|||
def test_get_avatar_url_default(self):
|
||||
user_profile = UserProfile.get_for_user(self.test_user)
|
||||
self.assertEqual(user_profile.avatar_choice, 'default')
|
||||
self.assertIn('default-user-avatar', user_profile.get_avatar_url())
|
||||
|
||||
def test_get_avatar_url_fallback_default(self):
|
||||
user_profile = UserProfile.get_for_user(self.test_user)
|
||||
user_profile.avatar_choice = 'Non-existent'
|
||||
self.assertIn('default-user-avatar', user_profile.get_avatar_url())
|
||||
|
||||
|
||||
class TestUserEditViewForNonSuperuser(TestCase, WagtailTestUtils):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import hashlib
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.templatetags.staticfiles import static
|
||||
from django.utils.http import urlencode
|
||||
|
||||
from wagtail.core.compat import AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME
|
||||
|
|
@ -28,8 +27,8 @@ def get_gravatar_url(email, size=50):
|
|||
size = int(size) * 2 # requested at retina size by default and scaled down at point of use with css
|
||||
gravatar_provider_url = getattr(settings, 'WAGTAIL_GRAVATAR_PROVIDER_URL', '//www.gravatar.com/avatar')
|
||||
|
||||
if gravatar_provider_url is None:
|
||||
return static('wagtailadmin/images/default-user-avatar.png')
|
||||
if (not email) or (gravatar_provider_url is None):
|
||||
return None
|
||||
|
||||
gravatar_url = "{gravatar_provider_url}/{hash}?{params}".format(
|
||||
gravatar_provider_url=gravatar_provider_url.rstrip('/'),
|
||||
|
|
|
|||
Loading…
Reference in a new issue