Skip to content

Commit

Permalink
Created has_gravatar function to verify if user has Gravatar account
Browse files Browse the repository at this point in the history
Changed naming from _get to _build
Changed response status_code to 200
Imported requests
  • Loading branch information
lleung030 committed Dec 6, 2023
1 parent b469817 commit 0f5fc5d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/gravatar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib

# Third Party (PyPI) Imports
import requests
import six.moves.urllib as urllib

# Django Imports
Expand All @@ -11,7 +12,7 @@
# http://en.gravatar.com/site/implement/images/
GRAVATAR_URL_PROTOCOL = 'https' if settings.SECURE_SSL_HOST or settings.SECURE_SSL_REDIRECT else 'http'
GRAVATAR_URL_PREFIX = getattr(settings, 'GRAVATAR_URL_PREFIX', '%s://%s' % (GRAVATAR_URL_PROTOCOL, 'www.gravatar.com',))
GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE', 'mm')
GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE', 'mp')
GRAVATAR_DEFAULT_SIZE = 80


Expand All @@ -24,7 +25,11 @@ def get_gravatar_hash(email):
return gravatar_hash


def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
return build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE)


def build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
"""
https://en.gravatar.com/site/implement/images/
"""
Expand All @@ -40,7 +45,14 @@ def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
url += urllib.parse.urlencode(
{
's': str(size),
'default': GRAVATAR_DEFAULT_IMAGE,
'default': default,
}
)
return url


def has_gravatar(email):
url = get_gravatar_for_email(email, default='404')
response = requests.get(url)
has_gravatar = response.status_code == 200
return has_gravatar

0 comments on commit 0f5fc5d

Please sign in to comment.