def buy_post(): ticket_name = request.form.get('ticket_name') # Set ticket name from form email = session['logged_in'] # Set email from logged in session quantity = request.form.get('quantity') # Set quanity from from errors = [] # Create an empty arry to store errors buy_msg = 'Failed to buy the ticket(s): ' # Set buy_msg to a failed message user = usr.get_user(email) balance = user.balance / 100 # Check that the ticket name meets requirements if not valid.validate_name(ticket_name): errors.append( "The name of the ticket must be no more than 60 characters using alphanumeric characters with no spaces at the begining and end" ) # Check that the quantity is valid if not quantity.isdigit(): errors.append("Quantity must be entered using 0-9") elif not valid.validate_quantity(quantity) or int(quantity) == 0: errors.append("You may only buy between 0 and 100 tickets inclusive") # If no errors in input if len(errors) == 0: # Try to buy the ticket ticket = tic.buy_ticket(ticket_name, quantity, balance, email) # If successfully bought change buy msg to show that if ticket is None: buy_msg = 'Successfully bought the ticket(s).' # If failed add to errors else: errors.append(ticket) buy_msg = buy_msg + str(ticket) # If there are errors in input create message elif len(errors) > 0: buy_msg += ", ".join( errors) + "." #adding all of the errors to the update message resp = make_response(redirect('/', code=303)) resp.set_cookie('buy_msg', buy_msg) return resp
def update_post(): ticket_name = request.form.get('ticket_name') email = session['logged_in'] quantity = request.form.get('quantity') price = request.form.get('price') expiration = request.form.get('expiration') errors = [] update_msg = 'Failed to update the ticket(s): ' if not valid.validate_name(ticket_name): errors.append( "The name of the ticket cannot be: blank, contain more than 60 characters, contain soecial characters, or any white space" ) #if not valid.validate_quantity(quantity) and int(quantity)!=0: #errors.append("You may only sell between 0 and a hundred tickets inclusive") if not valid.validate_quantity(quantity): errors.append( "You may only sell between one and a hundred tickets inclusive, and quantity listed must be in numeric form" ) if not valid.validate_price(price): errors.append( "Prices must be between $10 and $100 (whole numbers only)") if not valid.validate_date(expiration): errors.append( "Date must be in the format YYYYMMDD, no separators and cannot be blank" ) if len(errors) == 0: ticket = tic.update_ticket(ticket_name, quantity, price, expiration, email) if ticket is None: update_msg = 'Successfully updated the ticket(s)' else: errors.append(ticket) if len(errors) > 0: update_msg += ", ".join( errors) + "." #adding all of the errors to the updat message resp = make_response(redirect('/', code=303)) resp.set_cookie('update_msg', update_msg) return resp
def sell_post(): """ When someone tries to post a ticket listing to this route, validate the details, and if validation is successful attempt to list the ticket; may fail if ticket has already been posted by user. """ email = session['logged_in'] ticket_name = request.form.get('ticket_name') quantity = request.form.get('quantity') price = request.form.get('price') expiration = request.form.get('expiration') errors = [] sell_msg = 'Failed to list the ticket(s): ' if not valid.validate_name(ticket_name): errors.append( "The name of the ticket names must be longer than 6-characters, shorter than 60 characters, and be alpha-numeric with spaces (spaces are not allowed at the beginning or the end)" ) if not valid.validate_quantity(quantity): errors.append( "You may only sell between 1 and 100 tickets at a time with SeetGeek" ) if not valid.validate_price(price): errors.append( "Prices must be between $10 and $100 (whole numbers only)") if not valid.validate_not_expired(expiration): errors.append( "Ticket cannot be expired and must conform to YYYYMMDD format") if len(errors) == 0: ticket = tic.add_ticket(ticket_name, quantity, price, expiration, email) if ticket is None: sell_msg = 'Successfully listed the ticket(s)' else: errors.append(ticket) #merge the errors together. if len(errors) > 0: sell_msg += ", ".join(errors) + "." resp = make_response(redirect('/', code=303)) resp.set_cookie('sell_msg', sell_msg) return resp
def test_validation_qty_valid(): test_qty = "1" assert valid.validate_quantity(test_qty) is True
def test_validation_qty_outofrange(): test_qty = "0" assert valid.validate_quantity(test_qty) is False
def test_validation_qty_badint_inrange(): test_qty = "1E" assert valid.validate_quantity(test_qty) is False
def test_validation_qty_badint_outofrange(): test_qty = "zero" assert valid.validate_quantity(test_qty) is False