diff options
| author | rubenwardy <rw@rubenwardy.com> | 2018-06-11 22:49:25 +0100 |
|---|---|---|
| committer | rubenwardy <rw@rubenwardy.com> | 2018-06-11 22:52:37 +0100 |
| commit | b1c349cc3558b4642a700a489482ff95b038ce56 (patch) | |
| tree | 1b8cda09e538d9c571050d26ecbb1d5455dea5c0 /app/views | |
| parent | 40aac38d43c2d7c2220b10a1bef34ac85f960359 (diff) | |
| download | cheatdb-b1c349cc3558b4642a700a489482ff95b038ce56.tar.xz | |
Add comment system
Diffstat (limited to 'app/views')
| -rw-r--r-- | app/views/__init__.py | 2 | ||||
| -rw-r--r-- | app/views/packages/__init__.py | 8 | ||||
| -rw-r--r-- | app/views/threads.py | 136 |
3 files changed, 144 insertions, 2 deletions
diff --git a/app/views/__init__.py b/app/views/__init__.py index 09c7be8..5257675 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -51,7 +51,7 @@ def home_page(): packages = query.order_by(db.desc(Package.created_at)).limit(15).all() return render_template("index.html", packages=packages, count=count) -from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor, meta, thumbnails +from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor, meta, thumbnails, threads @menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' }) @app.route('/<path:path>/') diff --git a/app/views/packages/__init__.py b/app/views/packages/__init__.py index 07f529b..0c22a70 100644 --- a/app/views/packages/__init__.py +++ b/app/views/packages/__init__.py @@ -110,9 +110,15 @@ def package_page(package): releases = getReleases(package) requests = [r for r in package.requests if r.status == 0] + + review_thread = Thread.query.filter_by(package_id=package.id, private=True).first() + if review_thread is not None and not review_thread.checkPerm(current_user, Permission.SEE_THREAD): + review_thread = None + return render_template("packages/view.html", \ package=package, releases=releases, requests=requests, \ - alternatives=alternatives, similar_topics=similar_topics) + alternatives=alternatives, similar_topics=similar_topics, \ + review_thread=review_thread) @app.route("/packages/<author>/<name>/download/") diff --git a/app/views/threads.py b/app/views/threads.py new file mode 100644 index 0000000..e4973a5 --- /dev/null +++ b/app/views/threads.py @@ -0,0 +1,136 @@ +# Content DB +# Copyright (C) 2018 rubenwardy +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + + +from flask import * +from flask_user import * +from app import app +from app.models import * +from app.utils import triggerNotif, clearNotifications + +from flask_wtf import FlaskForm +from wtforms import * +from wtforms.validators import * + +@app.route("/threads/") +def threads_page(): + threads = Thread.query.filter_by(private=False).all() + return render_template("threads/list.html", threads=threads) + +@app.route("/threads/<int:id>/", methods=["GET", "POST"]) +def thread_page(id): + clearNotifications(url_for("thread_page", id=id)) + + thread = Thread.query.get(id) + if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD): + abort(404) + + if current_user.is_authenticated and request.method == "POST": + comment = request.form["comment"] + + if len(comment) <= 500 and len(comment) > 3: + reply = ThreadReply() + reply.author = current_user + reply.comment = comment + db.session.add(reply) + + thread.replies.append(reply) + db.session.commit() + + return redirect(url_for("thread_page", id=id)) + + else: + flash("Comment needs to be between 3 and 500 characters.") + + return render_template("threads/view.html", thread=thread) + + +class ThreadForm(FlaskForm): + title = StringField("Title", [InputRequired(), Length(3,100)]) + comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)]) + private = BooleanField("Private") + submit = SubmitField("Open Thread") + +@app.route("/threads/new/", methods=["GET", "POST"]) +@login_required +def new_thread_page(): + form = ThreadForm(formdata=request.form) + + package = None + if "pid" in request.args: + package = Package.query.get(int(request.args.get("pid"))) + if package is None: + flash("Unable to find that package!", "error") + + if package is None: + abort(403) + + def_is_private = request.args.get("private") or False + if not package.approved: + def_is_private = True + allow_change = package.approved + is_review_thread = package is not None and not package.approved + + # Check that user can make the thread + if is_review_thread and not (package.author == current_user or \ + package.checkPerm(current_user, Permission.APPROVE_NEW)): + flash("Unable to create thread!", "error") + return redirect(url_for("home_page")) + + # Only allow creating one thread when not approved + elif is_review_thread and package.review_thread is not None: + flash("A review thread already exists!", "error") + if request.method == "GET": + return redirect(url_for("thread_page", id=package.review_thread.id)) + + # Set default values + elif request.method == "GET": + form.private.data = def_is_private + form.title.data = request.args.get("title") or "" + + # Validate and submit + elif request.method == "POST" and form.validate(): + thread = Thread() + thread.author = current_user + thread.title = form.title.data + thread.private = form.private.data if allow_change else def_is_private + thread.package = package + db.session.add(thread) + + reply = ThreadReply() + reply.thread = thread + reply.author = current_user + reply.comment = form.comment.data + db.session.add(reply) + + thread.replies.append(reply) + + db.session.commit() + + if is_review_thread: + package.review_thread = thread + + if package is not None: + triggerNotif(package.author, current_user, + "New thread '{}' on package {}".format(thread.title, package.title), url_for("thread_page", id=thread.id)) + db.session.commit() + + db.session.commit() + + return redirect(url_for("thread_page", id=thread.id)) + + + return render_template("threads/new.html", form=form, allow_private_change=allow_change) |
