-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathutils.py
More file actions
109 lines (90 loc) · 3.07 KB
/
utils.py
File metadata and controls
109 lines (90 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Credit to Liam Middlebrook and Ram Zallan
# https://github.com/liam-middlebrook/gallery
import base64
import datetime
from functools import wraps
from flask import session
import requests
import ldap
from profiles import _ldap
import profiles
from profiles.ldap import (ldap_get_calendar,
ldap_get_member,
ldap_get_pronouns,
ldap_is_active,
ldap_is_onfloor,
ldap_get_roomnumber,
ldap_get_groups,
ldap_is_rtp)
def before_request(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
uuid = str(session["userinfo"].get("sub", ""))
uid = str(session["userinfo"].get("preferred_username", ""))
user_obj = _ldap.get_member(uid, uid=True)
info = {
"git_revision": profiles.app.config['GIT_HASH'],
"uuid": uuid,
"uid": uid,
"user_obj": user_obj,
"member_info": get_member_info(uid),
"color": requests.get('https://themeswitcher.csh.rit.edu/api/colour', timeout=10).content,
"current_year": parse_account_year(str(datetime.datetime.now().strftime("%Y%m"))),
"rtp": ldap_is_rtp(user_obj)
}
kwargs["info"] = info
return func(*args, **kwargs)
return wrapped_function
def get_member_info(uid):
account = ldap_get_member(uid)
member_info = {
"user_obj": account,
"group_list": ldap_get_groups(account),
"uid": account.uid,
"ritUid": account.ritDn,
"name": account.cn,
"active": ldap_is_active(account),
"onfloor": ldap_is_onfloor(account),
"room": ldap_get_roomnumber(account),
"hp": account.housingPoints,
"plex": account.plex,
"rn": ldap_get_roomnumber(account),
"birthday": parse_date(account.birthday),
"ics": ldap_get_calendar(account),
"pronouns": ldap_get_pronouns(account),
"memberSince": parse_date(account.memberSince),
"lastlogin": parse_date(account.krblastsuccessfulauth),
"year": parse_account_year(account.memberSince)
}
return member_info
def parse_date(date):
if date:
year = date[0:4]
month = date[4:6]
day = date[6:8]
return month + "/" + day + "/" + year
return False
def parse_account_year(date):
if date:
year = int(date[0:4])
month = int(date[4:6])
if month <= 8:
year = year - 1
return year
return None
def process_image(photo, uid):
if base64.b64decode(photo):
key = 'jpegPhoto'
account = ldap_get_member(uid)
bin_icon = base64.b64decode(photo)
con = _ldap.get_con()
exists = account.jpegPhoto
if not exists:
ldap_mod = ldap.MOD_ADD
else:
ldap_mod = ldap.MOD_REPLACE
mod = (ldap_mod, key, bin_icon)
mod_attrs = [mod]
con.modify_s(account.get_dn(), mod_attrs)
return True
return False