def comment(request): if request.method == 'POST': commentform = CommentForm(request.POST) if commentform.is_valid(): comment = commentform.cleaned_data['comment'] listing = commentform.cleaned_data['item'] new_comment = Comment(comment=comment, user=request.user, item=listing) new_comment.save() return render(request, 'auctions/listing.html', get_listing_view_context(listing)) return redirect(reverse('listing_view')) return redirect(reverse('listing_view'))
def bid_view(request, listing_id): listing = Listing.objects.get(id=listing_id) if request.method == "POST": form = BidForm(request.POST, listing=listing) if form.is_valid(): bid = form.save(commit=False) bid.bidder = request.user bid.listing = listing bid.save() return HttpResponseRedirect(reverse("listing", args=(listing.id, ))) else: return render( request, "auctions/listing.html", { "listing": listing, "comment_form": CommentForm(), "comments": Comment.objects.filter(listing=listing)[::-1], "bid_form": form, "bids": Bid.objects.filter(listing=listing)[::-1], "has_in_watchlist": has_in_watchlist( request.user, listing) }) return HttpResponseRedirect(reverse("listing", args=(listing.id, )))
def comment_view(request, listing_id): listing = Listing.objects.get(id=listing_id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.user = request.user comment.listing = listing comment.save() return HttpResponseRedirect(reverse("listing", args=(listing.id, ))) return HttpResponseRedirect(reverse("listing", args=(listing.id, )))
def get_listing_view_context(listing, message=None): comments = Comment.objects.filter(item=listing) return { 'listing': listing, 'comments': comments, 'message': message, 'bidform': BidForm(initial={ 'bid': listing.current_bid, 'item': listing }), 'commentform': CommentForm(initial={'item': listing}), 'closeform': CloseForm(initial={'item': listing}), 'watchlistform': WatchlistForm(initial={'item': listing}) }
def comments(request, product_id): product = Product.objects.get(id=product_id) if request.method == "POST": # Create a form instance and populate it with data from the request (binding): form = CommentForm(request.POST or None) # Check if the form is valid: if form.is_valid(): form.instance.product = product form.instance.user = request.user form.save() return HttpResponseRedirect(reverse('bids', args=(product_id,))) #return render(request, "auctions/bids {{ product.id }}.html", { # 'form': form, #}) else: #return HttpResponseRedirect(reverse('bids', args=(product_id,))) return render(request, "auctions/comments.html", { 'product':product, 'form':CommentForm })
def listing_view(request, listing_id): listing = Listing.objects.get(id=listing_id) bids = Bid.objects.filter(listing=listing)[::-1] return render( request, "auctions/listing.html", { "listing": listing, "comment_form": CommentForm(), "comments": Comment.objects.filter(listing=listing)[::-1], "bid_form": BidForm(), "bids": bids if len(bids) != 0 else False, "winner": bids[0] if len(bids) != 0 else None, "has_in_watchlist": has_in_watchlist(request.user, listing), "is_user_owner": listing.owner == request.user })
def testCommentFormValid(self): testForm = CommentForm(data={ "content": "TestContent", }) self.assertTrue(testForm.is_valid())
def testCommentFormInvalid(self): testForm = CommentForm(data={}) self.assertFalse(testForm.is_valid()) self.assertEquals(len(testForm.errors), 1)