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)
            }
        })