def put(self, id): participant_schema = ParticipantSchema(partial=True) participant = Participant.query.get_or_404(id) json_data = request.get_json(force=True) try: data = participant_schema.load(json_data) except ValidationError as err: return err.messages, HTTPStatus.BAD_REQUEST for key, value in data.items(): if key == "email" and participant.email != value: if Participant.query.filter_by(email=value).first(): return ( { "message": "User with this email already exists." }, HTTPStatus.CONFLICT, ) if key == "github" and participant.github != value: if Participant.query.filter_by(github=value).first(): return ( { "message": "User with this Github login already exists." }, HTTPStatus.CONFLICT, ) setattr(participant, key, value) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.OK
def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return { "message": "No input data provided" }, HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST if Participant.query.filter_by(email=json_data["email"]).first(): return ( { "message": "User with this email already exists." }, HTTPStatus.CONFLICT, ) if Participant.query.filter_by(github=json_data["github"]).first(): return ( { "message": "User with this Github login already exists." }, HTTPStatus.CONFLICT, ) participant = Participant(**data) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.CREATED
def get(self): participant_schema = ParticipantSchema(many=True, exclude=("hacknights", )) return ( { "participants": participant_schema.dump(Participant.query.all()) }, HTTPStatus.OK, )
def test_get_participant_when_logged_in(client, access_token, add_participants): """Test get participant details for logged in user.""" rv = client.get( '/participants/1/', headers={'Authorization': 'Bearer {}'.format(access_token)}) response = rv.get_json() participant_schema = ParticipantSchema() assert rv.status_code == HTTPStatus.OK assert response == participant_schema.dump(Participant.query.get(1))
def test_get_delete_participant_when_logged_in(client, auth_client, add_participants, method): """Test get and delete participant details for logged in user.""" rv = getattr(auth_client, method)("/participants/1/") response = rv.get_json() participant_schema = ParticipantSchema() assert rv.status_code == HTTPStatus.OK if method == "get": assert response == participant_schema.dump(Participant.query.get(1)) else: assert response["message"] == "Participant deleted successfully."
def put(self, id): participant_schema = ParticipantSchema(partial=True) participant = Participant.query.get_or_404(id) json_data = request.get_json(force=True) try: data = participant_schema.load(json_data) except ValidationError as err: return err.messages, HTTPStatus.BAD_REQUEST for key, value in data.items(): setattr(participant, key, value) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.OK
def test_create_participant_when_logged_in(app, auth_client, new_participant): """Test create new participant with logged in user and valid data.""" with app.app_context(): rv = auth_client.post("/api/participants/", json=new_participant) response = rv.get_json() schema = ParticipantSchema() participant = schema.dump(Participant.query.first()) assert rv.status_code == HTTPStatus.CREATED assert response["email"] == new_participant["email"] for value in new_participant.values(): assert value in participant.values() for value in participant.keys(): assert value in response
def test_put_participant_when_logged_in(app, auth_client, add_participants): """Test put participant details for logged in user and valid data.""" with app.app_context(): payload = {"last_name": "TestTest", "email": "*****@*****.**"} rv = auth_client.put( "/participants/1/", json=payload, ) response = rv.get_json() schema = ParticipantSchema() participant = schema.dump(Participant.query.first()) assert rv.status_code == HTTPStatus.OK assert response["last_name"] == payload["last_name"] assert response["email"] == payload["email"] assert participant["last_name"] == payload["last_name"]
def test_create_participant_when_logged_in(app, client, access_token, new_participant): """Test create new participant with logged in user and valid data.""" with app.app_context(): rv = client.post( "/participants/", headers={"Authorization": "Bearer {}".format(access_token)}, json=new_participant, ) response = rv.get_json() schema = ParticipantSchema() participant = schema.dump(Participant.query.first()) assert rv.status_code == HTTPStatus.CREATED assert response["participant"]["email"] == new_participant["email"] for value in new_participant.values(): assert value in participant.values()
def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return { "message": "No input data provided" }, HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST participant = Participant(**data) db.session.add(participant) db.session.commit() return participant_schema.dump(participant), HTTPStatus.CREATED
def post(self): participant_schema = ParticipantSchema() json_data = request.get_json(force=True) if not json_data: return {'message': 'No input data provided'}, \ HTTPStatus.BAD_REQUEST try: data = participant_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST participant = Participant(**data) db.session.add(participant) db.session.commit() return { "message": "Participant created successfully.", "participant": data }, HTTPStatus.CREATED
def get(self, id): participant_schema = ParticipantSchema() return participant_schema.dump( Participant.query.get_or_404(id)), HTTPStatus.OK