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 %}