Beispiel #1
0
    def get(self, request, *args, **kwargs):
        all_carts = Cart.objects.filter(user=request.user, is_ordered=False)
        carts = []
        for d in all_carts:
            data = {
                'id': d.id,
                'item_id': d.item_id,
                'name': d.item.name,
                'rate': d.rate,
                'weight': d.weight,
                'count': d.count,
                'image': str(d.item.image),
                'discount': d.item.discount,
                'discount_type': d.item.discount_type,
            }
            carts.append(data)

        data = {
            'carts': carts,
            'cart_count': get_cart_count(request.user),
            'favourite_count': get_favourite_count(request.user),
            'cart_total_amount': get_cart_total_amount(request.user),
        }

        return Response({'status': True, 'data': data}, status=ok)
Beispiel #2
0
    def get(self, request, *args, **kwargs):
        all_favourites = Favourite.objects.filter(user=request.user)
        favourites = []
        for d in all_favourites:
            data = {
                'id': d.id,
                'name': d.item.name,
                'item_id': d.item_id,
                'image': str(d.item.image)
            }
            favourites.append(data)

        data = {
            'favourites': favourites,
            'cart_count': get_cart_count(request.user),
            'favourite_count': get_favourite_count(request.user),
        }

        return Response({'status': True, 'data': data}, status=ok)
Beispiel #3
0
    def get(self, request, *args, **kwargs):
        all_carts = Cart.objects.filter(user=request.user, is_ordered=False)
        all_address = Address.objects.filter(user=request.user)
        all_city = City.objects.all()
        all_pincode = Pincode.objects.all()
        all_address = AddressSerializer(all_address, many=True)
        all_city = CitySerializer(all_city, many=True)
        all_pincode = PincodeSerializer(all_pincode, many=True)
        carts = []
        for d in all_carts:
            data = {
                'id': d.id,
                'name': d.item.name,
                'rate': d.rate,
                'weight': d.weight,
                'count': d.count,
            }
            carts.append(data)

        wallet = Wallet.objects.filter(user=request.user).first()
        balance = 0
        if wallet:
            balance = wallet.balance

        cart_total_amount = get_cart_total_amount(request.user),
        shipping_charge = 0
        if cart_total_amount[0] < 500:
            shipping_charge = 60
        data = {
            'carts': carts,
            'address': all_address.data,
            'pincode': all_pincode.data,
            'city': all_city.data,
            'cart_count': get_cart_count(request.user),
            'favourite_count': get_favourite_count(request.user),
            'cart_total_amount': cart_total_amount[0],
            'wallet_balance': balance,
            'shipping_charge': shipping_charge,
        }

        return Response({'status': True, 'data': data}, status=ok)
Beispiel #4
0
    def get(self, request, *args, **kwargs):
        categories = ItemCategory.objects.all()
        main_slides = MainSlideImage.objects.filter(is_available=True)
        sub_slides = SubSlideImage.objects.filter(is_available=True)

        cart_count = 0
        favourite_count = 0
        if request.user.is_authenticated:
            cart_count = get_cart_count(request.user)
            favourite_count = get_favourite_count(request.user)

        category_details = []
        for d in categories:
            details = {
                'id': d.id,
                'name': d.name,
                'slug': d.slug,
                'image': str(d.image) if d.image else None,
                'created_at': d.created_at,
                'updated_at': d.updated_at,
            }
            category_details.append(details)

        main_slide_images = []
        sub_slide_images = []
        for d in main_slides:
            data = {'image': str(d.image)}
            main_slide_images.append(data)
        for d in sub_slides:
            data = {'image': str(d.image)}
            sub_slide_images.append(data)

        data = {
            'category_details': category_details,
            'cart_count': cart_count,
            'favourite_count': favourite_count,
            'main_slide_images': main_slide_images,
            'sub_slide_images': sub_slide_images,
        }
        return Response({'status': True, 'data': data}, status=ok)
Beispiel #5
0
def get_item(request, sub_category_id, item_slug, item_id):
    item = Item.objects.filter(id=item_id).first()
    if not item:
        return Response({'status': False}, status=not_found)

    item = ItemSerializer(item, many=False)
    cart_count = 0
    favourite_count = 0
    is_in_cart = None
    if request.user.is_authenticated:
        cart_count = get_cart_count(request.user)
        favourite_count = get_favourite_count(request.user)
        check = Cart.objects.filter(user=request.user, item_id=item_id).first()
        if check:
            is_in_cart = True
        else:
            is_in_cart = False
    data = {
        'cart_count': cart_count,
        'favourite_count': favourite_count,
        'item': item.data,
        'is_in_cart': is_in_cart,
    }
    return Response({'status': True, 'data': data}, status=ok)
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        data = request.data['cart']
        print(data)
        item_rate = int(data['rate'])
        weight = None
        rate = item_rate
        if data['divide_by'] == '1':
            weight = data['weight']

        elif data['divide_by'] == '2':
            if data['value'] == '2':
                weight = data['weight'] / 2
                rate = item_rate / 2
            else:
                weight = data['weight']

        elif data['divide_by'] == '4':
            if data['value'] == '4':
                weight = data['weight'] / 4
                rate = item_rate / 4
            elif data['value'] == '3':
                weight = data['weight'] - data['weight'] / 4
                rate = item_rate - item_rate / 4
            elif data['value'] == '2':
                weight = data['weight'] / 2
                rate = item_rate / 2
            else:
                weight = data['weight']

        is_exist = Cart.objects.filter(item=data['id'],
                                       weight=weight,
                                       user=request.user,
                                       is_ordered=False).first()
        if is_exist:
            is_exist.count += data['count']
            is_exist.save()
            cart_count = get_cart_count(request.user)
            data = {
                'cart_count': cart_count,
            }
            return Response(
                {
                    'status': True,
                    'data': data,
                    'message': 'Success! Cart updated'
                },
                status=ok)

        Cart(
            weight=weight,
            count=data['count'],
            rate=rate,
            item_id=data['id'],
            user_id=request.user.id,
        ).save()

        cart_count = get_cart_count(request.user)
        data = {
            'cart_count': cart_count,
        }
        return Response(
            {
                'status': True,
                'data': data,
                'message': 'Success! Added into Cart'
            },
            status=ok)
