Ejemplo n.º 1
0
class Game(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime, default=dt.now, nullable=False)
    gametype = db.Column(db.Enum(GametypeEnum),
                         default=GametypeEnum.normal,
                         nullable=False)
    is_active = db.Column(db.Boolean(name="is_active"), default=False)

    winner_id = db.Column(db.Integer, db.ForeignKey("player.id"))

    plays = db.relationship("Play", backref="game", lazy=True)
    players = db.relationship(
        "Player",
        secondary=game_players,
        lazy="subquery",
        backref=db.backref("games", lazy=True),
    )

    def __repr__(self) -> str:
        return f"<Game {self.gametype} {self.date}"

    @classmethod
    def get_by_date(cls, date):
        new_date = dt.strptime(date, "%Y-%m-%dT%H:%M:%S")
        return cls.query.filter_by(date=new_date).first_or_404()

    def save(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 2
0
class Word(db.Model):
    __tablename__ = "word"

    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(100), unique=True, nullable=False)
    times_used = db.Column(db.Integer, default=1)

    language_id = db.Column(db.Integer, db.ForeignKey("language.id"))
    plays = db.relationship("Play", backref="word", lazy=True)

    def __repr__(self) -> str:
        return f"<Word {self.word}>"

    @classmethod
    def get_by_language(cls, language, page, per_page):
        return (cls.query.filter_by(language=language).order_by(
            desc(cls.times_used)).paginate(page=page, per_page=per_page))

    @classmethod
    def get_by_word(cls, word):
        return cls.query.filter_by(word=word).first_or_404()

    def update_word_count(self):
        self.times_used += 1

    def save(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 3
0
class Language(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120), unique=True)
    code = db.Column(db.String(2), unique=True)

    words = db.relationship("Word", backref="language", lazy=True)

    def __repr__(self) -> str:
        return f"<Language {self.name}>"

    @classmethod
    def get_by_name(cls, language_name):
        """Queries the database by language name.

        Args:
            language_name (string): Full language name

        Returns:
            Language object: Query result corresponding to selected language.
        """
        return cls.query.filter_by(name=language_name).first_or_404()

    def save(self):
        """Saves current status of language instance."""
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 4
0
class Player(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    avatar = db.Column(db.Integer)

    won_games = db.relationship("Game", backref="winner", lazy=True)
    plays = db.relationship("Play", backref="player", lazy=True)

    def __repr__(self) -> str:
        return f"Player {self.name}"

    @classmethod
    def get_by_name(cls, name):
        return cls.query.filter_by(name=name).first_or_404()

    def save(self):
        db.session.add(self)
        db.session.commit()
Ejemplo n.º 5
0
class TokenBlacklist(db.Model):
    """Blacklist representation"""

    id = db.Column(db.Integer, primary_key=True)
    jti = db.Column(db.String(36), nullable=False, unique=True)
    token_type = db.Column(db.String(10), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey("admin.id"), nullable=False)
    revoked = db.Column(db.Boolean(name="is_revoked"), nullable=False)
    expires = db.Column(db.DateTime, nullable=False)

    admin = db.relationship("Admin", lazy="joined")

    def to_dict(self):
        return {
            "token_id": self.id,
            "jti": self.jti,
            "token_type": self.token_type,
            "admin_identity": self.admin_identity,
            "revoked": self.revoked,
            "expires": self.expires,
        }