def PostUsers(self): db = professorsDB() # 1. unpack the body(data) lenght = self.headers["Content-Length"] # read the body body = self.rfile.read(int(lenght)).decode("utf-8") # parse the body parsed_body = parse_qs(body) # Save professor's info firstName = parsed_body["firstName"][0] lastName = parsed_body["lastName"][0] email = parsed_body["email"][0] passwd = parsed_body["passwd"][0] user = db.login(email) if user != None: self.send_response(422) self.end_headers() else: hashpwd = bcrypt.hash(passwd) db.CreateUser(firstName, lastName, email, hashpwd) # self.session['userId'] = user['id'] # 2. respond to the client self.send_response(201) self.send_header("Content-Type", "application/json") self.end_headers()
def AddRating(self): parts = self.path.split("/") professor_id = parts[-1] lenght = self.headers["Content-Length"] # read the body body = self.rfile.read(int(lenght)).decode("utf-8") # parse the body parsed_body = parse_qs(body) course = parsed_body["course"][0] rating = parsed_body["rating"][0] difficulty = parsed_body["difficulty"][0] retake = parsed_body["retake"][0] textbook = parsed_body["textbook"][0] attendance = parsed_body["attendance"][0] grade = parsed_body["grade"][0] comment = parsed_body["comment"][0] profid = professor_id db_update = professorsDB() db_update.insertRating( course, rating, difficulty, retake, textbook, attendance, grade, comment, profid) self.send_response(201) self.send_header("Content-Type", "application/json") self.end_headers()
def UpdateProfessor(self): if not self.LoginSession(): self.handle401() return parts = self.path.split("/") professor_id = parts[2] db_update = professorsDB() professor = db_update.getOneProfessor(professor_id) if professor != None: lenght = self.headers["Content-Length"] # read the body body = self.rfile.read(int(lenght)).decode("utf-8") # parse the body parsed_body = parse_qs(body) f_name = parsed_body["f_name"][0] l_name = parsed_body["l_name"][0] dept = parsed_body["dept"][0] direct = parsed_body["direct"][0] prof_id = professor_id db_update.UpdateProfessor( f_name, l_name, dept, direct, prof_id) self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() else: self.Handle_Not_Found()
def GetRatings(self): self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() db = professorsDB() professors = db.getRatings() self.wfile.write(bytes(json.dumps(professors), "utf-8"))
def GetOneRating(self): parts = self.path.split("/") professor_id = parts[2] db = professorsDB() professor = db.getOneRating(professor_id) if professor != None: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(professor), "utf-8")) else: self.Handle_Not_Found()
def run(): db = professorsDB() db.createProfessorsTable() db.createRatingsTable() db.createUsersTable() db = None # disconnect port = 8080 if len(sys.argv) > 1: port = int(sys.argv[1]) listen = ("0.0.0.0", port) server = HTTPServer(listen, MyrequestHandler) print("Server listening on", "{}:{}".format(*listen)) server.serve_forever()
def AddProfessor(self): # 1. unpack the body(data) lenght = self.headers["Content-Length"] # read the body body = self.rfile.read(int(lenght)).decode("utf-8") # parse the body parsed_body = parse_qs(body) # Save professor's info first_name = parsed_body["first_name"][0] last_name = parsed_body["last_name"][0] department = parsed_body["department"][0] directory = parsed_body["directory"][0] db_post = professorsDB() db_post.insertProfessor( first_name, last_name, department, directory) # Print # 2. respond to the client self.send_response(201) self.send_header("Content-Type", "application/json") self.end_headers()
def login(self): lenght = self.headers["Content-Length"] body = self.rfile.read(int(lenght)).decode("utf-8") # parse the body parsed_body = parse_qs(body) # Save professor's info email = parsed_body["email"][0] passwd = parsed_body["passwd"][0] db = professorsDB() user = db.login(email) if user != None: if bcrypt.verify(passwd, user["passwd"]): self.session['userId'] = user['id'] self.send_response(201) self.send_header("Content-Type", "application/json") self.end_headers() print("User logged as: " + email) else: self.handle401() else: self.handle401()