]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/packages/reviews.py
Fix wrong character limit on review form
[cheatdb.git] / app / blueprints / packages / reviews.py
1 # ContentDB
2 # Copyright (C) 2020  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
17 from . import bp
18
19 from flask import *
20 from flask_user import *
21 from flask_wtf import FlaskForm
22 from wtforms import *
23 from wtforms.validators import *
24 from app.models import db, PackageReview, Thread, ThreadReply
25 from app.utils import is_package_page, addNotification
26
27
28 @bp.route("/reviews/")
29 def list_reviews():
30         reviews = PackageReview.query.order_by(db.desc(PackageReview.created_at)).all()
31         return render_template("packages/reviews_list.html", reviews=reviews)
32
33
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")
39
40 @bp.route("/packages/<author>/<name>/review/", methods=["GET", "POST"])
41 @login_required
42 @is_package_page
43 def review(package):
44         if current_user in package.maintainers:
45                 flash("You can't review your own package!", "danger")
46                 return redirect(package.getDetailsURL())
47
48         review = PackageReview.query.filter_by(package=package, author=current_user).first()
49
50         form = ReviewForm(formdata=request.form, obj=review)
51
52         # Set default values
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
57
58         # Validate and submit
59         elif request.method == "POST" and form.validate():
60                 was_new = False
61                 if not review:
62                         was_new = True
63                         review = PackageReview()
64                         review.package = package
65                         review.author  = current_user
66                         db.session.add(review)
67
68                 review.recommends = form.recommends.data == "yes"
69
70                 thread = review.thread
71                 if not thread:
72                         thread = Thread()
73                         thread.author  = current_user
74                         thread.private = False
75                         thread.package = package
76                         thread.review = review
77                         db.session.add(thread)
78
79                         thread.watchers.append(current_user)
80
81                         reply = ThreadReply()
82                         reply.thread  = thread
83                         reply.author  = current_user
84                         reply.comment = form.comment.data
85                         db.session.add(reply)
86
87                         thread.replies.append(reply)
88                 else:
89                         reply = thread.replies[0]
90                         reply.comment = form.comment.data
91
92                 thread.title   = form.title.data
93
94                 db.session.commit()
95
96                 package.recalcScore()
97
98                 notif_msg = None
99                 if was_new:
100                         notif_msg = "New review '{}'".format(form.title.data)
101                 else:
102                         notif_msg = "Updated review '{}'".format(form.title.data)
103
104                 addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id), package)
105
106                 db.session.commit()
107
108                 return redirect(package.getDetailsURL())
109
110         return render_template("packages/review_create_edit.html", \
111                         form=form, package=package, review=review)
112
113
114 @bp.route("/packages/<author>/<name>/review/delete/", methods=["POST"])
115 @login_required
116 @is_package_page
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:
120                 abort(404)
121
122         thread = review.thread
123
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)
129
130         thread.review = None
131
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)
134
135         db.session.delete(review)
136         db.session.commit()
137
138         return redirect(thread.getViewURL())