Skip to content

Commit 0f5fc5d

Browse files
committed
Created has_gravatar function to verify if user has Gravatar account
Changed naming from _get to _build Changed response status_code to 200 Imported requests
1 parent b469817 commit 0f5fc5d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/gravatar/utils.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33

44
# Third Party (PyPI) Imports
5+
import requests
56
import six.moves.urllib as urllib
67

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

1718

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

2627

27-
def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
28+
def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
29+
return build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE)
30+
31+
32+
def build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
2833
"""
2934
https://en.gravatar.com/site/implement/images/
3035
"""
@@ -40,7 +45,14 @@ def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
4045
url += urllib.parse.urlencode(
4146
{
4247
's': str(size),
43-
'default': GRAVATAR_DEFAULT_IMAGE,
48+
'default': default,
4449
}
4550
)
4651
return url
52+
53+
54+
def has_gravatar(email):
55+
url = get_gravatar_for_email(email, default='404')
56+
response = requests.get(url)
57+
has_gravatar = response.status_code == 200
58+
return has_gravatar

0 commit comments

Comments
 (0)