from json import JSONEncoder from sqlalchemy.sql import func from app import db class Location(db.Model): __tablename__ = 'location' id = db.Column(db.Integer, primary_key=True) player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False) longitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None), nullable=False) # maybe check asdecimal and decimal_return_scale later? latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None), nullable=False) timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) def __str__(self): return f'{self.longitude}, {self.latitude}' class LocationEncoder(JSONEncoder): def default(self, location): return { 'player_name' : location.player.name, 'longitude' : location.longitude, 'latitude' : location.latitude, 'timestamp_utc' : str(location.timestamp) }