def delete(self, id): hacknight = Hacknight.query.get_or_404(id) participants = { participant.id for participant in hacknight.participants } json_data = request.get_json(force=True) ids_schema = Schema.from_dict( {"participants_ids": fields.List(fields.Int())}) try: data = ids_schema().load(json_data) except ValidationError as err: return err.messages, HTTPStatus.UNPROCESSABLE_ENTITY to_remove = participants.intersection(set(data["participants_ids"])) if not to_remove: return { "message": "No participant to delete" }, HTTPStatus.BAD_REQUEST hacknight.participants = [ participant for participant in hacknight.participants if participant.id not in to_remove ] db.session.commit() hacknight_schema = HacknightSchema() return hacknight_schema.dump(hacknight), HTTPStatus.OK
def post(self, id): hacknight = Hacknight.query.get_or_404(id) participants = [ participant.id for participant in hacknight.participants ] json_data = request.get_json(force=True) ids_schema = Schema.from_dict( {"participants_ids": fields.List(fields.Int())}) try: data = ids_schema().load(json_data) except ValidationError as err: return err.messages, HTTPStatus.UNPROCESSABLE_ENTITY new_participants = [ _id for _id in data["participants_ids"] if _id not in participants ] if not new_participants: return ( { "message": "No new participant has been provided" }, HTTPStatus.BAD_REQUEST, ) for new_participant in new_participants: hacknight.participants.append( Participant.query.get_or_404(new_participant)) db.session.add(hacknight) db.session.commit() hacknight_schema = HacknightSchema() return hacknight_schema.dump(hacknight), HTTPStatus.OK
def get(self, id): hacknight_schema = HacknightSchema() return ( {"hacknights": hacknight_schema.dump(Hacknight.query.get_or_404(id))}, HTTPStatus.OK, )
def post(self): hacknight_schema = HacknightSchema() json_data = request.get_json(force=True) if not json_data: return { "message": "No input data provided" }, HTTPStatus.BAD_REQUEST try: data = hacknight_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST hacknight = Hacknight.query.filter_by(date=data["date"]).first() if hacknight: return { "message": "Hacknight already exists." }, HTTPStatus.CONFLICT hacknight = Hacknight(date=data["date"]) db.session.add(hacknight) db.session.commit() return ( { "message": "Hacknight created successfully.", "hacknight": data }, HTTPStatus.CREATED, )
def get(self): hacknight_schema = HacknightSchema(many=True) return ( hacknight_schema.dump( Hacknight.query.order_by(Hacknight.date.desc()).all()), HTTPStatus.OK, )
def get(self): hacknight_schema = HacknightSchema(many=True, exclude=("participants", )) return ( hacknight_schema.dump( Hacknight.query.order_by(Hacknight.date.desc()).all()), HTTPStatus.OK, )
def get(self): hacknight_schema = HacknightSchema(many=True, exclude=("participants", )) return ( { "hacknights": hacknight_schema.dump(Hacknight.query.all()) }, HTTPStatus.OK, )