|
|
@ -71,6 +71,15 @@ class Role(db.Model): |
|
|
|
return '<Role %r>' % self.name |
|
|
|
|
|
|
|
|
|
|
|
class Follow(db.Model): |
|
|
|
__tablename__ = 'follows' |
|
|
|
follower_id = db.Column(db.Integer, db.ForeignKey('users.id'), |
|
|
|
primary_key=True) |
|
|
|
followed_id = db.Column(db.Integer, db.ForeignKey('users.id'), |
|
|
|
primary_key=True) |
|
|
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow) |
|
|
|
|
|
|
|
|
|
|
|
class User(UserMixin, db.Model): |
|
|
|
__tablename__ = 'users' |
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
@ -86,6 +95,16 @@ class User(UserMixin, db.Model): |
|
|
|
last_seen = db.Column(db.DateTime(), default=datetime.utcnow) |
|
|
|
avatar_hash = db.Column(db.String(32)) |
|
|
|
posts = db.relationship('Post', backref='author', lazy='dynamic') |
|
|
|
followed = db.relationship('Follow', |
|
|
|
foreign_keys=[Follow.follower_id], |
|
|
|
backref=db.backref('follower', lazy='joined'), |
|
|
|
lazy='dynamic', |
|
|
|
cascade='all, delete-orphan') |
|
|
|
followers = db.relationship('Follow', |
|
|
|
foreign_keys=[Follow.followed_id], |
|
|
|
backref=db.backref('followed', lazy='joined'), |
|
|
|
lazy='dynamic', |
|
|
|
cascade='all, delete-orphan') |
|
|
|
|
|
|
|
def __init__(self, **kwargs): |
|
|
|
super(User, self).__init__(**kwargs) |
|
|
@ -185,6 +204,28 @@ class User(UserMixin, db.Model): |
|
|
|
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( |
|
|
|
url=url, hash=hash, size=size, default=default, rating=rating) |
|
|
|
|
|
|
|
def follow(self, user): |
|
|
|
if not self.is_following(user): |
|
|
|
f = Follow(follower=self, followed=user) |
|
|
|
db.session.add(f) |
|
|
|
|
|
|
|
def unfollow(self, user): |
|
|
|
f = self.followed.filter_by(followed_id=user.id).first() |
|
|
|
if f: |
|
|
|
db.session.delete(f) |
|
|
|
|
|
|
|
def is_following(self, user): |
|
|
|
if user.id is None: |
|
|
|
return False |
|
|
|
return self.followed.filter_by( |
|
|
|
followed_id=user.id).first() is not None |
|
|
|
|
|
|
|
def is_followed_by(self, user): |
|
|
|
if user.id is None: |
|
|
|
return False |
|
|
|
return self.followers.filter_by( |
|
|
|
follower_id=user.id).first() is not None |
|
|
|
|
|
|
|
def __repr__(self): |
|
|
|
return '<User %r>' % self.username |
|
|
|
|
|
|
|