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.EndTime) <= 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.CurrentBid): return render_template( 'add_bid.html', message= 'You must make a bid higher than the current price (currently $' + str(item_row.CurrentBid) + ')') ### ... but it's possible to succeed :P ######################## # A bid at the buy_price closes the auction if (item_row.BuyItNow != None and price >= item_row.BuyItNow): # 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 add_bid(): if request.method == 'POST': post_params = request.form itemID = post_params["itemID"] userID = post_params["userID"] price = post_params["price"] current_time = sqlitedb.getTime() if (itemID == '') or (userID == '') or (price == ''): return render_template('add_bid.html', add_result = False, message = "Not all field is filled") item = sqlitedb.getItemById(itemID) print item[7] if (item == None): return render_template('add_bid.html', add_result = False, message = "Cannot find item") if (string_to_time(item[7]) <= string_to_time(current_time)): return render_template('add_bid.html', add_result = False, message = "The auction is already Closed") user = sqlitedb.getUserById(userID) if (user == None): return render_template('add_bid.html', add_result = False, message = "Cannot find user") if float(price) <= float(item[2]): return render_template('add_bid.html', add_result = False, message = "Price is Lower than current price") sqlitedb.addBid(userID, price, current_time, itemID) return render_template('add_bid.html', add_result = True, message = "Add Bid Successful") else: return render_template('add_bid.html')
def POST(self): post_params = web.input() itemID = post_params['itemID'] userID = post_params['userID'] Amount = post_params['price'] # Valid search if itemID == '' or userID == '' or Amount == '': return render_template( 'add_bid.html', message='Error: check valid ItemID, UserID, and Amount') else: curr_item = sqlitedb.getItemById(itemID) curr_user = sqlitedb.getUserById(userID) # Valid item in database if curr_item is None: return render_template('add_bid.html', message='Error: invalid ItemID') # Valid user elif curr_user is None: return render_template('add_bid.html', message='Error: invalid UserID') # User not bidding on own item elif curr_user.UserID == curr_item.Seller_UserID: return render_template( 'add_bid.html', message='Error: User cannot bid on own items') # Auction ongoing elif string_to_time(sqlitedb.getTime()) >= string_to_time( curr_item.Ends): return render_template('add_bid.html', message='Error: auction ended') # Auction Started elif string_to_time(sqlitedb.getTime()) < string_to_time( curr_item.Started): return render_template('add_bid.html', message='Errr: auction yet to start') # Valid bid amount elif float(Amount) < 0 or float(Amount) <= float( curr_item.First_Bid) or float(Amount) <= float( curr_item.Currently): return render_template('add_bid.html', message='Error: invalid amount') # Valid buy price if curr_item.Buy_Price is not None: #close auction if buy price met if float(Amount) >= float(curr_item.Buy_Price): successful_purchase = 'WOOT! You now own item: %s at the low low price of: %s.' % ( curr_item.Name, Amount) return render_template('add_bid.html', message=successful_purchase, add_result=sqlitedb.new_bid( itemID, userID, Amount)) #Verified no errors exist, place a new bid on the item for the amount specified successful_bid = 'You have bid on the amazing: %s at the low low price of: %s.' % ( curr_item.Name, Amount) return render_template('add_bid.html', message=successful_bid, add_result=sqlitedb.new_bid( itemID, userID, Amount))
def POST(self): try: post_params = web.input() add_result = '' item_id = post_params['itemID'] price = post_params['price'] user_id = post_params['userID'] current_time = sqlitedb.getTime() # Validation checks # make all input fields required if (item_id == '' or price == '' or user_id == ''): return render_template( 'add_bid.html', message='Error: All fields are required') # don't accept bids on items that don't exist if (sqlitedb.getItemById(item_id) == None): return render_template('add_bid.html', message='Error: Invalid Item ID !') # Don't accept bids from users that don't exist if (sqlitedb.getUserById(user_id) == None): return render_template('add_bid.html', message='Error: Invalid User ID !') # @TODO: add more validation checks # insert transaction t = sqlitedb.transaction() try: sqlitedb.db.insert('Bids', ItemID=item_id, UserID=user_id, Amount=price, Time=current_time) except Exception as e: t.rollback() message = str(e) print str(e) else: t.commit() message = 'Success ! Added bid for ' + str( item_id) + ' by ' + str(user_id) + ' at $' + str(price) print 'commited ' + str(t) # @TODO validations except Exception as e: message = str(e) return render_template('add_bid.html', message=message, add_result=add_result)
def POST(self): #get user input on userID, itemID, and bid amount post_params = web.input() userID = post_params['userID'] itemID = post_params['itemID'] Amount = post_params['price'] #error check if all values were input if userID == '' or itemID == '' or Amount == '': return render_template('add_bid.html', message = 'At least one of the following is invalid: UserID, ItemID, or Amount.') else: #if all values present, retrieve the specified users and items if possible curr_user = sqlitedb.getUserById(userID) curr_item = sqlitedb.getItemById(itemID) #if the specified user doesn't exist: if curr_user is None: return render_template('add_bid.html', message = 'Could not find user with UserID.') #if the specified user is the item's seller: elif curr_user.UserID == curr_item.Seller_UserID: return render_template('add_bid.html', message = 'UserID is the ID of the seller, cannot bid.') #if the specified item doesn't exist: elif curr_item is None: return render_template('add_bid.html', message = 'Could not find item with ItemID.') #if the specified amount is negative: elif float(Amount) < 0: return render_template('add_bid.html', message = 'The specified amount is negative.') #if the specified amount is less than the currently highest bid price: elif float(Amount) <= float(curr_item.First_Bid) or float(Amount) <= float(curr_item.Currently): return render_template('add_bid.html', message = 'The specified amount is too small.') #if the specified auction has not yet started: elif string_to_time(sqlitedb.getTime()) < string_to_time(curr_item.Started): return render_template('add_bid.html', message = 'The auction has not yet started.') #if the specified auction has already ended: elif string_to_time(sqlitedb.getTime()) >= string_to_time(curr_item.Ends): return render_template('add_bid.html', message = 'The auction has already ended.') #If the auction has a specified buy price: if curr_item.Buy_Price is not None: #if specified amount is greater than or equal to buy price: if float(Amount) >= float(curr_item.Buy_Price): #indicate that the auction has been purchased, and close the auction (IF SUCCESSFUL). #On the website, if the Result specifies "not successful", then this step has not been successful due to constraints. successful_purchase = 'You have purchased item: %s for: %s. NOTE: Check Result below to see if it was successful.' % (curr_item.Name, Amount) return render_template('add_bid.html', message = successful_purchase, add_result = sqlitedb.newBid(userID, itemID, Amount)) #place a new bid on the item with the specified amount successful_bid = 'You have placed a bid on item: %s for: %s. NOTE: Check Result below to see if it was successful.' % (curr_item.Name, Amount) return render_template('add_bid.html', message = successful_bid, add_result = sqlitedb.newBid(userID, itemID, Amount))
def POST(self): post_params = web.input() userID = post_params['userID'] location = post_params['location'] country = post_params['country'] if (userID == '') or (location == '') or (country == ''): return render_template('add_user.html', message='You must fill out every field') user_row = sqlitedb.getUserById(userID) if user_row != None: return render_template('add_user.html', message='This user ID is already taken.') sqlitedb.addUser(userID, location, country) return render_template('add_user.html', message='Success!' + '(' + userID + ')' + 'has been added.')
def POST(self): try: post_params = web.input() add_result = '' item_id = post_params['itemID'] price = post_params['price'] user_id = post_params['userID'] current_time = sqlitedb.getTime() # Must have all inputs filled out if (item_id == '' or price == '' or user_id == ''): return render_template('add_bid.html', message='Invalid entry: All fields required.') # Check item exists if (sqlitedb.getItemById(item_id) == None): return render_template('add_bid.html', message='Invalid entry: Item ID not found.') # Check user exists if (sqlitedb.getUserById(user_id) == None): return render_template('add_bid.html', message='Invalid entry: User ID not found.') t = sqlitedb.transaction() try: sqlitedb.db.insert('Bids', ItemID=item_id, UserID=user_id, Amount=price, Time=current_time) except Exception as e: add_result = 2 t.rollback() message = str(e) else: t.commit() add_result = 1 message = 'Success ! Added bid for ' + str(item_id) + ' by ' + str(user_id) + ' at $' + str(price) except Exception as e: message = str(e) return render_template('add_bid.html', message=message, add_result=add_result)
def POST(self): post_params = web.input() userID = post_params['userID'] rating = 0.0 location = post_params['location'] country = post_params['country'] # (1) All fields must be filled if (userID == '') or (location == '') or (country == ''): return render_template('add_user.html', message='You must fill out every field') user_row = sqlitedb.getUserById(userID) # (2) The user should not register before if user_row != None: return render_template('add_user.html', message='You have already been an auction user!') result = sqlitedb.addUser(userID, rating, location, country) if result != "success": return render_template("add_user.html", message="ERROR! " + result) return render_template('add_user.html', message='Welcome ' + userID + ' to the Auction System', add_result="success")
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 POST(self): post_params = web.input() user_id = post_params['userID'] item_id = post_params['itemID'] amount = post_params['price'] time = sqlitedb.getTime() if user_id == '' or item_id == '' or amount == '': return render_template('add_bid.html', message='These fields cannot be empty') item = sqlitedb.getItemById(item_id) if item is None: return render_template('add_bid.html', message='ItemID not match') user = sqlitedb.getUserById(user_id) if user is None: return render_template('add_bid.html', message='UserID not match') if amount < 0: return render_template('add_bid.html', message='Price cannot be negative') if amount <= item.First_Bid: return render_template( 'add_bid.html', message='Price is lower than minimum price seller set') if string_to_time(time) >= string_to_time( item.Ends) or string_to_time(time) < string_to_time( item.Started): return render_template( 'add_bid.html', message= 'No auction may have a bid before its start time or after its end time.' ) if user_id == item.Seller_UserID: return render_template( 'add_bid.html', message= 'A user may not bid on an item he or she is also selling') # TODO: No auction may have two bids at the exact same time. Don't know how to do that # not sure if triggers already handled these constraint, if not, we may need to handle in this funciton item.Number_of_Bids += 1 item.Currently = amount # Close the auction if amount is greater than buy_price the seller set if amount >= item.Buy_Price and item.Buy_Price is not None: sqlitedb.updateItem(item_id, time, amount) msg = '(Congratulations. Your bid %s achieves the buy_price and this %s is now yours)' % \ (amount, item.Name) else: msg = '(You just added a bid on item %s)' % item.Name sqlitedb.addBid(user_id, item_id, amount, time) return render_template('add_bid.html', message=msg)
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') # (2) There must be an item with that ID if not len(sqlitedb.getItemById(itemID)): return render_template('add_bid.html', message='There are no items with that ID') item_row = sqlitedb.getItemById(itemID)[0] # (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 for time') # (4) Users can't bid on auction items that have not been opened if (string_to_time(item_row.started) >= string_to_time(current_time)): return render_template('add_bid.html', message='That auction has not started yet, Please wait') # (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) An user may not bid on an item he or she is also selling if userID == item_row.sellerID: return render_template('add_bid.html', message='You can not bid on the item you are selling') # (6) 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 ' + str(item_row.currently)) ### ... but it's possible to succeed :P ######################## # A bid at the buy_price closes the auction if(item_row.buyPrice != None): if (float(price) >= float(item_row.buyPrice)): r1 = sqlitedb.addBid(itemID, price, userID, current_time) if r1 != "success": return render_template("add_bid.html", message="ERROR! " + r1) # Update ends to current_time r2 = sqlitedb.updateItemEndTime(itemID, current_time); if r2 != "success": return render_template("add_bid.html", message="ERROR! " + r1) return render_template('add_bid.html', message='Congratulations! You just closed that auction by making a bid at or above the buy price', add_result="success") # Add bid to Bid table in db r3 = sqlitedb.addBid(itemID, price, userID, current_time) if r3 != "success": return render_template("add_bid.html", message="ERROR! " + r3) elif r3 == "success": return render_template('add_bid.html', message='Success! You\'ve just placed a bid on ' + item_row.name + '(' + itemID + ')', add_result="success")