Beispiel #1
0
def profile_(user_id):
    """Return user's profile page or Update Profile or Delete Profile.

    Use GET, PATCH and DELETE methods respectively.
    """
    try:
        user = User.query.filter_by(id=user_id).one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    if request.method == 'GET':
        # Return user's profile page
        return render_template('profile.html',
                               user=user,
                               games=user.games,
                               owner=check_ownership())
    elif request.method == 'PATCH':
        # Update Profile
        attributes = request.get_json()['data']['attributes']
        patch_resource(attributes, user)
        flash('Profile updated!')
        return '', 204
    else:
        # Delete Profile
        games = user.games
        db_session.delete(user)
        db_session.commit()
        clear_games(*games)
        sign_out()
        flash('Profile deleted!')
        return '', 204
Beispiel #2
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
Beispiel #3
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'
Beispiel #4
0
def profile_game_(user_id, game_id):
    """Delete UserGame."""
    try:
        user = User.query.filter_by(id=user_id).one()
        game = Game.query.filter_by(id=game_id).one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    user.games.remove(game)
    db_session.commit()
    clear_games(game)
    flash('Game removed from the collection!')
    return '', 204
Beispiel #5
0
def club_game_(game_id):
    """Delete ClubGame."""
    club = Club.query.filter_by(id=1).scalar()
    try:
        game = Game.query.filter_by(id=game_id).one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    club.games.remove(game)
    db_session.commit()
    clear_games(game)
    flash('Game removed from the collection!')
    return '', 204
Beispiel #6
0
def clear_games(*games):
    """Remove orphaned games from the database.

    If any of the games is not owned by any user or the club,
    remove it from the database.
    """
    for game in games:
        if len(game.users) == 0 and len(game.clubs) == 0:
            categories = game.categories
            db_session.delete(game)
            db_session.commit()
            clear_categories(*categories)
Beispiel #7
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()
Beispiel #8
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
Beispiel #9
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
Beispiel #10
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))
Beispiel #11
0
def post_(post_id):
    """Update or Delete Post.

    Use PATCH and DELETE methods respectively.
    """
    post = Post.query.filter_by(id=post_id).scalar()
    if request.method == 'PATCH':
        # Update Post
        attributes = request.get_json()['data']['attributes']
        attributes.append({'name': 'edited', 'value': int(time.time())})
        patch_resource(attributes, post)
        flash('Post edited!')
        return '', 204
    else:
        # Delete Post
        db_session.delete(post)
        db_session.commit()
        flash('Post deleted!')
        return '', 204
Beispiel #12
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'))
Beispiel #13
0
def game_(game_id):
    """Return game page or Update Game.

    Use GET and POST methods respectively.
    """
    try:
        bgame = Game.query.filter_by(id=game_id).one()
    except sqlalchemy.orm.exc.NoResultFound:
        abort(404)
    if request.method == 'GET':
        # Return game page
        return render_template('game.html', game=bgame)
    else:
        # Update game info from bgg API
        game_info, bgg_categories = bgg_game_info(bgame.bgg_id)
        for key, value in game_info.iteritems():
            setattr(bgame, key, value)
        bgame.categories = bgg_categories
        db_session.commit()
        flash('Game info updated!')
        return redirect(url_for('game_', game_id=game_id))
Beispiel #14
0
def clear_categories(*categories):
    """Remove orphaned game categories from the database"""
    for category in categories:
        if len(category.games) == 0:
            db_session.delete(category)
            db_session.commit()