From ba1a660b1a41088c2e8fef5a0dbb3abab2142714 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 9 Jul 2018 08:33:04 +0200 Subject: [PATCH] Follow users --- calsocial/__init__.py | 21 +++++++++++++++++++++ calsocial/models.py | 17 +++++++++++++++++ calsocial/templates/profile-details.html | 3 +++ 3 files changed, 41 insertions(+) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 220d290..6623abd 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -266,5 +266,26 @@ class CalendarSocialApp(Flask): return render_template('profile-details.html', profile=profile) + @staticmethod + @route('/profile/@/follow') + def follow_user(username): + """View for following a user + """ + + from .models import db, Profile, User, UserFollow + + profile = current_user.profile + + if profile is None: + abort(404) + + if profile.user != current_user: + follow = UserFollow(follower_id=current_user.profile, + followed_id=profile, + accepted_at=datetime.utcnow()) + db.session.add(follow) + db.session.commit() + + return redirect(url_for('display_profile', username=username)) app = CalendarSocialApp(__name__) diff --git a/calsocial/models.py b/calsocial/models.py index 3ad7559..e1de74c 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -395,3 +395,20 @@ class AuditLog(db.Model): message = f'Audit: [{ip}] [{user.id}] ' + cls.get_message(log_type, user=user.username) logger.info(message) + + +class UserFollow(db.Model): + """Database model for user follows + """ + + #: The ID of the profile that is following the other + follower_id = db.Column(db.Integer(), db.ForeignKey('profiles.id'), primary_key=True) + + #: The ID of the profile being followed + followed_id = db.Column(db.Integer(), db.ForeignKey('profiles.id'), primary_key=True) + + #: The timestamp when the follow was initiated + initiated_at = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False) + + #: The timestamp when the follow was accepted + accepted_at = db.Column(db.DateTime(), nullable=True) diff --git a/calsocial/templates/profile-details.html b/calsocial/templates/profile-details.html index 034e94f..0dbdcaf 100644 --- a/calsocial/templates/profile-details.html +++ b/calsocial/templates/profile-details.html @@ -5,4 +5,7 @@ {{ profile.name }} @{{ profile.user.username}} + {% if profile.user != current_user %} +{% trans %}Follow{% endtrans %} + {% endif %} {% endblock content %}