Skip to content

Commit 01b6fc7

Browse files
committed
chom
1 parent a904044 commit 01b6fc7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

conditional/util/context_processors.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# pylint: disable=bare-except
22

3+
import hashlib
4+
import urllib
5+
36
from conditional import app
47
from conditional.models.models import FreshmanAccount
58
from conditional.util.cache import service_cache
@@ -31,11 +34,31 @@ def check_current_student(username):
3134
return ldap_is_current_student(member)
3235

3336

37+
@service_cache(maxsize=256)
38+
def get_rit_image(username: str) -> str:
39+
if username:
40+
addresses = [username + "@rit.edu", username + "@g.rit.edu"]
41+
for addr in addresses:
42+
url = (
43+
"https://gravatar.com/avatar/"
44+
+ hashlib.md5(addr.encode("utf8")).hexdigest()
45+
+ ".jpg?d=404&s=250"
46+
)
47+
try:
48+
with urllib.request.urlopen(url) as gravatar:
49+
if gravatar.getcode() == 200:
50+
return url
51+
except:
52+
continue
53+
return "https://www.gravatar.com/avatar/freshmen?d=mp&f=y"
54+
55+
3456
@app.context_processor
3557
def utility_processor():
3658
return {
3759
"get_csh_name": get_csh_name,
3860
"get_freshman_name": get_freshman_name,
61+
"get_rit_image": get_rit_image,
3962
"get_member_name": get_member_name,
4063
"check_current_student": check_current_student,
4164
}

0 commit comments

Comments
 (0)