-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (136 loc) · 4.55 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import json
import webapp2
import time
import model
def AsDict(message):
message = message.get()
print('\n Message As Dict\n')
print(message)
print(message.userKey)
user = message.userKey.get()
r = {'id': message.key.id(), 'username': user.username, 'name': user.name, 'msg': message.msg, 'time':str(message.time)}
# r.append({'id': 1234, 'username': 'Student Connect', 'name': 'Student Connect', 'msg': 'Hello', 'time':12323})
return r
def AsDictUser(user):
"""
course is a list in the user object
"""
course_list = model.QueryCourse(user.courses)
print('\n\n')
print(course_list)
print('\n\n')
course_out = []
for course in course_list:
course_out.append({'courseKey':course.key.id(), 'crn': course.crn, 'name':course.name})
print('\n\n')
print(course_out)
print('\n\n')
return {'id': user.key.id(), 'nameCheck': True, 'username': user.username, 'name': user.name, 'courses': course_out}
def AsDictCourse(course):
return {'courseID': course.key.id(), 'crn': course.crn, 'name': course.name}
class RestHandler(webapp2.RequestHandler):
def dispatch(self):
#time.sleep(1)
super(RestHandler, self).dispatch()
def SendJson(self, r):
self.response.headers['content-type'] = 'text/plain'
self.response.write(json.dumps(r))
class QueryCourseMessageHandler(RestHandler):
def post(self):
courses = json.loads(self.request.body)
r = {'courses':[]}
for course in courses:
print('\n')
print( course )
print('\n')
messages = model.CourseMessages(course['courseKey'])
print(messages)
if messages:
msg = [ AsDict(message) for message in messages]
course_dict = {'course': course['courseKey'], 'messages':msg}
print course_dict
r['courses'].append(course_dict)
self.SendJson(r)
class QueryHandler(RestHandler):
def get(self):
messages = model.AllMessages()
r = [ AsDict(message) for message in messages ]
self.SendJson(r)
class UpdateHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
message = model.UpdateMessage(r['id'], r['first'], r['last'], r['msg'])
r = AsDict(message)
self.SendJson(r)
class InsertMessageHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
course = model.QuerySingleCourse(int(r['courseKey']))
user = model.QueryUser(int(r['userKey']))
message = model.InsertMessage(user.key, r['msg'])
return_msg = model.AddMessageToCourse(course, message)
# r = AsDict(message)
msg = message.get()
time = str(msg.time)
r = {'msgID': msg.key.id(), 'time':time}
self.SendJson(r)
class InsertUserHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
course = model.AddCourseIfEmpty()
user = model.InsertUser(r['username'], r['name'], r['password'], course)
r = AsDictUser(user)
self.SendJson(r)
class DeleteHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
model.DeleteMessage(r['id'])
class UserQueryHandler(RestHandler):
def get(self):
users = model.AllUsers()
r = [ AsDictUser(user) for user in users ]
self.SendJson(r)
class UserSigninHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
print('\n\n')
print(r)
print('\n\n')
check = model.CheckUser(r['username'], r['password'])
print check
if check is not None:
check_key = check.key.id()
user = model.QueryUser(check_key)
r = AsDictUser(user)
else:
r = {'userCheck': False}
self.SendJson(r)
class UserIDQueryHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
print('\n\n')
print(r['user'])
print('\n\n')
user = model.QueryUser(int(r['user']))
r = AsDictUser(user)
self.SendJson(r)
class InsertCourseHandler(RestHandler):
def post(self):
r = json.loads(self.request.body)
course = model.InsertCourse(r['crn'], r['name'])
model.UpdateUser(r['id'], course)
r = AsDictCourse(course.get())
self.SendJson(r)
APP = webapp2.WSGIApplication([
('/rest/query', QueryHandler),
('/rest/insert-message', InsertMessageHandler),
('/rest/delete', DeleteHandler),
('/rest/update', UpdateHandler),
('/rest/message/<course>', QueryCourseMessageHandler),
('/rest/insert_user', InsertUserHandler),
('/rest/query-user', UserQueryHandler),
('/rest/user-signin', UserSigninHandler),
('/rest/query-user-id', UserIDQueryHandler),
('/rest/query-course-msg', QueryCourseMessageHandler),
('/rest/insert_course', InsertCourseHandler),
], debug=True)