def get(self, request):
        user = request.user
        orders = user.get_finished_orders()
        page_number = request.GET.get('page', 1)
        pag_res = pagination(orders, page_number, request.GET.copy())

        context = {
            'page_object': pag_res['page'],
            'is_paginated': pag_res['is_paginated'],
            'next_url': pag_res['next_url'],
            'prev_url': pag_res['prev_url'],
        }
        return render(request, 'users/user-orders-finished.html', context)
    def get(self, request):
        news = News.objects.filter(is_active=True).select_related('category')

        try:
            parents = Page.objects.get(action='news').get_ancestors(ascending=False, include_self=False)
        except:
            parents = Page.objects.none()

        page_number = request.GET.get('page', 1)
        pag_res = pagination(news, page_number, request.GET.copy())

        context = {
            'news': news,
            'parents': parents,

            'page_object': pag_res['page'],
            'is_paginated': pag_res['is_paginated'],
            'next_url': pag_res['next_url'],
            'prev_url': pag_res['prev_url'],
        }
        return render(request, 'news/news.html', context)
    def get(self, request, product_slug):
        product = get_object_or_404(Product.objects.select_related('category'), slug=product_slug, is_active=True)
        main_offer = product.main_offer()

        color_id = main_offer.color.id if main_offer.color else None
        size_id = main_offer.size.id if main_offer.size else None
        cup_id = main_offer.cup.id if main_offer.cup else None

        colors = product.get_colors()
        sizes = product.get_sizes(color_id=color_id)
        cups = product.get_cups(color_id=color_id, size_id=size_id)

        reviews = Review.objects.select_related('order_item', 'order_item__offer', 'order_item__offer__product' ).filter(order_item__offer__product__id=product.id)

        try:
            rating = round(sum(int(review.get_rating_display()) for review in reviews) / reviews.count())
        except:
            rating = 0

        page_number = request.GET.get('page', 1)
        pag_res = pagination(reviews, page_number, request.GET.copy())

        order_ocf = OrderOneClickForm()

        context = {
            'order_ocf': order_ocf,
            'colors': colors,
            'cups': cups,
            'sizes': sizes,
            'product': product,
            'main_offer': main_offer,
            'reviews': reviews,
            'rating': str(rating),

            'page_object': pag_res['page'],
            'is_paginated': pag_res['is_paginated'],
            'next_url': pag_res['next_url'],
            'prev_url': pag_res['prev_url'],
        }
        return render(request, 'products/product.html', context)
    def get(self, request):
        query = request.GET.get('query')
        category_slug = request.GET.get('category')
        is_new = request.GET.get('is_new')
        in_sale = request.GET.get('in_sale')
        is_bs = request.GET.get('is_bs')
        price_min = request.GET.get('price_min')
        price_max = request.GET.get('price_max')
        color_list = request.GET.getlist('color', [])
        size_list = request.GET.getlist('size', [])
        cup_list = request.GET.getlist('cup', [])
        pushup = request.GET.getlist('pushup', [])
        ordering = request.GET.get('ordering', 1)

        get_request = request.GET.copy()

        category = Category.objects.none()
        if category_slug:
            category = get_object_or_404(Category, slug=category_slug)
            enrty_offers = Offer.objects.filter(
                product__category=category, product__is_active=True, is_active=True).select_related(
                'product', 'promotion_sale', 'color', 'size', 'cup').annotate(
                price_with_sale=F('product__price') * (100 - Coalesce(F('promotion_sale__sale'), 0)) / 100)
        else:
            enrty_offers = Offer.objects.filter(product__is_active=True, is_active=True).select_related(
                'product', 'promotion_sale', 'color', 'size', 'cup').annotate(
                price_with_sale=F('product__price') * (100 - Coalesce(F('promotion_sale__sale'), 0)) / 100)

        try:
            in_sale = int(in_sale)
            enrty_offers = enrty_offers.exclude(promotion_sum_present=None, promotion_three_sales=None, promotion_min_present=None, promotion_sale=None)
        except:
            get_request.pop('in_sale', None)

        if query:
            enrty_offers = enrty_offers.filter(
                Q(product__name__icontains=query) |
                Q(product__vendor_code__icontains=query) |
                Q(product__category__name__icontains=query)
            ).distinct()

        try:
            is_new = int(is_new)
            enrty_offers = enrty_offers.filter(product__is_new=is_new)
        except:
            get_request.pop('is_new', None)

        try:
            is_bs = int(is_bs)
            enrty_offers = enrty_offers.filter(product__is_bs=is_bs)
        except:
            get_request.pop('is_bs', None)

        try:
            price_min = int(price_min)
            enrty_offers = enrty_offers.filter(price_with_sale__gte=price_min)
        except:
            get_request.pop('price_min', None)

        try:
            price_max = int(price_max)
            enrty_offers = enrty_offers.filter(price_with_sale__lte=price_max)
        except:
            get_request.pop('price_max', None)


        offers = enrty_offers

        if pushup:
            try:
                pushup = list(map(int, pushup))
                offers = offers.filter(product__pushup__in=pushup)
            except:
                get_request.pop('pushup', None)

        if color_list:
            try:
                color_list = list(map(int, color_list))
                offers = offers.filter(color__in=color_list)
            except:
                get_request.pop('color_list', None)

        if size_list:
            try:
                size_list = list(map(int, size_list))
                offers = offers.filter(size__in=size_list)
            except:
                get_request.pop('size_list', None)

        if cup_list:
            try:
                cup_list = list(map(int, cup_list))
                offers = offers.filter(cup__in=cup_list)
            except:
                get_request.pop('cup_list', None)

        offers = offers.order_by('product__id', 'price_with_sale')

        main_offers = []
        offer_old = None
        for offer in offers:
            if offer.product.id != offer_old:
                main_offers.append(offer)
                offer_old = offer.product.id

        if ordering:
            try:
                ordering = int(ordering)
                if ordering == 1:
                    main_offers.sort(key=lambda offer: offer.product.is_bs, reverse=True)
                elif ordering == 2:
                    main_offers.sort(key=lambda offer: offer.price_with_sale)
                elif ordering == 3:
                    main_offers.sort(key=lambda offer: offer.price_with_sale, reverse=True)
                elif ordering == 4:
                    main_offers.sort(key=lambda offer: offer.product.is_new, reverse=True)
                elif ordering == 5:
                    main_offers.sort(key=lambda offer: offer.promotion_sale if offer.promotion_sale else 0, reverse=True)
            except:
                get_request.pop('ordering', None)

        page_number = request.GET.get('page', 1)
        pag_res = pagination(main_offers, page_number, get_request)

        prop_list = {'colors': set(), 'sizes': set(), 'cups': set(), 'has_pushup': False}
        for offer in enrty_offers:
            if offer.color:
                prop_list['colors'].add(offer.color.id)
            if offer.size:
                prop_list['sizes'].add(offer.size.id)
            if offer.cup:
                prop_list['cups'].add(offer.cup.id)
            if offer.product.pushup is not None and not prop_list['has_pushup']:
                prop_list['has_pushup'] = True

        colors = Color.objects.filter(id__in=prop_list['colors']).annotate(checked=Case(
            When(id__in=color_list, then=Value(1)),
            default=0,
            output_field=BooleanField()
        ))

        sizes = Size.objects.filter(id__in=prop_list['sizes']).annotate(checked=Case(
            When(id__in=size_list, then=Value(1)),
            default=0,
            output_field=BooleanField()
        ))

        cups = Cup.objects.filter(id__in=prop_list['cups']).annotate(checked=Case(
            When(id__in=cup_list, then=Value(1)),
            default=0,
            output_field=BooleanField()
        ))

        if category:
            category_name = category.name
        elif in_sale:
            category_name = 'Распродажа'
        elif is_new:
            category_name = 'Новинки'
        elif is_bs:
            category_name = 'Хиты продаж'
        else:
            category_name = 'Все товары'

        context = {
            'price_min': price_min or '',
            'price_max': price_max or '',
            'ordering': ordering,
            'category_slug': category_slug,
            'category_name': category_name,

            'colors': colors,
            'sizes': sizes,
            'cups': cups,
            'pushup': pushup,
            'has_pushup': prop_list['has_pushup'],
            'category': category,
            'main_offers': main_offers,

            'page_object': pag_res['page'],
            'is_paginated': pag_res['is_paginated'],
            'next_url': pag_res['next_url'],
            'prev_url': pag_res['prev_url'],
        }
        return render(request, 'products/products.html', context)