]> git.lizzy.rs Git - cheatdb.git/blob - app/views/threads.py
Reorder new and popular, change number of packages in each
[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                         if not current_user in thread.watchers:
52                                 thread.watchers.append(current_user)
53
54                         msg = None
55                         if thread.package is None:
56                                 msg = "New comment on '{}'".format(thread.title)
57                         else:
58                                 msg = "New comment on '{}' on package {}".format(thread.title, thread.package.title)
59
60
61                         for user in thread.watchers:
62                                 if user != current_user:
63                                         triggerNotif(user, current_user, msg, url_for("thread_page", id=thread.id))
64
65                         db.session.commit()
66
67                         return redirect(url_for("thread_page", id=id))
68
69                 else:
70                         flash("Comment needs to be between 3 and 500 characters.")
71
72         return render_template("threads/view.html", thread=thread)
73
74
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")
80
81 @app.route("/threads/new/", methods=["GET", "POST"])
82 @login_required
83 def new_thread_page():
84         form = ThreadForm(formdata=request.form)
85
86         package = None
87         if "pid" in request.args:
88                 package = Package.query.get(int(request.args.get("pid")))
89                 if package is None:
90                         flash("Unable to find that package!", "error")
91
92         # Don't allow making threads on approved packages for now
93         if package is None or package.approved:
94                 abort(403)
95
96         def_is_private   = request.args.get("private") or False
97         if not package.approved:
98                 def_is_private = True
99         allow_change     = package.approved
100         is_review_thread = package is not None and not package.approved
101
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"))
107
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))
113
114         # Set default values
115         elif request.method == "GET":
116                 form.private.data = def_is_private
117                 form.title.data   = request.args.get("title") or ""
118
119         # Validate and submit
120         elif request.method == "POST" and form.validate():
121                 thread = Thread()
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)
127
128                 thread.watchers.append(current_user)
129                 if package is not None and package.author != current_user:
130                         thread.watchers.append(package.author)
131
132                 reply = ThreadReply()
133                 reply.thread  = thread
134                 reply.author  = current_user
135                 reply.comment = form.comment.data
136                 db.session.add(reply)
137
138                 thread.replies.append(reply)
139
140                 db.session.commit()
141
142                 if is_review_thread:
143                         package.review_thread = thread
144
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))
148
149                 db.session.commit()
150
151                 return redirect(url_for("thread_page", id=thread.id))
152
153
154         return render_template("threads/new.html", form=form, allow_private_change=allow_change)