def post(self): self.parser.add_argument("email", type=inputs.regex(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'), required=True, location="json", help='email format is incorrect') self.parser.add_argument("totalPrice", type=int, required=True, location="json", help='totalPrice format is incorrect') self.parser.add_argument("username", type=str, required=True, location="json", help='username format is incorrect') self.parser.add_argument("phone", type=str, required=True, location="json", help='phone format is incorrect') self.parser.add_argument("payType", type=str, required=True, location="json", help='payType format is incorrect') self.parser.add_argument("productList", type=list, required=True, location="json", help='productList format is incorrect') self.parser.add_argument("addressId", type=str, required=True, location="json", help='addressId format is incorrect') args = self.parser.parse_args() mallUsersInfo = MallUsersModel.query.filter_by(email=args.email).first() if mallUsersInfo.balance < args.totalPrice: return pretty_result(code.ERROR, msg="create order failed, balance ont enough!") else: for i in args.productList: proudctInfo = ProductsModel.query.filter_by(id=i).first() proudctInfo.status = "solded" ProductsModel.update(proudctInfo) mallUsersInfo.balance = mallUsersInfo.balance - args.totalPrice MallUsersModel.update(mallUsersInfo) order = OrdersModel(email=args.email,no=get_order_code(), username=args.username, totalPrice=args.totalPrice, phone=args.phone.replace("-", ""), couponPrice=0, payType=args.payType, status="noDelivery", productList=','.join([str(x) for x in args.productList]), addressId=args.addressId) OrdersModel.add(OrdersModel, order) return pretty_result(code.OK, msg="create order info successful!")
def post(self): data = request.get_json(force=True) data['_id'] = 0 order = OrdersModel(data) try: order.insert_to_db() except: return {"message": "An error occurred inserting the order."}, 500 return order.json(), 201
def put(self): self.parser.add_argument("no", type=inputs.regex(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'), required=True, location="json", help='no format is incorrect') self.parser.add_argument("status", type=str, required=True, location="json", help='status format is incorrect') args = self.parser.parse_args() orderInfo = OrdersModel.query.filter_by(no=args.no).first() orderInfo.status = args.status OrdersModel.update(orderInfo) return pretty_result(code.OK, msg="update order status info successful!")
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 get(self): self.parser.add_argument("email", type=inputs.regex(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'), required=True, location="args", help='email format is incorrect') self.parser.add_argument("status", type=str, required=True, location="args", help='status format is incorrect') self.parser.add_argument("pageNo", type=int, required=True, location="args", help='pageNo format is incorrect') self.parser.add_argument("pageSize", type=int, required=True, location="args", help='pageSize format is incorrect') args = self.parser.parse_args() if args.status == "all": orderInfo = OrdersModel.paginate(OrdersModel, args.pageNo, args.pageSize) elif args.status == "noDelivery": orderInfo = OrdersModel.paginate_by_status(OrdersModel,'noDelivery', args.pageNo, args.pageSize) else: orderInfo = OrdersModel.paginate_by_status(OrdersModel,'delivered', args.pageNo, args.pageSize) data=[] if orderInfo.items: for item in orderInfo.items: OrderItemVOS = [] productList = item.productList.split(',') for i in productList: productInfo = ProductsModel.query.filter_by(id=i).first() OrderItemVOS.append(productInfo.to_dict()) data.append({ 'no': item.no, 'totalPrice': item.totalPrice, 'username': item.username, 'phone': item.phone, 'payType': item.payType, 'status': item.status, 'productList': item.productList, 'addressId': item.addressId, 'createTime': item.createTime, 'OrderItemVOS': OrderItemVOS }) totalCount = len(data) result = { 'pageNo': args.pageNo, 'pageSize': args.pageSize, 'totalCount': totalCount, 'items': data, 'totalPage': math.ceil(totalCount/args.pageSize) } return pretty_result(code.OK, data=result, msg="get order info successful!")
def put(self, order_id): # Create or Update data = request.get_json(force=True) order = OrdersModel.find_by_order_id(order_id) if order is None: order = OrdersModel(data['action'], data['symbol'], data['price'], data['order_type'], data['order_size'], data['value'], data['account_id']) else: order.price = data['price'] order.order_size = data['order_size'] order.value = data['value'] order.order_type = data['order_type'] order.update_to_db() return order.json()
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 post(self, idd): parser = reqparse.RequestParser( ) # create parameters parser from request parser.add_argument('date', type=str, required=True, help="This field cannot be left blanck") parser.add_argument('total', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('shipping', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('taxes', type=float, required=True, help="This field cannot be left blanck") parser.add_argument('state', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('send_type', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('card_id', type=int, required=True, help="This field cannot be left blanck") parser.add_argument('address_id', type=int, required=True, help="This field cannot be left blanck") data = parser.parse_args() acc = AccountModel.find_by_id(idd) if not acc: return {'message': "There isn't a user with this id"}, 409 new_order = OrdersModel(idd, data.date, data.total, data.shipping, data.taxes, data.state, data.send_type, data.card_id, data.address_id) acc.orders.append(new_order) db.session.add(new_order) db.session.commit() return new_order.id, 200
def post(self, username): user = AccountsModel.find_by_username(username) if user: parser = reqparse.RequestParser() parser.add_argument(n.IDEVNT, type=int, required=True, help="Id of the event") parser.add_argument(n.TCK, type=int, required=True, help="Number of tickets the user wants to buy") data = parser.parse_args() with lock.lock: if username == g.user.username: event = EventModel.find_by_id(data[n.IDEVNT]) if event: price = event.price*data[n.TCK] if user.available_money >= price: if event.total_available_tickets >= data[n.TCK]: try: user.pay(price) event.sell(data[n.TCK]) new_order = OrdersModel(data[n.IDEVNT], data[n.TCK]) user.orders.append(new_order) new_order.save_to_db() user.save_to_db() event.save_to_db() return new_order.json(), 201 except: return {n.MSG: "Error Description"}, 500 else: return {n.MSG: "The event does not have enough available ticket"}, 400 else: return {n.MSG: "The user does not have enough money"}, 400 else: return {n.MSG: "Event not found"}, 404 else: return {n.MSG: "Username error"}, 400 else: return {n.MSG: "Username not found"}, 404
def post(self, username): data = self.parser() with lock.lock: if username == g.user.username: a = AccountsModel.find_by_username(username) e = EventModel.find_by_id(data['event_id']) if e.total_available_tickets >= data["tickets_bought"]: if a.available_money >= e.price * data["tickets_bought"]: e.total_available_tickets = EventModel.total_available_tickets - data[ "tickets_bought"] a.available_money -= data["tickets_bought"] * e.price o = OrdersModel(e.id, data["tickets_bought"]) a.orders.append(o) db.session.commit() return {"order": o.json()}, 201 else: return {'message': 'Not enough money'}, 501 else: return {'message': 'No enough tickets'}, 502 else: return { 'message': 'Endpoint username and g.user.username not equal' }, 400
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 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 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 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 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): orders = OrdersModel.get_orders()['orders'] orders_list = [order for order in orders if order['state'] == 2] return {'orders': orders_list}, 200
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 delete(self, id_user, id_order): order = OrdersModel.find_by_id_user_and_orderid(id_user, id_order) if order: order.delete_from_db() return {'message': "OK"}, 200 return {'message': "Order not found".format(id)}, 409
def get(self, id_user): orders = [ order.json() for order in OrdersModel.find_by_state(2, id_user) ] return {"orders": orders}, 200
def delete(self, order_id): order = OrdersModel.find_by_order_id(order_id) if order: order.delete_from_db() return {'message': 'order has been deleted'}
def get(self, id_user): orders = sorted( [order.json() for order in OrdersModel.find_by_id_user(id_user)], key=lambda x: x['id'], reverse=True) return {"orders": orders}, 200
def get(self): orders = OrdersModel.get_orders() return orders, 200
def get(self, id): with lock.lock: orders = OrdersModel.find_all() return { 'orders': list(map(lambda x: x.json(), orders)) }, 200 if orders else 404
def get(self, account_id): return {'orders': OrdersModel.find_all_orders_by_account(account_id)}
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
def _get_orders(self): return OrdersModel.get_orders()['orders']
def get(self, order_id): order = OrdersModel.find_by_order_id(order_id) if order: return jsonify({'orders': order.json()}) return {'message': 'Order not found'}, 404
def get(self): return OrdersModel.return_all(), 200