NDOJ/judge/jinja2/gravatar.py
2023-08-24 00:02:02 -05:00

30 lines
855 B
Python

import hashlib
from django.contrib.auth.models import AbstractUser
from django.utils.http import urlencode
from judge.models import Profile
from judge.utils.unicode import utf8bytes
from . import registry
@registry.function
def gravatar(profile, size=80, default=None, profile_image=None, email=None):
if profile_image:
return profile_image
if profile and profile.profile_image:
return profile.profile_image.url
if profile:
email = email or profile.user.email
if default is None:
default = profile.mute
gravatar_url = (
"//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