def delete(self, id): person = Person.query.filter_by(id=id).first() if not person: raise NotFoundError("Person") result = deserialize_request(relationship_item_schema, request.json) relation_type = result["relation_type"] if relation_type == "child": parent = person child = request._deserialized["person_id_for_relation_type"] elif relation_type == "parent": child = person parent = request._deserialized["person_id_for_relation_type"] try: parent.children.remove(child) except ValueError: return make_response( 404, error="person_id_for_relation_type is not a valid relation") db.session.add(parent) db.session.add(child) db.session.commit() return make_response(200)
def get(self, id): person = Person.query.filter_by(id=id).first() if not person: raise NotFoundError("Person") data = person_item_schema.dump(person) return make_response(200, data=data)
def post(self): person = person_item_schema.load(request.json, session=db.session) db.session.add(person) db.session.commit() # TODO generate url dynamically (blueprint is throwing it in /api namespace) headers = {"Location": f"/api/v1/person/{person.id}"} data = person_item_schema.dump(person) return make_response(201, headers=headers, data=data)
def delete(self, id): person = Person.query.filter_by(id=id).first() if not person: raise NotFoundError("Person") db.session.delete(person) db.session.commit() return make_response(200, data={})
def put(self, id): person = Person.query.filter_by(id=id).first() if not person: raise NotFoundError("Person") person.patch(request.json) db.session.add(person) db.session.commit() data = person_item_schema.dump(person) return make_response(200, data=data)
def post(self, id): person = Person.query.filter_by(id=id).first() if not person: raise NotFoundError("Person") result = deserialize_request(relationship_item_schema, request.json) relation_type = result["relation_type"] if relation_type == "child": parent = person child = request._deserialized["person_id_for_relation_type"] elif relation_type == "parent": child = person parent = request._deserialized["person_id_for_relation_type"] parent.children.append(child) db.session.add(parent) db.session.add(child) db.session.commit() return make_response(201)
def get(self): all_people = Person.query.all() # TODO pagination data = person_list_schema.dump(all_people) return make_response(200, data=data)
def get(self): return make_response(405)