def generate_data(request): # Data generation program random_products = [ 'Computer', 'Raspberry Pi', 'iPhone', 'Television', 'Jam', 'Xbox', 'Playstation', 'Homework' ] desc = "Very good product for sale. This product will make your life 200% better." fake = Faker() print( "---Starting data generation program----\nGenerating 50 random users and one auction each" ) n = 2 for _ in range(0, 50): profile = fake.simple_profile() user = User.objects.create_user(username=profile['username'], email=profile['mail'], password=fake.password()) user.first_name = profile['name'].split()[0] user.last_name = profile['name'].split()[1] user.save() auction = Auction(seller=user, title=sample(random_products, 1)[0], description=desc, min_price=round(uniform(10, 500)), deadline=timezone.now() + datetime.timedelta(hours=(72 + randint(0, 50)))) auction.save() print(str(n) + "% done") n = n + 2 print( "Generating complete.\nGenerating 50 random bids by random users on random auctions" ) n = 2 for i in range(0, 50): random_auction = sample(list(Auction.objects.all()), 1)[0] random_user = sample(list(User.objects.all()), 1)[0] winning_bid = 0 winner = 0 if random_auction.winning_bid is not None: winning_bid = random_auction.winning_bid.amount winner = random_auction.winning_bid.bidder if random_user != random_auction.seller and random_user != winner: if winning_bid == 0: bid_amount = random_auction.min_price + round(uniform(1, 10)) else: bid_amount = winning_bid + round(uniform(1, 10)) bid = Bid(bidder=random_user, amount=bid_amount, auction=random_auction) bid.save() random_auction.winning_bid = bid random_auction.save() print(str(n) + "% done") n = n + 2 messages.info(request, "Data Generation done! Click on Home to see the results :)") return render(request, "index.html")
def edit(request, auction_id): if auction_id == u'None': auction = Auction() form = AuctionForm(instance=auction) auction.seller = request.user form = AuctionForm() else: auction = Auction.objects.get(pk=auction_id) form = EditAuctionForm(instance=auction) return render_to_response('auctions/edit.html', {'form': form, 'auction': auction}, RequestContext(request))
def editAuction(request, auctionId): if Auction.exists(auctionId): objAuction = Auction.objects.get(id=auctionId) if objAuction.seller == request.user: data = request.data Description = data.get('Description') category = data.get('Category') ItemType = itemType.objects.get(id=category) if not Description: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Description is not provider.' } }) if not category: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'category is not provider.' } }) objAuction.itemDescription = Description objAuction.category = ItemType objAuction.topBidNo = objAuction.topBidNo + 1 objAuction.save() return JsonResponse({ 'statusCode': status.HTTP_200_OK, 'data': { 'Message': 'Auction Created Sussessfully.' } }) else: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'User can only edit own Auctions.' } }) else: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Auction Not Found.' } })
def bidauctions(request, auctionId): try: auction = Auction.objects.get(id=auctionId) except Auction.DoesNotExist: return JsonResponse({ 'statusCode': status.HTTP_404_NOT_FOUND, 'data': { 'Message': 'Auction Not Found!' } }) if request.method == 'GET': serializer = AuctionSerializer(auction) return JSONResponse({ 'statusCode': status.HTTP_200_OK, 'data': serializer.data }) elif request.method == 'POST': data = request.data Auctionid = data.get('Auctionid') BidVersion = data.get('BidVersion') bidAmount = data.get('bidAmount') Comment = data.get('Comment', '') print Auctionid #print validator.isValidId(Auctionid) if not Auctionid: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Auctionid is not provider.' } }) if not BidVersion and BidVersion != 0: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Auctionid is not provider.' } }) if not bidAmount: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Auctionid is not provider.' } }) if Auction.exists(Auctionid): auction = Auction.objects.get(id=Auctionid) else: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'invalid Auctionid.' } }) if auction.status != 1: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Auction is not active so cannot be bidded.' } }) elif auction.seller == request.user: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'You cannot bid on your own Action.' } }) elif float(bidAmount) - float(auction.topBid) < .01 or float( bidAmount) - float(auction.minPrice) < .01: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Difference between bidAmount and TopBid/MinPrice should be at least .01 .' } }) elif auction.deadline < datetime.now(): return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': ' Auction deadline is over, so cannot be bidded anymore.' } }) elif BidVersion != auction.topBidNo: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Concurrency Issue: Operation failed because another user has updated(placed a bid) record. Your changes have been lost. Please review their changes before trying again.' } }) auctionbid = Auction_Bid() auctionbid.bidUser = request.user auctionbid.auction = auction auctionbid.comment = Comment auctionbid.amount = bidAmount auctionbid.bidNumber = auction.topBidNo + 1 auctionbid.bidDate = datetime.now() auctionbid.save() #Implementing Soft Deadlines If user bid with in 5 mins of deadline end, deadline will be extended for extra 5 mins timeDiff = auction.deadline - datetime.now() Diffminutes = timeDiff.total_seconds() / 60 # Difference in Minures if (Diffminutes < 5): auction.deadline = auction.deadline + timedelta(minutes=5) auction.topBidNo = auction.topBidNo + 1 auction.topBid = bidAmount auction.save() emailSender.sendBidConfEmail(auction.title, bidAmount, request.user.email) emailSender.sendBidEmailToSeller(auction.title, bidAmount, auction.seller.email) if float(BidVersion) > 0: if Auction_Bid.Exist(auction.id, float(BidVersion)): PrevUser = Auction_Bid.loadByBidVersion( auction.id, float(BidVersion)).bidUser emailSender.sendBidEmailToPreviousBidder( auction.title, bidAmount, PrevUser.email) return JsonResponse({ 'statusCode': status.HTTP_200_OK, 'data': { 'Message': 'Auction Bid Placed successfully for auction ' + auction.title + ' with amount ' + str(bidAmount) } })
def createAuction(request): AuctionTitle = request.POST['AuctionTitle'] Deadline = request.POST['Deadline'] StartingPrice = request.POST['StartingPrice'] Description = request.POST['Description'] category = request.POST['Category'] ItemType = itemType.objects.get(id=category) if not AuctionTitle: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'AuctionTitle is not provider.' } }) if not Deadline: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Deadline is not provider.' } }) if not StartingPrice: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'StartingPrice is not provider.' } }) if not Description: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'Description is not provider.' } }) if not category: return JsonResponse({ 'statusCode': status.HTTP_400_BAD_REQUEST, 'data': { 'Message': 'category is not provider.' } }) objAuction = Auction() objAuction.title = AuctionTitle objAuction.minPrice = StartingPrice objAuction.seller = request.user objAuction.status = 1 objAuction.itemDescription = Description objAuction.deadline = datetime.strptime( Deadline, '%d/%m/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S') objAuction.category = ItemType objAuction.save() emailSender.sendAuctionConfEmail(objAuction.title, request.user.email) return JsonResponse({ 'statusCode': status.HTTP_200_OK, 'data': { 'Message': 'Auction Created Sussessfully.' } })
def delete(request, auction_id): auction = Auction.objects.get(pk=auction_id) Auction.delete(auction) return HttpResponseRedirect(reverse('index' ,args=('',)))