]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/threads/__init__.py
a4728a0352926bddf7ad9abff2a636bd7bd3720e
[cheatdb.git] / app / blueprints / threads / __init__.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
20 bp = Blueprint("threads", __name__)
21
22 from flask_user import *
23 from app.models import *
24 from app.utils import addNotification, clearNotifications
25
26 import datetime
27
28 from flask_wtf import FlaskForm
29 from wtforms import *
30 from wtforms.validators import *
31 from app.utils import get_int_or_abort
32
33 @bp.route("/threads/")
34 def list_all():
35         query = Thread.query
36         if not Permission.SEE_THREAD.check(current_user):
37                 query = query.filter_by(private=False)
38
39         pid = request.args.get("pid")
40         if pid:
41                 pid = get_int_or_abort(pid)
42                 query = query.filter_by(package_id=pid)
43
44         query = query.order_by(db.desc(Thread.created_at))
45
46         return render_template("threads/list.html", threads=query.all())
47
48
49 @bp.route("/threads/<int:id>/subscribe/", methods=["POST"])
50 @login_required
51 def subscribe(id):
52         thread = Thread.query.get(id)
53         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
54                 abort(404)
55
56         if current_user in thread.watchers:
57                 flash("Already subscribed!", "success")
58         else:
59                 flash("Subscribed to thread", "success")
60                 thread.watchers.append(current_user)
61                 db.session.commit()
62
63         return redirect(url_for("threads.view", id=id))
64
65
66 @bp.route("/threads/<int:id>/unsubscribe/", methods=["POST"])
67 @login_required
68 def unsubscribe(id):
69         thread = Thread.query.get(id)
70         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
71                 abort(404)
72
73         if current_user in thread.watchers:
74                 flash("Unsubscribed!", "success")
75                 thread.watchers.remove(current_user)
76                 db.session.commit()
77         else:
78                 flash("Not subscribed to thread", "success")
79
80         return redirect(url_for("threads.view", id=id))
81
82
83 @bp.route("/threads/<int:id>/", methods=["GET", "POST"])
84 def view(id):
85         thread = Thread.query.get(id)
86         if thread is None or not thread.checkPerm(current_user, Permission.SEE_THREAD):
87                 abort(404)
88
89         if current_user.is_authenticated and request.method == "POST":
90                 comment = request.form["comment"]
91
92                 if not current_user.canCommentRL():
93                         flash("Please wait before commenting again", "danger")
94                         if package:
95                                 return redirect(package.getDetailsURL())
96                         else:
97                                 return redirect(url_for("homepage.home"))
98
99                 if len(comment) <= 500 and len(comment) > 3:
100                         reply = ThreadReply()
101                         reply.author = current_user
102                         reply.comment = comment
103                         db.session.add(reply)
104
105                         thread.replies.append(reply)
106                         if not current_user in thread.watchers:
107                                 thread.watchers.append(current_user)
108
109                         msg = "New comment on '{}'".format(thread.title)
110                         addNotification(thread.watchers, current_user, msg, url_for("threads.view", id=thread.id), thread.package)
111                         db.session.commit()
112
113                         return redirect(url_for("threads.view", id=id))
114
115                 else:
116                         flash("Comment needs to be between 3 and 500 characters.")
117
118         return render_template("threads/view.html", thread=thread)
119
120
121 class ThreadForm(FlaskForm):
122         title   = StringField("Title", [InputRequired(), Length(3,100)])
123         comment = TextAreaField("Comment", [InputRequired(), Length(10, 500)])
124         private = BooleanField("Private")
125         submit  = SubmitField("Open Thread")
126
127 @bp.route("/threads/new/", methods=["GET", "POST"])
128 @login_required
129 def new():
130         form = ThreadForm(formdata=request.form)
131
132         package = None
133         if "pid" in request.args:
134                 package = Package.query.get(int(request.args.get("pid")))
135                 if package is None:
136                         flash("Unable to find that package!", "danger")
137
138         # Don't allow making orphan threads on approved packages for now
139         if package is None:
140                 abort(403)
141
142         def_is_private   = request.args.get("private") or False
143         if package is None:
144                 def_is_private = True
145         allow_change     = package and package.approved
146         is_review_thread = package and not package.approved
147
148         # Check that user can make the thread
149         if not package.checkPerm(current_user, Permission.CREATE_THREAD):
150                 flash("Unable to create thread!", "danger")
151                 return redirect(url_for("homepage.home"))
152
153         # Only allow creating one thread when not approved
154         elif is_review_thread and package.review_thread is not None:
155                 flash("A review thread already exists!", "danger")
156                 return redirect(url_for("threads.view", id=package.review_thread.id))
157
158         elif not current_user.canOpenThreadRL():
159                 flash("Please wait before opening another thread", "danger")
160
161                 if package:
162                         return redirect(package.getDetailsURL())
163                 else:
164                         return redirect(url_for("homepage.home"))
165
166         # Set default values
167         elif request.method == "GET":
168                 form.private.data = def_is_private
169                 form.title.data   = request.args.get("title") or ""
170
171         # Validate and submit
172         elif request.method == "POST" and form.validate():
173                 thread = Thread()
174                 thread.author  = current_user
175                 thread.title   = form.title.data
176                 thread.private = form.private.data if allow_change else def_is_private
177                 thread.package = package
178                 db.session.add(thread)
179
180                 thread.watchers.append(current_user)
181                 if package is not None and package.author != current_user:
182                         thread.watchers.append(package.author)
183
184                 reply = ThreadReply()
185                 reply.thread  = thread
186                 reply.author  = current_user
187                 reply.comment = form.comment.data
188                 db.session.add(reply)
189
190                 thread.replies.append(reply)
191
192                 db.session.commit()
193
194                 if is_review_thread:
195                         package.review_thread = thread
196
197                 notif_msg = "New thread '{}'".format(thread.title)
198                 if package is not None:
199                         addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id), package)
200
201                 editors = User.query.filter(User.rank >= UserRank.EDITOR).all()
202                 addNotification(editors, current_user, notif_msg, url_for("threads.view", id=thread.id), package)
203
204                 db.session.commit()
205
206                 return redirect(url_for("threads.view", id=thread.id))
207
208
209         return render_template("threads/new.html", form=form, allow_private_change=allow_change, package=package)