コード例 #1
0
def add(request, pk):
    # print(request.META)
    # для возврата на страницу покупки, после логина при покупке товара
    if 'login' in request.META.get('HTTP_REFERER'):
        return HttpResponseRedirect(reverse('mainapp:product_page', args=[pk]))

    # if not request.user.is_authenticated:
    #     return HttpResponseRedirect(reverse('auth:login'))

    # print(pk, type(pk))
    product = get_object_or_404(Product, pk=pk)
    basket = BasketItem.objects.filter(user=request.user,
                                       product=product).first()
    # basket = request.user.basketitem_set.filter(product=pk).first()

    if not basket:
        basket = BasketItem(user=request.user, product=product)
        # # basket = BasketItem.objects.create(user=request.user, product=product)  # not in db
        # Включить для работы с F объектом для избежания ошибок:
        # basket = BasketItem(user=request.user, product=product, quantity=1)

    # get basket.quantity -> python level -> update value -> python level -> db level
    basket.quantity += 1
    # update value on db level Для корректной работы доработать @receiver(pre_save...) внизу
    # if basket.pk:
    #     basket.quantity = F('quantity') + 1
    basket.save()
    db_profile_by_type(basket, 'UPDATE', connection.queries)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #2
0
ファイル: views.py プロジェクト: Ihor54/Django-shop
def basket_item_add(request, pk):
    product = get_object_or_404(Product, pk=pk)
    old_basket_item = BasketItem.objects.filter(user=request.user,
                                                product=product)

    if old_basket_item:
        old_basket_item[0].quantity += 1
        old_basket_item[0].save()
    else:
        new_basket_item = BasketItem(user=request.user, product=product)
        new_basket_item.quantity += 1
        new_basket_item.save()
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #3
0
ファイル: views.py プロジェクト: SorinGamureac/django_test
def add_product(request, pk):
    # if not request.user.is_authenticated:
    #     return HttpResponseRedirect(reverse('auth:login'))
    product = get_object_or_404(Product, pk=pk)
    basket = BasketItem.objects.filter(user=request.user,
                                       product=product).first()
    # basket = request.user.basketitem_set.filter(product=pk).first()

    if not basket:
        basket = BasketItem(user=request.user, product=product)  # not in db

    basket.quantity += 1
    basket.save()
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #4
0
ファイル: views.py プロジェクト: AlexeyDubrov/Django_geekshop
def add(request, pk):
    if 'login' in request.META.get('HTTP_REFERER'):
        return HttpResponseRedirect(
            reverse('main:product_page', kwargs={'pk': pk}))

    product = get_object_or_404(Product, pk=pk)
    basket = BasketItem.objects.filter(user=request.user,
                                       product=product).first()

    if not basket:
        basket = BasketItem(user=request.user, product=product)

    basket.quantity += 1
    basket.save()
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #5
0
    def get_context_data(self, **kwargs):
        data = super(OrderItemsCreate, self).get_context_data(**kwargs)
        OrderFormSet = inlineformset_factory(Order,
                                             OrderItem,
                                             form=OrderItemForm,
                                             extra=1)

        if self.request.POST:
            formset = OrderFormSet(self.request.POST)
        else:
            basket_items = BasketItem.get_items(self.request.user)
            if len(basket_items):
                OrderFormSet = inlineformset_factory(Order,
                                                     OrderItem,
                                                     form=OrderItemForm,
                                                     extra=len(basket_items))
                formset = OrderFormSet()
                for num, form in enumerate(formset.forms):
                    form.initial['product'] = basket_items[num].product
                    form.initial['quantity'] = basket_items[num].quantity
                    form.initial['price'] = basket_items[num].product.price
                basket_items.delete()
            else:
                formset = OrderFormSet()

        data['orderitems'] = formset
        return data
コード例 #6
0
ファイル: views.py プロジェクト: a-morev/django_course
def add(request, pk):
    if 'login' in request.META.get('HTTP_REFERER'):
        return HttpResponseRedirect(
            reverse('main:product_page', kwargs={'pk': pk}))
    product = get_object_or_404(Product, pk=pk)
    basket = BasketItem.objects.filter(user=request.user,
                                       product=product).first()

    if not basket:
        basket = BasketItem(user=request.user, product=product, quantity=1)

    if basket.pk:
        basket.quantity = F('quantity') + 1
    basket.save()
    # db_profile_by_type(basket, 'UPDATE', connection.queries)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
コード例 #7
0
def add(request, pk):
    if 'login' in request.META.get('HTTP_REFERER'):
        return HttpResponseRedirect(
            reverse('main:product_page', kwargs={'pk': pk}))

    # old_basket_item = BasketItem.get_product(user=request.user, product=product) версия препода Маслова. У меня не работает.
    product = get_object_or_404(Product, pk=pk)
    basket = BasketItem.objects.filter(user=request.user,
                                       product=product).first()

    if not basket:
        basket = BasketItem(user=request.user, product=product)  # not in db

    basket.quantity += 1
    basket.save()

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))