コード例 #1
0
    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
コード例 #2
0
    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
コード例 #3
0
    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
コード例 #4
0
    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
コード例 #5
0
    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