Example #1
0
def comment(request, ID):
    if request.method == "POST":
        comment_text = request.POST["comment"]
        comment = Comment(user=request.user,
                          listing=Listing.objects.get(pk=ID),
                          text=comment_text)
        comment.save()
        return HttpResponseRedirect(reverse("listing", args=(ID, )))
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'))
Example #3
0
def commenting(request, listing_id):
    # checks the request method
    if request.method == "POST":
        # saves the content of the comment
        comment = request.POST["comment"]
        # gets the info of the user who commented
        user = User.objects.get(pk=int(request.user.id))
        # gets the listing that received the comment
        listing = Listing.objects.get(pk=int(listing_id))
        # creates a comment entry in the database
        commentFile = Comment(listing_name=listing,
                              user_name=user,
                              comment=comment)
        # saves the entry
        commentFile.save()
        return HttpResponseRedirect(
            reverse("listing_entry", args=(listing_id, )))
Example #4
0
def cmntsubmit(request,listingid):
    if request.method == 'POST':
        now = datetime.now()
        dt = now.strftime('%d %B %Y %X')
        c = Comment()
        c.comment = request.POST.get('comment')
        c.user = request.user.username
        c.listingid = listingid
        c.time = dt
        c.save()
        return redirect('listingpage', id=listingid)
    else:
        return redirect('index')
Example #5
0
def listing(request, listing_id):
    try:
        item = Listing.objects.get(id=listing_id)
        item_id = item.id
        user = request.user.username
        id_user = request.user
        bidform = BidForm()
        userWatchlist = Watchlist.objects.filter(currentUser=id_user)
        commentForm = CommentForm()
        numBids = len(Bid.objects.filter(listingID=listing_id))
        comments = Comment.objects.filter(listingID=listing_id)

        if request.method == 'POST':

            # Comment
            if 'commentButton' in request.POST:
                commentForm = CommentForm(data=request.POST)
                if commentForm.is_valid():
                    # Create new comment object
                    new_comment = Comment()
                    new_comment = commentForm.save(commit=False)
                    new_comment.listingID = item
                    new_comment.commenter = id_user
                    new_comment.save()
                    message = django.contrib.messages.success(
                        request, 'Comment added successfully.')
                    return HttpResponseRedirect(
                        request.META.get('HTTP_REFERER'),
                        {"messages": message})

            # Watchlist
            if 'watchlistButton' in request.POST:
                # Check if item in userWatchlist
                for row in userWatchlist:
                    if item == row.itemWatch:
                        # Remove item from watchlist if item in watchlist
                        userWatchlist.filter(itemWatch=item).delete()
                        message = django.contrib.messages.success(
                            request, 'Item removed from watchlist.')
                        return HttpResponseRedirect(
                            request.META.get('HTTP_REFERER'),
                            {"messages": message})

                # Add item to watchlist
                w = Watchlist(currentUser=id_user, itemWatch=item)
                w.save()
                message = django.contrib.messages.success(
                    request, 'Item added to watchlist.')
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'),
                                            {"messages": message})

            # Place a bid
            if 'makeBid' in request.POST:
                bidform = BidForm(request.POST)
                if bidform.is_valid():
                    user_bid = bidform.cleaned_data['bid']
                    price = item.startBid

                    # Validation: check user bid > item.startBid
                    if user_bid > price:
                        # save bid to Bids
                        newBid = Bid(listingID=item,
                                     bidder=request.user,
                                     amount=user_bid)
                        newBid.save()
                        # save bid to item.startBid
                        item.startBid = user_bid
                        item.save()
                        message = django.contrib.messages.success(
                            request, 'Bid placed successfully.')
                        return HttpResponseRedirect(
                            request.META.get('HTTP_REFERER'),
                            {"messages": message})

                    else:
                        message = django.contrib.messages.success(
                            request,
                            'Error: please bid an amount larger than the current price.'
                        )
                        return HttpResponseRedirect(
                            request.META.get('HTTP_REFERER'),
                            {"messages": message})

            # Close Auction Button if owner logged in
            elif 'closeAuction' in request.POST:
                item.isOpen = False
                # get winning bid
                topBid = item.startBid
                # get top Bid model
                topBidObject = Bid.objects.filter(amount=topBid,
                                                  listingID=item_id)
                # get top Bidder from top Bid model
                if hasattr(topBidObject, 'bidder'):
                    topBidder = topBidObject.bidders
                else:
                    topBidder = item.owner
                # set item owner to top bidder
                item.owner = topBidder
                item.save()
                message = django.contrib.messages.success(
                    request, 'Closed auction successfully.')
                return redirect("/", {"messages": message})

        else:
            # get number of bids on items
            try:
                whoBid = Bid.objects.filter(bidder=request.user)[0]
                whoBid1 = whoBid.bidder
                numBids = len(Bid.objects.filter(listingID=listing_id))
                return render(
                    request, "auctions/listing.html", {
                        "item": item,
                        "user_id": user,
                        "commentForm": commentForm,
                        "bidForm": bidform,
                        "numBids": numBids,
                        "whoBid": whoBid1,
                        "watchlist": userWatchlist,
                        "comments": comments
                    })
            except:
                numBids = len(Bid.objects.filter(listingID=listing_id))
                return render(
                    request, "auctions/listing.html", {
                        "item": item,
                        "user_id": user,
                        "commentForm": commentForm,
                        "bidForm": bidform,
                        "numBids": numBids,
                        "watchlist": userWatchlist,
                        "comments": comments
                    })
    except TypeError:
        return redirect("/error")
    listing = Listing(title=title,
                      starting_price=price,
                      description=description,
                      owner=user,
                      image_url=image,
                      category=category_obj)
    listing.save()

# make watchlists
for user, listing in watchlists:
    user_model = get_user_model()
    user = user_model.objects.get(username=user)
    listing_obj = Listing.objects.get(title=listing)
    user.watched_items.add(listing_obj)

# make bids and associate them with the correct listing
for listing, bidder, bid in bids:
    user_model = get_user_model()
    user = user_model.objects.get(username=bidder)
    listing = Listing.objects.get(title=listing)
    bid = Bid(bidder=user, amount=bid, bid_listing=listing)
    bid.save()

# make comments and associate them with the correct listing
for listing, commenter, text in comments:
    user_model = get_user_model()
    user = user_model.objects.get(username=commenter)
    listing = Listing.objects.get(title=listing)
    comment = Comment(commenter=user, text=text, com_listing=listing)
    comment.save()
Example #7
0
def save_comment(list_id, user_id, comment):
    """
    Save listing comments
    """
    comment = Comment(user_id=user_id, listing_id=list_id, comment=comment)
    comment.save()
Example #8
0
def add_comment(listing, comment):
    new_comment = Comment(comment_listing=listing, comment_text=comment)
    new_comment.save()
    return True