|
@ -71,6 +71,15 @@ class Role(db.Model): |
|
|
return '<Role %r>' % self.name |
|
|
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): |
|
|
class User(UserMixin, db.Model): |
|
|
__tablename__ = 'users' |
|
|
__tablename__ = 'users' |
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
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) |
|
|
last_seen = db.Column(db.DateTime(), default=datetime.utcnow) |
|
|
avatar_hash = db.Column(db.String(32)) |
|
|
avatar_hash = db.Column(db.String(32)) |
|
|
posts = db.relationship('Post', backref='author', lazy='dynamic') |
|
|
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): |
|
|
def __init__(self, **kwargs): |
|
|
super(User, self).__init__(**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( |
|
|
return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( |
|
|
url=url, hash=hash, size=size, default=default, rating=rating) |
|
|
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): |
|
|
def __repr__(self): |
|
|
return '<User %r>' % self.username |
|
|
return '<User %r>' % self.username |
|
|
|
|
|
|
|
|