class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(12), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) first_name = db.Column(db.String(120)) last_name = db.Column(db.String(120)) password_hash = db.Column(db.String(128)) sent_messages = db.relationship('Message', back_populates='sender', lazy='dynamic', foreign_keys='Message.sender_id') received_messages = db.relationship('Message', back_populates='recipient', lazy='dynamic', foreign_keys='Message.recipient_id') def __repr__(self): return '<User {}>'.format(self.email) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String(128)) is_customer = db.Column(db.Boolean, index=True) email = db.Column(db.String(32), index=True, nullable=True) ref_id = db.Column(db.Integer) questions = db.relationship('Question', backref='author', lazy='dynamic') answers = db.relationship('Answer', backref='author', lazy='dynamic') pets = db.relationship('Pet', backref='owner', lazy='dynamic') # appointments = db.relationship('Appointment', backref='maker', lazy='dynamic') #Appointment表里有两个User的外键,所以这里不能反向查询 def get_jwt_token(self, expires_in=600): """获取JWT令牌""" return jwt.encode({'reset_password': self.id, 'exp': time() + expires_in},\ app.config['SECRET_KEY'], algorithm='HS256').decode('utf8') @staticmethod def verify_jwt_token(token): try: user_id = jwt.decode(token, app.config['SECRET_KEY'], algorithms='HS256')['reset_password'] except Exception as e: print(e) return return User.query.get(user_id) def set_password(self, raw_password): """密码加密""" self.password_hash = generate_password_hash(raw_password)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') profile = db.relationship('Profile', backref='user', lazy='dynamic') def __repr__(self): return '<User {}>'.format(self.username)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String(128)) is_customer = db.Column(db.Boolean, index=True) ref_id = db.Column(db.Integer) questions = db.relationship('Question', backref='author', lazy='dynamic') answers = db.relationship('Answer', backref='author', lazy='dynamic') pets = db.relationship('Pet', backref='owner', lazy='dynamic')
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(120), index=True, nullable=True) dob = db.Column(db.DateTime, index=True, nullable=True) address = db.Column(db.String(128), nullable=True) phone = db.Column(db.String(15), nullable=True) is_customer = db.Column(db.Boolean, index=True) questions = db.relationship('Question', backref='author', lazy='dynamic') answers = db.relationship('Answer', backref='author', lazy='dynamic')
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) active = db.Column('is_active', db.Boolean(), nullable=False, server_default='1') # User authentication information. The collation='NOCASE' is required # to search case insensitively when USER_IFIND_MODE is 'nocase_collation'. email = db.Column(db.String(255, collation='NOCASE'), nullable=False, unique=True) email_confirmed_at = db.Column(db.DateTime()) password = db.Column(db.String(255), nullable=False, server_default='') # User information first_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') last_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') # Define the relationship to Role via UserRoles roles = db.relationship('Role', secondary='user_roles')
class Message(db.Model): __tablename__ = 'messages' id = db.Column(db.Integer, primary_key=True) content_crypt = db.Column(db.String(250)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) sender_id = db.Column(db.Integer, db.ForeignKey('users.id')) recipient_id = db.Column(db.Integer, db.ForeignKey('users.id')) sender = db.relationship('User', back_populates='sent_messages', primaryjoin='Message.sender_id == User.id') recipient = db.relationship('User', back_populates='received_messages', primaryjoin='Message.recipient_id == User.id') def decrypted_message(self): f = Fernet(key) return f.decrypt(self.content_crypt.encode()).decode('utf-8')
class Question(db.Model): __tablename__ = 'questions' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(30)) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) anonymity = db.Column(db.Integer) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) answers = db.relationship('Answer', backref='question', lazy='dynamic') def __repr__(self): return '<Question asked on {} by {}: {} >'.format( str(self.timestamp)[0:10], self.author.username, self.body)
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) followed = db.relationship('User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') def __repr__(self): return '<User: {}>'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( digest, size) def follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): if self.is_following(user): self.followed.remove(user) def is_following(self, user): return self.followed.filter( followers.c.followed_id == user.id).count() > 0 def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter( followers.c.follower_id == self.id) own = Post.query.filter_by(user_id=self.id) return followed.union(own).order_by(Post.timestamp.desc()) def get_reset_password_token(self, expires_in=600): return jwt.encode( { 'reset_password': self.id, 'exp': time() + expires_in }, app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password'] except: return return User.query.get(id)