mirror of
https://github.com/rmontanana/microblog.git
synced 2025-08-16 15:55:51 +00:00
Chapter 6
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from hashlib import md5
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from flask_login import UserMixin
|
||||
from app import db, login
|
||||
@@ -15,6 +16,8 @@ class User(UserMixin, db.Model):
|
||||
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)
|
||||
|
||||
def __repr__(self):
|
||||
return "<User {}>".format(self.username)
|
||||
@@ -25,6 +28,12 @@ class User(UserMixin, db.Model):
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
class Post(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
|
Reference in New Issue
Block a user