def register(): """ this method writes new unique user information into the users table in the database. then creates a new session and directs them to the current game.""" form = forms.RegForm() # if not form. is where the magic happends in flask-wtforms if not form.validate_on_submit(): print form.errors return render_template("signup.html", form=form) name = form.name.data email = form.email.data password = form.password.data latitude = float(form.lat.data) longitude = float(form.long.data) # from here to the comment above this is flask-wtf library. http://flask.pocoo.org/docs/patterns/wtforms/ to read more about this existing = db_session.query(User).filter_by(email=email).first() print existing if existing: flash ('Email is already in use please try again') return redirect(url_for('display_signup')) else: new_user = User(email=email, password=password, name=name, longitude=longitude, latitude=latitude) db_session.add(new_user) db_session.commit() db_session.refresh(new_user) user = db_session.query(User).filter_by(email=email, password=password).one() session['user_id'] = user.id return redirect(url_for('current_game'))
def create_games(): """this method will write to the game table.""" game_title = request.form['game_title'] game_description = request.form['game_description'] existing = db_session.query(Game).filter_by(game_title = game_title).first() if existing: flash ('This game title is already in use please try again for a unique game title.') return redirect(url_for('display_make_game')) else: new_game = Game(game_title=game_title, game_description=game_description) db_session.add(new_game) db_session.commit() db_session.refresh(new_game) game_id = new_game.id return redirect(url_for('display_make_challenge', game_id=game_id))
def next_challenge(game_id): """ if the user CLICKS on +1 CHALLENGE BUTTON this method will WRITE the challenge to the challenge table in the database and display a new blank form till user presses submit.""" story = request.form.get('story') puzzle = request.form['puzzle'] solution = request.form['correct_solution'] # challenge_position = request.form['challenge_position'] add in a hidden form input that will auto count how many challenges are made if story is None or puzzle is None or solution is None: flash ('Please fill out everything on the page as this is all critical information your players will need in order to play your game.') return redirect(url_for('display_make_challenge')) else: new_challenge = Challenge(game_id=game_id, story=story, puzzle=puzzle, solution=solution) # how do I add a counter? db_session.add(new_challenge) db_session.commit() db_session.refresh(new_challenge) return redirect(url_for('next_challenge', game_id=game_id))
def update_collection(): """can add to collection with card title""" while True: try: card_name = request.form['name'] # checking to see if card is real card from Card class and getting the card card_from_table = db_session.query(Card).filter_by(name=card_name).one() col_itm_card = Collection_item(cards_id = card_from_table.id) db_session.add(col_itm_card) db_session.commit() db_session.refresh(col_itm_card) flash("you've successfully added a card to your collection") return redirect ("/update") except sqlalchemy.orm.exc.NoResultFound: flash("Update countered! Try again") return redirect("/update")