From 5639c3f57849a54b6e68b2e9edeea704f42822a1 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 12 Jul 2018 09:56:55 +0200 Subject: [PATCH] Make it possible to accept follow requests --- calsocial/__init__.py | 40 ++++++++++++++++++++++++ calsocial/models.py | 6 ++++ calsocial/templates/follow-requests.html | 13 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 calsocial/templates/follow-requests.html diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 49d515d..3ff3383 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -419,5 +419,45 @@ class CalendarSocialApp(Flask): return render_template('index.html', calendar=calendar, user_only=False) + @staticmethod + @route('/follow-requests') + @login_required + def follow_requests(): + """View for listing follow requests + """ + + from .models import UserFollow + + requests = UserFollow.query \ + .filter(UserFollow.followed == current_user.profile) \ + .filter(UserFollow.accepted_at.is_(None)) + + return render_template('follow-requests.html', requests=requests) + + @staticmethod + @route('/follow-request//accept') + @login_required + def accept_follow(follower_id): + """View for accepting a follow request + """ + + from .models import db, UserFollow + + try: + req = UserFollow.query \ + .filter(UserFollow.followed == current_user.profile) \ + .filter(UserFollow.follower_id == follower_id) \ + .one() + except NoResultFound: + abort(404) + + if req.accepted_at is None: + req.accept() + + db.session.add(req) + db.session.commit() + + return redirect(url_for('follow_requests')) + app = CalendarSocialApp(__name__) diff --git a/calsocial/models.py b/calsocial/models.py index 45d18f1..9ac6220 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -604,6 +604,12 @@ class UserFollow(db.Model): # pylint: disable=too-few-public-methods #: The timestamp when the follow was accepted accepted_at = db.Column(db.DateTime(), nullable=True) + def accept(self): + """Accept this follow request + """ + + self.accepted_at = datetime.utcnow() + class Notification(db.Model): """Database model for notifications diff --git a/calsocial/templates/follow-requests.html b/calsocial/templates/follow-requests.html new file mode 100644 index 0000000..43560f9 --- /dev/null +++ b/calsocial/templates/follow-requests.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} +

{% trans %}Follow requests{% endtrans %}

+ +{% endblock content %}