2 # Copyright (C) 2020 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 from flask_user import *
21 from flask_wtf import FlaskForm
23 from wtforms.validators import *
24 from app.models import db, PackageReview, Thread, ThreadReply
25 from app.utils import is_package_page, addNotification
28 @bp.route("/reviews/")
30 reviews = PackageReview.query.order_by(db.desc(PackageReview.created_at)).all()
31 return render_template("packages/reviews_list.html", reviews=reviews)
34 class ReviewForm(FlaskForm):
35 title = StringField("Title", [InputRequired(), Length(3,100)])
36 comment = TextAreaField("Comment", [InputRequired(), Length(10, 2000)])
37 recommends = RadioField("Private", [InputRequired()], choices=[("yes", "Yes"), ("no", "No")])
38 submit = SubmitField("Save")
40 @bp.route("/packages/<author>/<name>/review/", methods=["GET", "POST"])
44 if current_user in package.maintainers:
45 flash("You can't review your own package!", "danger")
46 return redirect(package.getDetailsURL())
48 review = PackageReview.query.filter_by(package=package, author=current_user).first()
50 form = ReviewForm(formdata=request.form, obj=review)
53 if request.method == "GET" and review:
54 form.title.data = review.thread.title
55 form.recommends.data = "yes" if review.recommends else "no"
56 form.comment.data = review.thread.replies[0].comment
59 elif request.method == "POST" and form.validate():
63 review = PackageReview()
64 review.package = package
65 review.author = current_user
66 db.session.add(review)
68 review.recommends = form.recommends.data == "yes"
70 thread = review.thread
73 thread.author = current_user
74 thread.private = False
75 thread.package = package
76 thread.review = review
77 db.session.add(thread)
79 thread.watchers.append(current_user)
83 reply.author = current_user
84 reply.comment = form.comment.data
87 thread.replies.append(reply)
89 reply = thread.replies[0]
90 reply.comment = form.comment.data
92 thread.title = form.title.data
100 notif_msg = "New review '{}'".format(form.title.data)
102 notif_msg = "Updated review '{}'".format(form.title.data)
104 addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id), package)
108 return redirect(package.getDetailsURL())
110 return render_template("packages/review_create_edit.html", \
111 form=form, package=package, review=review)
114 @bp.route("/packages/<author>/<name>/review/delete/", methods=["POST"])
117 def delete_review(package):
118 review = PackageReview.query.filter_by(package=package, author=current_user).first()
119 if review is None or review.package != package:
122 thread = review.thread
124 reply = ThreadReply()
125 reply.thread = thread
126 reply.author = current_user
127 reply.comment = "_converted review into a thread_"
128 db.session.add(reply)
132 notif_msg = "Deleted review '{}', comments were kept as a thread".format(thread.title)
133 addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id), package)
135 db.session.delete(review)
138 return redirect(thread.getViewURL())