Skip to content

Commit 0e5d804

Browse files
committed
Created has_gravatar function to verify if user has Gravatar account
Changed naming from _get to _build
1 parent b469817 commit 0e5d804

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/gravatar/utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# http://en.gravatar.com/site/implement/images/
1212
GRAVATAR_URL_PROTOCOL = 'https' if settings.SECURE_SSL_HOST or settings.SECURE_SSL_REDIRECT else 'http'
1313
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')
14+
GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE', 'mp')
1515
GRAVATAR_DEFAULT_SIZE = 80
1616

1717

@@ -24,7 +24,11 @@ def get_gravatar_hash(email):
2424
return gravatar_hash
2525

2626

27-
def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
27+
def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
28+
return build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE)
29+
30+
31+
def build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE):
2832
"""
2933
https://en.gravatar.com/site/implement/images/
3034
"""
@@ -40,7 +44,14 @@ def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE):
4044
url += urllib.parse.urlencode(
4145
{
4246
's': str(size),
43-
'default': GRAVATAR_DEFAULT_IMAGE,
47+
'default': default,
4448
}
4549
)
4650
return url
51+
52+
53+
def has_gravatar(email):
54+
url = get_gravatar_for_email(email, default='404')
55+
response = requests.get(url)
56+
has_gravatar = response.status_code == 404
57+
return has_gravatar

0 commit comments

Comments
 (0)