diff options
| author | rubenwardy <rw@rubenwardy.com> | 2020-07-09 04:10:09 +0100 |
|---|---|---|
| committer | rubenwardy <rw@rubenwardy.com> | 2020-07-09 04:10:09 +0100 |
| commit | 307b8f8ddea30cb44ad02170b40dd3c0d1d3d6c3 (patch) | |
| tree | 50572120643ec9f6f5da9b68c507bd4b61d1d5ff /app/blueprints | |
| parent | 9d033acffff319e4f38b98a09ff2519f3f36dc88 (diff) | |
| download | cheatdb-307b8f8ddea30cb44ad02170b40dd3c0d1d3d6c3.tar.xz | |
Add reviews
Fixes #173
Diffstat (limited to 'app/blueprints')
| -rw-r--r-- | app/blueprints/packages/__init__.py | 2 | ||||
| -rw-r--r-- | app/blueprints/packages/reviews.py | 98 | ||||
| -rw-r--r-- | app/blueprints/threads/__init__.py | 3 |
3 files changed, 101 insertions, 2 deletions
diff --git a/app/blueprints/packages/__init__.py b/app/blueprints/packages/__init__.py index e4fc4f2..99616bf 100644 --- a/app/blueprints/packages/__init__.py +++ b/app/blueprints/packages/__init__.py @@ -18,4 +18,4 @@ from flask import Blueprint bp = Blueprint("packages", __name__) -from . import packages, screenshots, releases +from . import packages, screenshots, releases, reviews diff --git a/app/blueprints/packages/reviews.py b/app/blueprints/packages/reviews.py new file mode 100644 index 0000000..4322e8d --- /dev/null +++ b/app/blueprints/packages/reviews.py @@ -0,0 +1,98 @@ +# Content DB +# Copyright (C) 2020 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 . import bp + +from flask import * +from flask_user import * +from flask_wtf import FlaskForm +from wtforms import * +from wtforms.validators import * +from app.models import db, PackageReview, Thread, ThreadReply +from app.utils import is_package_page, triggerNotif + +class ReviewForm(FlaskForm): + title = StringField("Title", [InputRequired(), Length(3,100)]) + comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)]) + recommends = RadioField("Private", [InputRequired()], choices=[("yes", "Yes"), ("no", "No")]) + submit = SubmitField("Save") + +@bp.route("/packages/<author>/<name>/review/", methods=["GET", "POST"]) +@login_required +@is_package_page +def review(package): + review = PackageReview.query.filter_by(package=package, author=current_user).first() + + form = ReviewForm(formdata=request.form, obj=review) + + # Set default values + if request.method == "GET" and review: + form.title.data = review.thread.title + form.recommends.data = "yes" if review.recommends else "no" + form.comment.data = review.thread.replies[0].comment + + # Validate and submit + elif request.method == "POST" and form.validate(): + was_new = False + if not review: + was_new = True + review = PackageReview() + review.package = package + review.author = current_user + db.session.add(review) + + review.recommends = form.recommends.data == "yes" + + thread = review.thread + if not thread: + thread = Thread() + thread.author = current_user + thread.private = False + thread.package = package + thread.review = review + db.session.add(thread) + + thread.watchers.append(current_user) + + reply = ThreadReply() + reply.thread = thread + reply.author = current_user + reply.comment = form.comment.data + db.session.add(reply) + + thread.replies.append(reply) + else: + reply = thread.replies[0] + reply.comment = form.comment.data + + thread.title = form.title.data + + db.session.commit() + + notif_msg = None + if was_new: + notif_msg = "New review '{}' on package {}".format(form.title.data, package.title) + else: + notif_msg = "Updated review '{}' on package {}".format(form.title.data, package.title) + + for maintainer in package.maintainers: + triggerNotif(maintainer, current_user, notif_msg, url_for("threads.view", id=thread.id)) + + db.session.commit() + + return redirect(package.getDetailsURL()) + + return render_template("packages/review_create_edit.html", form=form, package=package) diff --git a/app/blueprints/threads/__init__.py b/app/blueprints/threads/__init__.py index a55d55e..c0b878c 100644 --- a/app/blueprints/threads/__init__.py +++ b/app/blueprints/threads/__init__.py @@ -206,7 +206,8 @@ def new(): notif_msg = None if package is not None: notif_msg = "New thread '{}' on package {}".format(thread.title, package.title) - triggerNotif(package.author, current_user, notif_msg, url_for("threads.view", id=thread.id)) + for maintainer in package.maintainers: + triggerNotif(maintainer, current_user, notif_msg, url_for("threads.view", id=thread.id)) else: notif_msg = "New thread '{}'".format(thread.title) |
