def sign_up():
    """
    Creates a new user and logs them in
    """
    form = SignUpForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        user = User(username=form.data['username'],
                    email=form.data['email'],
                    password=form.data['password'],
                    free_currency=1000)
        db.session.add(user)
        db.session.commit()

        new_deck = Deck(user_id=user.to_dict()['id'],
                        name=f"{user.to_dict()['username']}'s deck")
        db.session.add(new_deck)
        db.session.commit()

        card_arr = random.choice(decks)
        for card in card_arr:
            new_card = Card(
                user_id=user.to_dict()['id'],
                card_type=card,
                deck_id=new_deck.to_dict_lite()['id'],
            )
            db.session.add(new_card)

        db.session.commit()

        login_user(user)
        return user.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}, 401
Exemple #2
0
def new_deck(id):
    req = request.json
    new_deck = Deck(user_id=id, name=req)
    db.session.add(new_deck)
    db.session.commit()
    return new_deck.to_dict_lite()