|
|
|
from secrets import token_hex
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from flask_login import UserMixin
|
|
|
|
from sqlalchemy.ext.associationproxy import association_proxy
|
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
|
|
|
|
from app import db, login
|
|
|
|
from app.models import GamePlayer, Role
|
|
|
|
|
|
|
|
class User(UserMixin, db.Model):
|
|
|
|
""" !Always call set_auth_hash() after creating new instance! """
|
|
|
|
__tablename__ = 'user'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(64), unique=True, nullable=False)
|
|
|
|
auth_hash = db.Column(db.String(32), unique=True, nullable=True)
|
|
|
|
password_hash = db.Column(db.String(128))
|
|
|
|
last_login = db.Column(db.DateTime)
|
|
|
|
|
|
|
|
user_games = db.relationship(
|
|
|
|
'GamePlayer',
|
|
|
|
back_populates='user',
|
|
|
|
cascade='save-update, merge, delete, delete-orphan')
|
|
|
|
games = association_proxy('user_games', 'game',
|
|
|
|
creator=lambda game: GamePlayer(game=game))
|
|
|
|
|
|
|
|
|
|
|
|
locations = db.relationship(
|
|
|
|
'Location',
|
|
|
|
lazy='select',
|
|
|
|
backref=db.backref('player', lazy='joined'),
|
|
|
|
cascade="save-update, merge, delete, delete-orphan")
|
|
|
|
|
|
|
|
def set_password(self, password):
|
|
|
|
self.password_hash = generate_password_hash(password)
|
|
|
|
|
|
|
|
def set_auth_hash(self):
|
|
|
|
self.auth_hash = token_hex(16)
|
|
|
|
|
|
|
|
def check_password(self, password):
|
|
|
|
if not password or not self.password_hash:
|
|
|
|
return False
|
|
|
|
return check_password_hash(self.password_hash, password)
|
|
|
|
|
|
|
|
def locations_during_game(self, game):
|
|
|
|
# pylint: disable=not-an-iterable
|
|
|
|
if not self.locations:
|
|
|
|
return None
|
|
|
|
if game is None:
|
|
|
|
return self.locations
|
|
|
|
game_start = game.start_time or datetime.min
|
|
|
|
game_end = game.end_time or datetime.max
|
|
|
|
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_during_game(game), key=lambda location: location.timestamp)
|
|
|
|
|
|
|
|
def role_in_game(self, game):
|
|
|
|
'''Returns the role as Role enum of player in given game. Returns None if player does not participate in game'''
|
|
|
|
gameplayer = self.player_in(game)
|
|
|
|
if not gameplayer:
|
|
|
|
return None
|
|
|
|
return gameplayer.role
|
|
|
|
|
|
|
|
def owns_game_played_by(self, player):
|
|
|
|
'''Self is an owner of a game the player participates in'''
|
|
|
|
return self in [gameplayer.user for gameplayers in
|
|
|
|
[game.players for game in player.games]
|
|
|
|
for gameplayer in gameplayers if gameplayer.role == Role.owner]
|
|
|
|
|
|
|
|
def player_in(self, game):
|
|
|
|
# pylint: disable=not-an-iterable
|
|
|
|
'''Returns GamePlayer object for given game, or None if user does not participate in given game'''
|
|
|
|
gameplayers = [gameplayer for gameplayer in self.user_games if gameplayer.game == game]
|
|
|
|
if not gameplayers:
|
|
|
|
return None
|
|
|
|
return gameplayers[0]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete_orphans():
|
|
|
|
User.query.filter(~User.user_games.any()).delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
@login.user_loader
|
|
|
|
def load_user(id):
|
|
|
|
return User.query.get(int(id))
|