def put(self, param1=None, param2=None, param3=None, param4=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return data["username"] = msg[1]["username"] ps = PostService() if param1 and not param2 and not param3: response = ps.edit_post(param1, data) self.write(json.dumps(response)) elif param1 and param2 and param3: response = ps.edit_reply(param3, data) self.write(json.dumps(response))
def post(self, param=None): if param is None: self.write(json.dumps({"status": 0, "message": "Access Denied"})) return elif param == "login": try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return us = UserService() result = us.login(data) self.write(json.dumps(result)) elif param == "signup": try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return us = UserService() result = us.register(data) self.write(json.dumps(result)) elif param == "changepwd": auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return us = UserService() result = us.change_password(data) self.write(json.dumps(result)) elif param == "forgotpwd": data = tornado.escape.json_decode(self.request.body) print(data) elif param == "schools": data = tornado.escape.json_decode(self.request.body) print(data)
def get(self, param1=None): try: cid = self.get_query_argument("cid") except: self.write(json.dumps({ "status": 0, "message": "course id needed" })) return auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) return data = {} data["username"] = msg[1]["username"] data["course_id"] = cid ps = PostService() if param1 is None: response = ps.get_questions(data) self.write(json.dumps(response)) else: response = ps.get_post(param1, data) self.write(json.dumps(response))
def delete(self, param1=None, param2=None, param3=None, param4=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) data = {} data["username"] = msg[1]["username"] ps = CourseService() # /course/{cid}/tag/{tag_name} if param1 is not None and param2 == "tag" and param3 is not None: response = ps.delete_tag(param1, param3, data) self.write(json.dumps(response))
def login(self, data): try: user = session.query(User).filter_by( username=data["username"]).one() except KeyError: return { "status": 0, "message": "Invalid JSON field", "courses": [] } except NoResultFound: return {"status": 0, "message": "No username found", "courses": []} except MultipleResultsFound: pass try: if user.password != self.hash_password(data["password"]): return { "status": 0, "message": "Wrong password", "courses": [] } except KeyError: return { "status": 0, "message": "Invalid JSON field", "courses": [] } # get all the course id associated with the user course_list = [] for uc in user.courses: c = uc.u_c d = {} d["course_id"] = c.id d["course_name"] = c.course_name d["course_title"] = c.course_title course_list.append(d) # generate Token a_u = AuthUtil() jwt_token = a_u.generateToken(user.username) return { "status": 1, "message": "Success", "courses": course_list, "token": jwt_token.decode("utf-8") }
def delete(self, param1=None, param2=None, param3=None, param4=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) data = {} data["username"] = msg[1]["username"] ps = PostService() if param1 and not param2 and not param3: response = ps.delete_post(param1, data) self.write(json.dumps(response)) elif param1 and param2 and param3: response = ps.delete_reply(param3, data) self.write(json.dumps(response))
def post(self, param1=None, param2=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return data["username"] = msg[1]["username"] cs = CourseService() # /course/{cid}/tag if param1 is not None and param2 == "tag": response = cs.create_tag(param1, data) self.write(json.dumps(response)) elif param1 is not None: try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return # Enroll for course # Create courses else: # Invalid request type print("Invalid Request")
def post(self, param1=None, param2=None, param3=None, param4=None): auth_header = self.request.headers.get('Authorization') au = AuthUtil() msg = au.checkToken(auth_header) if not msg[0]: self.write(msg[1]) try: data = tornado.escape.json_decode(self.request.body) except: self.write( json.dumps({ "status": 0, "message": "Invalid json format" })) return data["username"] = msg[1]["username"] ps = PostService() # posts/ if param1 is None and param2 is None and param3 is None \ and param4 is None: response = ps.create_post(data) self.write(json.dumps(response)) # posts/{pid}/answer elif param1 is not None and param2 == "answer" and param3 is None \ and param4 is None: response = ps.create_reply(param1, data) self.write(json.dumps(response)) # posts/{pid}/vote elif param2 == "vote" and param3 is None and param4 is None: response = ps.update_post_vote(param1, data) self.write(json.dumps(response)) # posts/{pid}/answer/{rid}/vote elif param2 == "answer" and param4 == "vote": response = ps.update_reply_vote(param1, data) self.write(json.dumps(response))