Exemple #1
0
 def test_user_can_only_have_a_bid_per_item(self):
     """
     Given an existing bid on an item
     when we try to save a new bid for the same user and the same item
     then an exception is thrown
     """
     Bid(item=self.item, user=self.user, amount=Decimal(1)).save()
     with self.assertRaises(UserAlreadyHasABid):
         Bid(item=self.item, user=self.user, amount=Decimal(2)).save()
Exemple #2
0
 def test_save_bid_less_amount_fails_for_same_user(self):
     """
     Given an existing bid on an item
     when we save a bid with less amount for the same user
     then an exception is thrown
     """
     Bid(item=self.item, user=self.user, amount=Decimal(2)).save()
     with self.assertRaises(HigherBidAlreadyExists):
         Bid(item=self.item, user=self.user, amount=Decimal(1)).save()
Exemple #3
0
 def test_save_bid(self):
     """
     Given an item without best_bid
     when we save a bid,
     then that bid becomes the best_bid
     """
     bid = Bid(item=self.item, user=self.user, amount=Decimal(1))
     bid.save()
     self.item.refresh_from_db()
     self.assertEqual(self.item.best_bid.pk, bid.pk)
Exemple #4
0
 def test_user_can_have_bids_on_several_items(self):
     """
     Given an existing bid on an item
     when we save a new bid for the same user and another item
     then the user will have 2 bids
     """
     Bid(item=self.item, user=self.user, amount=Decimal(2)).save()
     Bid(item=self.builder.item("newitem"),
         user=self.user,
         amount=Decimal(2)).save()
     self.assertEqual(Bid.objects.filter(user=self.user).count(), 2)
Exemple #5
0
def bid_from_home(request):
    # view that allows user to bid from home page if authenticated.
        if request.method == "POST":
            if request.user.is_authenticated:
                p_id = request.POST['product_id']
                auction = Auction.objects.get(product_id=p_id)
                if timezone.now() >= auction.start_time and timezone.now() < auction.end_time:
                   
                    product = Product.objects.get(id=p_id)
                    product.auction_price += int(request.POST['bid'])
                    product.save()
                    new_bid = Bid()
                    # auction = get_object_or_404(Auction, pk=pk)
                    new_bid.product_id = product
                    new_bid.auction_id = auction
                    new_bid.user_id = request.user
                    new_bid.bid_time = timezone.now()
                    new_bid.bid_views += 1
                    new_bid.save()
                    messages.error(request, "Bidding is done!")
                else:
                    messages.error(request, "Bidding is closed!")
                
   
            else:
                messages.error(request, "Please register or sign in to bid!")
            
        # else:
        #     return render(request, "home.html")
            
        return redirect(reverse('home'))
Exemple #6
0
def one_auction(request):
    # Allow user to auction a product
    if request.method == "POST":
        if request.user.is_authenticated:
            p_id = request.POST['product_id']
            auction = Auction.objects.get(product_id=p_id)
            if timezone.now() >= auction.start_time and timezone.now(
            ) < auction.end_time:

                product = Product.objects.get(id=p_id)
                product.auction_price += int(request.POST['Raise'])
                product.save()
                new_bid = Bid()

                new_bid.product_id = product
                new_bid.auction_id = auction
                new_bid.user_id = request.user
                new_bid.bid_time = timezone.now()
                new_bid.bid_views += 1
                new_bid.save()
                messages.error(request, "Bidding is Updated!")
            else:
                messages.error(request, "Bidding is closed!")

        else:
            messages.error(request, "Please register or sign in to bid!")

    return redirect(reverse('auctions'))
Exemple #7
0
 def test_bid_can_be_updated_to_a_higher_amount(self):
     """
     Given an existing bid on an item
     when we update that bid to a higher amount
     then the bid is properly updated
     """
     bid = Bid(item=self.item, user=self.user, amount=Decimal(2))
     bid.save()
     bid.amount = Decimal(3)
     bid.save()
     bid.refresh_from_db()
     self.assertEqual(bid.amount, Decimal(3))
Exemple #8
0
 def test_bid_updated_to_lower_amount_fails(self):
     """
     Given an existing bid on an item
     when we update that bid to a lower amount
     then an exception is raised
     """
     bid = Bid(item=self.item, user=self.user, amount=2)
     bid.save()
     with self.assertRaises(LowerAmountNotAllowed):
         bid.amount = Decimal(1)
         bid.save()
def open_auction(request):
    """ Allows a registered user to bid on open auctions """
    """ Thanks to Dehinde - Shogbanmu because I really struggled with this part """
    if request.method == "POST":
        product_id = request.POST['product_id']
        auction = Auction.objects.get(product_id=product_id)
        """ Make sure Auction is still Open """
        if timezone.now() < auction.end_time:
            product = Product.objects.get(id=product_id)
            current_bid = Bid.objects.filter(product_id=product_id)

            if current_bid:
                """ If there has been a previous Bid then """
                print("Route A")
                bid = current_bid[0]
                bid.user_id = request.user
                bid.bid_time = timezone.now()
                bid.bid_views += 1
                bid.bid_price += int(request.POST['UpBid'])
                bid.save()
                auction.current_price = bid.bid_price
                auction.bid_number += 1
                auction.current_bidder = str(bid.user_id)
                auction.save()
            else:
                """ If there has not been a previous Bid then """
                print("Route B")
                new_bid = Bid()
                new_bid.product_id = product
                new_bid.auction_id = auction
                new_bid.user_id = request.user
                new_bid.bid_time = timezone.now()
                new_bid.bid_views += 1
                new_bid.bid_price += int(request.POST['UpBid'])
                new_bid.save()
                auction.current_price = new_bid.bid_price
                auction.bid_number += 1
                auction.current_bidder = str(new_bid.user_id)
                auction.save()
            messages.error(request, "Well done you have placed a bid.")
        else:
            auction.status = "Closed"
            auction.save()
            messages.error(request, "This Auction is closed.")

    return redirect(reverse('auctions'))
Exemple #10
0
def one_auction(request):
    
    # Allow user to bid on a specifc product base on the start time and end time of an auction
    # If an auction time has ended a message is displayed to the user
    # If an auction time has not ended user can update thier bid
    
    if request.method == "POST":
       
            p_id = request.POST['product_id']
            auction = Auction.objects.get(product_id=p_id)
            if timezone.now() >= auction.start_time and timezone.now() < auction.end_time:
               
                product = Product.objects.get(id=p_id)
                product.auction_price += int(request.POST['Raise'])
                product.save()
                bids = Bid.objects.filter(product_id=p_id)
                if bids:
                    bid = bids[0]
                    bid.user_id = request.user
                    bid.bid_time = timezone.now()
                    bid.bid_views += 1
                    bid.save()
                
                else:
                    new_bid = Bid()
                    new_bid.product_id = product
                    new_bid.auction_id = auction
                    new_bid.user_id = request.user
                    new_bid.bid_time = timezone.now()
                    new_bid.bid_views += 1
                    new_bid.save()
                messages.error(request, "You have just raised your bid!")
            else:
                messages.error(request, "Auction has closed!")
            
    return redirect(reverse('auctions'))