def test_invalid(self):
     form_data = {"title": "", "body": "", "score": ""}
     form = ReviewForm(data=form_data)
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors["title"], [u"This field is required."])
     self.assertEqual(form.errors["body"], [u"This field is required."])
     self.assertEqual(form.errors["score"], [u"This field is required."])
Exemplo n.º 2
0
def productdetails(request, id):
    if 'username' not in request.session:
        reviewform = ReviewForm(request.POST or None)
    else:
        reviewform = ReviewForm(request.POST or None,
                                initial={
                                    'review_by': request.session['username'],
                                    'product_id': id
                                })

    if request.method == "POST":
        if reviewform.is_valid():
            review_by = reviewform.cleaned_data.get('review_by')
            description = reviewform.cleaned_data.get('description')
            review = Review(review_by=review_by,
                            description=description,
                            product_id=id)
            try:
                review.save()
            except:
                return HttpResponse("<h1> Unable to sumbit review. </h1>")
    product = Products.objects.get(id=id)
    reviews = Review.objects.filter(product_id=id)
    cart_obj, cart_created = Cart.objects.new_or_get(request)
    context = {
        'product': product,
        'reviewform': reviewform,
        'reviews': reviews,
        'cart': cart_obj
    }
    return render(request, 'front/productdetails.html', context)
 def test_valid(self):
     form_data = {
         "title": "My review",
         "body": "Review content",
         "score": "3"
     }
     form = ReviewForm(data=form_data)
     self.assertTrue(form.is_valid())
Exemplo n.º 4
0
 def test_review_rating_is_required(self):
     form = ReviewForm({
         'review_rating': '',
     })
     self.assertFalse(form.is_valid())
     self.assertIn('review_rating', form.errors.keys())
     self.assertEqual(form.errors['review_rating'][0],
                      "This field is required")
Exemplo n.º 5
0
 def test_review_text_max_length(self):
     form = ReviewForm({
         'review_text':
         'Lorem ipsum dolor sit amet consectetur, adipisicing elit. At ducimus est eum odio consequatur in ipsa ipsam ullam accusantium dolorem? Mollitia facilis quisquam, voluptate iure facere numquam delectus nesciunt reiciendis. Lorem ipsum dolor sit amet consectetur, adipisicing elit. At ducimus est eum odio consequatur in ipsa ipsam ullam accusantium dolorem? Mollitia facilis quisquam, voluptate iure facere numquam delectus nesciunt reiciendis. Lorem ipsum dolor sit amet consectetur, adipisicing elit. At ducimus est eum odio consequatur in ipsa ipsam ullam accusantium dolorem? Mollitia facilis quisquam, voluptate iure facere numquam delectus nesciunt reiciendis.',
         'review_rating': 5,
     })
     self.assertFalse(form.is_valid())
     self.assertIn('review_text', form.errors.keys())
     self.assertEqual(
         form.errors['review_text'][0],
         "Try keep it short and sweet, maximum 500 characters")
Exemplo n.º 6
0
 def test_valid_review_form_submits(self):
     form = ReviewForm({
         'review_rating': '5',
         'review_text': 'Some text',
         'review_date': datetime.datetime.now(),
         'user_anonymous': False,
     })
     self.assertTrue(form.is_valid)
Exemplo n.º 7
0
def shop_item(request, slug):
    product = get_object_or_404(Product, slug=slug)
    reviews = Review.objects.filter(category=product.slug)
    form = ReviewForm()
    ctx = {
        'product': product,
        'reviews': reviews,
        'form': form,
    }
    return render(request, 'shop_item.html', ctx)
def favourite_product(request, pk):
    """Allow the user to 'Favourite' a product and
    display on profile page """
    product = get_object_or_404(Product, pk=pk)
    if request.user.is_authenticated:
        user = request.user
        if Favourite.objects.filter(user=user, product=product).count() == 0:
            favourite = Favourite()
            favourite.user = user
            favourite.product = product
            favourite.save()
        else:
            Favourite.objects.filter(user=user, product=product).delete()

    form = ReviewForm()
    return redirect(product_detail, product.pk)
Exemplo n.º 9
0
def review(request):
    title = request.POST.get('title')
    category = request.POST.get('product_slug')
    star = request.POST.get('star')
    description = request.POST.get('description')
    if request.method == 'POST':
        form = ReviewForm(request.POST)
        form.title = title
        form.star = star
        form.description = description
        if form.is_valid():
            review = form.save(commit=False)
            review.category = category
            review.save()
            return render(request, 'review_new_ajax.html', {
                'review': review,
            })
    return redirect('shop_item', slug=category)
Exemplo n.º 10
0
 def test_max_value_of_review_rating(self):
     form = ReviewForm({'review_rating': 10})
     self.assertFalse(form.is_valid())
     self.assertIn('review_rating', form.errors.keys())
     self.assertEqual(form.errors['review_rating'][0],
                      "The maximum value is 5")