Beispiel #7
0
def get_order(request, *args, **kwargs):
    orders = Order.objects.filter(user=request.user).order_by('-date', '-time')
    print(len(orders))
    all_items = Cart.objects.filter(user=request.user, is_ordered=True)

    total, current, delivered, canceled = 0, 0, 0, 0
    ordered_items = []
    for order in orders:
        total += 1
        if order.status == 'Delivered':
            delivered += 1
        elif order.status == 'Canceled':
            canceled += 1
        else:
            current += 1
        items = []
        for item in all_items:
            if order.id == item.order_id:
                data = {
                    'id': item.id,
                    'name': item.item.name,
                    'order_id': item.order_id,
                    'weight': item.weight,
                    'count': item.count,
                    'rate': item.rate,
                }

                items.append(data)
        if not items:
            continue
        else:
            ordered_items.append({
                'order_id': order.id,
                'payment': order.payment,
                'status': order.status,
                'total_cost': order.total_cost,
                'shipping_charge': order.shipping_charge,
                'items': items,
                'address': {
                    'address_type': order.address.address_type,
                    'home_number': order.address.home_number,
                    'street': order.address.street,
                    'area': order.address.area,
                    'landmark': order.address.landmark,
                    'city': order.address.city.name,
                    'pincode': order.address.pincode.pincode,
                }
            })
            print(len(ordered_items))

    orders_count = {
        'total': total,
        'current': current,
        'delivered': delivered,
        'canceled': canceled,
    }
    data = {
        'cart_count': get_cart_count(request.user),
        'favourite_count': get_favourite_count(request.user),
        'ordered_items': ordered_items,
        'orders_count': orders_count
    }
    return Response({'status': True, 'data': data}, status=ok)
Beispiel #8
0
def get_items(request, category_name, category_id, sub_category_id=None):
    paginate = '2'
    all_categories = ItemCategory.objects.all()
    all_sub_categories = ItemSubCategory.objects.filter(
        category_id=category_id)
    all_sub_category_ids = []
    if sub_category_id:
        all_sub_category_ids.append(sub_category_id)
    else:
        all_sub_category_ids = pluck(all_sub_categories, 'id')
    if 'search_keyword' in request.GET:
        search_keyword = request.GET['search_keyword']
        sort_by = request.GET['sort'].strip()
        if sort_by == 'A':
            all_items = Item.objects.filter(
                sub_category_id__in=all_sub_category_ids,
                name__contains=search_keyword,
                is_available=True).order_by('name')
        elif sort_by == 'D':
            all_items = Item.objects.filter(
                sub_category_id__in=all_sub_category_ids,
                name__contains=search_keyword,
                is_available=True).order_by('-name')
        elif sort_by == 'L':
            all_items = Item.objects.filter(
                sub_category_id__in=all_sub_category_ids,
                name__contains=search_keyword,
                is_available=True).order_by('rate')
        elif sort_by == 'H':
            all_items = Item.objects.filter(
                sub_category_id__in=all_sub_category_ids,
                name__contains=search_keyword,
                is_available=True).order_by('-rate')
        else:
            all_items = Item.objects.filter(
                sub_category_id__in=all_sub_category_ids,
                name__contains=search_keyword,
                is_available=True).order_by('name')
    else:
        search_keyword = ''
        sort_by = ''

        all_items = Item.objects.filter(
            sub_category_id__in=all_sub_category_ids,
            is_available=True).order_by('name')

    paginator = Paginator(all_items, paginate)
    page = request.GET.get('page')
    try:
        all_items = paginator.page(page)
    except PageNotAnInteger:
        all_items = paginator.page(1)
    except EmptyPage:
        all_items = paginator.page(paginator.num_pages)

    all_sub_categories = ItemSubCategory.objects.all()
    category_names = []
    for category in all_categories:
        sub_categories = []
        for sub_category in all_sub_categories:
            if category.id == sub_category.category_id:
                sub_categories.append({
                    'id': sub_category.id,
                    'name': sub_category.name,
                    'slug': sub_category.slug,
                })

        category_names.append({
            'id': category.id,
            'name': category.name,
            'slug': category.slug,
            'sub_categories': sub_categories,
        })
    all_categories = ItemCategorySerializer(all_categories, many=True)
    all_sub_categories = ItemSubCategorySerializer(all_sub_categories,
                                                   many=True)
    items = ItemSerializer(all_items, many=True)

    cart_count = 0
    favourite_count = 0
    if request.user.is_authenticated:
        cart_count = get_cart_count(request.user)
        favourite_count = get_favourite_count(request.user)

    # if all_items.paginator:
    #     print(dir(all_items))
    #     if all_items.has_previous:
    #         print(all_items.next_page_number,'***********')
    #     else:
    #         print('no')
    data = {
        'items': items.data,
        'categories': all_categories.data,
        'sub_categories': all_sub_categories.data,
        'category_names': category_names,
        'search_keyword': search_keyword,
        'sort_by': sort_by,
        'cart_count': cart_count,
        'favourite_count': favourite_count,
    }
    return Response({'status': True, 'data': data}, status=ok)