forked from PrincetonCS-UCA/LabQueue
/
QueueManager.py
97 lines (91 loc) · 3.54 KB
/
QueueManager.py
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
import logging
from datetime import datetime, timedelta
import webapp2
from google.appengine.api import channel, users, memcache
from google.appengine.ext import ndb
from HelpRequest import HelpRequest, help_queue_key
from LabTA import LabTA, labta_key, is_ta, update_active_tas
import json
import ChannelManager
def get_json_queue():
q = HelpRequest.query(ancestor=help_queue_key())
q = q.filter(HelpRequest.been_helped == False)
q = q.filter(HelpRequest.canceled == False)
q = q.order(HelpRequest.request_datetime)
q.fetch()
queue = []
for e in q:
tmp = {}
tmp['name'] = e.name
tmp['email'] = e.netid
tmp['help_msg'] = e.help_msg
tmp['course'] = e.course
queue.append(tmp)
return json.dumps(queue)
class GetQueue(webapp2.RequestHandler):
# returns the current queue as a JSON object
# returns it on the requester's channel, NOT in response
# assume the channel was already created
def get(self):
user = users.get_current_user()
json_queue = get_json_queue()
channel.send_message(user.email(), json_queue)
class AddToQueue(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
q = HelpRequest.query(HelpRequest.in_queue == True,
HelpRequest.netid == user.email(),
ancestor=help_queue_key())
if q.count() != 0:
self.response.write('Unable to add to queue')
return
hr = HelpRequest(parent=help_queue_key())
hr.netid = user.email()
hr.name = self.request.get('name')
hr.help_msg = self.request.get('help_msg')
hr.course = self.request.get('course')
hr.put()
ChannelManager.queue_update()
class MarkAsHelped(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
q = HelpRequest.query(HelpRequest.in_queue == True,
HelpRequest.netid == self.request.get('email'),
ancestor=help_queue_key())
if q.count() != 1:
logging.error("Database corrupted for user {}".format(user.email()))
return
update_active_tas(user.email())
hr = q.get()
hr.been_helped = True
hr.helped_datetime = datetime.utcnow()
hr.attending_ta = user.email()
hr.put()
ChannelManager.queue_update()
ta = LabTA.query(LabTA.email == hr.attending_ta, ancestor=labta_key()).fetch()[0]
ChannelManager.notify_request_accepted(hr.netid, ta.first_name, ta.img_path)
class CancelFromQueue(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
q = HelpRequest.query(HelpRequest.in_queue == True,
HelpRequest.netid == self.request.get('email'),
ancestor=help_queue_key())
if q.count() != 1:
logging.error("Database corrupted for user {}".format(user.email()))
return
if is_ta(user.email()):
update_active_tas(user.email())
hr = q.get()
hr.canceled = True
hr.put()
ChannelManager.queue_update()
# Currently this is pretty insecure, theoretically any user could just
# be a dick and hit the URL...
class ClearQueue(webapp2.RequestHandler):
@ndb.toplevel
def post(self):
q = HelpRequest.query(HelpRequest.in_queue == True, ancestor=help_queue_key()).fetch()
for hr in q:
hr.canceled = True
hr.put()
ChannelManager.queue_update()