NDOJ/judge/jinja2/gravatar.py

31 lines
858 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
2023-08-24 04:14:53 +00:00
def gravatar(profile, size=80, default=None, profile_image=None, email=None):
2023-08-24 03:14:09 +00:00
if profile_image:
2023-08-24 04:14:53 +00:00
return profile_image
2023-10-11 02:01:06 +00:00
if profile and profile.profile_image_url:
return profile.profile_image_url
2023-08-24 05:02:02 +00:00
if profile:
2023-10-11 00:37:36 +00:00
email = email or profile.email
2023-08-24 05:02:02 +00:00
if default is None:
2023-10-11 00:37:36 +00:00
default = profile.is_muted
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