コード例 #1
0
def check_game_category(category_name):
    """Check if the game category is already in the database;
    if not, make a new entry. Return the category.
    """
    category = GameCategory.query.filter_by(name=category_name).scalar()
    if not category:
        new_category = GameCategory(name=category_name)
        db_session.add(new_category)
        db_session.commit()
        category = GameCategory.query.filter_by(name=category_name).scalar()
    return category
コード例 #2
0
def add_club_admin(user_email):
    """Add user to club admins."""
    user = User.query.filter_by(email=user_email).scalar()
    if not user:
        print 'User not found'
    elif user.club_admin:
        print 'User is already an admin'
    else:
        admin = ClubAdmin(user_id=user.id)
        db_session.add(admin)
        db_session.commit()
        print 'User added to club admins'
コード例 #3
0
def patch_resource(attributes, my_obj):
    """Patch database resource.

    Args:
        attributes (list): list of dictionaries;
            each dictionary is in the following format:
            {'name': attr_name, 'value': attr_value}.
        my_obj: instance of any of the models classes.
    """
    for attribute in attributes:
        setattr(my_obj, attribute['name'], attribute['value'])
    db_session.add(my_obj)
    db_session.commit()
コード例 #4
0
def check_user(email, name, picture):
    """Check if the user is already in the database;
    if not, make a new entry. Return user's id.
    """
    user = User.query.filter_by(email=email).scalar()
    new_user = False
    if not user:
        print 'adding new user to the db'
        user = User(email=email, name=name, picture=picture)
        db_session.add(user)
        db_session.commit()
        user = User.query.filter_by(email=email).scalar()
        new_user = True
    else:
        print 'user already exists'
    return user.id, new_user
コード例 #5
0
def check_game(bgg_id):
    """Check if the game is already in the database;
    if not, make a new entry. Return the game.
    """
    bgame = Game.query.filter_by(bgg_id=bgg_id).scalar()
    if not bgame:
        # Get the game info from bgg API
        game_info, bgg_categories = bgg_game_info(bgg_id)
        # Add the game to the database
        bgame = Game(**game_info)
        bgame.categories = bgg_categories
        db_session.add(bgame)
        db_session.commit()
        print 'Game added to the database!'
    else:
        print 'Game already in the database'
    return bgame
コード例 #6
0
def profile_game_add(user_id):
    """Create UserGame or return page with form to do so.

    Use POST and GET methods respectively.
    """
    if request.method == 'GET':
        # Show the game options matching the specified name
        bgg_options = bgg_game_options(request.args['name'])
        return render_template('game-options.html', games=bgg_options)
    else:
        # Add the chosen game to the database
        game = check_game(request.form['bgg-id'])
        user = User.query.filter_by(id=user_id).scalar()
        user.games.append(game)
        db_session.add(user)
        db_session.commit()
        flash('Game added to the collection!')
        return redirect(url_for('profile_', user_id=user_id))
コード例 #7
0
def post_add():
    """Create Post or return page with form to do so.

    Use POST and GET methods respectively.
    """
    if request.method == 'GET':
        return render_template('post-new.html')
    else:
        # Add Post to the database
        post_data = {
            'user_id': session['user_id'],
            'subject': request.form['subject'],
            'body': request.form['body'],
            'posted': int(time.time())
        }
        post = Post(**post_data)
        db_session.add(post)
        db_session.commit()
        flash('Post created!')
        return redirect(url_for('home'))