Burathar
4 years ago
20 changed files with 33 additions and 390 deletions
@ -1,19 +0,0 @@ |
|||||||
from flask import render_template |
|
||||||
from app import app, db |
|
||||||
|
|
||||||
@app.errorhandler(403) |
|
||||||
def forbidden_error(error): |
|
||||||
return render_template('403.html'), 403 |
|
||||||
|
|
||||||
@app.errorhandler(404) |
|
||||||
def not_found_error(error): |
|
||||||
return render_template('404.html'), 404 |
|
||||||
|
|
||||||
@app.errorhandler(405) |
|
||||||
def method_not_allowed_error(error): |
|
||||||
return render_template('405.html'), 405 |
|
||||||
|
|
||||||
@app.errorhandler(500) |
|
||||||
def internal_error(error): |
|
||||||
db.session.rollback() |
|
||||||
return render_template('500.html'), 500 |
|
@ -1,72 +0,0 @@ |
|||||||
from flask_wtf import FlaskForm |
|
||||||
from wtforms import StringField, PasswordField, SubmitField, DateTimeField, BooleanField, HiddenField, FloatField, SelectField |
|
||||||
from wtforms.validators import DataRequired, EqualTo, ValidationError, Length, NumberRange |
|
||||||
from pytz import timezone |
|
||||||
from app.models import Player, Objective, Role |
|
||||||
|
|
||||||
class LoginForm(FlaskForm): |
|
||||||
username = StringField('Username', validators=[DataRequired()]) |
|
||||||
password = PasswordField('Password', validators=[DataRequired()]) |
|
||||||
remember_me = BooleanField('Remember Me', default=True) |
|
||||||
submit = SubmitField('Sign In') |
|
||||||
|
|
||||||
class RegistrationForm(FlaskForm): |
|
||||||
username = StringField('Username', validators=[DataRequired(), Length(min=0, max=64)]) |
|
||||||
password = PasswordField('Password', validators=[DataRequired(), Length(min=0, max=128)]) |
|
||||||
password2 = PasswordField( |
|
||||||
'Repeat Password', validators=[DataRequired(), EqualTo('password')]) |
|
||||||
submit = SubmitField('Register') |
|
||||||
|
|
||||||
def validate_username(self, username): |
|
||||||
player = Player.query.filter_by(name=username.data).first() |
|
||||||
if player is not None: |
|
||||||
raise ValidationError('Please use a different username.') |
|
||||||
|
|
||||||
class CreateGameForm(FlaskForm): |
|
||||||
game_name = StringField('Game Name', validators=[DataRequired(), Length(min=0, max=64)]) |
|
||||||
start_time_disabled = BooleanField('No start time') |
|
||||||
start_time = DateTimeField(id='datetimepicker_start', format="%d-%m-%Y %H:%M") |
|
||||||
end_time_disabled = BooleanField('No end time') |
|
||||||
end_time = DateTimeField(id='datetimepicker_end', format="%d-%m-%Y %H:%M") |
|
||||||
timezone = HiddenField(validators=[DataRequired()]) |
|
||||||
submit = SubmitField('Create') |
|
||||||
|
|
||||||
def validate_start_time(self, start_time): |
|
||||||
self.date_time_validator(self.start_time_disabled, start_time) |
|
||||||
|
|
||||||
def validate_end_time(self, end_time): |
|
||||||
self.date_time_validator(self.end_time_disabled, end_time) |
|
||||||
|
|
||||||
def date_time_validator(self, disabled, date_time): |
|
||||||
if disabled.data: |
|
||||||
date_time.data = None |
|
||||||
return |
|
||||||
clientzone = timezone(self.timezone.data) |
|
||||||
date_time_utc = clientzone.localize(date_time.data).astimezone(timezone('UTC')) |
|
||||||
date_time.data = date_time_utc |
|
||||||
|
|
||||||
class ObjectiveForm(FlaskForm): |
|
||||||
objective_name = StringField('Objective Name', validators=[Length(min=0, max=64)]) |
|
||||||
latitude = FloatField('Latitude', validators=[DataRequired(), NumberRange(min=-90, max=90)]) |
|
||||||
longitude = FloatField('Longitude', validators=[DataRequired(), NumberRange(min=-180, max=180)]) |
|
||||||
submit = SubmitField('Save') |
|
||||||
|
|
||||||
def validate_objective_name(self, objective_name): |
|
||||||
if objective_name.data == '': return |
|
||||||
objective = Objective.query.filter_by(name=objective_name.data).first() |
|
||||||
if objective is not None: |
|
||||||
raise ValidationError('Please use a different name.') |
|
||||||
|
|
||||||
class PlayerUpdateForm(FlaskForm): |
|
||||||
role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[DataRequired()]) |
|
||||||
submit = SubmitField('Update') |
|
||||||
|
|
||||||
class PlayerAddForm(FlaskForm): |
|
||||||
name = StringField('Player Name', validators=[DataRequired(), Length(min=0, max=64)]) |
|
||||||
role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[DataRequired()]) |
|
||||||
submit_add = SubmitField('Create') |
|
||||||
|
|
||||||
class PlayerCreateForm(FlaskForm): |
|
||||||
name = StringField('Player Name', validators=[DataRequired(), Length(min=0, max=64)]) |
|
||||||
role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[DataRequired()]) |
|
||||||
submit_create = SubmitField('Create') |
|
@ -1,216 +0,0 @@ |
|||||||
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 |
|
||||||
|
|
||||||
@app.route('/') |
|
||||||
@app.route('/index') |
|
||||||
@login_required |
|
||||||
def index(): |
|
||||||
return render_template("index.html", title='Home') |
|
||||||
|
|
||||||
@app.route('/login', methods=['GET', 'POST']) |
|
||||||
def login(): |
|
||||||
if current_user.is_authenticated: |
|
||||||
return redirect(url_for('index')) |
|
||||||
form = LoginForm() |
|
||||||
if form.validate_on_submit(): |
|
||||||
player = Player.query.filter_by(name=form.username.data).first() |
|
||||||
if player is None or not player.check_password(form.password.data): |
|
||||||
flash('Invalid username or password') |
|
||||||
return redirect(url_for('login')) |
|
||||||
login_user(player, remember=form.remember_me.data) |
|
||||||
return redirect(url_for('index')) |
|
||||||
return render_template('login.html', title='Sign In', form=form) |
|
||||||
|
|
||||||
@app.route('/logout') |
|
||||||
@login_required |
|
||||||
def logout(): |
|
||||||
logout_user() |
|
||||||
return redirect(url_for('index')) |
|
||||||
|
|
||||||
@app.route('/register', methods=['GET', 'POST']) |
|
||||||
def register(): |
|
||||||
if current_user.is_authenticated: |
|
||||||
return redirect(url_for('index')) |
|
||||||
form = RegistrationForm() |
|
||||||
if form.validate_on_submit(): |
|
||||||
player = Player(name=form.username.data) |
|
||||||
player.set_password(form.password.data) |
|
||||||
player.set_auth_hash() |
|
||||||
db.session.add(player) |
|
||||||
db.session.commit() |
|
||||||
flash('Congratulations, you are now a registered user!') |
|
||||||
return redirect(url_for('login')) |
|
||||||
return render_template('register.html', title='Register', form=form) |
|
||||||
|
|
||||||
@app.route('/create_game', methods=['GET', 'POST']) |
|
||||||
@login_required |
|
||||||
def create_game(): |
|
||||||
print(current_user.is_authenticated) |
|
||||||
form = CreateGameForm() |
|
||||||
if form.validate_on_submit(): |
|
||||||
game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) |
|
||||||
game.game_players.append(GamePlayer(player=current_user, role=Role['owner'])) #check if this works, otherwise use 'owner' |
|
||||||
db.session.add(game) |
|
||||||
db.session.commit() |
|
||||||
flash(f"'{game.name}' had been created!") |
|
||||||
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
||||||
return render_template('create_game.html', title='Create Game', form=form) |
|
||||||
|
|
||||||
@app.route('/game/<game_name>/dashboard') |
|
||||||
@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) |
|
||||||
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) |
|
||||||
form_add = PlayerAddForm() |
|
||||||
form_create = PlayerCreateForm() |
|
||||||
|
|
||||||
if form_add.submit_add.data and form_add.validate_on_submit(): |
|
||||||
player = Player.query.filter_by(form_add.name.data).first_or_404() |
|
||||||
game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) |
|
||||||
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
||||||
|
|
||||||
if form_create.submit_create.data and form_create.validate_on_submit(): |
|
||||||
player = Player(name=form_create.name.data) |
|
||||||
player.set_auth_hash() |
|
||||||
game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) |
|
||||||
db.session.commit() |
|
||||||
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
||||||
|
|
||||||
return render_template('add_player.html', title=f'Add Player for {game_name}', form_add=form_add, form_create=form_create, game=game) |
|
||||||
|
|
||||||
@app.route('/game/<game_name>/removeplayer/<player_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) |
|
||||||
player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404() |
|
||||||
game.players.remove(player) |
|
||||||
db.session.commit() |
|
||||||
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
||||||
|
|
||||||
@app.route('/game/<game_name>/player/<player_name>', methods=['GET', 'POST']) |
|
||||||
@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) |
|
||||||
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) |
|
||||||
if form.validate_on_submit(): |
|
||||||
gameplayer.role = Role[form.role.data] |
|
||||||
db.session.commit() |
|
||||||
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
||||||
return render_template('player.html', title=f'{player.name} in {game_name}', game=game, player=player, form=form, json=json, location_encoder=LocationEncoder) |
|
||||||
|
|
||||||
@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('player', auth_hash=auth_hash, _external=True)) |
|
||||||
return serve_pil_image(img) |
|
||||||
|
|
||||||
@app.route('/player/<auth_hash>') |
|
||||||
@login_required |
|
||||||
def player(auth_hash): |
|
||||||
player = Player.query.filter_by(auth_hash=auth_hash).first_or_404() |
|
||||||
return render_template('player.html',title=f'Player: {player.name}', player=player) |
|
||||||
|
|
||||||
def is_game_owner(game): |
|
||||||
return current_user in [gameplayer.player for gameplayer in game.game_players if gameplayer.role == Role.owner] |
|
||||||
|
|
||||||
def is_player_game_owner(player): |
|
||||||
return current_user in [gameplayer.player for gameplayer in [game for game in player.games].game_players if game_player.role == Role.owner] |
|
||||||
|
|
||||||
def is_objective_owner(objective): |
|
||||||
return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner] |
|
||||||
|
|
||||||
@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) |
|
||||||
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('game_dashboard', game_name=game.name)) |
|
||||||
return render_template('objective.html', title=f'Add Objective for {game_name}', form=form, objective=objective, owner=True) |
|
||||||
|
|
||||||
@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) |
|
||||||
if is_objective_owner(objective): |
|
||||||
db.session.delete(objective) |
|
||||||
db.session.commit() |
|
||||||
return redirect(url_for('game_dashboard', game_name=objective.game.name)) |
|
||||||
|
|
||||||
def generate_qr_code(url): |
|
||||||
qr = qrcode.QRCode( |
|
||||||
version=None, |
|
||||||
error_correction=qrcode.constants.ERROR_CORRECT_M, |
|
||||||
box_size=30, |
|
||||||
border=4, |
|
||||||
) |
|
||||||
qr.add_data(url) |
|
||||||
qr.make(fit=True) |
|
||||||
return qr.make_image(fill_color='black', back_color='white') |
|
||||||
|
|
||||||
# Source: https://stackoverflow.com/questions/7877282/how-to-send-image-generated-by-pil-to-browser |
|
||||||
def serve_pil_image(pil_img): |
|
||||||
img_io = BytesIO() |
|
||||||
pil_img.save(img_io, 'PNG', quality=70) |
|
||||||
img_io.seek(0) |
|
||||||
return send_file(img_io, mimetype='image/png') |
|
||||||
|
|
||||||
@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) |
|
||||||
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() |
|
||||||
owner = is_objective_owner(objective) |
|
||||||
qrcode = generate_qr_code(objective) if owner else None |
|
||||||
form = ObjectiveForm() |
|
||||||
if form.submit.data and form.validate() and owner: |
|
||||||
objective.name = form.objective_name.data |
|
||||||
objective.longitude = form.longitude.data |
|
||||||
objective.latitude = form.latitude.data |
|
||||||
db.session.commit() |
|
||||||
return redirect(url_for('game_dashboard', game_name=objective.game.name)) |
|
||||||
return render_template('objective.html', title='Objective view', objective=objective, owner=owner, form=form, qrcode=qrcode) |
|
@ -1,13 +0,0 @@ |
|||||||
{% extends 'base.html' %} |
|
||||||
{% import 'bootstrap/wtf.html' as wtf %} |
|
||||||
|
|
||||||
{% block app_content %} |
|
||||||
<h1>Sign In</h1> |
|
||||||
<div class="row"> |
|
||||||
<div class="col-md-4"> |
|
||||||
{{ wtf.quick_form(form, button_map={'submit': 'primary'}) }} |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<br> |
|
||||||
<p>New User? <a href="{{ url_for('register') }}">Click to Register!</a></p> |
|
||||||
{% endblock %} |
|
@ -1,13 +0,0 @@ |
|||||||
{% extends "base.html" %} |
|
||||||
{% import 'bootstrap/wtf.html' as wtf %} |
|
||||||
|
|
||||||
{% block app_content %} |
|
||||||
<h1>Register</h1> |
|
||||||
<div class="row"> |
|
||||||
<div class="col-md-4"> |
|
||||||
{{ wtf.quick_form(form, button_map={'submit': 'primary'}) }} |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<br> |
|
||||||
<p>Already have an account? <a href="{{ url_for('login') }}">Click to Sign In!</a></p> |
|
||||||
{% endblock %} |
|
@ -1,34 +0,0 @@ |
|||||||
import unittest |
|
||||||
from app import create_app, db |
|
||||||
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder |
|
||||||
from config import Config |
|
||||||
|
|
||||||
class TestConfig(Config): |
|
||||||
TESTING = True |
|
||||||
SQLALCHEMY_DATABASE_URI = 'sqlite://' |
|
||||||
|
|
||||||
class RoutesCase(unittest.TestCase): |
|
||||||
def setUp(self): |
|
||||||
self.app = create_app(TestConfig) |
|
||||||
self.app_context = self.app.app_context() |
|
||||||
self.app_context.push() |
|
||||||
db.create_all() |
|
||||||
|
|
||||||
def tearDown(self): |
|
||||||
db.session.remove() |
|
||||||
db.drop_all() |
|
||||||
self.app_context.pop() |
|
||||||
|
|
||||||
def test_is_game_owner(self): |
|
||||||
g = Game(name='TestGame') |
|
||||||
p1 = Player(name='Henk') |
|
||||||
p2 = Player(name='Alfred') |
|
||||||
g.players.append(p1) |
|
||||||
g.players.append(p2) |
|
||||||
db.session.append(g) |
|
||||||
db.session.commit() |
|
||||||
self.assertEqual(True, True) |
|
||||||
self.assertFalse('Foo'.isupper()) |
|
||||||
|
|
||||||
if __name__ == '__main__': |
|
||||||
unittest.main(verbosity=2) |
|
Loading…
Reference in new issue