def sellticket(): message = "" ticket_name = request.form['name'] ticket_quantity = int(request.form['quantity']) ticket_price = float(request.form['price']) ticket_date = request.form['date'] try: ticket_date = datetime.datetime.strptime(ticket_date, '%Y%m%d') except ValueError: message = "Invalid date format. Please use the format YYYMMDD, i.e. 20200421." user_email = request.form['user'] user = bn.get_user(user_email) # check name if not is_ticket_name_valid(ticket_name): message = "Ticket name is invalid." # check quantity if not is_quantity_of_tickets_valid(ticket_quantity): message = "Ticket quantity must be between 0 and 100." # check price if not is_ticket_price_valid(ticket_price): message = "Ticket price is invalid." # check date if not is_ticket_date_valid(ticket_date): message = "Ticket date is invalid." if not message: # if message is empty, indicating no validation errors message = "Ticket created successfully." bn.sell_ticket(ticket_name, ticket_quantity, ticket_price, ticket_date, user.id) # redirect user to profile page with result message return redirect("/?message={}".format(message))
def sell_post(): ticket_name = request.form['name'] ticket_quantity = int(request.form['quantity']) ticket_price = float(request.form['price']) ticket_date = request.form['date'] ticket_date = datetime.datetime.strptime(ticket_date, '%Y-%m-%d') user_email = request.form['user'] user = bn.get_user(user_email) message = "" # check name if not is_valid_ticket_name(ticket_name): message = "Ticket name is invalid." # check quantity if not is_valid_ticket_quanitity(ticket_quantity): message = "Ticket quantity must be between 0 and 100." # check price if not is_valid_ticket_price(ticket_price): message = "Ticket price is invalid." # check date if not is_ticket_date_valid(ticket_date): message = "Ticket date is invalid." if not message: # if message is empty, indicating no validation errors message = "Ticket created successfully." bn.sell_ticket(ticket_name, ticket_quantity, ticket_price, ticket_date, user.id) # redirect user to profile page with result message return redirect("/?message={}".format(message))
def test_sell_ticket_duplicate_name(self): """ Duplicate name | user=<user in DB> name="Not Unique" quantity=1 price=10.00 expiryDate=date(2030, 1, 1) | Error: "A ticket with that name already exists." """ # The most straightforward way to have a ticket with a duplicate name # is to just insert the same ticket into the DB twice. # Prepare DB new_user = User() new_user.name = TEST_USER.name new_user.email = TEST_USER.email new_user.password = TEST_USER.password new_user.balance = TEST_USER.balance db.session.add(new_user) db.session.commit() # Set up parameters user = new_user name = "Not Unique" quantity = 1 price = 10.00 expiryDate = date(2030, 1, 1) # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == False # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == "A ticket with that name already exists."
def sell_ticket(): email = session['logged_in'] user = bn.get_user(email) ticket_name = request.form.get('name_sell') ticket_quantity = int(request.form.get('quantity_sell')) ticket_price = int(request.form.get('price_sell')) ticket_date = request.form.get('expdate_sell') error_message = "" error_list = [] # validate ticket name error_list.append(validate_ticket_name(ticket_name, error_message)) # validate ticket quantity error_list.append(validate_ticket_quantity(ticket_quantity, error_message)) # validate ticket price error_list.append(validate_ticket_price(ticket_price, error_message)) # validate ticket date error_list.append(validate_ticket_date(ticket_date, error_message)) # For any errors, redirect back to / and show an error message tickets = bn.get_all_tickets() if error_list[0] != "": return render_template('index.html', user=user, sell_message=error_list[0], tickets=tickets) elif error_list[1] != "": return render_template('index.html', user=user, sell_message=error_list[1], tickets=tickets) elif error_list[2] != "": return render_template('index.html', user=user, sell_message=error_list[2], tickets=tickets) elif error_list[3] != "": return render_template('index.html', user=user, sell_message=error_list[3], tickets=tickets) # The added new ticket information will be posted on the user profile page else: try: bn.sell_ticket(user, ticket_name, ticket_quantity, ticket_price, ticket_date) tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets) except exc.IntegrityError: bn.rollback( ) # need to roll the database back before uniquness error return render_template( 'index.html', user=user, sell_message="This ticket name already exists", tickets=tickets)
def test_backend_get_ticket_T1(): # Set up valid ticket bn.sell_ticket(test_ticket.name, test_ticket.quantity, test_ticket.price, test_ticket.date, test_ticket.user) # Test #1 (valid ticket id) result = bn.get_ticket(test_ticket.id) assert result is not None assert result.name == test_ticket.name
def test_get_ticket_valid(): # First clear teh database of tickets so that IntegrityError is not encountered db.session.query(Ticket).delete() db.session.commit() # Set up valid ticket bn.sell_ticket(test_ticket.id, test_ticket.name, test_ticket.quantity, test_ticket.price, test_ticket.date, test_ticket.email) # Test if the ticket posted for sale is in the database result = bn.get_ticket(test_ticket.name) assert result is not None assert result.name == test_ticket.name
def update_post(user): statusMessage = '' email = session['logged_in'] quantity_old = request.form.get('quantity-old') name_old = request.form.get('name-old') price_old = request.form.get('price-old') expiration_date_old = request.form.get('expiration-date-old') # New update changes. quantity_new = request.form.get('quantity-new') name_new = request.form.get('name-new') price_new = request.form.get('price-new') expiration_date_new = request.form.get('expiration-date-new') # Checking validity of the 'new' parameters. if not(checkTicketName(name_new)): statusMessage = "Error: The updated name has to alphanumeric, have no spaces in the begining or end and be between 6 and 60 characters." elif not(checkQuantity(quantity_new)): statusMessage = "Error: The updated quantity of tickets needs to be between 1 and 100." elif not(checkPrice(price_new)): statusMessage = "Error: The updated price needs to be between $10 and $100." elif not(checkDateFormat(expiration_date_new)): statusMessage = "Error: The updated exipiration date needs to be follow the 'YYYYMMDD' format." elif not(checkExpire(expiration_date_new)): statusMessage = "Error: The updated exipiration date cannot be expired." elif not(bn.verify_ticket(quantity_old, name_old, price_old, expiration_date_old, email)): statusMessage = "Error: The entered ticket either does not exist or was entered incorrectly, please try again." if statusMessage != '': tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, updateMessage=statusMessage) else: # deletes old ticket(s). bn.delete_ticket(quantity_old, name_old, price_old, expiration_date_old, email) # submits new ticket(s) to the database. bn.sell_ticket(quantity_new, name_new, email, price_new, expiration_date_new) # updates the ticket list. tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, updateMessage='Listing update successful')
def sell_ticket(user): ticket_name = request.form.get('name') ticket_quantity = int(float(request.form.get('quantity'))) ticket_price = float(request.form.get('price')) ticket_date = request.form.get('exp_date') error_message = "" # There must not be a space at beginning or end, and the name mus tbe alphanumeric if not check_spaces(ticket_name): return render_template('index.html', user=user, message="Invalid spaces found in word") # Ticket name must be shorter than 60 characters if len(ticket_name) > 60: return render_template('index.html', user=user, message="Ticket name is too long") # Ticket quantity must be greater than 0 and less than or equal to 100 if not check_quantity(0, 101, ticket_quantity): return render_template('index.html', user=user, message="Invalid quantity of tickets") # Ticket price has to be of range [10,100] if ticket_price > 100 or ticket_price < 10: return render_template('index.html', user=user, message="Ticket price outside of valid range") # Ticket date must be in valid format - YYYYMMDD # Assumption: ticket dates will start from today (2020-11-26) and go onwards if (int(ticket_date[:4]) < 2020 or int(ticket_date[4:6]) < 0 or int(ticket_date[4:6]) > 12 or int(ticket_date[6:]) < 0 or int(ticket_date[4:6]) > 31): return render_template('index.html', user=user, message="Invalid ticket date") bn.sell_ticket(ticket_name, ticket_quantity, ticket_price, ticket_date, user.email) tickets = bn.get_all_tickets() # Add the ticket to the user's list of tickets. return render_template('index.html', user=user, ticket=tickets)
def sell_post(user): statusMessage = '' # Gets the information needed from the form to create the Ticket object. email = session['logged_in'] quantity = request.form.get('sell-quantity') name = request.form.get('sell-name') price = request.form.get('sell-price') date = request.form.get('sell-expiration-date') # checks validity of the parameters specified requirements for 'sell'. if not(checkQuantity(quantity)): statusMessage = "Error: The quantity of the tickets has to be between 1 and 100." elif not(checkTicketName(name)): statusMessage = "Error: The name has to alphanumeric, have no spaces in the beginning or end and be between 6 and 60 characters." elif not(checkDateFormat(date)): statusMessage = "Error: The date has to be in the format 'YYYYMMDD'." elif not(checkExpire(date)): statusMessage = "Error: The date cannot be expired." elif not(checkPrice(price)): statusMessage = "Error: The price has to be between $10 and $100." if statusMessage != '': tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, sellMessage=statusMessage) else: # submits the ticket into the database, which then displays in the available tickets. bn.sell_ticket(quantity, name, email, price, date) # updates tickets. tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, sellMessage='Listing posted successful')
def sell_ticket(user): name = request.form.get('Name').strip() price = float(request.form.get('Price')) date = request.form.get('Date').replace("/", "") quantity = int(request.form.get('Quantity')) error_message = bn.sell_ticket(name, price, date, quantity, user) if error_message: session['error_message'] = error_message return render_template('index.html', user=user, tickets=bn.get_available_tickets(), msg=error_message) # Any response will have the webpage reload itself. return ('', 200)
def test_sell_ticket_user_not_in_db(self): """ User object that doesn't exist in database | user=<user not in DB> name="Unique" quantity=1 price=10.00 expiryDate=date(2030, 1, 1) | Internal Error: user does not exist in database """ # Prepare DB new_user = User() new_user.name = TEST_USER.name new_user.email = TEST_USER.email new_user.password = TEST_USER.password new_user.balance = TEST_USER.balance # Skip adding new_user to DB # Set up parameters user = new_user name = "Unique" quantity = 1 price = 10.00 expiryDate = date(2030, 1, 1) # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == "Internal Error: user does not exist in database"
def sell_post(user): """ Called when a user submits the sell form. If the given inputs match the R4 requirements, a new ticket is added for sale with the given quantity, price and expiry date. Otherwise an error message is shown. """ name = request.form.get('name') quantity = request.form.get('quantity') price = request.form.get('price') expiry = request.form.get('expiry') error = validate_ticket_format.check_for_sell_ticket_format_error( name, quantity, price, expiry) if not error: error = bn.sell_ticket(user, name, int(quantity), float(price), datetime.strptime(expiry, '%Y%m%d').date()) if not error: flash('Successfully sold the ticket', 'info') return redirect('/') else: flash('Unable to sell ticket: ' + error, 'error') return redirect('/')
def sell_post(user): sell_name = request.form.get('sell_name') qty = request.form.get('sell_qty') price = request.form.get('sell_price') date = request.form.get('sell_date') error_list = [] error_list = bn.sell_ticket(sell_name, qty, price, date) tickets = bn.get_all_tickets() if len(error_list) > 0: return render_template('index.html', user=user, tickets=tickets, balance=user.balance, message=error_list[0]) else: return render_template('index.html', user=user, tickets=tickets, balance=user.balance, message='Ticket Posted for Sale')
def test_sell_ticket_valid_with_fraction(self): """ All inputs valid, price with fractional part | user=<user in DB> name="Unique" quantity=1 price=12.34 expiryDate=date(2030, 1, 1) | No error """ # Prepare DB new_user = User() new_user.name = TEST_USER.name new_user.email = TEST_USER.email new_user.password = TEST_USER.password new_user.balance = TEST_USER.balance db.session.add(new_user) db.session.commit() # Set up parameters user = new_user name = "Unique" quantity = 1 price = 12.34 expiryDate = date(2030, 1, 1) # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == False
def test_sell_ticket_user_bad_type(self): """ Non-User type user parameter | user=None name="Unique" quantity=1 price=10.00 expiryDate=date(2030, 1, 1) | Internal Error: 'user' must be of type 'User' """ # Prepare DB new_user = User() new_user.name = TEST_USER.name new_user.email = TEST_USER.email new_user.password = TEST_USER.password new_user.balance = TEST_USER.balance db.session.add(new_user) db.session.commit() # Set up parameters user = None name = "Unique" quantity = 1 price = 10.00 expiryDate = date(2030, 1, 1) # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == "Internal Error: 'user' must be of type 'User'"
def test_sell_ticket_expiryDate_bad_type(self): """ Non-date type expiryDate parameter | user=<user in DB> name="Unique" quantity=1 price=10.00 expiryDate=None | Internal Error: 'expiryDate' must be of type 'date' """ # Prepare DB new_user = User() new_user.name = TEST_USER.name new_user.email = TEST_USER.email new_user.password = TEST_USER.password new_user.balance = TEST_USER.balance db.session.add(new_user) db.session.commit() # Set up parameters user = new_user name = "Unique" quantity = 1 price = 10.00 expiryDate = None # Call function ret_value = sell_ticket(user, name, quantity, price, expiryDate) # Check return value assert ret_value == "Internal Error: 'expiryDate' must be of type 'date'"
def sell_form_post(user): name = request.form.get('name') quantity = int(request.form.get('quantity')) price = int(request.form.get('price')) expireDate = request.form.get('expireDate') sellErrorMessage = None if not (bn.validateTicketName(name)): sellErrorMessage = "Invalid ticket name" elif not (bn.validateTicketQuantity(quantity)): sellErrorMessage = "Invalid ticket quantity" elif not (bn.validateTicketPrice(price)): sellErrorMessage = "Invalid ticket price" elif not (bn.validateTicketExpiryDate(expireDate)): sellErrorMessage = "Invalid ticket expiry date" if sellErrorMessage: return redirect(url_for('.profile', sellErrorMessage=sellErrorMessage)) error = bn.sell_ticket(user.email, name, quantity, price, expireDate) if error: return redirect(url_for('.profile', sellErrorMessage=error)) return redirect('/')