def POST(self): post_params = web.input() item_id = int(post_params['itemID']) user_id = post_params['userID'] price = float(post_params['price']) current_time = sqlitedb.getTime() test = "select * from Bids where (userID = $user_id) and (itemID = $item_id)" message = "A bid of $%s has been placed by %s for item %s" % (price,user_id,item_id) add_result = "" t = sqlitedb.transaction() try: started = sqlitedb.query("select Started from Items where ItemID = $item_id", {'item_id': item_id}) ends = sqlitedb.query("select Ends from Items where ItemID = $item_id", {'item_id': item_id}) buyPrice = sqlitedb.query("select Buy_Price from Items where ItemID = $item_id", {'item_id': item_id})[0].Buy_Price s = sqlitedb.query("select * from Bids where (ItemID = $item_id) and (Amount >= $buyPrice)",{'item_id': item_id, 'buyPrice': buyPrice}) if (sqlitedb.isResultEmpty(s)): add_result = "done" sqlitedb.query("insert into Bids values ($user_id,$currTime,$price,$item_id)", {'user_id':user_id,'currTime':current_time, 'price':price,'item_id':item_id, 'started': started[0].Started, 'ends': ends[0].Ends}) else: message = "INSERT FAILED!" add_result = "" except Exception as e: message = str(e) add_result = "" else: t.commit() return render_template('add_bid.html',message = message, add_result = add_result)
def POST(self): global item post_params = web.input() item_id = post_params['item-id'] bid_amt = post_params['bid'] bidder = post_params['bidder'] current_time = sqlitedb.getTime() # use user testuser1 sqlitedb.enterBid(item_id, bid_amt, current_time, bidder) updateCurrentItemMap(item_id) return render_template('bid.html', item = item, loggedIn = loggedIn, user = user)
def GET(self, itemID): current_time = sqlitedb.getTime() item_row = sqlitedb.getItemById(itemID) is_open = current_time < item_row.ends bids = sqlitedb.getBidsByItemId(itemID) winner = str(sqlitedb.getWinnerId(itemID)) return render_template('view_item.html', item = item_row, is_open = is_open, bids = bids, winner = winner )
def updateCurrentItemMap(item_id): global item current_time = sqlitedb.getTime() item_found = sqlitedb.getItemById(item_id) item['id'] = item_found.id item['name'] = HTMLParser.HTMLParser().unescape(item_found.name) item['current_bid'] = item_found.current_bid item['buy_price'] = item_found.buy_price item['first_bid'] = item_found.first_bid item['num_bids'] = item_found.num_bids item['start'] = item_found.start item['end'] = item_found.end item['user_id'] = item_found.user_id item['description'] = HTMLParser.HTMLParser().unescape(item_found.description) if (string_to_time(str(item_found.end)) < string_to_time(str(current_time)) or (item_found.current_bid >= item_found.buy_price and item_found.buy_price is not None) or string_to_time(str(item_found.start)) < string_to_time(str(current_time))): item['isOpen'] = False # Need to account if the query does not return anything! item['winner'] = sqlitedb.getAuctionWinner(item['id']) else: item['isOpen'] = True
def POST(self): post_params = web.input() item_id = post_params['itemid'] user_id = post_params['userid'] amount = post_params['amount'] current_time = sqlitedb.getTime() web.debug(item_id) web.debug(user_id) web.debug(amount) t = sqlitedb.transaction() try: sqlitedb.addBid(item_id, user_id, current_time, amount) except: t.rollback() raise web.seeother('/500?errortype={0}&errorvalue={1}'.format(sys.exc_info()[0], sys.exc_info()[1])) else: t.commit() return render_template('place_bid.html', itemid = item_id, username = session.get('username', None))
def GET(self): item_id = web.input(itemid=None).itemid if item_id is None: return render_template('find_auction.html', username = session.get('username', None)) else: # check the status of the current auction current_time = sqlitedb.getTime() item = sqlitedb.getItemById(item_id) if item is None: return render_template('find_auction.html', status = "NOT FOUND", username = session.get('username', None)) # calculate current price current_price = sqlitedb.getCurrentPrice(item_id, current_time) if current_price is None: current_price = item.First_Bid item.Currently = current_price web.debug(item.Currently) web.debug(item.Buy_Price) # check the current status of the auction status = getAuctionStatus(current_time, item.Started, item.Ends, item.Currently, item.Buy_Price) if status == "NOT FOUND": return render_template('find_auction.html', status = status, username = session.get('username', None)) # modify values inside item to make it consistent with the current timestamp bids = sqlitedb.getBidsByItemID(item_id, current_time) categories = sqlitedb.getCategoryByItemID(item_id) if status == "OPEN": number_of_bids = sqlitedb.getNumberOfBids(item_id, current_time) item.Number_of_Bids = number_of_bids item.Currently = current_price return render_template('find_auction.html', item = item, categories = categories, bids = bids, status = status, username = session.get('username', None)) else: winner = sqlitedb.getWinner(item_id) return render_template('find_auction.html', item = item, categories = categories, bids = bids, status = status, winner = winner, username = session.get('username', None))
def querySearchArgs(self,message,itemid,userid,price): t = sqlitedb.transaction() try: bidClosed = sqlitedb.isAuctionClosed(itemid) if bidClosed: message = "Bid not placed! I'm sory the auction is closed for item id:" + str(itemid) return self.getNotOverloaded(itemid,message) currentTime = sqlitedb.getTime() sqlitedb.placeBid(currentTime,itemid,userid,price) sqlitedb.updateNumberOfBidsAndCurrently(itemid,price) except Exception as e: t.rollback() message = "Bid not placed! " + str(e) if str(e)=='constraint failed': message = "Bid not placed! You wagered more than the buy price. Just trying to save you money!" if str(e)=='foreign key constraint failed': message = "Bid not placed! User " + userid + " does not exist!" return self.getNotOverloaded(itemid,message) else: t.commit() message = userid + ' successfully placed a bid of $' + str(price) +' itemID=' + str(itemid) return self.getNotOverloaded(itemid,message)
def GET(self): current_time = sqlitedb.getTime() return render_template('curr_time.html', time = current_time)
def POST(self): post_params = web.input() itemID = post_params['itemID'] price = post_params['price'] userID = post_params['userID'] current_time = sqlitedb.getTime() ### Many ways to fail... ####################################### # (1) All fields must be filled if (itemID == '') or (price == '') or (userID == ''): return render_template('add_bid.html', message = 'You must fill out every field' ) item_row = sqlitedb.getItemById(itemID) # (2) There must be an item with that ID if item_row == None: return render_template('add_bid.html', message = 'There are no items with that ID' ) # (3) Users can't bid on closed auction items if (string_to_time(item_row.ends) <= string_to_time(current_time)): return render_template('add_bid.html', message = 'That auction is already closed' ) # (4) UserID must correspond to an existing user in User table user_row = sqlitedb.getUserById(userID); if user_row == None: return render_template('add_bid.html', message = 'There are no users with that ID' ) # (5) Don't accept bids <= current highest bid if float(price) <= float(item_row.currently): return render_template('add_bid.html', message = 'You must make a bid higher than the current price (currently $' + item_row.currently + ')' ) ### ... but it's possible to succeed :P ######################## # A bid at the buy_price closes the auction if (price >= item_row.buy_price): # Update ends to current_time sqlitedb.updateItemEndTime(itemID, current_time); return render_template( 'add_bid.html', message = 'Congratulations! You just closed that auction by making a bid at or above the buy price' ) # Add bid to Bid table in db sqlitedb.addBid(itemID, price, userID, current_time) return render_template( 'add_bid.html', message = 'Success! You\'ve just placed a bid on ' + item_row.name + '(' + itemID + ')' )
def GET(self): current_time = sqlitedb.getTime() return render_template('curr_time.html', time = current_time, loggedIn = loggedIn, user = user)
def POST(self): post_params = web.input() item_id = post_params['itemID'] user_id = post_params['userID'] min_price = post_params['minPrice'] max_price = post_params['maxPrice'] category = post_params['category'] status = post_params['status'] search_result = [status] src1 = "" src2 = "" src3 = "" src4 = "" src5 = "" src6 = "" itemId = "" minPrice = "" maxPrice = "" winnerStr = "" currTime = sqlitedb.getTime() if (item_id != ""): itemId = int(item_id) src1 = " (select ItemID from Items where ItemID = $itemId) " if (user_id != ""): src2 = " (select ItemID from Sales where UserID = $userId) " if (min_price != ""): minPrice = float(min_price) src3 = " (select ItemID from Items where Currently > $minPrice) " if (max_price != ""): maxPrice = float(max_price) src4 = " (select ItemID from Items where Currently < $maxPrice) " if (status == "open" ): src5 = " (select ItemID from Items where (Started < $currTime) and (Ends > $currTime)) " if (status == "close"): winnerStr = "select distinct ItemID, UserID as Winner from Bids where UserID in (select UserID from (select UserID, max(Amount) from Bids group by ItemID))" src5 = " (select ItemID from Items where ((Started < $currTime) and (Ends < $currTime)) or Currently > Buy_Price)" if (status == "notStarted"): src5 = " (select ItemID from Items where (Started > $currTime) and (Ends > $currTime)) " if (status == "all") : src5 = " (select ItemID from Items) " if (category != ""): src6 = " (select ItemID from Categories where (Category = $category)) " arr = [src1,src2,src3,src4,src5,src6] query_string = "select distinct * from Items inner join Sales on Items.ItemID = Sales.ItemID where Items.ItemID in" first = False for i in xrange(len(arr)): if first == False and arr[i] != "": first = True query_string += arr[i] elif arr[i] != "": query_string += "and Items.ItemID in" + arr[i] t = sqlitedb.transaction() if (winnerStr != ""): cols = "q1.ItemID,Ends,First_Bid,Name,Started,Number_of_Bids,UserID,Buy_Price,Currently,Description,Winner" query_string = "select distinct " + cols + " from (" +query_string + ") q1 inner join (" + winnerStr + ") q2 on q1.ItemID = q2.ItemID" try: result = sqlitedb.query(query_string, {'userId' : user_id, 'itemId': itemId, 'maxPrice':maxPrice, 'minPrice': minPrice, 'currTime':currTime, 'category':category}) search_result = result except Exception as e: search_result = [] else: t.commit() return render_template('search.html',search_result = search_result)
def GET(self): current_time = sqlitedb.getTime() return render_template('curr_time.html', time=current_time)
def POST(self): #transaction and test t = sqlitedb.transaction() try: post_params = web.input() item_id = int(post_params['itemID']) user_id = post_params['userID'] price = float(post_params['price']) current_time = sqlitedb.getTime() started = sqlitedb.query( "select Started from Items where ItemID = $item_id", {'item_id': item_id}) ends = sqlitedb.query( "select Ends from Items where ItemID = $item_id", {'item_id': item_id}) buyPrice = sqlitedb.query( "select Buy_Price from Items where ItemID = $item_id", {'item_id': item_id})[0].Buy_Price #current_price = sqlitedb.query("select Currently from Items where ItemID = $item_id", {'item_id': item_id})[0].Currently if (buyPrice != None): closed = sqlitedb.query( "select * from Bids where (ItemID = $item_id) and (Amount >= $buyPrice)", { 'item_id': item_id, 'buyPrice': buyPrice }) else: closed = None if (closed is None): #add bid to the database sqlitedb.updateTime( "insert into Bids values ($item_id, $user_id, $price, $currTime)", { 'item_id': item_id, 'user_id': user_id, 'price': price, 'currTime': current_time }) sqlitedb.updateTime( "update Items set Currently = $currently where ItemID = $item_id", { 'currently': price, 'item_id': item_id }) add_result = "done" update_message = 'Hello, %s. Previously added a bid for %d. of price $%d.' % ( user_id, item_id, price) else: update_message = "Failed. Try again." add_result = "" except Exception as e: t.rollback() print str(e) add_result = "" update_message = "Failed. Try again." else: t.commit() return render_template('add_bid.html', message=update_message, add_result=add_result)
def GET(self): current_time = sqlitedb.getTime() add_result = True return render_template('add_bid.html')
def POST(SELF): post_params = web.input() item_id = post_params['itemID'] user_id = post_params['userID'] min_price = post_params['minPrice'] max_price = post_params['maxPrice'] category = post_params['category'] status = post_params['status'] description = post_params['description'] line1 = "" line2 = "" line3 = "" line4 = "" line5 = "" line6 = "" line7 = "" Item_id = "" userId = "" minPrice = "" maxPrce = "" #grab current time current_time = sqlitedb.getTime() print current_time #do some query based on the input try: if (item_id != ""): Item_id = int(item_id) line1 = "(select ItemID from Items where ItemID = $Item_id) " if (user_id != ""): userId = user_id line2 = "(select ItemID from Items where Seller_UserID = $userId) " if (min_price != ""): minPrice = float(min_price) line3 = "(select ItemID from Items where Currently >= $minPrice) " if (max_price != ""): maxPrce = float(max_price) line4 = "(select ItemID from Items where Currently <= $maxPrice) " if (category != ""): line5 = "(select ItemID from Categories where Category = $category)" except Exception as e: message = "please write valid input!" return render_template('search.html', message=message) if (status == "open"): line6 = "(select ItemID from Items where (Started <= $current_time) and (Ends >= $current_time) or Currently > Buy_Price)" if (status == "all"): line6 = "(select ItemID from Items)" if (status == "close"): line6 = "(select ItemID from Items where (Started < $current_time) and (Ends < $current_time))" if (status == "notStarted"): line6 = "(select ItemID from Items where (Started > $current_time) and (Ends > $current_time))" if (description != ""): line7 = "(select ItemID from Items where instr(Description, $description) > 0)" res = [line1, line2, line3, line4, line5, line6, line7] query_string = "select distinct * from Items inner join Categories on Items.ItemID = Categories.ItemID where Items.ItemID in " isFirst = False for i in range(len(res)): if isFirst == False and res[i] != "": isFirst = True query_string += res[i] elif res[i] != "": query_string += "and Items.ItemID in " query_string += res[i] query_string += " group by Items.ItemID" result = sqlitedb.query( query_string, { 'Item_id': Item_id, 'userId': userId, 'minPrice': minPrice, 'maxPrice': maxPrce, 'category': category, 'current_time': current_time, 'description': description }) #check status count = 0 status = range(len(result)) itemID = range(len(result)) counter = 0 cur_time = string_to_time(sqlitedb.getTime()) for a in result: for b in a: count += 1 if (count % 11 == 1): itemID[counter] = int(a[b]) if (count % 11 == 6): start = string_to_time(a[b]) if (count % 11 == 3): end = string_to_time(a[b]) if (count % 11 == 8): cur = int(a[b]) if (count % 11 == 9): buyprice = a[b] if (cur_time > start and cur_time > end): status[counter] = "CLOSE" if (buyprice != None): buy = int(buyprice) if (cur >= buy): status[counter] = "CLOSE" if (cur_time > start and cur_time < end): status[counter] = "OPEN" if (cur_time < start and cur_time < end): status[counter] = "NOT START" count = 0 counter += 1 return render_template('search.html', search_result=result, status=status, itemID=itemID)
def GET(self): current_time = sqlitedb.getTime() return render_template('curr_time.html', time = current_time, username = session.get('username', None))