def buy_post(user): statusMessage = '' # Gets the information needed from the form to create the Ticket object. email = session['logged_in'] quantity = request.form.get('buy-quantity') name = request.form.get('buy-name') if (checkTicketExists(name)): if 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(checkQuantity(quantity)): statusMessage = "Error: The quantity of the tickets has to be between 1 and 100." elif not(bn.isEnoughTickets(name, quantity)): statusMessage = "Error: The specified quantity of tickets not available." elif not (hasEnoughBalance(user, name, quantity)): statusMessage = "Error: Your balance is too low!" if statusMessage != '': tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, buyMessage=statusMessage) else: # evaulates which ticket you want to "buy" and deletes it from the database. bn.buy_ticket(name, quantity) tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, buyMessage='Purchase successful') else: statusMessage = "Ticket does not exist." tickets = bn.get_all_tickets() return render_template('index.html', user=user, tickets=tickets, buyMessage=statusMessage)
def buy_post(): title = request.form.get('buy-name') try: quantity = int(request.form.get('buy-quantity')) except: return redirect('/?bMessage=Field Requires Integer') ticket = bn.get_ticket(title) if not re.search(regex_title, title): return redirect('/?bMessage=Name Format Error') elif not ticket: return redirect('/?bMessage=Ticket Does Not Exist') email = session['logged_in'] user = bn.get_user(email) serviceFee = ticket.price * quantity * 0.35 tax = ticket.price * quantity * 0.05 cost = (ticket.price * quantity + serviceFee + tax) if quantity <= 0 or quantity > 100: return redirect('/?bMessage=Invalid Quantity') elif quantity > ticket.quantity: return redirect('/?bMessage=Not Enough Tickets Left') elif user.balance < cost: return redirect('/?bMessage=Insufficient Funds') bn.buy_ticket(title, quantity, cost, user) return render_template('temp.html', message='Bought')
def buy(): """ Route to buy a ticket. This route will validate the ticket form, if valid it will update the database through a backend function """ if 'logged_in' not in session: return redirect('/login') email = session['logged_in'] # Get user information user = bn.get_user(email) # Sets the error message to blank initially error_message = "" # Get information from the form name = request.form.get('name') quantity = request.form.get('quantity') # Get all tickets to pass to backend function tickets = bn.get_all_tickets() error_message = check_ticket_form(name, quantity) if not error_message: if bn.buy_ticket(name, user, int(quantity)): message = "Tickets bought succesfully" else: error_message = "Ticket could not be bought" # Checks if there is an error, and if there is set the error message if len(error_message) > 0: session['error'] = error_message message = session["error"] del session["error"] return render_template('index.html', buy_message=message, user=user, tickets=tickets)
def buy_post(): user_email = session['logged_in'] ticket_name = request.form.get('buy-name') ticket_quantity = int(request.form.get('buy-quantity')) error_message = None tik = bn.get_ticket(ticket_name) if (ticket_name_check(ticket_name) is None): # no match in regex error_message = 'Ticket name is incorrect' elif quantity_check(ticket_quantity): error_message = "Invalid quantity in ticket buy form" elif not tik: return "Ticket does not exist" elif tik.quantity < ticket_quantity: return "Not enough tickets for sale" elif bn.get_balance(user_email) < (tik.price * ticket_quantity * 1.40): return "Insufficient balance" else: error_message = bn.buy_ticket(user_email, ticket_name, ticket_quantity) return error_message
def test_ticket_invalid_quantity(self): ''' Test case B1.3 This tests when the user requests too many tickets ''' self.assertFalse( bn.buy_ticket('t1', BackEndBuyTicketTest.test_user_2, 101))
def buy_form_post(user): name = request.form.get('buyName') quantity = int(request.form.get('buyQuantity')) buyErrorMessage = None # Checks for ticket arguments validity if not (bn.validateTicketName(name)): buyErrorMessage = "Invalid ticket name" elif not (bn.validateTicketExists(name)): buyErrorMessage = "Invalid ticket name: ticket does not exist" elif not (bn.validateTicketQuantity(quantity)): buyErrorMessage = "Invalid ticket quantity" if buyErrorMessage: return redirect(url_for('.profile', buyErrorMessage=buyErrorMessage)) try: # Tries to buy ticket, checks for sufficient quantity and purchasing funds buyErrorMessage = bn.buy_ticket(user, name, quantity) except IntegrityError: buyErrorMessage = "Could not buy ticket" if buyErrorMessage: return redirect(url_for('.profile', buyErrorMessage=buyErrorMessage)) return redirect('/')
def test_ticket_does_not_exist(self): ''' Test case B1.2 This tests when the ticket does not exist ''' self.assertFalse( bn.buy_ticket('invalid', BackEndBuyTicketTest.test_user_2, 0))
def buyticket(): ticket_id = int(request.form['ticket_id']) buyer_email = request.form['user'] buyer = bn.get_user(buyer_email) message = "" user_balance = buyer.balance ticket_price = bn.get_ticket(ticket_id).price # check for sufficient funds if not does_user_have_sufficient_balance(user_balance, ticket_price): message = "User balance does not have sufficient funds." if not message: # if message is empty, indicating no validation errors bn.buy_ticket(ticket_id, buyer.email) message = "Ticket purchased successfully." # redirect user to profile page with result message return redirect("/?message={}".format(message))
def test_insufficient_funds(self): ''' Test case B1.5 This tests when the user does not have the funds ''' self.assertFalse( bn.buy_ticket('t1', BackEndBuyTicketTest.test_user_2, 72)) # Verify no funds have been transferred or tickets self.assertTrue(BackEndBuyTicketTest.test_user.balance == 0) self.assertTrue(BackEndBuyTicketTest.test_user_2.balance == 5000) self.assertTrue(BackEndBuyTicketTest.test_ticket.quantity == 100)
def buy_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.buy_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_database_integrity_fix(self): ''' Test case B1.7 This tests if the owner of the ticket does not have a balance the function fixes and doesn't crash ''' quantity = 2 tickets_value = quantity * BackEndBuyTicketTest.test_ticket_invalid_user.price self.assertTrue( bn.buy_ticket('t2', BackEndBuyTicketTest.test_user_2, quantity)) # Make sure owner has gotten funds # user = User.query.filter_by(email=invalid_user.email).first() self.assertTrue( BackEndBuyTicketTest.invalid_user.balance == tickets_value)
def buy_post(user): name = request.form.get('name') quantity = request.form.get('quantity') # validate ticket name name_error = validate_ticket_format.check_for_ticket_name_error(name) # validate ticket quantity quantity_error = validate_ticket_format.check_for_ticket_quantity_error( quantity) if name_error: flash(name_error, 'error') return redirect('/') if quantity_error: flash(quantity_error, 'error') return redirect('/') # get current ticket current_ticket = bn.get_ticket_by_name(name) # validate existence of current ticket to buy if not current_ticket: error_message = "The ticket does not exist." flash(error_message, 'error') return redirect('/') # validate the number ticket to buy if int(quantity) > int(current_ticket.quantity): error_message = "The ticket quantity is less than the quantity requested." flash(error_message, 'error') return redirect('/') total_price = calculate_price_ticket(quantity, current_ticket.price) # get current user # validate balance and ticket price if total_price > float(user.balance): error_message = "Must have more balance than the ticket price." flash(error_message, 'error') return redirect('/') # try to buy ticket buy_error = bn.buy_ticket(user.email, total_price, name, quantity) if buy_error: flash(buy_error, 'error') return redirect('/') flash('Ticket was purchased successfully.', 'info') return redirect('/')
def buy_post(user): buy_name = request.form.get('buy_name') qty = request.form.get('buy_qty') error_list = [] error_list = bn.buy_ticket(buy_name, qty, user) 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 Purchased')
def test_valid_buy(self): ''' Test case B1.1 (also tests B1.4, B1.6 and B1.8) This tests the ticket existing and a correct quantity to buy ''' quantity = 2 test_ticket = BackEndBuyTicketTest.test_ticket test_user_2 = BackEndBuyTicketTest.test_user_2 owner = BackEndBuyTicketTest.test_user tickets_value = quantity * test_ticket.price + ( quantity * test_ticket.price * 0.4) self.assertTrue(bn.buy_ticket('t1', test_user_2, quantity)) # Check changes to database took place # owner = User.query.filter_by(email=test_ticket.email).first() # buyer = User.query.filter_by(email=test_user_2.email).first() # ticket = Ticket.query.filter_by(name=test_ticket.name).first() self.assertTrue(owner.balance == quantity * test_ticket.price) self.assertTrue(test_user_2.balance == 5000 - tickets_value) self.assertTrue(test_ticket.quantity == 100 - quantity)