def post(self, request, sub_category_id): """ creates a new product :param request: :param sub_category_id: sub_category_id :request_data { "name": "Product_name", "category": 10, "sub_category": 10, "image": 10, "price":100.50, "quantity":10, "p_type": "16 GB, 32 GB, 64 GB", "p_color": "Green, Blue, Yellow", "description": "It is a secret description" } :return: """ try: data = request.data data['wholesaler'] = request.user.id serializer = serializers.AddProductSerializer(data=data) if serializer.is_valid(): serializer.save() else: return Utils.dispatch_failure(request, 'VALIDATION_ERROR', serializer.errors) return Utils.dispatch_success(request, serializer.data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, referral_code): try: User.objects.get(referral_code=referral_code) return Utils.dispatch_success(request, 'SUCCESS') except User.DoesNotExist: return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, id): """ Get particular store :param request: :param id: :return: """ try: user = User.objects.get(id=id) return Utils.dispatch_success(request, serializers.BasicUserInfoSerializer(user).data) except User.DoesNotExist: return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, product_id): """ Returns a particular product based on sub_category id :param request: :param product_id: product_id :return: """ try: product = Product.objects.get(id=product_id) serializer = serializers.ProductSerializer(product) return Utils.dispatch_success(request, serializer.data) except Product.DoesNotExist: return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, category_id): """ Return the list of sub categories :param request: :param category_id: :return: """ try: is_all_sub_category = request.GET.get('all', None) if is_all_sub_category: sub_category_list = SubCategory.objects.filter(category=category_id) else: user = request.user user_type = user.user_type if user_type == User.WHOLESALER: products = Product.objects.filter(wholesaler=user, sub_category__category=category_id) sub_category_list = [product.sub_category for product in products] sub_category_list = set(sub_category_list) else: sub_category_list = SubCategory.objects.filter(category=category_id) serializer = serializers.SubCategorySerializer(sub_category_list, many=True, context={"user": request.user}) return Utils.dispatch_success(request, serializer.data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request, order_id): """ { "order_status" : "PACKED" "delivery_boy" : 34 "pickup_address" : "ANy Really Long ADdress" } :param request: :param order_id: :return: """ try: order_item = Order.objects.get(id=order_id) data = request.data if data.get('order_status'): order_item.order_status = data['order_status'] if data.get('delivery_boy'): order_item.delivery_boy = User.objects.get(id=data['delivery_boy']) if data.get('pickup_address'): order_item.pickup_address = data['pickup_address'] order_item.save() return Utils.dispatch_success(request, OrderSerializer(order_item).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request, order_id): """ { "order_status" : DELIVERED / PICKUPED "paid_by" : CASH / UPI "upi_id" : 14621849198541265621 } :param request: :param order_id: :return: """ try: order_item = Order.objects.get(id=order_id) data = request.data if data.get('order_status'): order_item.order_status = data['order_status'] if data['order_status'] == Order.DELIVERED: order_item.is_delivered = True order_item.delivered_time = datetime.datetime.now() if data.get('paid_by'): order_item.paid_by = data['paid_by'] if data.get('upi_id'): order_item.upi_id = data['upi_id'] order_item.save() return Utils.dispatch_success(request, OrderSerializer(order_item).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request): """ request_data:{ "aadhaar_document":2, "license_document":3, } :param request: :return: """ try: user = request.user data = request.data if user.user_type == User.DELIVERY_MAN: if data.get('aadhaar_document'): user.aadhaar_document = Media.objects.get( id=data['aadhaar_document']) if data.get('license_document'): user.license_document = Media.objects.get( id=data['license_document']) user.save() return Utils.dispatch_success(request, 'SUCCESS') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def delete(self, request, id): """ Delete particular store :param request: :param id: :return: """ try: user = User.objects.get(id=id) user.delete() return Utils.dispatch_success(request, "SUCCESS") except User.DoesNotExist: return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, *args, **kwargs): """ Return the list of categories :param request: :return: """ try: is_all_category = request.GET.get('all', None) is_sub_category = request.GET.get('sub_category', None) if is_all_category: category_list = Category.objects.all() else: user = request.user user_type = user.user_type if user_type == User.WHOLESALER: products = Product.objects.filter(wholesaler=user) category_list = [product.sub_category.category for product in products] category_list = set(category_list) else: category_list = Category.objects.all() serializer = serializers.CategorySerializer(category_list, many=True, context={"user": request.user, 'is_sub_category': is_sub_category}) return Utils.dispatch_success(request, serializer.data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def post(self, request): """ Post the User email to verify the user exists or not :param request: :return: """ return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') if User.objects.filter( username=request.data['username']) else Utils.dispatch_success(request, ["SUCCESS"])
def get(self, request, order_id): try: order_items = Order.objects.get(id=order_id) return Utils.dispatch_success( request, serializers.OrderSerializer(order_items).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): try: order_items = Order.objects.filter( customer=request.user).order_by('-order_time') return Utils.dispatch_success( request, serializers.OrderSerializer(order_items, many=True).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request): """ :param request: :return: """ try: user = request.user data = request.data serializer = serializers.UpdateDeliveryBoyDetailsSerializer( user, data=data, partial=True) if serializer.is_valid(): serializer.save() return Utils.dispatch_success( request, serializers.DeliveryBoyDetailsSerializer(user).data) return Utils.dispatch_failure(request, serializer.errors) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request, id): """ Update particular store :param request: :param id: :return: """ try: user = User.objects.get(id=id) data = request.data serializer = serializers.BasicUserInfoSerializer(user, data=data, partial=True) if serializer.is_valid(): serializer.save() return Utils.dispatch_success(request, serializer.data) return Utils.dispatch_failure(request, 'VALIDATION_ERROR', serializer.errors) except User.DoesNotExist: return Utils.dispatch_failure(request, 'OBJECT_RESOURCE_NOT_FOUND') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ Returns the list of users :param request: :return: """ try: serializer = serializers.BasicUserInfoSerializer(request.user) return Utils.dispatch_success(request, serializer.data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def post(self, request): """ { "address" : "ADDRESS", "delivery_time" : "4PM - 5PM", "pincode" : "PINCODE", "contact" : "98932782398", "contact_name" : "HJBGFHJSBJA", "delivery_charge": 50 } :param request: :return: """ try: cart_items = Cart.objects.filter(customer=request.user) today = datetime.date.today() order = Order( customer=request.user, order_id= f'ORDER{today.year}{int(today.month):02}{int(today.day):02}{Order.objects.all().count():07}', total_price=sum([ float(cart_item.product.sbs_price) * float(cart_item.quantity) for cart_item in cart_items ]), total_item=len(cart_items), address=request.data['address'], delivery_time=request.data['delivery_time'], contact=request.data['contact'], contact_name=request.data['contact_name'], pincode=request.data['pincode'], delivery_charge=request.data['delivery_charge']) order.save() request.user.address = request.data['address'] request.user.pincode = request.data['pincode'] request.user.save() for cart_item in cart_items: ordered_product = OrderedProducts( order=order, product=cart_item.product, quantity=cart_item.quantity, price_per_item=cart_item.product.sbs_price, image=cart_item.image, total_price=float(cart_item.product.sbs_price) * float(cart_item.quantity), comments=cart_item.comments) ordered_product.save() cart_item.delete() return Utils.dispatch_success(request, 'SUCCESS') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, order_id): """ Provide Order ID to get Details :param request: :param order_id: :return: """ try: order_item = Order.objects.get(id=order_id) return Utils.dispatch_success(request, OrderSerializer(order_item).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def post(self, request): """ request_data = { "username" : 9876543210, "user_type": "WHOLSESALER", # or 'CUSTOMER' "name": "STORE_NAME", "password": "******", "address":"Address" } """ try: data = request.data try: User.objects.get(username=data["username"]) except User.DoesNotExist: user = User(username=data["username"], user_type=data["user_type"], first_name=data["name"], address=data.get("address", '')) user.save() if data.get('email'): user.email = data['email'] user.set_password(data['password']) if user.user_type == User.CUSTOMER: if data.get('referral_code'): referred_user = User.objects.filter(referral_code=data['referral_code']) if referred_user: user.referred_by = referred_user user.referral_code = f"{user.first_name[:4].upper()}" \ f"{(int(hashlib.sha256(user.username.encode('utf-8')).hexdigest(), 16) % 10 ** 6)}" user.save() return Utils.dispatch_success(request, ['SUCCESS']) else: return Utils.dispatch_failure(request, 'VALIDATION_ERROR') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ :param request: :return: """ try: user = request.user return Utils.dispatch_success( request, serializers.DeliveryBoyDetailsSerializer(user).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def delete(self, request, cart_id): """ :param request: :return: """ try: cart = Cart.objects.filter(id=cart_id).first() if cart: cart.delete() return Utils.dispatch_success(request, 'SUCCESS') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, *args, **kwargs): """ Return the list of categories :param request: :return: """ try: category_list = Category.objects.all() serializer = serializers.CustomerCategorySerializer(category_list, many=True) return Utils.dispatch_success(request, serializer.data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ pagination added use - page_number = 1 Page size is customizable - page_size = 10 ----------------------------------------------- for Completed orders give param status=completed :param request: :return: """ try: if request.GET.get('status') == 'completed': queryset = Order.objects.filter(delivery_boy=request.user, order_status=Order.DELIVERED) else: queryset = Order.objects.filter( delivery_boy=request.user, order_status__in=[Order.PACKED, Order.PICKUPED]) page_size = request.GET.get('page_size', None) if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number', None) if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = OrderSerializer(queryset, many=True) response_data = { "data": serializer.data if queryset else [], "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1, "total": len(queryset) } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ for pending Orders param = order_status=pending for order history no param pagination added use - page_number = 1 Page size is customizable - page_size = 10 :param request: :return: """ try: if request.GET.get('order_status') == 'pending': queryset = Order.objects.filter(order_status=Order.PENDING).order_by('order_time') else: queryset = Order.objects.all().exclude(order_status=Order.PENDING).order_by('-order_time') page_size = request.GET.get('page_size') if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number') if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = OrderSerializer(queryset, many=True) response_data = { "data": serializer.data if queryset else [], "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1 } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ returns the list all the products based on queryset :param request: :note: pagination added use - page_number = 1 Page size is customizable - page_size = 10 :return: """ try: queryset = Product.objects.filter(is_published=True) page_size = request.GET.get('page_size') if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number') if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = serializers.CustomerProductSerializer(queryset, many=True) data = serializer.data shuffle(data) else: data = [] response_data = { "data": data, "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1 } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request, sub_category_id): """ returns the list of products :param request: :param sub_category_id: sub_category_id :note: for recently added use - recent=true query param pagination added use - page_number = 1 Page size is customizable - page_size = 10 :return: """ try: if request.user.user_type == User.WHOLESALER: queryset = Product.objects.filter(sub_category=sub_category_id, wholesaler=request.user) else: if request.GET.get('recent', None): queryset = Product.objects.filter(is_edited=True, is_drafted=False).order_by('-updated_at') else: queryset = Product.objects.filter(sub_category=sub_category_id) page_size = request.GET.get('page_size', None) if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number', None) if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = serializers.ProductSerializer(queryset, many=True) response_data = { "data": serializer.data if queryset else [], "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1 } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ :param request: :return: """ try: queryset = SubCategory.objects.filter( is_fav=True).order_by('fav_order') return Utils.dispatch_success( request, serializers.FavoriteSerializer(queryset, many=True).data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ returns the list all the productsbased on queryset :param request: :note: for not published - not_published=true query param for published - published=true query param pagination added use - page_number = 1 Page size is customizable - page_size = 10 :return: """ try: if request.GET.get('not_published'): queryset = Product.objects.filter(is_drafted=True, is_published=False).order_by('-updated_at') elif request.GET.get('published'): queryset = Product.objects.filter(is_published=True).order_by('-updated_at') else: queryset = Product.objects.all().order_by('-updated_at') page_size = request.GET.get('page_size') if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number') if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = serializers.ProductSerializer(queryset, many=True) response_data = { "data": serializer.data if queryset else [], "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1 } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def put(self, request): """ To Update Buyed need to send response { "order_products" : [1,2,3,4,5,6,7,8,9,10] } note: These id's are ordered_products :param request: :return: """ try: data = request.data OrderedProducts.objects.filter(id__in=data['order_products']).update(is_admin_buyed=True) return Utils.dispatch_success(request, 'SUCCESS') except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})
def get(self, request): """ pagination added use - page_number = 1 Page size is customizable - page_size = 10 param = is_available = true :param request: :return: """ try: if request.GET.get('is_available'): queryset = User.objects.filter(user_type=User.DELIVERY_MAN, is_available=True) return Utils.dispatch_success(request, serializers.BasicUserInfoSerializer(queryset, many=True).data) else: queryset = User.objects.filter(user_type=User.DELIVERY_MAN) page_size = request.GET.get('page_size') if page_size: page_size = int(page_size) else: page_size = 10 page_number = request.GET.get('page_number') if page_number: page_number = int(page_number) else: page_number = 1 if queryset: paginator = Paginator(queryset, per_page=page_size) queryset = paginator.page(page_number) serializer = serializers.UserManagementSerializer(queryset, many=True) response_data = { "data": serializer.data if queryset else [], "current_page": page_number, "page_size": page_size, "total_pages": paginator.num_pages if queryset else 1 } return Utils.dispatch_success(request, response_data) except Exception as e: print(e) return Utils.dispatch_failure(request, 'INTERNAL_SERVER_ERROR', {"error": str(e)})