class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) access_token = db.Column(db.String(128)) email = db.Column(db.String(254), unique=True, nullable=False) nickname = db.Column(db.String(30)) password_hash = db.Column(db.String(128)) email_hash = db.Column(db.String(128)) github = db.Column(db.String(255)) website = db.Column(db.String(255)) bio = db.Column(db.String(120)) messages = db.relationship('Message', back_populates='author', cascade='all') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_email_hash() def set_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_email_hash(self): if self.email is not None and self.email_hash is None: self.email_hash = hashlib.md5(self.email.encode('utf-8')).hexdigest() # encode for py23 compatible @property def is_admin(self): return self.email == current_app.config['CATCHAT_ADMIN_EMAIL'] @property def gravatar(self): return 'https://gravatar.com/avatar/%s?d=monsterid' % self.email_hash
class Message(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text, nullable=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref='messages')
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(254), unique=True, nullable=False) nickname = db.Column(db.String(30)) password_hash = db.Column(db.String(128)) github = db.Column(db.String(255)) bio = db.Column(db.String(120)) website = db.Column(db.String(255)) email_hash = db.Column(db.String(128)) messages = db.relationship('Message', backref='author') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_email_hash() def generate_email_hash(self): if self.email is not None and self.email_hash is None: self.email_hash = hashlib.md5(self.email.encode('utf-8')).hexdigest() @property def gravatar(self): return 'https://gravatar.com/avatar/%s?d=monsterid' % \ self.email_hash