def post(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) if len(user.cart.foods) == 0: return Response({"detail": "Cart may not be empty"}, status=status.HTTP_400_BAD_REQUEST) data = request.data if data.get("addressId") is None: return Response({"detail": "addressId is required"}, status=status.HTTP_400_BAD_REQUEST) addresses = user.getAddresses() for address in addresses: if address.addressId == data.get("addressId"): user.cart.commit(data.get("discountId"), data.get("addressId"), user.wallet.walletId) return Response(status=status.HTTP_200_OK) return Response({"detail": "addressId not valid "}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) data = request.data if data.get("invoiceId") is None: return Response({"detail": "invoiceId is required"}, status=status.HTTP_400_BAD_REQUEST) if data.get("text") is None: return Response({"detail": "text is required"}, status=status.HTTP_400_BAD_REQUEST) if data.get("rate") is None: return Response({"detail": "rate is required"}, status=status.HTTP_400_BAD_REQUEST) userInvoices = user.invoices for invoice in userInvoices: if invoice.invoiceId == data.get("invoiceId"): if invoice.statusId != 3: return Response( {"detail": "Cannot add comment in this stage"}, status=status.HTTP_400_BAD_REQUEST) try: invoice.addComment(data.get("text"), data.get("rate")) return Response(status=status.HTTP_200_OK) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "invoiceId is not valid"}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) return Response(user.cart.data, status=status.HTTP_200_OK)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) lst = user.getAddresses() out = [x.data for x in lst] return Response(out, status=status.HTTP_200_OK)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) favorites = user.favorites return Response([x.data for x in favorites], status=status.HTTP_200_OK)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) shopId = request.GET.get("shopId") if shopId is None: return Response({"detail": "shopId parameter is required"}) invoices = Invoice.getInvoiceByShopName(user, shopId) return Response([x.data for x in invoices], status=status.HTTP_200_OK)
def get(self, request, invoiceId): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) userInvoices = user.invoices for invoice in userInvoices: if invoice.invoiceId == int(invoiceId): return Response(invoice.data, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "invoiceId is not valid"}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) data = { "first_name": user.first_name, "last_name": user.last_name, "phone_number": user.phone_number, "email": user.email } return Response(data, status=status.HTTP_200_OK)
def delete(self, request, addressId): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) addresses = user.getAddresses() for address in addresses: if address.addressId == int(addressId): address.delete() return Response(status=status.HTTP_200_OK) return Response({"detail": "Address Not Found"}, status=status.HTTP_404_NOT_FOUND)
def delete(self, request, foodId): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) foodCart = user.cart.foods for food in foodCart: if food.foodId == int(foodId): user.cart.removeFood(food) return Response(status=status.HTTP_200_OK) return Response({"detail": "Invalid foodId"}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) address = Address(**request.data) if address.is_valid(): try: address.insert(user) return Response(status=status.HTTP_200_OK) except Exception as ex: return Response({"detail": ex}, status=status.HTTP_400_BAD_REQUEST) else: return Response(address.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) user.updateData(**request.data) if user.is_valid(): try: user.update() return Response(status=status.HTTP_200_OK) except Exception as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) else: return Response(user.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) userInvoices = user.invoices out = [] for invoice in userInvoices: price = 0 out.append(invoice.data) for food in invoice.foods: price += food.price out[-1]["totalPrice"] = price return Response(out, status=status.HTTP_200_OK)
def post(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) data = request.data if data.get("shopId") is None: return Response({"detail": "shopId is required"}, status=status.HTTP_400_BAD_REQUEST) try: user.removeFavorite(data.get("shopId")) except ObjectNotFoundException as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_200_OK)
def put(self, request, addressId): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) addresses = user.getAddresses() for address in addresses: if address.addressId == int(addressId): address.updateData(**request.data) if address.is_valid(): try: address.update() return Response(status=status.HTTP_200_OK) except Exception as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) else: return Response(address.errors, status=status.HTTP_400_BAD_REQUEST) return Response({"detail": "Address Not Found"}, status=status.HTTP_404_NOT_FOUND)
def post(self, request): try: user = validateUserToken(request) except ValidationError as ex: return Response({"detail": str(ex)}, status=status.HTTP_401_UNAUTHORIZED) data = request.data if "foodId" not in data.keys(): return Response({"detail": "FoodId is required"}, status=status.HTTP_400_BAD_REQUEST) cart = user.cart try: cart.addFood(data.get("foodId")) except ObjectNotFoundException as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) except InsertNotAllowedException as ex: return Response({"detail": str(ex)}, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_200_OK)