From b7a6826fda1f296ae59881618de75653b7566d02 Mon Sep 17 00:00:00 2001 From: hackerkid Date: Fri, 24 Feb 2017 00:43:56 +0530 Subject: [PATCH] Add support for getting medium size profile images. --- zerver/tests/test_upload.py | 15 +++++++++++++++ zerver/views/users.py | 8 ++++---- zproject/urls.py | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index 4142aa1a07..babb4d7f7d 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -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 diff --git a/zerver/views/users.py b/zerver/views/users.py index 7af1e5858f..bdf1dd49e9 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -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 diff --git a/zproject/urls.py b/zproject/urls.py index d9c750124f..6525f348db 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -93,6 +93,7 @@ i18n_urls = [ {'template_name': 'zerver/reset_done.html'}), # Avatar + url(r'^avatar/(?P[\S]+)?/(?P[\S]+)?', zerver.views.users.avatar, name='zerver.views.users.avatar'), url(r'^avatar/(?P[\S]+)?', zerver.views.users.avatar, name='zerver.views.users.avatar'), # Registration views, require a confirmation ID.