Browse Source

Add delete player button

feature_tests
Burathar 5 years ago
parent
commit
98bc78e9a6
  1. 6
      app/models.py
  2. 12
      app/routes.py
  3. 8
      app/templates/game_dashboard.html
  4. 2
      app/templates/objective.html

6
app/models.py

@ -154,6 +154,12 @@ class Player(UserMixin, db.Model): @@ -154,6 +154,12 @@ class Player(UserMixin, db.Model):
return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end),
key=lambda location: location.timestamp)
@staticmethod
def delete_orphans():
Player.query.filter(~Player.player_games.any()).delete()
db.session.commit()
@login.user_loader
def load_user(id):
return Player.query.get(int(id))

12
app/routes.py

@ -120,6 +120,16 @@ def add_player(game_name): @@ -120,6 +120,16 @@ def add_player(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))
def generate_objective_qr_code(objective):
qr = qrcode.QRCode(
version=None,
@ -163,7 +173,7 @@ def objective(objective_hash): @@ -163,7 +173,7 @@ def objective(objective_hash):
@app.route('/objective/<objective_hash>/delete', methods=['GET'])
@login_required
def objective_delete(objective_hash):
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):

8
app/templates/game_dashboard.html

@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
<th scope="col">Bunnies Caught</th>
<th scope="col">Been Caught</th>
<th scope="col">Last location</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@ -36,7 +37,10 @@ @@ -36,7 +37,10 @@
<td>{% with location = player.last_location(game) %}
{% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %}
{{ location }}
<td>{% endwith %}
{% endwith %}</td>
<td><a href="{{ url_for('remove_player', game_name=game.name, player_name=player.name) }}">
<button class="btn btn-danger">Delete</button></a>
</td>
</tr>
{% endfor %}
</tbody>
@ -65,7 +69,7 @@ @@ -65,7 +69,7 @@
<td>{{ objective.longitude }}</td>
<td>{{ objective.found_by|length }}</td>
<td><a href="{{ url_for('objective', objective_hash = objective.hash) }}">{{ objective.hash }}</a></td>
<td><a href="{{ url_for('objective_delete', objective_hash = objective.hash) }}">
<td><a href="{{ url_for('delete_objective', objective_hash = objective.hash) }}">
<button class="btn btn-danger">Delete</button></a>
</td>
</tr>

2
app/templates/objective.html

@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
{{ wtf.form_field(form.submit, class='btn btn-primary') }}
</form>
{% if objective.hash %}
<a href="{{ url_for('objective_delete', objective_hash = objective.hash) }}">
<a href="{{ url_for('delete_objective', objective_hash = objective.hash) }}">
<button class="btn btn-danger">Delete</button></a>
{% endif %}
</div>

Loading…
Cancel
Save