class Tile(db.Model): tile_id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32)) description = db.Column(db.String(255)) background_image = db.Column(db.String(32)) background_colour = db.Column(db.String(7)) css_class = db.Column(db.String(32)) def __repr__(self): return '<Tile %r (%d)>' % ( self.name, self.tile_id )
class Map(db.Model): map_id = db.Column(db.Integer, primary_key=True) x = db.Column(db.Integer, nullable=False) y = db.Column(db.Integer, nullable=False) z = db.Column(db.Integer, nullable=False) world = db.Column(db.Integer, nullable=False) tile_id = db.Column(db.Integer, db.ForeignKey('tile.tile_id'), nullable=False) name = db.Column(db.String(32)) css_class = db.Column(db.String(32)) def __repr__(self): return '<Map %r (%d)>' % ( self.name, self.map_id )
class Character(db.Model): character_id = db.Column(db.Integer, primary_key=True) account_id = db.Column(db.Integer, db.ForeignKey('account.account_id')) name = db.Column(db.String(64), index=True, unique=True) xp = db.Column(db.Integer, nullable=False, default=0) disabled = db.Column(db.String(10)) x = db.Column(db.Integer, nullable=False) y = db.Column(db.Integer, nullable=False) z = db.Column(db.Integer, nullable=False) world = db.Column(db.Integer, nullable=False) def __repr__(self): return '<Character %r (%d)>' % ( self.name, self.character_id )
class Account(db.Model): account_id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), index=True, unique=True, nullable=False) max_characters = db.Column(db.Integer, nullable=False, default=3) characters = db.relationship('Character', backref='account', lazy='dynamic') account_auths = db.relationship('AccountAuth', backref='account', lazy='dynamic') def is_active(self): """True, as all users are active.""" return True def get_id(self): """Return the email address to satisfy Flask-Login's requirements.""" return self.email def is_authenticated(self): """Return True if the user is authenticated.""" return (self.account_id is not None) def is_anonymous(self): """False, as anonymous users aren't supported.""" return False def __repr__(self): return '<Account %r (%d)>' % ( self.email, self.account_id )
class AccountAuth(db.Model): __tablename__ = 'account_auth' account_auth_id = db.Column(db.Integer, primary_key=True) account_id = db.Column(db.Integer, db.ForeignKey('account.account_id')) auth_type = db.Column(db.String(10), nullable=False) def get_class_by_tablename(self, tablename): """Return class reference mapped to table. :param tablename: String with name of table. :return: Class reference or None. """ for c in db.Model._decl_class_registry.values(): if hasattr(c, '__tablename__') and c.__tablename__ == tablename: return c def auth_info(self): """Returns the object from the relevant auth class :return: Object from db or None """ authclass = self.get_class_by_tablename( "account_auth_" + self.auth_type ) if authclass == None: # Non-existant authentication method return None return authclass.query.filter_by( account_auth_id = self.account_auth_id ).one() def __repr__(self): return '<AccountAuth %d (type: %r)>' % ( self.account_auth_id, self.auth_type )
class AccountAuthLocal(db.Model): __tablename__ = 'account_auth_local' id = db.Column(db.Integer, primary_key=True) account_auth_id = db.Column(db.Integer, db.ForeignKey('account_auth.account_auth_id')) # Extra parameters required specifically for this auth method # Encrypted password password = db.Column(db.String(255), nullable=False) def __repr__(self): return '<AccountAuthLocal %d (%r)>' % ( self.id, self.password )