Browse Source

move objective routes to separate file

testing
Burathar 4 years ago
parent
commit
482bc7aeea
  1. 2
      app/game_settings/__init__.py
  2. 2
      app/main/__init__.py
  3. 81
      app/main/routes.py
  4. 81
      app/main/routes_objective.py
  5. 2
      app/templates/base.html
  6. 2
      app/templates/game_owner_dashboard.html
  7. 10
      app/templates/game_settings/edit_game.html

2
app/game_settings/__init__.py

@ -2,4 +2,4 @@ from flask import Blueprint
bp = Blueprint('game', __name__) bp = Blueprint('game', __name__)
from app.game_settings import handlers from app.game_settings import routes

2
app/main/__init__.py

@ -2,4 +2,4 @@ from flask import Blueprint
bp = Blueprint('main', __name__) bp = Blueprint('main', __name__)
from app.main import routes from app.main import routes, routes_objective

81
app/main/routes.py

@ -12,11 +12,10 @@ from sqlalchemy import and_
from app import db from app import db
from app.main import bp from app.main import bp
from app.utils import generate_qr_code, serve_pil_image, get_game_if_owner from app.utils import get_game_if_owner
from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder,\ from app.models import User, Game, Role, GamePlayer, ObjectiveMinimalEncoder,\
LocationEncoder, PlayerCaughtPlayer, Review, Location LocationEncoder, PlayerCaughtPlayer, Review, Location
from app.main.forms import ObjectiveForm, PlayerAddForm, UserCreateForm, \ from app.main.forms import PlayerAddForm, UserCreateForm, PlayerUpdateForm, CatchBunnyForm
PlayerUpdateForm, CatchBunnyForm
@bp.before_app_request @bp.before_app_request
def before_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 redirect(url_for('main.game_dashboard', game_name=current_user.games[0].name))
return render_template("index.html", title='Home') return render_template("index.html", title='Home')
@bp.route('/game/<game_name>/dashboard') @bp.route('/game/<game_name>/dashboard')
@login_required @login_required
def game_dashboard(game_name): 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 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) 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/<game_name>/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/<objective_hash>/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/<objective_hash>/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/<objective_hash>', 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/<username>/send_location', methods=['POST']) @bp.route('/user/<username>/send_location', methods=['POST'])
@login_required @login_required

81
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/<game_name>/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/<objective_hash>/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/<objective_hash>/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/<objective_hash>', 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)

2
app/templates/base.html

@ -26,7 +26,7 @@
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="{{ url_for('main.index') }}">Home</a></li> <li><a href="{{ url_for('main.index') }}">Home</a></li>
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
<li><a href="{{ url_for('main.create_game') }}">Create Game</a></li> <li><a href="{{ url_for('game.create_game') }}">Create Game</a></li>
{% endif %} {% endif %}
</ul> </ul>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">

2
app/templates/game_owner_dashboard.html

@ -9,7 +9,7 @@
{% block app_content %} {% block app_content %}
<h1>{{ game.name }} Dashboard</h1> <h1>{{ game.name }} Dashboard</h1>
<a href="{{ url_for('main.change_game_settings', game_name=game.name) }}"> <a href="{{ url_for('game.change_game_settings', game_name=game.name) }}">
<button class="btn btn-primary">Change Game Settings</button> <button class="btn btn-primary">Change Game Settings</button>
</a> </a>
{% if game.unreviewed_bunny_photos() %} {% if game.unreviewed_bunny_photos() %}

10
app/templates/game_settings/edit_game.html

@ -40,20 +40,20 @@
</form> </form>
<hr> <hr>
{% if game.hidden %} {% if game.hidden %}
<a href="{{ url_for('main.publish_game', game_name=game.name) }}"> <a href="{{ url_for('game.publish_game', game_name=game.name) }}">
<button class="btn btn-success">Publish Game</button> <button class="btn btn-success">Publish Game</button>
</a> </a>
{% else %} {% else %}
<a href="{{ url_for('main.hide_game', game_name=game.name) }}"> <a href="{{ url_for('game.hide_game', game_name=game.name) }}">
<button class="btn btn-success">Hide Game</button> <button class="btn btn-success">Hide Game</button>
</a> </a>
{% endif %} {% endif %}
{% if game.paused %} {% if game.paused %}
<a href="{{ url_for('main.resume_game', game_name=game.name) }}"> <a href="{{ url_for('game.resume_game', game_name=game.name) }}">
<button class="btn btn-primary">Resume Game</button> <button class="btn btn-primary">Resume Game</button>
</a> </a>
{% else %} {% else %}
<a href="{{ url_for('main.pause_game', game_name=game.name) }}"> <a href="{{ url_for('game.pause_game', game_name=game.name) }}">
<button class="btn btn-primary">Pause Game</button> <button class="btn btn-primary">Pause Game</button>
</a> </a>
{% endif %} {% endif %}
@ -89,7 +89,7 @@
// Delete Game button // Delete Game button
function deleteGame() { function deleteGame() {
if (confirm("Are you sure you want to delete this game?")) { if (confirm("Are you sure you want to delete this game?")) {
window.location.href = "{{ url_for('main.delete_game', game_name=game.name) }}" window.location.href = "{{ url_for('game.delete_game', game_name=game.name) }}"
} }
} }
</script> </script>

Loading…
Cancel
Save