NDOJ/judge/jinja2/gravatar.py

30 lines
764 B
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
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(email, size=80, default=None):
if isinstance(email, Profile):
if default is None:
default = email.mute
email = email.user.email
elif isinstance(email, AbstractUser):
email = email.email
2022-05-14 17:57:27 +00:00
gravatar_url = (
"//www.gravatar.com/avatar/"
+ hashlib.md5(utf8bytes(email.strip().lower())).hexdigest()
+ "?"
)
args = {"d": "identicon", "s": str(size)}
2020-01-21 06:35:58 +00:00
if default:
2022-05-14 17:57:27 +00:00
args["f"] = "y"
2020-01-21 06:35:58 +00:00
gravatar_url += urlencode(args)
return gravatar_url