def get(self, idd): try: return { "addresses": OrdersModel.find_by_id(idd).json()["address"] }, 200 except: return {"message": "Order with id [{}] Not Found".format(idd)}, 404
def post(self, ): parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('account_id', type=int, required=True, help="This field cannot be left blank") parser.add_argument('order_id', type=str, required=True, help="This field cannot be left blank") data = parser.parse_args() account_id, order_id = data['account_id'], data['order_id'] account = AccountModel.find_by_id(account_id) order = OrdersModel.find_by_id(order_id) if account and order: try: MailSender.send_ticket_order_mail(account.email, account.name, order) return {"message": "Email sended correctly"}, 200 except (FileNotFoundError, smtplib.SMTPException) as e: print(e) return {'message': 'Something went wrong'}, 500 else: return {"message": "Account or Order not found"}, 404
def delete(self, idd, id_sub): order = OrdersModel.find_by_id(idd) if order: deleted = order.delete_address(id_sub) if deleted: return {'message': "OK"}, 201 return { 'message': "Address with id [{}] Not found in order with id [{}]".format( id_sub, idd) }, 409
def delete(self, idd, id_article): order = OrdersModel.find_by_id(idd) article = ArticlesModel.find_by_id(id_article) if not order: return { 'message': "Order with id [{}] doesn t exists".format(idd) }, 404 if not article in order.articles: return { 'message': "Article with id [{}] Not found in order with id [{}]".format( id_article, idd) }, 404 order.delete_article(id_article) return {'message': "OK"}, 201
def post(self, idd): order = OrdersModel.find_by_id(idd) if order is None: return { "message": "Order with id [{}] not found ".format(idd) }, 404 parser = reqparse.RequestParser( ) # create parameters parser from request # define all input parameters need and its type parser.add_argument('price', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('id_book', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('quant', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() book = BookModel.find_by_id(data.id_book) if book: if book.total_available >= data.quant: book.num_sales += data.quant book.total_available -= data.quant book.save_to_db() article = ArticlesModel(data.price, book.genre, data.quant, book.id) order.add_article(article) article.save_to_db() order.save_to_db() return article.id, 200 return { "message": "Not enought books with id [{}] for the order".format( data.id_book) }, 404 return { "message": "Book with id [{}] not found ".format(data.id_book) }, 404
def put(self, idd): parser = reqparse.RequestParser( ) # create parameters parser from request # define all input parameters need and its type parser.add_argument('state', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() order = OrdersModel.find_by_id(idd) if order: id_user = order.id_user order.state = data.get('state') order.save_to_db() return order.json(), 200 return {'message': "Order not found"}, 400
def get(self, idd, id_sub): order = OrdersModel.find_by_id(idd) if order is None: return { "message": "Order with id [{}] not found ".format(idd) }, 404 addresses = order.json_with_address_id()["address"] address_list = [ addresses[i] for i in range(len(addresses)) if addresses[i]["id"] == int(id_sub) ] try: address = address_list[0] return address, 200 except: return { "message": "Address with id [{}] Not found in Order with id [{}]".format( id_sub, id) }, 404
def get(self, idd, id_article): order = OrdersModel.find_by_id(idd) if order is None: return { "message": "Order with id [{}] not found ".format(idd) }, 404 articles = order.json()["articles"] article_list = [ articles[i] for i in range(len(articles)) if articles[i]["id"] == int(id_article) ] try: article = article_list[0] return article, 200 except: return { "message": "Article with id [{}] Not found in Order with id [{}]".format( id_article, idd) }, 404
def delete(self, idd): order = OrdersModel.find_by_id(idd) if order: order.delete_from_db() return {'message': "OK"}, 200 return {'message': "Order with id [{}] Not found".format(idd)}, 409
def post(self, idd, id_sub, address_id=None): order = OrdersModel.find_by_id(idd) account = AccountModel.find_by_id(id_sub) if account is None: return { 'message': "Account with id [{}] Not found".format(id_sub) }, 404 if order is None: return { "message": "Order with id [{}] not found ".format(idd) }, 404 # Si no pasamos address id por parametro pedimos los parametros para crear una nueva direccion address = None if address_id is None: if len(account.addresses) == 3: return { 'message': "Account with id [{}] cannot have more addresses".format( id_sub) }, 404 parser = reqparse.RequestParser() # define the input parameters need and its type parser.add_argument('label_name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('name', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('surnames', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('street', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('number', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('cp', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('city', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('province', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('telf', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() address = AddressModel(data['label_name'], data['name'], data['surnames'], data['street'], data['number'], data['cp'], data['city'], data['province'], data['telf']) account.addresses.append(address) # Si pasamos la id de la direccion la buscamos en la cuenta else: address = account.find_address_by_id(address_id) if address is not None: order.add_address(address) order.save_to_db() return {'message': "OK"}, 200 return { 'message': "Address with id [{}] Not found".format(address_id) }, 404
def get(self, idd): order = OrdersModel.find_by_id(idd) if order: return {"orders": order.json()}, 200 return {'message': "Order with id [{}] doesn't exist".format(idd)}, 409