Browse Source

fix some code style errors

feature_tests
Burathar 4 years ago
parent
commit
024e053c77
  1. 44
      app/routes.py

44
app/routes.py

@ -1,12 +1,12 @@ @@ -1,12 +1,12 @@
import json
import qrcode
from flask import render_template, flash, redirect, url_for, request, abort, send_file
from flask_login import login_user, logout_user, current_user, login_required
from sqlalchemy import and_
from io import BytesIO
from app import app, db
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder
from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm, PlayerUpdateForm
import json
import qrcode
from io import BytesIO
@app.route('/')
@app.route('/index')
@ -67,15 +67,17 @@ def create_game(): @@ -67,15 +67,17 @@ def create_game():
@login_required
def game_dashboard(game_name):
#game = Game.query.filter(Game.game_players.any(and_(GamePlayer.player.has(Player.name == current_user.name), GamePlayer.role == 'owner'))).first_or_404()
game = Game.query.filter_by(name = game_name).first_or_404()
if not is_game_owner(game): abort(403)
game = Game.query.filter_by(name=game_name).first_or_404()
if not is_game_owner(game):
abort(403)
return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, objective_encoder=ObjectiveMinimalEncoder, location_encoder=LocationEncoder)
@app.route('/game/<game_name>/addplayer', methods=['GET', 'POST'])
@login_required
def add_player(game_name):
game = Game.query.filter_by(name=game_name).first_or_404()
if not is_game_owner(game): abort(403)
if not is_game_owner(game):
abort(403)
form_add = PlayerAddForm()
form_create = PlayerCreateForm()
@ -97,7 +99,8 @@ def add_player(game_name): @@ -97,7 +99,8 @@ def add_player(game_name):
@login_required
def remove_player(game_name, player_name):
game = Game.query.filter_by(name=game_name).first_or_404()
if not is_game_owner(game): abort(403)
if not is_game_owner(game):
abort(403)
player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404()
game.players.remove(player)
db.session.commit()
@ -107,7 +110,8 @@ def remove_player(game_name, player_name): @@ -107,7 +110,8 @@ def remove_player(game_name, player_name):
@login_required
def game_player(game_name, player_name):
game = Game.query.filter_by(name=game_name).first_or_404()
if not is_game_owner(game): abort(403)
if not is_game_owner(game):
abort(403)
player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404()
gameplayer = [gameplayer for gameplayer in player.player_games if gameplayer.game == game][0]
form = PlayerUpdateForm(role=gameplayer.role.name)
@ -120,9 +124,10 @@ def game_player(game_name, player_name): @@ -120,9 +124,10 @@ def game_player(game_name, player_name):
@app.route('/player/<auth_hash>/qrcode.png')
@login_required
def player_qrcode(auth_hash):
player = Player.query.filter_by(auth_hash = auth_hash).first_or_404()
if not is_player_game_owner(player): abort(403)
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True))
player = Player.query.filter_by(auth_hash=auth_hash).first_or_404()
if not is_player_game_owner(player):
abort(403)
img = generate_qr_code(url_for('player', auth_hash=auth_hash, _external=True))
return serve_pil_image(img)
def is_game_owner(game):
@ -137,8 +142,9 @@ def is_objective_owner(objective): @@ -137,8 +142,9 @@ def is_objective_owner(objective):
@app.route('/game/<game_name>/add_objective', methods=['GET', 'POST'])
@login_required
def add_objective(game_name):
game = Game.query.filter_by(name = game_name).first_or_404()
if not is_game_owner(game): abort(403)
game = Game.query.filter_by(name=game_name).first_or_404()
if not is_game_owner(game):
abort(403)
form = ObjectiveForm()
objective = Objective(name='', latitude=52.0932, longitude=5.12405)
if form.validate_on_submit():
@ -153,8 +159,9 @@ def add_objective(game_name): @@ -153,8 +159,9 @@ def add_objective(game_name):
@app.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 is_objective_owner(objective): abort(403)
objective = Objective.query.filter_by(hash=objective_hash).first_or_404()
if not is_objective_owner(objective):
abort(403)
if is_objective_owner(objective):
db.session.delete(objective)
db.session.commit()
@ -181,15 +188,16 @@ def serve_pil_image(pil_img): @@ -181,15 +188,16 @@ def serve_pil_image(pil_img):
@app.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 is_objective_owner(objective): abort(403)
objective = Objective.query.filter_by(hash=objective_hash).first_or_404()
if not is_objective_owner(objective):
abort(403)
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True))
return serve_pil_image(img)
@app.route('/objective/<objective_hash>', methods=['GET', 'POST'])
@login_required
def objective(objective_hash):
objective = Objective.query.filter_by(hash = objective_hash).first_or_404()
objective = Objective.query.filter_by(hash=objective_hash).first_or_404()
owner = is_objective_owner(objective)
qrcode = generate_qr_code(objective) if owner else None
form = ObjectiveForm()

Loading…
Cancel
Save