def post(self): team_schema = TeamSchema() json_data = request.get_json(force=True) if not json_data: return {"message": "No input data provided"}, HTTPStatus.BAD_REQUEST try: data = team_schema.load(json_data) except ValidationError as err: return (err.messages), HTTPStatus.BAD_REQUEST if Team.query.filter_by(project_name=data["project_name"]).first(): return ( {"message": "Team with that project_name already exists."}, HTTPStatus.CONFLICT, ) if Team.query.filter_by(project_url=data["project_url"]).first(): return ( {"message": "Team with that project_url already exists."}, HTTPStatus.CONFLICT, ) team = Team(**data) db.session.add(team) db.session.commit() return team_schema.dump(team), HTTPStatus.CREATED
def post(self, id): team = Team.query.get_or_404(id) members = [member.id for member in team.members] json_data = request.get_json(force=True) ids_schema = Schema.from_dict({"members_ids": fields.List(fields.Int())}) try: data = ids_schema().load(json_data) except ValidationError as err: return err.messages, HTTPStatus.UNPROCESSABLE_ENTITY new_members = [_id for _id in data["members_ids"] if _id not in members] if not new_members: return ( {"message": "No new member has been provided"}, HTTPStatus.BAD_REQUEST, ) for new_member in new_members: team.members.append(Participant.query.get_or_404(new_member)) db.session.add(team) db.session.commit() team_schema = TeamSchema() return team_schema.dump(team), HTTPStatus.OK
def put(self, id): team_schema = TeamSchema(partial=True) team = Team.query.get_or_404(id) json_data = request.get_json(force=True) try: data = team_schema.load(json_data) except ValidationError as err: return err.messages, HTTPStatus.BAD_REQUEST for key, value in data.items(): setattr(team, key, value) db.session.add(team) db.session.commit() return team_schema.dump(team), HTTPStatus.OK
def test_create_team_with_valid_data( auth_client, new_team, ): """Test add team with valid data in payload.""" team_schema = TeamSchema() rv = auth_client.post( "/api/teams/", data=json.dumps(new_team), ) response = rv.get_json() team_db = Team.query.first() assert rv.status_code == HTTPStatus.CREATED assert response == team_schema.dump(team_db)
def test_get_team_when_logged_in(auth_client, add_teams): """Test get team details when logged in.""" team_db = Team.query.first() rv = auth_client.get(f"/api/teams/{team_db.id}/") response = rv.get_json() assert rv.status_code == HTTPStatus.OK assert response == TeamSchema().dump(team_db)
def test_edit_team_data_when_logged_in(app, auth_client, add_teams): """Test edit team details for logged in user and valid data.""" with app.app_context(): payload = { "project_name": "New Project", "description": "Another description" } rv = auth_client.put( "/api/teams/1/", json=payload, ) response = rv.get_json() schema = TeamSchema() team = schema.dump(Team.query.first()) assert rv.status_code == HTTPStatus.OK for key, value in payload.items(): assert team.get(key) == value assert response.get(key) == value
def delete(self, id): team = Team.query.get_or_404(id) members = {member.id for member in team.members} json_data = request.get_json(force=True) ids_schema = Schema.from_dict({"members_ids": fields.List(fields.Int())}) try: data = ids_schema().load(json_data) except ValidationError as err: return err.messages, HTTPStatus.UNPROCESSABLE_ENTITY to_remove = members.intersection(set(data["members_ids"])) if not to_remove: return {"message": "No member to delete"}, HTTPStatus.BAD_REQUEST team.members = [member for member in team.members if member.id not in to_remove] db.session.commit() team_schema = TeamSchema() return team_schema.dump(team), HTTPStatus.OK
def get(self, id): team_schema = TeamSchema() return team_schema.dump(Team.query.get_or_404(id)), HTTPStatus.OK
def get(self): team_schema = TeamSchema(many=True, exclude=("members",)) return ( team_schema.dump(Team.query.all()), HTTPStatus.OK, )