]> git.lizzy.rs Git - cheatdb.git/blob - app/views/threads.py
Add comment system
[cheatdb.git] / app / views / threads.py
1 # Content DB
2 # Copyright (C) 2018  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
18 from flask import *
19 from flask_user import *
20 from app import app
21 from app.models import *
22 from app.utils import triggerNotif, clearNotifications
23
24 from flask_wtf import FlaskForm
25 from wtforms import *
26 from wtforms.validators import *
27
28 @app.route("/threads/")
29 def threads_page():
30         threads = Thread.query.filter_by(private=False).all()
31         return render_template("threads/list.html", threads=threads)
32
33 @app.route("/threads/<int:id>/", methods=["GET", "POST"])
34 def thread_page(id):
35         clearNotifications(url_for("thread_page", id=id))
36
37         thread = Thread.query.get(id)
38         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
39                 abort(404)
40
41         if current_user.is_authenticated and request.method == "POST":
42                 comment = request.form["comment"]
43
44                 if len(comment) <= 500 and len(comment) > 3:
45                         reply = ThreadReply()
46                         reply.author = current_user
47                         reply.comment = comment
48                         db.session.add(reply)
49
50                         thread.replies.append(reply)
51                         db.session.commit()
52
53                         return redirect(url_for("thread_page", id=id))
54
55                 else:
56                         flash("Comment needs to be between 3 and 500 characters.")
57
58         return render_template("threads/view.html", thread=thread)
59
60
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")
66
67 @app.route("/threads/new/", methods=["GET", "POST"])
68 @login_required
69 def new_thread_page():
70         form = ThreadForm(formdata=request.form)
71
72         package = None
73         if "pid" in request.args:
74                 package = Package.query.get(int(request.args.get("pid")))
75                 if package is None:
76                         flash("Unable to find that package!", "error")
77
78         if package is None:
79                 abort(403)
80
81         def_is_private   = request.args.get("private") or False
82         if not package.approved:
83                 def_is_private = True
84         allow_change     = package.approved
85         is_review_thread = package is not None and not package.approved
86
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"))
92
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))
98
99         # Set default values
100         elif request.method == "GET":
101                 form.private.data = def_is_private
102                 form.title.data   = request.args.get("title") or ""
103
104         # Validate and submit
105         elif request.method == "POST" and form.validate():
106                 thread = Thread()
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)
112
113                 reply = ThreadReply()
114                 reply.thread  = thread
115                 reply.author  = current_user
116                 reply.comment = form.comment.data
117                 db.session.add(reply)
118
119                 thread.replies.append(reply)
120
121                 db.session.commit()
122
123                 if is_review_thread:
124                         package.review_thread = thread
125
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))
129                         db.session.commit()
130
131                 db.session.commit()
132
133                 return redirect(url_for("thread_page", id=thread.id))
134
135
136         return render_template("threads/new.html", form=form, allow_private_change=allow_change)