From 595a0f385e595cc2e31fac0800e061b0ed650416 Mon Sep 17 00:00:00 2001 From: Burathar Date: Sat, 11 Jul 2020 14:27:19 +0200 Subject: [PATCH] work on player page and qrcode --- app/models.py | 15 +++++-- app/routes.py | 21 +++++++--- app/templates/player.html | 84 +++++++++++++++++++++++++++++++++++---- 3 files changed, 104 insertions(+), 16 deletions(-) diff --git a/app/models.py b/app/models.py index 4e8f259..9e6c3b2 100644 --- a/app/models.py +++ b/app/models.py @@ -144,16 +144,23 @@ class Player(UserMixin, db.Model): def check_password(self, password): return check_password_hash(self.password_hash, password) - def last_location(self, game=None): + def locations_game(self, game): # pylint: disable=not-an-iterable if not self.locations: return None if game is None: - return max((location for location in self.locations), key=lambda location: location.timestamp) + return self.locations game_start = game.start_time or datetime.min game_end = game.end_time or datetime.max - return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end), - key=lambda location: location.timestamp) + return (location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end) + + def last_location(self, game=None): + # pylint: disable=not-an-iterable + if not self.locations: + return None + if game is None: + return max(self.locations, key=lambda location: location.timestamp) + return max(self.locations_game(game), key=lambda location: location.timestamp) @staticmethod def delete_orphans(): diff --git a/app/routes.py b/app/routes.py index e0cb324..cbf6f48 100644 --- a/app/routes.py +++ b/app/routes.py @@ -115,11 +115,22 @@ def game_player(game_name, player_name): 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) + 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//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)) + return serve_pil_image(img) 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] @@ -149,14 +160,14 @@ def delete_objective(objective_hash): db.session.commit() return redirect(url_for('game_dashboard', game_name=objective.game.name)) -def generate_objective_qr_code(objective): +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_for('objective', objective_hash=objective.hash, _external=True)) + qr.add_data(url) qr.make(fit=True) return qr.make_image(fill_color='black', back_color='white') @@ -172,7 +183,7 @@ def serve_pil_image(pil_img): 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_objective_qr_code(objective) + img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True)) return serve_pil_image(img) @app.route('/objective/', methods=['GET', 'POST']) @@ -180,7 +191,7 @@ def objective_qrcode(objective_hash): def objective(objective_hash): objective = Objective.query.filter_by(hash = objective_hash).first_or_404() owner = is_objective_owner(objective) - qrcode = generate_objective_qr_code(objective) if owner else None + 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 diff --git a/app/templates/player.html b/app/templates/player.html index c77cdf9..f1a5a6a 100644 --- a/app/templates/player.html +++ b/app/templates/player.html @@ -1,18 +1,88 @@ {% extends "base.html" %} {% import 'bootstrap/wtf.html' as wtf %} +{% block head %} +{{ super() }} + + +{% endblock %} + {% block app_content %}

Player: {{ player.name }}


-
- {{ form.hidden_tag() }} - {% for gameplayer in player.player_games if gameplayer.game == game %} - {{ wtf.form_field(form.role, class='form-control') }} - {% endfor %} - {{ wtf.form_field(form.submit, class='btn btn-primary', value='Update') }} -
+
+
+ {{ form.hidden_tag() }} + {% for gameplayer in player.player_games if gameplayer.game == game %} + {{ wtf.form_field(form.role, class='form-control') }} + {% endfor %} + {{ wtf.form_field(form.submit, class='btn btn-primary', value='Update') }} +
+
+ {% if player.auth_hash %} +
+ qr_code_failed +
+ {% endif %}
+
+{% endblock %} + + +{% block scripts %} +{{ super() }} +{{ moment.include_moment() }} + {% endblock %} \ No newline at end of file