-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhousing.py
More file actions
63 lines (51 loc) · 2.23 KB
/
housing.py
File metadata and controls
63 lines (51 loc) · 2.23 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
from datetime import datetime
from conditional.models.models import InHousingQueue
from conditional.models.models import OnFloorStatusAssigned
from conditional.util.ldap import ldap_get_current_students, ldap_get_member, ldap_is_current_student
def get_housing_queue(is_eval_director=False):
# Generate a dictionary of dictionaries where the UID is the key
# and {'time': <datetime obj>} is the value. We are doing a left
# outer join on the two tables to get a single result that has
# both the member's UID and their on-floor datetime.
in_queue = {
entry.uid: {
'time': entry.onfloor_granted
} for entry in InHousingQueue.query.outerjoin(
OnFloorStatusAssigned,
OnFloorStatusAssigned.uid == InHousingQueue.uid
).with_entities(
InHousingQueue.uid,
OnFloorStatusAssigned.onfloor_granted
).all()
}
# CSHMember accounts that are in queue
potential_accounts = []
if is_eval_director:
potential_accounts = ldap_get_current_students()
else:
potential_accounts = [ldap_get_member(username) for username in in_queue]
potential_accounts = [user for user in potential_accounts if ldap_is_current_student(user)]
# Populate a list of dictionaries containing the name, username,
# and on-floor datetime for each current studetn who has on-floor status
# and is not already assigned to a room
queue = [
{
"uid": account.uid,
"name": account.cn,
"points": account.housingPoints,
"time": in_queue.get(account.uid, {}).get('time', datetime.now()) or datetime.now(),
"in_queue": account.uid in in_queue
} for account in potential_accounts
if is_eval_director or account.roomNumber is None
]
# Sort based on time (ascending) and then points (decending).
queue.sort(key=lambda m: m['time'])
queue.sort(key=lambda m: m['points'], reverse=True)
return queue
def get_queue_position(username):
queue = get_housing_queue()
try:
index = next(index for (index, d) in enumerate(queue) if d["uid"] == username) + 1
except (KeyError, StopIteration):
index = None
return index, len(queue)