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
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
def post(cls): _parser = reqparse.RequestParser() _parser.add_argument('categoryTypeID', type=str, required=True, help="This field cannot be blank.") _parser.add_argument('quantity', type=int, required=True, help="This field cannot be blank.") _parser.add_argument('comments', type=str, required=False) userID = get_jwt_identity() data = _parser.parse_args() request = RequestModel(userID=userID, categoryTypeID=data['categoryTypeID'], quantity=data['quantity'], comments=data['comments']) request.save_to_db() #Add request to queue queueItem = QueueItemModel(userID=userID, categoryTypeID=data['categoryTypeID'], requestID=request.id) queueItem.save_to_db() return {"message": "Request created successfully"}, 201
def _parse_requests(self): _requests = [ x for x in self._raw['resources'] if x['_type'] == 'request' ] for req in _requests: self.requests.append(RequestModel(req))
from app import app from db import db from models.request import RequestModel from models.user import UserModel db.init_app(app) with app.app_context(): db.create_all() parker = UserModel(email='*****@*****.**') req = RequestModel(url_link='https://google.com') req.user = parker db.session.add(parker) db.session.add(req) db.session.commit()