コード例 #1
0
ファイル: views.py プロジェクト: Effessio/catalog
def product_add(request, producer_id):
    producer = get_object_or_404(Producer, pk=producer_id)
    if producer.moderator == request.user:
        if request.method == 'POST':
            form = ProductForm(request.POST)
            if form.is_valid():
                product = form.save(commit=False)
                product.producer = producer
                product.save()
                return HttpResponseRedirect(product.get_absolute_url())
        else:
            form = ProductForm()
            return render(request, 'goods/add_product.html', {'form': form})
    else:
        error_message = "you don't have permission to access this page"
        return render(request, 'goods/authorization_error.html', {'error_message': error_message})
コード例 #2
0
ファイル: views.py プロジェクト: Effessio/catalog
def product_edit(request,product_id):
    product = get_object_or_404(Product, pk=product_id)
    if product.producer.moderator == request.user:
        if request.method == 'POST':
            form = ProductForm(request.POST)
            if form.is_valid():
                product.article = form.cleaned_data['article']
                product.name = form.cleaned_data['name']
                product.description = form.cleaned_data['description']
                product.save()
                return HttpResponseRedirect(product.get_absolute_url())
        else:
            form = ProductForm(instance=product)
            return render(request, 'goods/add_product.html', {'form': form})
    else:
        error_message = "you don't have permission to access this page"
        return render(request, 'goods/authorization_error.html', {'error_message': error_message})
コード例 #3
0
ファイル: views.py プロジェクト: MaxGvozd/Krinitsa_crm
def create_product(request):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'products.html', context={'form': form})
    elif request.method == 'POST':
        form = ProductForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'congratulation.html')
        else:
            return render(request, 'products.html', context={"form": form})
コード例 #4
0
def product(request, product_id):
    """ Product endpoint with information and ability to buy.
    :param request:
    :param product_id: id product from Product model.
    :return: for GET - product information page
             for POST - save product in user basket and redirect to basket.
    """
    product = Product.objects.filter(id=product_id).first()

    if request.method == 'GET':
        quantity_form = ProductForm()
        return render(request, 'product.html', {'product': product, 'quantity_form': quantity_form})

    elif request.method == 'POST':
        quantity_form = ProductForm(request.POST)

        if not quantity_form.is_valid():
            return HttpResponse(status=404)

        quantity = quantity_form.cleaned_data["quantity"]

        if request.user.is_authenticated:
            user_id = request.session.get('_auth_user_id')
            user = User.objects.get(pk=user_id)

            basket = Basket.objects.filter(user=user).first()
            basket_item = BasketItem.objects.filter(basket=basket, product=product).first()

            if basket_item is None:
                BasketItem.objects.create(basket=basket, product=product, quantity=int(quantity))
            else:
                basket_item.quantity += int(quantity)
                basket_item.save()

            return redirect('/basket/')
        else:
            return redirect('/login/')
    else:
        return HttpResponse(status=405)