Beispiel #1
0
class User(UserMixin, SurrogatePK, Model):
    """A user of the app."""

    __tablename__ = 'users'
    email = Column(db.String(80), unique=True, nullable=False)
    #: The hashed password
    password = Column(db.Binary(128), nullable=True)
    created_at = Column(ArrowType, nullable=False, default=arrow.utcnow)
    display_name = Column(db.String(80), nullable=True)
    full_name = Column(db.String(100), nullable=True)
    active = Column(db.Boolean(), default=False)
    is_admin = Column(db.Boolean(), default=False)

    def __init__(self, email, password=None, **kwargs):
        """Create instance."""
        db.Model.__init__(self, email=email, **kwargs)
        if password:
            self.set_password(password)
        else:
            self.password = None

    def set_password(self, password):
        """Set password."""
        self.password = bcrypt.generate_password_hash(password)

    def check_password(self, value):
        """Check password."""
        return bcrypt.check_password_hash(self.password, value)

    def __repr__(self):
        """Represent instance as a unique string."""
        return f'<User({self.email!r})>'

    def __eq__(self, other):
        return other and self.id == other.id
Beispiel #2
0
class Crossword(SurrogatePK, Model):
    """Crossword challenge."""

    __tablename__ = 'crosswords'
    name = Column(db.String(80), nullable=False)
    score = Column(db.Float(), nullable=False, default=0)
    description = deferred(Column(db.Text(), nullable=True))
    """^ Markdown field"""
    body = deferred(Column(db.Text(), nullable=False))
    solution = deferred(Column(db.Text(), nullable=False))

    def __str__(self):
        return f'{super().__str__()}: {self.name}'
Beispiel #3
0
class ContestStage(SurrogatePK, Model):
    """Crossword challenge."""

    __tablename__ = 'contests_stages'
    name = Column(db.String(150), nullable=False)
    finish = Column(ArrowType(), nullable=True, default=None)

    contest_id = reference_col('contests', nullable=False)
    contest = relationship('Contest', backref='stages')

    __table_args__ = (
        UniqueConstraint('contest_id', 'name'),
    )
Beispiel #4
0
class Contest(SurrogatePK, Model):
    """Crossword challenge."""

    __tablename__ = 'contests'
    name = Column(db.String(150), nullable=False)
    slug = Column(db.String(20), nullable=False, unique=True)
    description = Column(db.Text(), nullable=True)
    """^ Markdown field"""
    start = Column(ArrowType(), nullable=True, default=None)
    end = Column(ArrowType(), nullable=True, default=None)

    rules = Column(db.Text(), nullable=True)
    """^ Markdown field"""

    @property
    def active(self):
        if not self.start:
            return False

        now = arrow.utcnow()
        return self.start <= now and (not self.end or now < self.end)

    def __str__(self):
        return f'{super().__str__()}: {self.slug}'
Beispiel #5
0
class Role(SurrogatePK, Model):
    """A role for a user."""

    __tablename__ = 'roles'
    name = Column(db.String(80), unique=True, nullable=False)
    user_id = reference_col('users', nullable=True)
    user = relationship('User', backref='roles')

    def __init__(self, name, **kwargs):
        """Create instance."""
        db.Model.__init__(self, name=name, **kwargs)

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Role({name})>'.format(name=self.name)