Example #1
0
File: views.py Project: tomik/honey
def _view_game(game_id, init_path, game_edit_form=None, comment_form=None):
    """
    Views existing game on given (comment) path.

    When there are errors with comment for instance of this one can be passed in and errors displayed.
    """
    game = db.get_game(game_id)
    if not game:
        abort(404)
    # fetch games owner
    db.annotate(game)
    comments=list(db.get_comments_for_game(game_id))
    for comment in comments:
        db.annotate(comment)
    # forms
    if not comment_form:
        comment_form = forms.CommentForm()
    user = db.get_user_by_username(session.get("username", None))
    typeahead_players = []
    if user and game.is_owner(user) and not game_edit_form:
        game_edit_form = forms.GameEditForm.form_factory(game.type)()
        # hint for inputting players names
        typeahead_players = dict([(player.name, player.rank) for player in db.get_players()])
    print game.nodes
    return render_template("view_game.html",
        game=game,
        init_path=init_path,
        comments=comments,
        comment_paths=[(str(c["_id"]), c["path"]) for c in comments],
        comment_form=comment_form,
        typeahead_players=typeahead_players,
        game_edit_form=game_edit_form)
Example #2
0
File: views.py Project: tomik/honey
def view_player(player_id):
    """
    Views existing player based on his id.

    Pagination is provided in query string via keys games_page.
    """
    player = db.get_player(player_id)
    if not player:
        abort(404)
    queries = dict(urlparse.parse_qsl(request.query_string))
    games_page = int(queries.get("games_page", 1))
    # paginate games
    games_per_page = app.config["games_per_page_in_player_view"]
    games_from, games_to = (games_page - 1) * games_per_page, games_page * games_per_page
    games_cursor = db.get_games_for_player(player._id)
    num_all_games = games_cursor.count()
    games = list(games_cursor)[games_from:games_to]
    games_pagination = Pagination(games_per_page, games_page, num_all_games,
            lambda page: url_for("view_player", player_id=player_id, games_page=page))
    for game in games:
        db.annotate(game)
    return render_template("view_player.html",
        player=player,
        games=games,
        games_pagination=games_pagination)
Example #3
0
File: views.py Project: tomik/honey
def _view_games(games, url_maker):
    queries = dict(urlparse.parse_qsl(request.query_string))
    page = int(queries.get("page", 1))
    per_page = app.config["games_per_page"]
    num_all_games = len(games)
    game_from, game_to = (page - 1) * per_page, page * per_page
    games = [db.annotate(game) for game in (games[game_from:game_to])]
    pagination = Pagination(per_page, page, num_all_games, url_maker)
    return render_template("games_list.html", menu_toggle_games=True, games=games, pagination=pagination)
Example #4
0
File: views.py Project: tomik/honey
def view_sgf(game_id):
    """Views sgf for game identified by game_id"""
    game = db.get_game(game_id)
    if not game:
        abort(404)
    annotated_comments = [db.annotate(c) for c in db.get_comments_for_game(game_id)]
    # this changes the game itself
    # but that is fine since we don't intend to save it
    game = db.patch_game_with_comments(game, annotated_comments)
    response = make_response(sgf.makeSgf([game.export()]))
    response.headers["Content-type"] = "text/plain"
    return response
Example #5
0
File: views.py Project: tomik/honey
def post_comment(game_id):
    """Posts comment for given game and path. Requires login. Called via ajax."""
    game = db.get_game(game_id)
    if not game:
        abort(404)
    form = forms.CommentForm(request.form)
    if form.validate_on_submit():
        username = session["username"]
        user = db.get_user_by_username(username)
        if not user:
            app.logger.warning("comment without user")
            abort(500)
        comment = db.create_comment(user._id, game_id, form.short_path, form.comment.data)
        db.annotate(comment)
        can_delete_comment = comment.is_owner(user)
        comment_template = app.jinja_env.from_string(
            """
            {%% from 'macros.html' import render_comment_in_game %%} {{ render_comment_in_game(comment, %s) }}
            """ % can_delete_comment)
        return jsonify(err=None, comment_id = str(comment["_id"]), comment_path=comment["path"], comment_html = comment_template.render(comment=comment))
    return jsonify(err="Invalid comment.")
Example #6
0
File: views.py Project: tomik/honey
def view_user(username):
    """
    Views existing user based on his username.

    Pagination is provided in query string via keys games_page, comments_page.
    """
    user = db.get_user_by_username(username)
    if not user:
        abort(404)
    queries = dict(urlparse.parse_qsl(request.query_string))
    games_page = int(queries.get("games_page", 1))
    comments_page = int(queries.get("comments_page", 1))
    # paginate games
    games_per_page = app.config["games_per_page_in_user_view"]
    games_from, games_to = (games_page - 1) * games_per_page, games_page * games_per_page
    games_cursor = db.get_games_for_user(user._id)
    num_all_games = games_cursor.count()
    games = list(games_cursor)[games_from:games_to]
    games_pagination = Pagination(games_per_page, games_page, num_all_games,
            lambda page: url_for("view_user", username=username, games_page=page, comments_page=comments_page))
    for game in games:
        db.annotate(game)
    # paginate comments
    comments_per_page = app.config["comments_per_page_in_user_view"]
    comments_from, comments_to = (comments_page - 1) * comments_per_page, comments_page * comments_per_page
    comments_cursor = db.get_comments_for_user(user._id)
    num_all_comments = comments_cursor.count()
    comments = list(comments_cursor)[comments_from:comments_to]
    comments_pagination = Pagination(comments_per_page, comments_page, num_all_comments,
            lambda page: url_for("view_user", username=username, games_page=games_page, comments_page=page))
    for comment in comments:
        db.annotate(comment)
    return render_template("view_user.html",
        user=user,
        games=games,
        comments=comments,
        games_pagination=games_pagination,
        comments_pagination=comments_pagination)