2 # Copyright (C) 2018 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/>.
19 from flask_user import *
21 from app.models import *
22 from app.utils import triggerNotif, clearNotifications
24 from flask_wtf import FlaskForm
26 from wtforms.validators import *
28 @app.route("/threads/")
30 threads = Thread.query.filter_by(private=False).all()
31 return render_template("threads/list.html", threads=threads)
33 @app.route("/threads/<int:id>/", methods=["GET", "POST"])
35 clearNotifications(url_for("thread_page", id=id))
37 thread = Thread.query.get(id)
38 if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
41 if current_user.is_authenticated and request.method == "POST":
42 comment = request.form["comment"]
44 if len(comment) <= 500 and len(comment) > 3:
46 reply.author = current_user
47 reply.comment = comment
50 thread.replies.append(reply)
53 return redirect(url_for("thread_page", id=id))
56 flash("Comment needs to be between 3 and 500 characters.")
58 return render_template("threads/view.html", thread=thread)
61 class ThreadForm(FlaskForm):
62 title = StringField("Title", [InputRequired(), Length(3,100)])
63 comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
64 private = BooleanField("Private")
65 submit = SubmitField("Open Thread")
67 @app.route("/threads/new/", methods=["GET", "POST"])
69 def new_thread_page():
70 form = ThreadForm(formdata=request.form)
73 if "pid" in request.args:
74 package = Package.query.get(int(request.args.get("pid")))
76 flash("Unable to find that package!", "error")
81 def_is_private = request.args.get("private") or False
82 if not package.approved:
84 allow_change = package.approved
85 is_review_thread = package is not None and not package.approved
87 # Check that user can make the thread
88 if is_review_thread and not (package.author == current_user or \
89 package.checkPerm(current_user, Permission.APPROVE_NEW)):
90 flash("Unable to create thread!", "error")
91 return redirect(url_for("home_page"))
93 # Only allow creating one thread when not approved
94 elif is_review_thread and package.review_thread is not None:
95 flash("A review thread already exists!", "error")
96 if request.method == "GET":
97 return redirect(url_for("thread_page", id=package.review_thread.id))
100 elif request.method == "GET":
101 form.private.data = def_is_private
102 form.title.data = request.args.get("title") or ""
104 # Validate and submit
105 elif request.method == "POST" and form.validate():
107 thread.author = current_user
108 thread.title = form.title.data
109 thread.private = form.private.data if allow_change else def_is_private
110 thread.package = package
111 db.session.add(thread)
113 reply = ThreadReply()
114 reply.thread = thread
115 reply.author = current_user
116 reply.comment = form.comment.data
117 db.session.add(reply)
119 thread.replies.append(reply)
124 package.review_thread = thread
126 if package is not None:
127 triggerNotif(package.author, current_user,
128 "New thread '{}' on package {}".format(thread.title, package.title), url_for("thread_page", id=thread.id))
133 return redirect(url_for("thread_page", id=thread.id))
136 return render_template("threads/new.html", form=form, allow_private_change=allow_change)