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 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.session.query(Session.query.filter_by(id=session_id).exists()).scalar(): 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 validate_beers(self): still_beers = True while still_beers: try: i = len(self._beer_model_list) beer_name = self._form["beer_{}".format(i)] beer_abv = self._form["beer_abv_{}".format(i)] brewery = self._form["brewery_{}".format(i)] style = self._form["style_{}".format(i)] votes = self._form["votes_{}".format(i)] win = self._form["win_{}".format(i)] username = self._form["username_{}".format(i)] val_res = ValidatorResults() validator_list = [ (self._empty_string_validator, (beer_name, beer_name)), (self._empty_string_validator, (brewery, beer_name)), (self._abv_validator, (beer_abv, beer_name)), (self._empty_string_validator, (style, beer_name)), (self._votes_validator, (votes, beer_name)), (self._win_validator, (win, beer_name)), (self._validate_user, (username, beer_name)), ] for func, args in validator_list: val_res = func(args[0], args[1]) if not val_res.success: return val_res self._beer_model_list.append( Beer( beer_name=beer_name, beer_abv=float(beer_abv), brewery=brewery, style=style, votes=int(votes), win=int(win), username=username, session_id=self._session_model.id, )) except KeyError: still_beers = False val_res = self._validate_winner(self._beer_model_list) if not val_res.success: return val_res return ValidatorResults("", True)
def add_beer(row): # CSV Format # session, date, username, order, beer, brewery, score, win, specific type, type, abv db.session.add( Beer( beer_name=row[4], beer_abv=float(row[10]), brewery=row[5], style=row[9], votes=int(row[6]), win=int(row[7]), username=row[2], session_id=int(row[0]), )) db.session.commit()
def test_beer(): """Function for a test add of a beer """ if request.method == 'POST': beer_name = request.form['beer_name'] brewery = request.form['brewery'] votes = request.form['votes'] win = request.form['win'] session_id = request.form['session'] username = g.user.username error = None if not beer_name: error = 'Beer name is required' elif not brewery: error = 'Brewery is retired' elif not votes: votes = 0 elif not win: win = 0 elif not username: print(username) if error is None: db.session.add(Beer(beer_name=beer_name, brewery=brewery, votes=int(votes), win=int(win), username=username, session_id=int(session_id))) db.session.commit() return redirect(url_for('sessions.beer_list')) flash(error) return render_template('sessions/test_beer.html')