zulip/zerver/lib/avatar.py

40 lines
1.3 KiB
Python
Raw Normal View History

from __future__ import absolute_import
from django.conf import settings
if False:
from zerver.models import UserProfile
from typing import Text
from zerver.lib.avatar_hash import gravatar_hash, user_avatar_hash
from zerver.lib.upload import upload_backend, MEDIUM_AVATAR_SIZE
def avatar_url(user_profile, medium=False):
# type: (UserProfile, bool) -> Text
return get_avatar_url(
2017-01-24 07:06:13 +01:00
user_profile.avatar_source,
user_profile.email,
user_profile.avatar_version,
2017-01-24 07:06:13 +01:00
medium=medium)
def get_avatar_url(avatar_source, email, avatar_version, medium=False):
# type: (Text, Text, int, bool) -> Text
url = _get_unversioned_avatar_url(
avatar_source,
email,
medium)
url += '&version=%d' % (avatar_version,)
return url
def _get_unversioned_avatar_url(avatar_source, email, medium=False):
# type: (Text, Text, bool) -> Text
if avatar_source == u'U':
hash_key = user_avatar_hash(email)
return upload_backend.get_avatar_url(hash_key, medium=medium)
elif settings.ENABLE_GRAVATAR:
gravitar_query_suffix = "&s=%s" % (MEDIUM_AVATAR_SIZE,) if medium else ""
hash_key = gravatar_hash(email)
return u"https://secure.gravatar.com/avatar/%s?d=identicon%s" % (hash_key, gravitar_query_suffix)
else:
return settings.DEFAULT_AVATAR_URI+'?x=x'