Skip to content

Commit ee6fc41

Browse files
authored
packet glue (#404)
* migration, url fixes * chom
1 parent 736d203 commit ee6fc41

File tree

1 file changed

+94
-7
lines changed

1 file changed

+94
-7
lines changed

conditional/util/context_processors.py

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# pylint: disable=bare-except
22

3-
from conditional import app
4-
from conditional.models.models import FreshmanAccount
3+
import hashlib
4+
import urllib
5+
from functools import lru_cache
6+
from datetime import datetime
7+
from typing import Callable
8+
9+
from conditional import app, packet_bp
10+
from conditional.models.models import FreshmanAccount, Freshman, UpperSignature
511
from conditional.util.cache import service_cache
612
from conditional.util.ldap import ldap_get_member, ldap_is_current_student
713

@@ -34,8 +40,89 @@ def check_current_student(username):
3440
@app.context_processor
3541
def utility_processor():
3642
return {
37-
"get_csh_name": get_csh_name,
38-
"get_freshman_name": get_freshman_name,
39-
"get_member_name": get_member_name,
40-
"check_current_student": check_current_student
41-
}
43+
"get_csh_name": get_csh_name,
44+
"get_freshman_name": get_freshman_name,
45+
"get_member_name": get_member_name,
46+
"check_current_student": check_current_student,
47+
}
48+
49+
50+
# pylint: disable=bare-except
51+
@lru_cache(maxsize=128)
52+
def packet_get_csh_name(username: str) -> str:
53+
try:
54+
member = ldap_get_member(username)
55+
return member.cn + " (" + member.uid + ")"
56+
except:
57+
return username
58+
59+
60+
def get_roles(sig: UpperSignature) -> dict[str, str]:
61+
"""
62+
Converts a signature's role fields to a dict for ease of access.
63+
:return: A dictionary of role short names to role long names
64+
"""
65+
out = {}
66+
if sig.eboard:
67+
out["eboard"] = sig.eboard
68+
if sig.active_rtp:
69+
out["rtp"] = "RTP"
70+
if sig.three_da:
71+
out["three_da"] = "3DA"
72+
if sig.w_m:
73+
out["wm"] = "Wiki Maintainer"
74+
if sig.webmaster:
75+
out["webmaster"] = "Webmaster"
76+
if sig.c_m:
77+
out["cm"] = "Constitutional Maintainer"
78+
if sig.drink_admin:
79+
out["drink"] = "Drink Admin"
80+
return out
81+
82+
83+
# pylint: disable=bare-except
84+
@lru_cache(maxsize=256)
85+
def get_rit_name(username: str) -> str:
86+
try:
87+
freshman = Freshman.query.filter_by(rit_username=username).first()
88+
return freshman.name + " (" + username + ")"
89+
except:
90+
return username
91+
92+
93+
# pylint: disable=bare-except
94+
@lru_cache(maxsize=256)
95+
def get_rit_image(username: str) -> str:
96+
if username:
97+
addresses = [username + "@rit.edu", username + "@g.rit.edu"]
98+
for addr in addresses:
99+
url = (
100+
"https://gravatar.com/avatar/"
101+
+ hashlib.md5(addr.encode("utf8")).hexdigest()
102+
+ ".jpg?d=404&s=250"
103+
)
104+
try:
105+
with urllib.request.urlopen(url) as gravatar:
106+
if gravatar.getcode() == 200:
107+
return url
108+
except:
109+
continue
110+
return "https://www.gravatar.com/avatar/freshmen?d=mp&f=y"
111+
112+
113+
def log_time(label: str) -> None:
114+
"""
115+
Used during debugging to log timestamps while rendering templates
116+
"""
117+
print(label, datetime.now())
118+
119+
120+
@packet_bp.context_processor
121+
def packet_utility_processor() -> dict[str, Callable]:
122+
return {
123+
"get_csh_name": packet_get_csh_name,
124+
"get_rit_name": get_rit_name,
125+
"get_rit_image": get_rit_image,
126+
"log_time": log_time,
127+
"get_roles": get_roles,
128+
}

0 commit comments

Comments
 (0)