From 6582fd8466aa4924941bb5a0795ef2fcf8289597 Mon Sep 17 00:00:00 2001 From: Burathar Date: Fri, 10 Jul 2020 13:02:59 +0200 Subject: [PATCH] Add last_location method to Player --- app/models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/models.py b/app/models.py index dd539b7..031d5f9 100644 --- a/app/models.py +++ b/app/models.py @@ -7,6 +7,8 @@ from secrets import token_hex from flask_login import UserMixin import json from json import JSONEncoder +from datetime import datetime +import pytz class Role(Enum): @@ -137,6 +139,15 @@ class Player(UserMixin, db.Model): def check_password(self, password): return check_password_hash(self.password_hash, password) + def last_location(self, game = None): + if not self.locations: + return None + if game is None: + return max(location.timestamp for location in self.locations) + game_start = game.start_time or datetime.min + game_end = game.end_time or datetime.max + return max(location.timestamp for location in self.locations if location.timestamp > game_start and location.timestamp < game_end) + @login.user_loader def load_user(id): return Player.query.get(int(id))