Add support for getting medium size profile images.

This commit is contained in:
hackerkid 2017-02-24 00:43:56 +05:30 committed by Tim Abbott
parent b0f53fd1a8
commit b7a6826fda
3 changed files with 20 additions and 4 deletions

View File

@ -385,6 +385,21 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
redirect_url = response['Location']
self.assertTrue(redirect_url.endswith(avatar_url(cordelia) + '&foo=bar'))
def test_get_user_avatar_medium(self):
# type: () -> None
self.login("hamlet@zulip.com")
cordelia = get_user_profile_by_email('cordelia@zulip.com')
cordelia.avatar_source = UserProfile.AVATAR_FROM_USER
cordelia.save()
response = self.client_get("/avatar/cordelia@zulip.com/medium?foo=bar")
redirect_url = response['Location']
self.assertTrue(redirect_url.endswith(avatar_url(cordelia, True) + '&foo=bar'))
response = self.client_get("/avatar/%s/medium?foo=bar" % (cordelia.id,))
redirect_url = response['Location']
self.assertTrue(redirect_url.endswith(avatar_url(cordelia, True) + '&foo=bar'))
def test_non_valid_user_avatar(self):
# type: () -> None

View File

@ -115,8 +115,8 @@ def update_user_backend(request, user_profile, email,
# TODO: Since eventually we want to support using the same email with
# different organizations, we'll eventually want this to be a
# logged-in endpoint so that we can access the realm_id.
def avatar(request, email_or_id):
# type: (HttpRequest, str) -> HttpResponse
def avatar(request, email_or_id, medium=None):
# type: (HttpRequest, str, bool) -> HttpResponse
"""Accepts an email address or user ID and returns the avatar"""
try:
int(email_or_id)
@ -128,13 +128,13 @@ def avatar(request, email_or_id):
try:
# If there is a valid user account passed in, use its avatar
user_profile = get_user_func(email_or_id)
url = avatar_url(user_profile)
url = avatar_url(user_profile, medium=medium)
except UserProfile.DoesNotExist:
# If there is no such user, treat it as a new gravatar
email = email_or_id
avatar_source = 'G'
avatar_version = 1
url = get_avatar_url(avatar_source, email, avatar_version)
url = get_avatar_url(avatar_source, email, avatar_version, medium=medium)
# We can rely on the url already having query parameters. Because
# our templates depend on being able to use the ampersand to

View File

@ -93,6 +93,7 @@ i18n_urls = [
{'template_name': 'zerver/reset_done.html'}),
# Avatar
url(r'^avatar/(?P<email_or_id>[\S]+)?/(?P<medium>[\S]+)?', zerver.views.users.avatar, name='zerver.views.users.avatar'),
url(r'^avatar/(?P<email_or_id>[\S]+)?', zerver.views.users.avatar, name='zerver.views.users.avatar'),
# Registration views, require a confirmation ID.