class Tag(BaseModel): __tablename__ = "tags" name = db.Column(db.String(32), unique=True, nullable=False) tag_type = db.Column(ChoiceType(TagType, impl=db.Integer()), nullable=False) games_in_genre = db.relationship('Game', secondary="assoc_game_genres", back_populates="genres") games_in_tag = db.relationship('Game', secondary="assoc_game_tags", back_populates="tags") def __init__(self, name, tag_type="Generic"): self.name = name self.tag_type = tag_type @property def is_genre(self): return self.tag_type is TagType.GENRE @property def is_generic(self): return self.tag_type is TagType.GENERIC
class Company(BaseModel): __tablename__ = "companies" name = db.Column(db.String(32), unique=True, nullable=False) founding_date = db.Column(db.Date) developed_games = db.relationship('Game', secondary="assoc_game_developers", back_populates="developers") published_games = db.relationship('Game', secondary="assoc_game_publishers", back_populates="publishers") def __init__(self, name, founding_date=None): self.name = name self.founding_date = founding_date
class GameInfo(db.Model): __tablename__ = "game_info" game_id = db.Column(db.Integer, db.ForeignKey('games.id'), primary_key=True) release_date = db.Column(db.Date) description = db.Column(db.Text) game = db.relationship('Game', back_populates="info") def __init__(self, release_date=None, description=None): self.release_date = release_date self.description = description
class Game(BaseModel): __tablename__ = "games" name = db.Column(db.String(32), unique=True, nullable=False) developers = db.relationship('Company', secondary=game_developer, back_populates="developed_games") publishers = db.relationship('Company', secondary=game_publisher, back_populates="published_games") genres = db.relationship('Tag', secondary=game_genres, back_populates="games_in_genre") tags = db.relationship('Tag', secondary=game_tags, back_populates="games_in_tag") info = db.relationship('GameInfo', uselist=False, back_populates="game") def __init__(self, name): self.name = name
class User(BaseModel): __tablename__ = "users" email = db.Column(EmailType(length=255), unique=True) username = db.Column(db.String(32), unique=True, nullable=False) password_hash = db.Column(PasswordType(max_length=255, schemes=['argon2']), nullable=False) status = db.Column(ChoiceType(UserStatus, impl=db.Integer()), nullable=False) level = db.Column(ChoiceType(UserLevel, impl=db.Integer()), nullable=False) created_time = db.Column(db.DateTime(timezone=False), default=datetime.utcnow, nullable=False) last_login_date = db.Column(db.DateTime(timezone=False), default=None) def __init__(self, username, password, email=None): self.username = username self.password_hash = password self.email = email self.status = UserStatus.ACTIVE # if I add email verification later, this will change. self.level = UserLevel.STANDARD @classmethod def by_username(cls, username): user = cls.query.filter_by(username=username).first() return user @classmethod def by_id(cls, uid): user = cls.query.filter_by(id=uid).first() return user @property def is_active(self): return self.status == UserStatus.ACTIVE @property def is_mod(self): return self.level >= UserLevel.MOD @property def is_admin(self): return self.level >= UserLevel.ADMIN
from pf import db from pf.models import BaseModel game_developer = db.Table("assoc_game_developers", db.Column("game_id", db.Integer, db.ForeignKey("games.id")), db.Column("developer_id", db.Integer, db.ForeignKey("companies.id"))) game_publisher = db.Table("assoc_game_publishers", db.Column("game_id", db.Integer, db.ForeignKey("games.id")), db.Column("publisher_id", db.Integer, db.ForeignKey("companies.id"))) game_genres = db.Table("assoc_game_genres", db.Column("game_id", db.Integer, db.ForeignKey("games.id")), db.Column("genre_id", db.Integer, db.ForeignKey("tags.id"))) game_tags = db.Table("assoc_game_tags", db.Column("game_id", db.Integer, db.ForeignKey("games.id")), db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"))) class Game(BaseModel): __tablename__ = "games" name = db.Column(db.String(32), unique=True, nullable=False) developers = db.relationship('Company', secondary=game_developer, back_populates="developed_games") publishers = db.relationship('Company', secondary=game_publisher, back_populates="published_games") genres = db.relationship('Tag', secondary=game_genres, back_populates="games_in_genre") tags = db.relationship('Tag', secondary=game_tags, back_populates="games_in_tag") info = db.relationship('GameInfo', uselist=False, back_populates="game") def __init__(self, name): self.name = name class GameInfo(db.Model): __tablename__ = "game_info"