Browse Source

Add delete player button

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

8
app/models.py

@ -144,7 +144,7 @@ class Player(UserMixin, db.Model):
def check_password(self, password): def check_password(self, password):
return check_password_hash(self.password_hash, password) return check_password_hash(self.password_hash, password)
def last_location(self, game = None): def last_location(self, game=None):
if not self.locations: if not self.locations:
return None return None
if game is None: if game is None:
@ -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), return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end),
key=lambda location: location.timestamp) key=lambda location: location.timestamp)
@staticmethod
def delete_orphans():
Player.query.filter(~Player.player_games.any()).delete()
db.session.commit()
@login.user_loader @login.user_loader
def load_user(id): def load_user(id):
return Player.query.get(int(id)) return Player.query.get(int(id))

14
app/routes.py

@ -117,9 +117,19 @@ def add_player(game_name):
game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data]))
db.session.commit() db.session.commit()
return redirect(url_for('game_dashboard', game_name=game.name)) 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) 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): def generate_objective_qr_code(objective):
qr = qrcode.QRCode( qr = qrcode.QRCode(
version=None, version=None,
@ -163,7 +173,7 @@ def objective(objective_hash):
@app.route('/objective/<objective_hash>/delete', methods=['GET']) @app.route('/objective/<objective_hash>/delete', methods=['GET'])
@login_required @login_required
def objective_delete(objective_hash): def delete_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()
if not is_objective_owner(objective): abort(403) if not is_objective_owner(objective): abort(403)
if is_objective_owner(objective): if is_objective_owner(objective):

8
app/templates/game_dashboard.html

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

2
app/templates/objective.html

@ -34,7 +34,7 @@
{{ wtf.form_field(form.submit, class='btn btn-primary') }} {{ wtf.form_field(form.submit, class='btn btn-primary') }}
</form> </form>
{% if objective.hash %} {% 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> <button class="btn btn-danger">Delete</button></a>
{% endif %} {% endif %}
</div> </div>

Loading…
Cancel
Save