def put(self, id): user_identifier = get_jwt_identity() order = Order.query.filter_by(id=id).first() user = User.query.filter_by(id=user_identifier).first() if order is not None: if user.admin: post_data = request.get_json() schema = OrderSchema(only=["tracking_no"]) try: post_data = schema.load(post_data) except ValidationError as err: return {"errors": err.messages}, 422 order.tracking_no = post_data.get("tracking_no") db.session.commit() order.confirm_received = True response_obj = { "success": True } return response_obj else: response_obj = { "success": False, "message": "Unauthorised Access" } return make_response(jsonify(response_obj), 403) else: response_obj = { "success": False, "message": "No such Order" } return make_response(jsonify(response_obj), 404)
def post(self, id): cart = UserCart.query.filter_by(id=id).first() if cart is not None: user_id = get_jwt_identity() items = cart.items total = 0 for item in items: total += item.quantity * item.product.retail_price order = Order(order_date=int(datetime.now().timestamp() * 1000), total_price=total, user_id=user_id) db.session.add(order) db.session.commit() for item in items: ordered = OrderedProduct(order_id=order.id, product_id=item.product.id, quantity=item.quantity) db.session.delete(item) db.session.add(ordered) db.session.commit() order_schema = OrderSchema() response_obj = {"success": True, "data": order_schema.dump(order)} return response_obj else: response_obj = {"success": False, "message": "No such user cart"} return make_response(jsonify(response_obj), 404)
def get(self, id): order = Order.query.filter_by(id=id).first() if order is not None: order_schema = OrderSchema() response_obj = {"success": True, "data": order_schema.dump(order)} return response_obj else: response_obj = {"success": False, "message": "No such Order"} return make_response(jsonify(response_obj), 404)
def get(self): user_identifier = get_jwt_identity() orders = Order.query.filter_by(user_id=user_identifier).all() orders_schema = OrderSchema(many=True) response_obj = { "success": True, "data": { "orders": orders_schema.dump(orders) } } return response_obj
def get(self, id): user_identifier = get_jwt_identity() order = Order.query.filter_by(id=id).first() user = User.query.filter_by(id=user_identifier).first() if order is not None: if user_identifier == order.user_id or user.admin: order_schema = OrderSchema( exclude=["user", "items.product.id", "items.product.retail_price", "items.product.stock"]) response_order = order_schema.dump(order) address_schema = AddressSchema(exclude=["user","id"]) address = Address.query.filter_by(id=response_order["address_id"]).first() address = address_schema.dump(address) address["address_2"] = decrypt(address["address_2"]) address["address_1"] = decrypt(address["address_1"]) address["contact"] = decrypt(address["contact"]) address["postal_code"] = decrypt(address["postal_code"]) address["contact"] = "*" * len(address["contact"][:-2]) + address["contact"][-2:] response_order["address"] = address response_order["order_date"] = str(datetime.fromtimestamp(int(response_order["order_date"]) / 1000)) response_obj = { "success": True, "data": response_order } return response_obj else: response_obj = { "success": False, "message": "Unauthorised Access" } return make_response(jsonify(response_obj), 403) else: response_obj = { "success": False, "message": "No such Order" } return make_response(jsonify(response_obj), 404)
def get(self): user_identifier = get_jwt_identity() user = User.query.filter_by(id=user_identifier).first() if user.admin: orders = Order else: orders = Order.query.filter_by(user_id=user_identifier) order_schema = OrderSchema(many=True, exclude=["user", "items.product.id", "items.product.retail_price", "items.product.stock"]) response_orders = pagination.paginate(orders, order_schema) for order in response_orders["data"]: order["order_date"] = str(datetime.fromtimestamp(int(order["order_date"]) / 1000)) response_obj = { "success": True, "data": response_orders } return response_obj
def post(self): user_id = get_jwt_identity() cart = UserCart.query.filter_by(id=user_id).first() post_data = request.get_json(force=True) card = Card.query.filter_by(id=post_data.get("credit_card_id")).first() address = Address.query.filter_by(id=post_data.get("address_id")).first() if cart is not None and cart.items != []: if card: if cart.user_id == user_id and card.user_id == user_id: if address: if address.user_id == user_id: items = cart.items total = 0 for item in items: total += item.quantity * item.product.retail_price order = Order( order_date=int(datetime.now().timestamp() * 1000), total_price=total, user_id=user_id, address_id=post_data.get("address_id") ) db.session.add(order) db.session.commit() for item in items: ordered = OrderedProduct( order_id=order.id, product_id=item.product.id, quantity=item.quantity ) db.session.delete(item) db.session.add(ordered) db.session.commit() order_schema = OrderSchema( exclude=["user", "items.product.id", "items.product.retail_price", "items.product.stock", "address_id"]) address = Address.query.filter_by(id=post_data.get("address_id")).first() response_order = order_schema.dump(order) address_schema = AddressSchema(exclude=["user"]) response_order["order_date"] = str(datetime.fromtimestamp(int(response_order["order_date"])/1000)) address = address_schema.dump(address) address["address_2"] = decrypt(address["address_2"]) address["address_1"] = decrypt(address["address_1"]) address["contact"] = decrypt(address["contact"]) address["postal_code"] = decrypt(address["postal_code"]) address["contact"] = "*" * len(address["contact"][:-2]) + address["contact"][-2:] response_order["address"] = address response_obj = { "success": True, "data": response_order } return response_obj else: response_obj = { "success": False, "message": "Unauthorised Access" } return make_response(jsonify(response_obj), 403) else: address = Address.query.filter_by(user_id=user_id, default=True).first() if address: items = cart.items total = 0 for item in items: total += item.quantity * item.product.retail_price order = Order( order_date=int(datetime.now().timestamp() * 1000), total_price=total, user_id=user_id, address_id=address.id ) db.session.add(order) db.session.commit() for item in items: ordered = OrderedProduct( order_id=order.id, product_id=item.product.id, quantity=item.quantity ) db.session.delete(item) db.session.add(ordered) db.session.commit() address_schema = AddressSchema(exclude=["user"]) order_schema = OrderSchema( exclude=["user", "items.product.id", "items.product.retail_price", "items.product.stock", "address_id"]) response_order = order_schema.dump(order) response_order["order_date"] = str(datetime.fromtimestamp(int(response_order["order_date"])/1000)) address = address_schema.dump(address) address["address_2"] = decrypt(address["address_2"]) address["address_1"] = decrypt(address["address_1"]) address["contact"] = decrypt(address["contact"]) address["postal_code"] = decrypt(address["postal_code"]) address["contact"] = "*" * len(address["contact"][:-2]) + address["contact"][-2:] response_order["address"] = address response_obj = { "success": True, "data": response_order } return response_obj else: response_obj = { "success": False, "message": "No Address found" } return make_response(jsonify(response_obj), 404) else: response_obj = { "success": False, "message": "Unauthorised Access" } return make_response(jsonify(response_obj), 403) else: response_obj = { "success": False, "message": "Card not found or No Card selected" } return make_response(jsonify(response_obj), 404) else: response_obj = { "success": False, "message": "No such user cart or No item in cart" } return make_response(jsonify(response_obj), 404)