Skip to content
Merged

chom #406

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions conditional/util/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# pylint: disable=bare-except

import hashlib
import urllib

from conditional import app
from conditional.models.models import FreshmanAccount
from conditional.util.cache import service_cache
Expand Down Expand Up @@ -31,11 +34,31 @@ def check_current_student(username):
return ldap_is_current_student(member)


@service_cache(maxsize=256)
def get_rit_image(username: str) -> str:
if username:
addresses = [username + "@rit.edu", username + "@g.rit.edu"]
for addr in addresses:
url = (
"https://gravatar.com/avatar/"
+ hashlib.md5(addr.encode("utf8")).hexdigest()
+ ".jpg?d=404&s=250"
)
try:
with urllib.request.urlopen(url) as gravatar:
if gravatar.getcode() == 200:
return url
except:
continue
return "https://www.gravatar.com/avatar/freshmen?d=mp&f=y"


@app.context_processor
def utility_processor():
return {
"get_csh_name": get_csh_name,
"get_freshman_name": get_freshman_name,
"get_rit_image": get_rit_image,
"get_member_name": get_member_name,
"check_current_student": check_current_student,
}