Skip to content

Commit

Permalink
Merge pull request #12 from CHT-OJ/gravatar
Browse files Browse the repository at this point in the history
fix profile not match when create new user
  • Loading branch information
iotran207 authored Aug 24, 2024
2 parents c9e657b + 4b37ed8 commit b7ca3ee
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions judge/jinja2/gravatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ def gravatar(email, size=80, default=None):
elif isinstance(email, AbstractUser):
user_id = email.id
email = email.email

if not Profile.objects.get(id=user_id).avt_url:
gravatar_url = 'https://www.gravatar.com/avatar/' + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() + '?'
args = {'d': 'identicon', 's': str(size)}
if default:
args['f'] = 'y'

gravatar_url += urlencode(args)
return gravatar_url
else:
return f"/avatar{Profile.objects.get(id=user_id).avt_url.thumbnail[f'{size}x{size}']}"
try:
prof = Profile.objects.get(id=user_id)
if not prof.avt_url:
return fallback(email, size, default)
else:
return f"/avatar{prof.avt_url.thumbnail[f'{size}x{size}']}"
except Profile.DoesNotExist:
return fallback(email, size, default)

def fallback(email, size, default):
gravatar_url = 'https://www.gravatar.com/avatar/' + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() + '?'
args = {'d': 'identicon', 's': str(size)}
if default:
args['f'] = 'y'

gravatar_url += urlencode(args)
return gravatar_url

0 comments on commit b7ca3ee

Please sign in to comment.