def post(self): # Acquire all of the data in a dict of each argument defined in the parser above. data = DinnerRegistrar.parser.parse_args() if ProfessorModel.find_by_id(data["professorID"]) is None: return { "Message": "Dinner could not be created as no such professor could be found with id {}." .format(data["professorID"]) }, 404, { "Access-Control-Allow-Origin": "*" } if data["userID"]: if data["userID"] == -1: # Create a new ProfessorModel object containing the passed properties. newDinner = DinnerModel(**( data.pop("userID") )) ## ** automatically separates dict keywords into arguments # Iterate the amount of dinners for this professor by one associatedProfessor = ProfessorModel.find_by_id( data["professorID"]) associatedProfessor.dinnerCount += 1 associatedProfessor.save_to_db() # Save the new professor to the database. newDinner.save_to_db() return newDinner.json(), 201, { "Access-Control-Allow-Origin": "*" } elif not UserModel.find_by_id(data["userID"]): return { "Message": "There is no user in the database with that ID. Could not create dinner" }, 404, { "Access-Control-Allow-Origin": "*" } else: user = UserModel.find_by_id(data["userID"]) user.dinnerCount += 1 user.semDinnerCount += 1 user.save_to_db() # Create a new ProfessorModel object containing the passed properties. newDinner = DinnerModel( **data) ## ** automatically separates dict keywords into arguments # Iterate the amount of dinners for this professor by one associatedProfessor = ProfessorModel.find_by_id(data["professorID"]) associatedProfessor.dinnerCount += 1 associatedProfessor.save_to_db() # Save the new professor to the database. newDinner.save_to_db() return newDinner.json(), 201, {"Access-Control-Allow-Origin": "*"}
def get(self, uniqueID): professorOfInterest = ProfessorModel.find_by_id(uniqueID) if (professorOfInterest): return professorOfInterest.json(), { "Access-Control-Allow-Origin": "*" } return { "Message": "No professor could be found with that ID" }, 200, { "Access-Control-Allow-Origin": "*" }
def delete(self, uniqueID): if (ProfessorModel.find_by_id(uniqueID)): if DinnerModel.return_by_professorID(uniqueID): return { "Message": "Professor with id {} is assigned to dinner(s) and cannot be deleted. Sorry bby <3 <3 <3" .format(uniqueID) }, 400, { "Access-Control-Allow-Origin": "*" } ProfessorModel.find_by_id(uniqueID).delete_from_db() return { "Message": "Professor with id {} deleted.".format(uniqueID) }, 200, { "Access-Control-Allow-Origin": "*" } return { "Message": "No professor with {} found.".format(uniqueID) }, 200, { "Access-Control-Allow-Origin": "*" }
def put(self, uniqueID): data = ProfessorResource.parser.parse_args() if (ProfessorModel.find_by_id(uniqueID)): professorOfInterest = ProfessorModel.find_by_id(uniqueID) professorOfInterest.firstName = data["firstName"] professorOfInterest.lastName = data["lastName"] professorOfInterest.genderPronouns = data["genderPronouns"] professorOfInterest.department = data["department"] professorOfInterest.title = data["title"] professorOfInterest.school = data["school"] professorOfInterest.email = data["email"] else: professorOfInterest = ProfessorModel(uniqueID=uniqueID, **data) professorOfInterest.save_to_db() return ProfessorModel.find_by_id(uniqueID).json(), 200, { "Access-Control-Allow-Origin": "*" }
def post(self): # Acquire all of the data in a dict of each argument defined in the parser above. data = ProfessorRegistrar.parser.parse_args() # Error trapping to see if a professor already exists with that particular idea if (ProfessorModel.find_by_id(data.uniqueID)): return { "Message": "A professor with that Unique ID already exists" }, 400, { "Access-Control-Allow-Origin": "*" } # Create a new ProfessorModel object containing the passed properties. newProf = ProfessorModel( **data) ## ** automatically separates dict keywords into arguments # Save the new professor to the database. newProf.save_to_db() return ProfessorModel.return_last_item().json(), 200, { "Access-Control-Allow-Origin": "*" }
def get(self): return ProfessorModel.return_all_professors(), 200, { "Access-Control-Allow-Origin": "*" }
def put(self, id): data = DinnerResource.parser.parse_args() if (DinnerModel.find_by_id(id)): dinnerOfInterest = DinnerModel.find_by_id(id) if not ProfessorModel.find_by_id(data["professorID"]): return { "Message": "There is no professor in the database with that ID" }, 404, { "Access-Control-Allow-Origin": "*" } if data["userID"]: if data["userID"] == -1: if UserModel.find_by_id(dinnerOfInterest.userID): user = UserModel.find_by_id(dinnerOfInterest.userID) user.dinnerCount -= 1 user.semDinnerCount -= 1 user.save_to_db() dinnerOfInterest.userID = None elif not UserModel.find_by_id(data["userID"]): return { "Message": "There is no user in the database with that ID" }, 404, { "Access-Control-Allow-Origin": "*" } elif UserModel.find_by_id(data["userID"]): dinnerOfInterest.userID = data["userID"] dinnerOfInterest.timeStamp = data["timeStamp"] dinnerOfInterest.topic = data["topic"] dinnerOfInterest.description = data["description"] dinnerOfInterest.studentLimit = data["studentLimit"] dinnerOfInterest.address = data["address"] dinnerOfInterest.dietaryRestrictions = data["dietaryRestrictions"] if ProfessorModel.find_by_id(dinnerOfInterest.professorID): professor = ProfessorModel.find_by_id( dinnerOfInterest.professorID) professor.dinnerCount -= 1 professor.save_to_db() dinnerOfInterest.professorID = data["professorID"] dinnerOfInterest.catering = data["catering"] dinnerOfInterest.transportation = data["transportation"] dinnerOfInterest.invitationSentTimeStamp = data[ "invitationSentTimeStamp"] # Assign new userID else: if not ProfessorModel.find_by_id(data["professorID"]): return { "Message": "There is no professor in the database with that ID" }, 404, { "Access-Control-Allow-Origin": "*" } if not UserModel.find_by_id( data["userID"]) or data["userID"] != -1: return { "Message": "There is no user in the database with that ID" }, 404, { "Access-Control-Allow-Origin": "*" } dinnerOfInterest = DinnerModel(id=id, **data) # If the dinner gains a userID, but is not completely done "not 2", then update the status to 1, which # means it is claimed but does not have a user yet. if dinnerOfInterest.userID and dinnerOfInterest.status is not 2: dinnerOfInterest.status = 1 dinnerOfInterest.save_to_db() # increase the number of dinners for this new userID if data["userID"] and UserModel.find_by_id(data["userID"]): user = UserModel.find_by_id(data["userID"]) user.dinnerCount += 1 user.semDinnerCount += 1 user.save_to_db() professor = ProfessorModel.find_by_id(data["professorID"]) professor.dinnerCount += 1 professor.save_to_db() return dinnerOfInterest.json(), 200, { "Access-Control-Allow-Origin": "*" }