def get(self,uid,itemID): if (not self.user) or (str(self.user.key().id())!=uid): self.redirect('/login') return item=Item.getByID(itemID) latestBid=Bid.getLatestBidByItem(itemID) myLatestBid=Bid.getLatestBidByItemAndUser(itemID,uid) if not item or dt.datetime.now()+dt.timedelta(hours=8)>item.endDate: self.redirect('/'+uid) return self.render("item.html",user=self.user,item=item,latestBid=latestBid,myLatestBid=myLatestBid)
def post(self,uid,itemID): #currently do not need authentication if (not self.user) or (str(self.user.key().id())!=uid): self.redirect('/login') return item=Item.getByID(itemID) if not item: self.redirect('/'+uid) return customerBid=self.request.get("customerBidPost") customerBid=float(customerBid) #Lock Mechanism to prevent concurrent bidding try: #lock mechanism #if lock then wait lock=memcache.get("lock"+itemID) count=0 while lock and count<=9: memcache.set("sleep"+itemID+uid,0) sleep=0 while sleep<=100: memcache.incr("sleep"+itemID+uid) sleep=memcache.get("sleep"+itemID+uid) lock=memcache.get("lock"+itemID) #set lock memcache.set("lock"+itemID,True) latestBid=Bid.getLatestBidByItem(itemID) #check if the bidding info is valid # if later than the end bid time or lower than the latest bid amount, invalid bidding if item.endDate<(dt.datetime.now()+dt.timedelta(hours=8)) or (latestBid and customerBid<=latestBid.bidAmount): raise Exception outBidder=None #expire old bid if latestBid: # to identify the identify of the outBidder if latestBid.bidder!=uid: outBidder=latestBid.bidder latestBid.expire() latestBid.save() #insert the new bid bid=Bid(bidder=uid,item=itemID,bidAmount=customerBid,timeStamp=dt.datetime.now()+dt.timedelta(hours=8),activeFlag=True) bid.save() #update bidder info if itemID not in self.user.myBidItems: self.user.myBidItems.append(itemID) self.user.save() #send an email on every bid try: send_bid_email(self.user,item) except: pass #send email to the outBidder if outBidder: outBidder=Bidder.by_id(outBidder) try: send_outbid_email(outBidder,item) except: pass self.redirect('/'+uid+'?result=success') except: error="Your Bid Is Invalid" myLatestBid=Bid.getLatestBidByItemAndUser(itemID,uid) self.render("item.html",user=self.user,item=item,latestBid=latestBid,myLatestBid=myLatestBid,error=error) finally: #release lock success=memcache.delete("lock"+itemID) count=0 while not success and count<10: success=memcache.delete("lock"+itemID) count+=1