From e82166f87e89f329e9fd94a26dfc8651c69deb91 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 28 Jul 2018 15:30:59 +0100 Subject: [PATCH] Add subscribe/unsubscribe button --- app/models.py | 9 +++++++++ app/templates/threads/view.html | 21 ++++++++++++++++---- app/views/threads.py | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index ef26bbd..2487e73 100644 --- a/app/models.py +++ b/app/models.py @@ -731,6 +731,15 @@ class Thread(db.Model): watchers = db.relationship("User", secondary=watchers, lazy="subquery", \ backref=db.backref("watching", lazy=True)) + + def getSubscribeURL(self): + return url_for("thread_subscribe_page", + id=self.id) + + def getUnsubscribeURL(self): + return url_for("thread_unsubscribe_page", + id=self.id) + def checkPerm(self, user, perm): if not user.is_authenticated: return not self.private diff --git a/app/templates/threads/view.html b/app/templates/threads/view.html index 397fba3..71580de 100644 --- a/app/templates/threads/view.html +++ b/app/templates/threads/view.html @@ -6,11 +6,24 @@ Threads {% block content %}

{% if thread.private %}🔒 {% endif %}{{ thread.title }}

+ {% if thread.package or current_user.is_authenticated %} + {% if thread.package %} +

Package: {{ thread.package.title }}

+ {% endif %} - {% if thread.package %} -

- Package: {{ thread.package.title }} -

+ {% if current_user.is_authenticated %} + {% if current_user in thread.watchers %} +
+ + +
+ {% else %} +
+ + +
+ {% endif %} + {% endif %} {% endif %} {% if thread.private %} diff --git a/app/views/threads.py b/app/views/threads.py index 316ca4d..37ac3d1 100644 --- a/app/views/threads.py +++ b/app/views/threads.py @@ -32,6 +32,41 @@ def threads_page(): query = query.filter_by(private=False) return render_template("threads/list.html", threads=query.all()) + +@app.route("/threads//subscribe/", methods=["POST"]) +@login_required +def thread_subscribe_page(id): + thread = Thread.query.get(id) + if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD): + abort(404) + + if current_user in thread.watchers: + flash("Already subscribed!", "success") + else: + flash("Subscribed to thread", "success") + thread.watchers.append(current_user) + db.session.commit() + + return redirect(url_for("thread_page", id=id)) + + +@app.route("/threads//unsubscribe/", methods=["POST"]) +@login_required +def thread_unsubscribe_page(id): + thread = Thread.query.get(id) + if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD): + abort(404) + + if current_user in thread.watchers: + flash("Unsubscribed!", "success") + thread.watchers.remove(current_user) + db.session.commit() + else: + flash("Not subscribed to thread", "success") + + return redirect(url_for("thread_page", id=id)) + + @app.route("/threads//", methods=["GET", "POST"]) def thread_page(id): clearNotifications(url_for("thread_page", id=id))