diff --git a/app/game_settings/__init__.py b/app/game_settings/__init__.py index d123274..3278ca2 100644 --- a/app/game_settings/__init__.py +++ b/app/game_settings/__init__.py @@ -2,4 +2,4 @@ from flask import Blueprint bp = Blueprint('game', __name__) -from app.game_settings import handlers +from app.game_settings import routes diff --git a/app/main/__init__.py b/app/main/__init__.py index f1c69e2..0e73c07 100644 --- a/app/main/__init__.py +++ b/app/main/__init__.py @@ -2,4 +2,4 @@ from flask import Blueprint bp = Blueprint('main', __name__) -from app.main import routes \ No newline at end of file +from app.main import routes, routes_objective \ No newline at end of file diff --git a/app/main/routes.py b/app/main/routes.py index 0b6e26a..9b1acb2 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -12,11 +12,10 @@ from sqlalchemy import and_ from app import db from app.main import bp -from app.utils import generate_qr_code, serve_pil_image, get_game_if_owner -from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder,\ +from app.utils import get_game_if_owner +from app.models import User, Game, Role, GamePlayer, ObjectiveMinimalEncoder,\ LocationEncoder, PlayerCaughtPlayer, Review, Location -from app.main.forms import ObjectiveForm, PlayerAddForm, UserCreateForm, \ - PlayerUpdateForm, CatchBunnyForm +from app.main.forms import PlayerAddForm, UserCreateForm, PlayerUpdateForm, CatchBunnyForm @bp.before_app_request def before_request(): @@ -32,8 +31,6 @@ def index(): return redirect(url_for('main.game_dashboard', game_name=current_user.games[0].name)) return render_template("index.html", title='Home') - - @bp.route('/game//dashboard') @login_required def game_dashboard(game_name): @@ -208,78 +205,6 @@ def game_player(game_name, username): return redirect(url_for('main.game_dashboard', game_name=game.name)) return render_template('game_player.html', title=f'{user.name} in {game_name}', player=player, form=form, json=json, location_encoder=LocationEncoder) -@bp.route('/game//add_objective', methods=['GET', 'POST']) -@login_required -def add_objective(game_name): - game = get_game_if_owner(game_name) - form = ObjectiveForm() - objective = Objective(name='', latitude=52.0932, longitude=5.12405) - if form.validate_on_submit(): - objective = Objective(name=form.objective_name.data, longitude=form.longitude.data, latitude=form.latitude.data) - objective.set_hash() - game.objectives.append(objective) - db.session.commit() - flash(f"Objective has been added!") - return redirect(url_for('main.game_dashboard', game_name=game.name)) - return render_template('objective.html', title=f'Add Objective for {game_name}', - form=form, objective=objective, owner=True) - -@bp.route('/objective//delete', methods=['GET']) -@login_required -def delete_objective(objective_hash): - objective = Objective.query.filter_by(hash=objective_hash).first_or_404() - if not objective.owned_by(current_user): - abort(403) - else: - db.session.delete(objective) - db.session.commit() - return redirect(url_for('main.game_dashboard', game_name=objective.game.name)) - -@bp.route('/objective//qrcode.png') -@login_required -def objective_qrcode(objective_hash): - objective = Objective.query.filter_by(hash=objective_hash).first_or_404() - if not objective.owned_by(current_user): - abort(403) - img = generate_qr_code(url_for('main.objective', objective_hash=objective.hash, _external=True)) - return serve_pil_image(img) - -@bp.route('/objective/', methods=['GET', 'POST']) -@login_required -def objective(objective_hash): - objective = Objective.query.filter_by(hash=objective_hash).first_or_404() - if current_user.role_in_game(objective.game) == Role.bunny: - if not objective.game.is_active(): - flash("Its not find an objective before or after a game, or if the game is not in 'active' mode.") - return redirect(url_for('main.game_dashboard', game_name=objective.game.name)) - player = current_user.player_in(objective.game) - if not objective in player.found_objectives: - player.found_objectives.append(objective) - db.session.commit() - if objective.name: - flash(f'You found objective: {objective.name}!') - else: - flash('You found an objective!') - elif objective.name: - flash(f"You have already found objective '{objective.name}'") - else: - flash('You have already found this objective') - return redirect(url_for('main.game_dashboard', game_name=objective.game.name)) - if not objective.owned_by(current_user): - flash("Only bunnies in an objective's game can find objectives!") - abort(403) - - qrcode = generate_qr_code(objective) - form = ObjectiveForm() - form.old_name = objective.name - if form.submit.data and form.validate(): - objective.name = form.objective_name.data - objective.longitude = form.longitude.data - objective.latitude = form.latitude.data - db.session.commit() - return redirect(url_for('main.game_dashboard', game_name=objective.game.name)) - return render_template('objective.html', title='Objective view', - objective=objective, owner=True, form=form, qrcode=qrcode) @bp.route('/user//send_location', methods=['POST']) @login_required diff --git a/app/main/routes_objective.py b/app/main/routes_objective.py new file mode 100644 index 0000000..850c05e --- /dev/null +++ b/app/main/routes_objective.py @@ -0,0 +1,81 @@ +from flask import render_template, flash, redirect, url_for, abort +from flask_login import current_user, login_required + +from app import db +from app.main import bp +from app.utils import generate_qr_code, serve_pil_image, get_game_if_owner +from app.models import Objective, Role +from app.main.forms import ObjectiveForm + +@bp.route('/game//add_objective', methods=['GET', 'POST']) +@login_required +def add_objective(game_name): + game = get_game_if_owner(game_name) + form = ObjectiveForm() + _objective = Objective(name='', latitude=52.0932, longitude=5.12405) + if form.validate_on_submit(): + _objective = Objective(name=form.objective_name.data, longitude=form.longitude.data, latitude=form.latitude.data) + _objective.set_hash() + game.objectives.append(_objective) + db.session.commit() + flash("Objective has been added!") + return redirect(url_for('main.game_dashboard', game_name=game.name)) + return render_template('objective.html', title=f'Add Objective for {game_name}', + form=form, objective=_objective, owner=True) + +@bp.route('/objective//delete', methods=['GET']) +@login_required +def delete_objective(objective_hash): + _objective = Objective.query.filter_by(hash=objective_hash).first_or_404() + if not _objective.owned_by(current_user): + abort(403) + else: + db.session.delete(_objective) + db.session.commit() + return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) + +@bp.route('/objective//qrcode.png') +@login_required +def objective_qrcode(objective_hash): + _objective = Objective.query.filter_by(hash=objective_hash).first_or_404() + if not _objective.owned_by(current_user): + abort(403) + img = generate_qr_code(url_for('main.objective', objective_hash=_objective.hash, _external=True)) + return serve_pil_image(img) + +@bp.route('/objective/', methods=['GET', 'POST']) +@login_required +def objective(objective_hash): + _objective = Objective.query.filter_by(hash=objective_hash).first_or_404() + if current_user.role_in_game(_objective.game) == Role.bunny: + if not _objective.game.is_active(): + flash("Its not find an objective before or after a game, or if the game is not in 'active' mode.") + return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) + player = current_user.player_in(_objective.game) + if not _objective in player.found_objectives: + player.found_objectives.append(_objective) + db.session.commit() + if _objective.name: + flash(f'You found objective: {_objective.name}!') + else: + flash('You found an objective!') + elif _objective.name: + flash(f"You have already found objective '{_objective.name}'") + else: + flash('You have already found this objective') + return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) + if not _objective.owned_by(current_user): + flash("Only bunnies in an objective's game can find objectives!") + abort(403) + + qrcode = generate_qr_code(_objective) + form = ObjectiveForm() + form.old_name = _objective.name + if form.submit.data and form.validate(): + _objective.name = form.objective_name.data + _objective.longitude = form.longitude.data + _objective.latitude = form.latitude.data + db.session.commit() + return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) + return render_template('objective.html', title='Objective view', + objective=_objective, owner=True, form=form, qrcode=qrcode) diff --git a/app/templates/base.html b/app/templates/base.html index 5452e2d..b26bc76 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -26,7 +26,7 @@