From f1fab33c8db272c0f6d3cfcfbc9f0242d99b2d94 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 9 Jul 2018 13:04:55 +0200 Subject: [PATCH] Display following/followed users on the profile page --- calsocial/models.py | 30 ++++++++++++++++++++++++ calsocial/templates/profile-details.html | 16 +++++++++++++ 2 files changed, 46 insertions(+) diff --git a/calsocial/models.py b/calsocial/models.py index e1de74c..3c1a6b1 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -194,6 +194,32 @@ class Profile(db.Model): # pylint: disable=too-few-public-methods return f'' + @property + def followed_list(self): + """List of profiles this profile is following + """ + + # This will always be empty for remote profiles + if not self.user: + return [] + + return Profile.query \ + .join(UserFollow.followed) \ + .filter(UserFollow.follower == self) + + @property + def follower_list(self): + """List of profiles that follow this profile + """ + + # This will always be empty for remote profiles + if not self.user: + return [] + + return Profile.query \ + .join(UserFollow.follower) \ + .filter(UserFollow.followed == self) + class Event(db.Model): """Database model for events @@ -404,9 +430,13 @@ class UserFollow(db.Model): #: The ID of the profile that is following the other follower_id = db.Column(db.Integer(), db.ForeignKey('profiles.id'), primary_key=True) + follower = db.relationship('Profile', foreign_keys=[follower_id]) + #: The ID of the profile being followed followed_id = db.Column(db.Integer(), db.ForeignKey('profiles.id'), primary_key=True) + followed = db.relationship('Profile', foreign_keys=[followed_id]) + #: The timestamp when the follow was initiated initiated_at = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False) diff --git a/calsocial/templates/profile-details.html b/calsocial/templates/profile-details.html index 0dbdcaf..75db8d8 100644 --- a/calsocial/templates/profile-details.html +++ b/calsocial/templates/profile-details.html @@ -8,4 +8,20 @@ {% if profile.user != current_user %} {% trans %}Follow{% endtrans %} {% endif %} + +

+ {% trans %}Follows{% endtrans %} +

+ + {% for followed in profile.followed_list %} +{{ followed }} + {% endfor %} + +

+ {% trans %}Followers{% endtrans %} +

+ + {% for follower in profile.follower_list %} +{{ follower }} + {% endfor %} {% endblock content %}