contentdb/app/blueprints/packages/reviews.py

193 lines
5.8 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2020-07-09 05:10:09 +02:00
# Copyright (C) 2020 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2020-07-09 05:10:09 +02:00
# 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
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2020-07-09 05:10:09 +02:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2020-07-09 05:10:09 +02:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from . import bp
from flask import *
from flask_login import current_user, login_required
2020-07-09 05:10:09 +02:00
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
2021-08-18 23:09:41 +02:00
from app.models import db, PackageReview, Thread, ThreadReply, NotificationType, PackageReviewVote, Package
from app.utils import is_package_page, addNotification, get_int_or_abort, isYes, is_safe_url
2021-08-17 22:16:43 +02:00
from app.tasks.webhooktasks import post_discord_webhook
2020-07-09 05:10:09 +02:00
2020-07-10 21:30:31 +02:00
@bp.route("/reviews/")
def list_reviews():
2020-12-04 00:41:11 +01:00
page = get_int_or_abort(request.args.get("page"), 1)
num = min(40, get_int_or_abort(request.args.get("n"), 100))
pagination = PackageReview.query.order_by(db.desc(PackageReview.created_at)).paginate(page, num, True)
return render_template("packages/reviews_list.html", pagination=pagination, reviews=pagination.items)
2020-07-10 21:30:31 +02:00
2020-07-09 05:10:09 +02:00
class ReviewForm(FlaskForm):
title = StringField("Title", [InputRequired(), Length(3,100)])
comment = TextAreaField("Comment", [InputRequired(), Length(10, 2000)])
2020-07-09 05:10:09 +02:00
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):
if current_user in package.maintainers:
flash("You can't review your own package!", "danger")
2021-07-24 05:30:14 +02:00
return redirect(package.getURL("packages.view"))
2020-07-09 05:10:09 +02:00
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
2020-12-05 00:07:19 +01:00
elif form.validate_on_submit():
2020-07-09 05:10:09 +02:00
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()
2020-07-09 05:32:13 +02:00
package.recalcScore()
2020-07-09 05:10:09 +02:00
if was_new:
notif_msg = "New review '{}'".format(form.title.data)
2020-12-05 04:44:34 +01:00
type = NotificationType.NEW_REVIEW
2020-07-09 05:10:09 +02:00
else:
notif_msg = "Updated review '{}'".format(form.title.data)
2020-12-05 04:44:34 +01:00
type = NotificationType.OTHER
2020-07-09 05:10:09 +02:00
2020-12-05 04:44:34 +01:00
addNotification(package.maintainers, current_user, type, notif_msg,
url_for("threads.view", id=thread.id), package)
2020-07-09 05:10:09 +02:00
2021-08-17 22:16:43 +02:00
if was_new:
post_discord_webhook.delay(thread.author.username,
"Reviewed {}: {}".format(package.title, thread.getViewURL(absolute=True)), False)
2020-07-09 05:10:09 +02:00
db.session.commit()
2021-07-24 05:30:14 +02:00
return redirect(package.getURL("packages.view"))
2020-07-09 05:10:09 +02:00
2020-12-04 03:23:04 +01:00
return render_template("packages/review_create_edit.html",
2020-07-10 20:26:37 +02:00
form=form, package=package, review=review)
@bp.route("/packages/<author>/<name>/review/delete/", methods=["POST"])
@login_required
@is_package_page
def delete_review(package):
review = PackageReview.query.filter_by(package=package, author=current_user).first()
if review is None or review.package != package:
abort(404)
thread = review.thread
reply = ThreadReply()
reply.thread = thread
reply.author = current_user
reply.comment = "_converted review into a thread_"
db.session.add(reply)
thread.review = None
notif_msg = "Deleted review '{}', comments were kept as a thread".format(thread.title)
2020-12-05 04:44:34 +01:00
addNotification(package.maintainers, current_user, NotificationType.OTHER, notif_msg, url_for("threads.view", id=thread.id), package)
2020-07-10 20:26:37 +02:00
db.session.delete(review)
db.session.commit()
return redirect(thread.getViewURL())
2021-08-18 23:09:41 +02:00
def handle_review_vote(package: Package, review_id: int):
if current_user in package.maintainers:
flash("You can't vote on the reviews on your own package!", "danger")
return
review: PackageReview = PackageReview.query.get(review_id)
if review is None or review.package != package:
abort(404)
if review.author == current_user:
flash("You can't vote on your own reviews!", "danger")
return
2021-08-19 14:45:32 +02:00
is_positive = isYes(request.form["is_positive"])
2021-08-18 23:09:41 +02:00
vote = PackageReviewVote.query.filter_by(review=review, user=current_user).first()
if vote is None:
vote = PackageReviewVote()
vote.review = review
vote.user = current_user
2021-08-19 14:45:32 +02:00
vote.is_positive = is_positive
2021-08-18 23:09:41 +02:00
db.session.add(vote)
2021-08-19 14:45:32 +02:00
elif vote.is_positive == is_positive:
db.session.delete(vote)
2021-08-18 23:09:41 +02:00
else:
2021-08-19 14:45:32 +02:00
vote.is_positive = is_positive
2021-08-18 23:09:41 +02:00
review.update_score()
db.session.commit()
@bp.route("/packages/<author>/<name>/review/<int:review_id>/", methods=["POST"])
@login_required
@is_package_page
def review_vote(package, review_id):
handle_review_vote(package, review_id)
next_url = request.args.get("r")
if next_url and is_safe_url(next_url):
return redirect(next_url)
else:
return redirect(review.thread.getViewURL())