class User(UserMixin, SurrogatePK, Model): """A user of the app.""" __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False) email = Column(db.String(80), unique=True, nullable=False) #: The hashed password password = Column(db.Binary(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) active = Column(db.Boolean(), default=False) def __init__(self, username, email, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, 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) @property def full_name(self): """Full user name.""" return '{0} {1}'.format(self.first_name, self.last_name) def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username)
class User(Model): __tablename__ = 'users' id = Column(db.Integer, primary_key=True, doc='id') username = Column(db.String(), unique=True, nullable=False, index=True, doc='user name') email = Column(db.String(), unique=True, nullable=False, index=True, doc='password') password = Column(db.Binary(128), nullable=True) inserted_at = Column(db.DateTime, nullable=False, index=True, server_default=db.func.now(), doc='insert time') updated_at = Column(db.DateTime, nullable=False, index=True, server_default=db.func.now(), onupdate=db.func.now(), doc='update time') token: str = None def __init__(self, username, email, password=None, **kwargs): Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, value): return bcrypt.check_password_hash(self.password, value) def __repr__(self): return f'<User({self.username!r})>'