def post(self): data = self.parser.parse_args() if data["form_type"] == "add": # since put it will update regardless # TODO delete if PeopleModel.find_person(data['user']): return {'message': "This user already exists"}, 400 person = PeopleModel(data["user"]) try: person.save_to_db() except Exception as e: return { "message": "An error occurred while saving the item.", "error": "{}".format(e) }, 500 return redirect(url_for("home")) elif data["form_type"] == "del": person = PeopleModel.find_person(data['user']) if person: try: person.delete_from_db() except Exception as e: return { "message": "An error occurred while deleting", "error": "{}".format(e) }, 500 else: return {'message': "Doesn't exist."}, 400 return redirect(url_for("home"))
def put(self, name, **kwargs): data = People.parser.parse_args() person = PeopleModel.find_by_name(name) if person: person.role = data['role'] person.email = data['email'] else: person = PeopleModel(name, **data) person.save_to_db() return person.json(), 201
def post(self, name, **kwargs): if PeopleModel.find_by_name(name): return { 'message': f"A person with name '{name}' already exists" }, 400 data = People.parser.parse_args() person = PeopleModel(name, **data) try: person.save_to_db() except Exception as e: return {"message": f"An error occurred: {e}"}, 500 return person.json(), 201