def post(self):

        data = _request_parser.parse_args() #Validacao das condicoes de entrada

        if RequestModel.find_by_name(data['name']):
            return {'message': "A Request with name '{}' already exists".format(data['name'])}, 400

        request = RequestModel(**data)

        try:
            request.save_to_db()
        except:
            return {'message': "An error occured while creating the request"}, 500

        return request.json(), 201
Exemple #2
0
    def post(self):
        """Post endpoint for adding requests into the database.

        Headers: {Authorization: JWT jwt_token, ContentType: application/json}
        Body must be json with priority, target_date, product_area, client_name,
        title, description fields. Database must have matching client_name or
        will return json message 400 error. If error occurs while inserting into
        database will return json message and 500 error. On successful insert
        into database returns json of request and 201 code."""
        parser = Parser()
        parser.required_fields(
            'priority',
            'target_date',
            'product_area',
            'client_name',
            'title',
            'description')
        data = parser.parse_args()
        if not ClientModel.select(data['client_name']):
            return {
                'message': 'Client does not exist'}, 400
        request = RequestModel(**data)
        client_name = data['client_name']
        priority = data['priority']
        update_list = []
        try:
            while RequestModel.select_same_priority(client_name, priority):
                update_list.append(
                    RequestModel.select_same_priority(
                        client_name, priority))
                priority += 1
            request.save_to_db()
            for req in update_list:
                req.priority += 1
                req.save_to_db()
        except BaseException:
            return {'message': 'Something went wrong'}, 500
        return request.json(), 201