from flamejam import app, db, mail
from flamejam.utils import get_slug
from flamejam.models import Jam, Game, User, Comment, GamePackage, \
    GameScreenshot, JamStatusCode, Rating
from flamejam.models.rating import RATING_CATEGORIES
from flamejam.forms import WriteComment, GameEditForm, GameAddScreenshotForm, \
    GameAddPackageForm, GameAddTeamMemberForm, GameCreateForm, RateGameForm
from flask import render_template, url_for, redirect, flash, request, abort
from flask.ext.login import login_required, current_user
@app.route("/jams/<jam_slug>/create-game/", methods = ("GET", "POST"))
def create_game(jam_slug):
    jam = Jam.query.filter_by(slug = jam_slug).first_or_404()
    r = current_user.getParticipation(jam)
    if not r or not
        flash("You cannot create a game without participating in the jam.", category = "error")
        return redirect(jam.url())
        flash("You already have a game.")
        return redirect(
    enabled = (JamStatusCode.RUNNING <= jam.getStatus().code <= JamStatusCode.PACKAGING)
    form = GameCreateForm(request.form, obj = None)
    if enabled and form.validate_on_submit():
        game = Game(,
        return redirect(url_for("edit_game", jam_slug = jam_slug, game_id =
    return render_template("jam/game/create.html", jam = jam, enabled = enabled, form = form)
@app.route("/jams/<jam_slug>/<game_id>/edit/", methods = ("GET", "POST"))
def edit_game(jam_slug, game_id):
    jam = Jam.query.filter_by(slug = jam_slug).first_or_404()
    game = Game.query.filter_by(is_deleted = False, id = game_id).first_or_404()
    if not game or not current_user in
    form = GameEditForm(request.form, obj = game)
    package_form = GameAddPackageForm()
    screenshot_form = GameAddScreenshotForm()
    if form.validate_on_submit():
        slug = get_slug(
        # if not = slug).first() in (game, None):
            # flash("A game with a similar title already exists. Please choose another title.", category = "error")
        # else:
        #form.populate_obj(game) this breaks dynamic stuff below
        game.title =
        game.description = = =
        if game.jam.getStatus().code < 4:
            for c in RATING_CATEGORIES:
                setattr(game, "score_" + c + "_enabled", form.get(c).data)
        game.slug = get_slug(game.title)
        flash("Your settings have been applied.", category = "success")
        return redirect(game.url())
    if package_form.validate_on_submit():
        s = GamePackage(game,,
        flash("Your package has been added.", "success")
        return redirect(request.url)
    if screenshot_form.validate_on_submit():
        s = GameScreenshot(,, game)
        flash("Your screenshot has been added.", "success")
        return redirect(request.url)
    return render_template("jam/game/edit.html", jam = jam, game = game,
        form = form, package_form = package_form, screenshot_form = screenshot_form)
def game_package_edit(id, action):
    if not action in ("delete"):
    p = GamePackage.query.filter_by(id = id).first_or_404()
    if not current_user in
    if action == "delete":
    return redirect(url_for("edit_game", jam_slug =, game_id =
def game_screenshot_edit(id, action):
    if not action in ("up", "down", "delete"):
    s = GameScreenshot.query.filter_by(id = id).first_or_404()
    if not current_user in
    if action == "up":
    elif action == "down":
    elif action == "delete":
        i = 0
        for x in
            x.index = i
            i += 1
    return redirect(url_for("edit_game", jam_slug =, game_id =
@app.route('/jams/<jam_slug>/<game_id>/', methods = ("POST", "GET"))
def show_game(jam_slug, game_id):
    comment_form = WriteComment()
    jam = Jam.query.filter_by(slug = jam_slug).first_or_404()
    game = Game.query.filter_by(is_deleted = False, id = game_id).filter_by(jam = jam).first_or_404()
    if current_user.is_authenticated() and comment_form.validate_on_submit():
        comment = Comment(, game, current_user)
        # notify the team
        for user in
            if user.notify_game_comment:
                body = render_template("emails/comment.txt", recipient=user, comment=comment)
                mail.send_message(subject=current_user.username + " commented on " + game.title, recipients=[], body=body)
        flash("Your comment has been posted.", "success")
        return redirect(game.url())
    rating = Rating.query.filter_by(game_id =, user_id = current_user.get_id()).first()
    return render_template('jam/game/info.html', game = game, form = comment_form, rating = rating)
@app.route("/jams/<jam_slug>/<game_id>/rate/", methods = ("GET", "POST"))
def rate_game(jam_slug, game_id):
    jam = Jam.query.filter_by(slug = jam_slug).first_or_404()
    game = Game.query.filter_by(, is_deleted=False, id=game_id).first_or_404()
    form = RateGameForm()
    if jam.getStatus().code != JamStatusCode.RATING:
        flash("This jam is not in the rating phase. Sorry, but you cannot rate right now.", "error")
        return redirect(game.url())
    if current_user in
        flash("You cannot rate on your own game. Go rate on one of these!", "warning")
        return redirect(url_for("jam_games", jam_slug = jam.slug))
    # Allow only users who participate in this jam to vote.
    if not current_user in jam.participants:
        flash("You cannot rate on this game. Only participants are eligible for vote.", "error")
        return redirect(url_for("jam_games", jam_slug = jam.slug))
    rating = Rating.query.filter_by(,
    if rating:
        flash("You are editing your previous rating of this game.", "info")
    if form.validate_on_submit():
        new = rating == None
        if not rating:
            rating = Rating(game, current_user,,
            rating.text =
        for c in ["overall"] + game.ratingCategories:
            rating.set(c, form.get(c).data)
        flash("Your rating has been " + ("submitted" if new else "updated") + ".", "success")
        return redirect(url_for("jam_games", jam_slug = jam.slug))
    elif rating:
        for c in ["overall"] + game.ratingCategories:
            form.get(c).data = rating.get(c) = rating.text
    return render_template('jam/game/rate.html', jam = jam, game = game, form = form)