def post(self, item_id, amount): """Item add or update.""" try: if amount <= 0: raise ValidationError("'%s' is an invalid item amount. It must be a positive natural number" % amount) else: item_dict = self.__cartservice.to_dict() item_dict.update({item_id: amount}) item_list = [{"item_id": key, "amount": value} for key, value in item_dict.items()] items_info = {"item_list": item_list} req = post("%s/api/product/list" % (self.__url), headers=self.__headers, json=items_info) req.raise_for_status() result = req.json() self.__cartservice.update_item(item_id, amount) for item in item_list: product = next(p for p in result["products"] if p["id"] == item["item_id"]) product["amount"] = item["amount"] jsonsend = CartResponse.marshall_json(dict(**result)) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, sessionid): """Session information.""" try: in_data = SearchRequest.parse_json() session = self.__sessionservice.select_by_id(sessionid) sessions = self.__sessionservice.select(gender=session.gender) total = self.__productservice.get_total(sessionid=sessionid, **in_data) brands = self.__productservice.select_brands(sessionid=sessionid, **in_data) kinds = self.__productservice.select_kinds(sessionid=sessionid, **in_data) pricerange = self.__productservice.select_pricerange( sessionid=sessionid) jsonsend = SessionResultsResponse.marshall_json({ "sessions": sessions, "total": total, "brands": brands, "kinds": kinds, "pricerange": pricerange }) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, ftype, arg, page): """Finder products paginated by 'search query', 'kind' or 'brand'""" try: if ftype not in self.__finder_types: raise ValidationError( "'%s' is an invalid URL finder type. Valid: 'search', 'brand' and 'kind'" % ftype) if page <= 0: raise ValidationError( "'%s' is an invalid URL page value. It must be a positive natural number" % page) else: in_data = SearchProductsRequest.parse_json() req = post("%s/api/%s/%s/%s" % (self.__url, ftype, arg, page), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() else: jsonsend = SearchProductsResultsResponse.marshall_json( req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, item_id): """Item removal.""" try: self.__cartservice.remove_item(item_id) item_list = self.__cartservice.to_list() if item_list == []: raise NoContentError() else: items_info = {"item_list": item_list} req = post("%s/api/product/list" % (self.__url), headers=self.__headers, json=items_info) req.raise_for_status() result = req.json() for item in item_list: product = next(p for p in result["products"] if p["id"] == item["item_id"]) product["amount"] = item["amount"] jsonsend = CartResponse.marshall_json(dict(**result)) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def get(self): """Get current (logged in) user""" try: jsonsend = UserResponse.marshall_json(current_user.to_dict()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def get(self, user_slug, order_slug): """Order information.""" try: order = self.__orderservice.select_by_slug(user_slug=user_slug, order_slug=order_slug) items_list = [item.to_dict() for item in order.items] items_info = {"item_list": items_list} url = app.config["WILLSTORES_WS"] headers = { "Authorization": "Bearer %s" % os.getenv("ACCESS_TOKEN") } req = post("%s/api/product/list" % (url), headers=headers, json=items_info) req.raise_for_status() result = req.json() for item in items_list: product = next(p for p in result["products"] if p["id"] == item["item_id"]) product["amount"] = item["amount"] jsonsend = OrderResponse.marshall_json( dict(order.to_dict(), **result)) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def delete(self, user_slug, order_slug): """Order delete.""" try: self.__orderservice.delete(user_slug=user_slug, order_slug=order_slug) return {}, 200 except Exception as error: return ErrorHandler(error).handle_error()
def get(self): """Total products registered.""" try: numproducts = self.__productservice.products_count() jsonsend = ProductsCountResponse.marshall_json({"count": numproducts}) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def get(self, productid): """Product information.""" try: product = self.__productservice.select_by_id(productid) jsonsend = ProductResultsResponse.marshall_json(product.get_dict()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self): """Product list total price.""" try: in_data = ProductListRequest.parse_json() _, total = self.__productservice.select_by_item_list(**in_data) jsonsend = ProductTotalResponse.marshall_json({"total": total}) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def delete(self, slug): """Order delete.""" try: user_slug = current_user.uuid_slug req = delete("%s/api/order/delete/%s/%s" % (self.__url, user_slug, slug), headers=self.__headers) req.raise_for_status() return {}, req.status_code except Exception as error: return ErrorHandler(error).handle_error()
def get(self, productid): """Product information.""" try: req = get("%s/api/product/%s" % (self.__url, productid), headers=self.__headers) req.raise_for_status() jsonsend = ProductResultsResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def get(self): """User login""" try: if current_user.is_authenticated is False: next_url = request.args.get("next") if next_url is not None and is_safe_url(next_url): session["next_url"] = next_url return redirect(url_for("google.login")) else: return redirect(url_for("frontend.index")) except Exception as error: return ErrorHandler(error).handle_error()
def get(self, slug): """Order information.""" try: user_slug = current_user.uuid_slug req = get("%s/api/order/%s/%s" % (self.__url, user_slug, slug), headers=self.__headers) req.raise_for_status() jsonsend = OrderResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, query, page): """Search products paginated""" try: in_data = SearchProductsRequest.parse_json() products = self.__productservice.select(query=query, page=page, **in_data) jsonsend = SearchProductsResultsResponse.marshall_json( {"products": [p.get_dict_min() for p in products]}) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self): """Product information list with total price.""" try: in_data = ProductListRequest.parse_json() products, total = self.__productservice.select_by_item_list(**in_data) jsonsend = ProductsListResponse.marshall_json( { "products": [p.get_dict_min() for p in products], "total": total } ) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, gender="Men"): """Gender information.""" try: in_data = GenderRequest.parse_json() req = post("%s/api/gender/%s" % (self.__url, gender), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() jsonsend = GenderResultsResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self): """Orders for the logged in user.""" try: in_data = UserOrdersRequest.parse_json() user_slug = current_user.uuid_slug req = post("%s/api/order/user/%s" % (self.__url, user_slug), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() else: jsonsend = UserOrdersResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, sessionid): """Session information.""" try: in_data = SearchRequest.parse_json() req = post("%s/api/session/%s" % (self.__url, sessionid), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() jsonsend = SessionResultsResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, user_slug): """Orders for a user.""" try: in_data = UserOrdersRequest.parse_json() user_info = self.__orderservice.select_by_user_slug( user_slug=user_slug, **in_data) url = app.config["WILLSTORES_WS"] with Session() as sess: sess.headers["Authorization"] = "Bearer %s" % os.getenv( "ACCESS_TOKEN") processed_user_info = asyncio.run( process_orders(sess, url, user_info)) jsonsend = UserOrdersResponse.marshall_json(processed_user_info) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def put(self): """Order insert.""" try: in_data = OrderInsertRequest.parse_json() url = app.config["WILLSTORES_WS"] headers = { "Authorization": "Bearer %s" % os.getenv("ACCESS_TOKEN") } req = post("%s/api/product/total" % (url), headers=headers, json={"item_list": in_data["item_list"]}) req.raise_for_status() self.__orderservice.insert(**in_data) return {}, 201 except Exception as error: return ErrorHandler(error).handle_error()
def post(self, brand): """Brand information.""" try: in_data = SearchRequest.parse_json() total = self.__productservice.get_total(brand=brand, **in_data) brands = self.__productservice.select_brands(brand=brand, **in_data) kinds = self.__productservice.select_kinds(brand=brand, **in_data) pricerange = self.__productservice.select_pricerange(brand=brand) jsonsend = SearchResultsResponse.marshall_json( { "total": total, "brands": brands, "kinds": kinds, "pricerange": pricerange } ) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, ftype, arg): """Finder information by 'search query', 'kind' or 'brand'""" try: if ftype not in self.__finder_types: raise ValidationError( "'%s' is an invalid URL finder type. Valid: 'search', 'brand' and 'kind'" ) else: in_data = SearchRequest.parse_json() req = post("%s/api/%s/%s" % (self.__url, ftype, arg), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() else: jsonsend = SearchResultsResponse.marshall_json(req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, gender="Men"): """Gender information.""" try: in_data = GenderRequest.parse_json() discounts = self.__productservice.super_discounts(gender=gender, **in_data) sessions = self.__sessionservice.select(gender=gender) brands = self.__productservice.select_brands(gender=gender) kinds = self.__productservice.select_kinds(gender=gender) jsonsend = GenderResultsResponse.marshall_json({ "discounts": [d.get_dict_min() for d in discounts], "sessions": sessions, "brands": brands, "kinds": kinds }) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def post(self, sessionid, page): """Session products paginated""" try: if page <= 0: raise ValidationError( "'%s' is an invalid URL page value. It must be a positive natural number" % page) else: in_data = SearchProductsRequest.parse_json() req = post("%s/api/session/%s/%s" % (self.__url, sessionid, page), headers=self.__headers, json=in_data) req.raise_for_status() if req.status_code == 204: raise NoContentError() jsonsend = SearchProductsResultsResponse.marshall_json( req.json()) return jsonsend except Exception as error: return ErrorHandler(error).handle_error()
def put(self): """Make an order based on the cart items.""" try: try: item_list = self.__cartservice.to_list() except NoContentError: raise ValidationError("Cart is empty") json_order = { "user_slug": current_user.uuid_slug, "item_list": item_list } req = put("%s/api/order/insert" % (self.__url), headers=self.__headers, json=json_order) req.raise_for_status() self.__cartservice.empty() return {}, 201 except Exception as error: return ErrorHandler(error).handle_error()