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)
51 if not current_user in thread.watchers:
52 thread.watchers.append(current_user)
55 if thread.package is None:
56 msg = "New comment on '{}'".format(thread.title)
58 msg = "New comment on '{}' on package {}".format(thread.title, thread.package.title)
61 for user in thread.watchers:
62 if user != current_user:
63 triggerNotif(user, current_user, msg, url_for("thread_page", id=thread.id))
67 return redirect(url_for("thread_page", id=id))
70 flash("Comment needs to be between 3 and 500 characters.")
72 return render_template("threads/view.html", thread=thread)
75 class ThreadForm(FlaskForm):
76 title = StringField("Title", [InputRequired(), Length(3,100)])
77 comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
78 private = BooleanField("Private")
79 submit = SubmitField("Open Thread")
81 @app.route("/threads/new/", methods=["GET", "POST"])
83 def new_thread_page():
84 form = ThreadForm(formdata=request.form)
87 if "pid" in request.args:
88 package = Package.query.get(int(request.args.get("pid")))
90 flash("Unable to find that package!", "error")
92 # Don't allow making threads on approved packages for now
93 if package is None or package.approved:
96 def_is_private = request.args.get("private") or False
97 if not package.approved:
99 allow_change = package.approved
100 is_review_thread = package is not None and not package.approved
102 # Check that user can make the thread
103 if is_review_thread and not (package.author == current_user or \
104 package.checkPerm(current_user, Permission.APPROVE_NEW)):
105 flash("Unable to create thread!", "error")
106 return redirect(url_for("home_page"))
108 # Only allow creating one thread when not approved
109 elif is_review_thread and package.review_thread is not None:
110 flash("A review thread already exists!", "error")
111 if request.method == "GET":
112 return redirect(url_for("thread_page", id=package.review_thread.id))
115 elif request.method == "GET":
116 form.private.data = def_is_private
117 form.title.data = request.args.get("title") or ""
119 # Validate and submit
120 elif request.method == "POST" and form.validate():
122 thread.author = current_user
123 thread.title = form.title.data
124 thread.private = form.private.data if allow_change else def_is_private
125 thread.package = package
126 db.session.add(thread)
128 thread.watchers.append(current_user)
129 if package is not None and package.author != current_user:
130 thread.watchers.append(package.author)
132 reply = ThreadReply()
133 reply.thread = thread
134 reply.author = current_user
135 reply.comment = form.comment.data
136 db.session.add(reply)
138 thread.replies.append(reply)
143 package.review_thread = thread
145 if package is not None:
146 triggerNotif(package.author, current_user,
147 "New thread '{}' on package {}".format(thread.title, package.title), url_for("thread_page", id=thread.id))
151 return redirect(url_for("thread_page", id=thread.id))
154 return render_template("threads/new.html", form=form, allow_private_change=allow_change)