Esempio n. 1
0
def product_view(request, slug):
    context = {}

    item = Item.objects.filter(slug=slug).first()
    if request.POST:
        form = ReviewForm(request.POST)
        if form.is_valid():

            user = request.user
            item = Item.objects.filter(slug=slug).first()
            message = form.cleaned_data.get('message')
            rating = form.cleaned_data.get('rating')

            Review.objects.create(message=message,
                                  rating=rating,
                                  user=user,
                                  item=item)

            return redirect('product', slug=slug)
        else:
            context['form'] = form
    else:
        if request.user.is_authenticated:
            bought_by_user = OrderItem.objects.filter(
                item=item, order__user=request.user,
                order__is_paid=True).exists()
            if bought_by_user:
                context['bought_by_user'] = True
                context['form'] = ReviewForm()

    context['item'] = item
    context['reviews'] = Review.objects.filter(item=item)

    return render(request, 'product.html', context)
Esempio n. 2
0
def item_view(request, section_name, subsection_name, item_name):
    form = ReviewForm()
    item = get_object_or_404(Item, slug=item_name)
    reviews = Review.objects.filter(item=item)
    
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid():
            Review.objects.create(
                item=item,
                name=form.cleaned_data['name'],
                text=form.cleaned_data['text'],
                stars=form.cleaned_data['stars'],
            )
            return redirect(f'/catalog/{section_name}/{subsection_name}/{item_name}')
    
    return render(
        request,
        'item_page.html',
        {
            'item': item,
            'reviews': reviews,
            'form': form,
        }
    )
Esempio n. 3
0
 def post(self, request, *args, **kwargs):
     if request.user.is_authenticated:
         form = ReviewForm(loads(request.body.decode('utf-8')))
         if form.is_valid():
             review = form.save()
             review.author = request.user.name
             review.save()
             return JsonResponse({'result': True})
         else:
             return JsonResponse({'result': False, 'errors': form.errors})
     else:
         return JsonResponse({'result': False, 'authenticate': True})
Esempio n. 4
0
def add_review(request, id):
    if not request.user.is_authenticated:
        messages.info(request, "You need to be logged in in order to give a review")
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.product = Product.objects.get(id=id)
            review.user = request.user
            review.save()
            messages.success(request, 'Review saved')
            return redirect('shop:products_detail', id)
    else:
        return redirect('shop:products_detail', id)
Esempio n. 5
0
    def post(self, request, product_id):
        product = Product.objects.get(pk=product_id)
        form = ReviewForm(request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.product = product
            review.user = request.user
            review.save()
            return redirect('/')

        context = {
            'form': form,
            'product': product
        }

        return render(request, 'product.html', context)
Esempio n. 6
0
def detail(request, slug):
    product = Product.objects.get(active=True, slug=slug)
    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.product = product
            review.user = request.user
            review.save()
            messages.success(request, "Review saved")
        else:
            messages.error(request, "Invalid form")
    else:
        form = ReviewForm()

    categories = Category.objects.filter(active=True)
    context = {"product": product, "categories": categories, "form": form}
    return render(request, "shop/detail.html", context)
def add_review(request, id):
    cart_product_form = CartAddProductForm()
    target_product = get_object_or_404(Product, id=id, available=True)
    reviews = Review.objects.all().filter(product=target_product)
    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.product = target_product
            review.author = request.user
            review.published_date = timezone.now()
            review.save()
            return render(
                request, 'shop/product/detail.html', {
                    'product': target_product,
                    'cart_product_form': cart_product_form,
                    'reviews': reviews
                })
    else:
        form = ReviewForm()
    return render(request, 'shop/product/add_review.html', {'form': form})
Esempio n. 8
0
def good_view(request, good_id):
    form = ReviewForm()
    is_feedback = check_feedback(request, good_id)
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        if form.is_valid():
            if is_feedback:
                try:
                    user_id = request.session['_auth_user_id']
                except KeyError:
                    user_id = 0
                if user_id:
                    Feedback(
                        person_name=str(form.cleaned_data['person_name']),
                        description=str(form.cleaned_data['description']),
                        score=str(form.cleaned_data['score']),
                        good_id=good_id,
                        author_id=request.session['_auth_user_id'],
                        csrftoken=request.COOKIES['csrftoken'],
                    ).save()
                else:
                    Feedback(
                        person_name=str(form.cleaned_data['person_name']),
                        description=str(form.cleaned_data['description']),
                        score=str(form.cleaned_data['score']),
                        good_id=good_id,
                        csrftoken=request.COOKIES['csrftoken'],
                    ).save()
                return redirect('good', good_id=good_id)
    else:
        form = form
    context = {
        'form': form,
        'good_id': good_id,
        'good': get_good_info_and_feedback(good_id)[0],
        'menus': get_menu(),
        'is_feedback': is_feedback,
    }
    add_good_to_cart(request)
    return render(request, 'good.html', context)
Esempio n. 9
0
def product_detail(request, id, slug, category_slug=None):
    category = None
    categories = Category.objects.all()
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
    product = get_object_or_404(Product, id=id, slug=slug, available=True)
    cart_product_form = CartAddProductForm()

    # отзыв на продукт
    reviews = Review.objects.filter(product=id)
    reviews_count = Review.objects.all()
    if request.method == "POST":  #после отправки данных формы на сервер методом POST
        form = ReviewForm(request.POST)
        if form.is_valid():
            review_f = form.save(commit=False)
            review_f.author = request.user
            review_f.date = datetime.now()
            review_f.product = Product.objects.get(id=id)
            review_f.save()

            return redirect(
                'shop:product_detail',
                id=product.id,
                slug=product.slug,
            )  #переадресация на ту же стр
    else:
        form = ReviewForm()  #создание формы для ввода комментария

    assert isinstance(request, HttpRequest)
    return render(
        request, 'shop/product/detail.html', {
            'product': product,
            'form': form,
            'reviews': reviews,
            'reviews_count': reviews_count,
            'cart_product_form': cart_product_form,
            'category': category,
            'categories': categories
        })
Esempio n. 10
0
def detail(request, slug):
    product = Product.objects.get(active=True, slug=slug)
    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid():
            review = form.save(commit=False)
            review.product = product
            review.user = request.user
            review.save()
            messages.success(request, "Review saved")
        else:
            messages.error(request, "Invalid form")
    else:
        form = ReviewForm()

    categories = Category.objects.filter(active=True)
    products = Product.objects.filter(active=True)
    shops = Main_Category.objects.all()
    sess = request.session.get("data", {"items": []})
    cart_products = Product.objects.filter(active=True, slug__in=sess["items"])
    cart_products_sum = Product.objects.filter(
        active=True,
        slug__in=sess["items"]).aggregate(Sum('price'))['price__sum']
    site_data = site_settings.objects.first()

    context = {
        "product": product,
        "categories": categories,
        "slug": slug,
        "products": products,
        "shops": shops,
        "cart_products_sum": cart_products_sum,
        "cart_products": cart_products,
        "site_data": site_data
    }

    return render(request, "shop/detail.html", context)
Esempio n. 11
0
 def test_invalid_form(self):
     # No data passed
     form = ReviewForm()
     self.assertFalse(form.is_valid())
Esempio n. 12
0
 def test_valid_form(self):
     data = {"review": "Text"}
     form = ReviewForm(data=data)
     self.assertTrue(form.is_valid())
Esempio n. 13
0
 def test_review_form(self):
     data = {"review": self.review}
     form = ReviewForm(data=data)
     self.assertEqual(form.is_valid(), self.expected_result)