def add_session(): if request.method == "POST": print(request.form) if not request.form["session_id"]: error = "There needs to be a session id" if not request.form["date"]: error = "There needs to be a date" if not request.form["beers"] or int(request.form["beers"]) == 0: error = "There needs to be at least one beer, but one beer would be a lonely flight club dog dog dog dog dog dog dog " else: # Check Session isn't duplicate session_id = request.form["session_id"] if db_func.check_if_session_exists(session_id): error = "This is a duplicate session" else: # Add the session date = request.form["date"] db.session.add(Session(id=session_id, date=date)) db.session.commit() # Extract Number of Beers num_beers = int(request.form["beers"]) # Loop Through Form # TODO (dan) there is a way for this form to submit without there being beers # should probably fix the front end.. for i in range(num_beers): beer_name = request.form["beer_name{}".format(i)] brewery = request.form["brewery{}".format(i)] style = request.form["style{}".format(i)] votes = request.form["votes{}".format(i)] win = request.form["win{}".format(i)] username = request.form["username{}".format(i)] db.session.add( Beer( beer_name=beer_name, brewery=brewery, style=style, votes=votes, win=win, username=username, session_id=session_id, )) db.session.commit() return render_template("sessions/fc{}".format(int(session_id))) flash(error) return render_template("sessions/add_session.html") return render_template("sessions/add_session.html")
def view_session(id): # If this isn't a valid session, send a 404. if not db_func.check_if_session_exists(id): abort(404) fc_session = FCSession(int(id)) return render_template( "sessions/session_view.html", session_id=fc_session.id, date=fc_session.date, winner=fc_session.winner, beers=fc_session.beers, avg_abv=fc_session.session_avg_abv, )
def _validate_session_id(session_id): try: session_id = int(session_id) except: return ValidatorResults( "Session_id needs to be a positive integer", False) if db_func.check_if_session_exists(session_id): return ValidatorResults("Session already exists", False) if session_id <= 0: return ValidatorResults( "Session id should be positve and greater than 0", False) return ValidatorResults("", True)
def test_add_session(app, test_client): with app.app_context(): db_func.add_session(1, "1/1/2020") assert db_func.check_if_session_exists(1) == True
def test_no_session_check(app, test_client): # This database should be empty at the start. with app.app_context(): assert db_func.check_if_session_exists(1) == False