]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/todo/__init__.py
12b062222c486d1e0b103c886f69643d1d5f0198
[cheatdb.git] / app / blueprints / todo / __init__.py
1 # ContentDB
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 from flask import *
18 from flask_user import *
19 import flask_menu as menu
20 from app.models import *
21 from app.querybuilder import QueryBuilder
22 from app.utils import get_int_or_abort
23
24 bp = Blueprint("todo", __name__)
25
26 @bp.route("/todo/", methods=["GET", "POST"])
27 @login_required
28 def view():
29         canApproveNew = Permission.APPROVE_NEW.check(current_user)
30         canApproveRel = Permission.APPROVE_RELEASE.check(current_user)
31         canApproveScn = Permission.APPROVE_SCREENSHOT.check(current_user)
32
33         packages = None
34         if canApproveNew:
35                 packages = Package.query.filter_by(approved=False, soft_deleted=False).order_by(db.desc(Package.created_at)).all()
36
37         releases = None
38         if canApproveRel:
39                 releases = PackageRelease.query.filter_by(approved=False).all()
40
41         screenshots = None
42         if canApproveScn:
43                 screenshots = PackageScreenshot.query.filter_by(approved=False).all()
44
45         if not canApproveNew and not canApproveRel and not canApproveScn:
46                 abort(403)
47
48         if request.method == "POST":
49                 if request.form["action"] == "screenshots_approve_all":
50                         if not canApproveScn:
51                                 abort(403)
52
53                         PackageScreenshot.query.update({ "approved": True })
54                         db.session.commit()
55                         return redirect(url_for("todo.view"))
56                 else:
57                         abort(400)
58
59         topic_query = ForumTopic.query \
60                         .filter_by(discarded=False)
61
62         total_topics = topic_query.count()
63         topics_to_add = topic_query \
64                         .filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
65                         .count()
66
67         return render_template("todo/list.html", title="Reports and Work Queue",
68                 packages=packages, releases=releases, screenshots=screenshots,
69                 canApproveNew=canApproveNew, canApproveRel=canApproveRel, canApproveScn=canApproveScn,
70                 topics_to_add=topics_to_add, total_topics=total_topics)
71
72
73 @bp.route("/todo/topics/")
74 @login_required
75 def topics():
76         qb    = QueryBuilder(request.args)
77         qb.setSortIfNone("date")
78         query = qb.buildTopicQuery()
79
80         tmp_q = ForumTopic.query
81         if not qb.show_discarded:
82                 tmp_q = tmp_q.filter_by(discarded=False)
83         total = tmp_q.count()
84         topic_count = query.count()
85
86         page  = get_int_or_abort(request.args.get("page"), 1)
87         num   = get_int_or_abort(request.args.get("n"), 100)
88         if num > 100 and not current_user.rank.atLeast(UserRank.EDITOR):
89                 num = 100
90
91         query = query.paginate(page, num, True)
92         next_url = url_for("todo.topics", page=query.next_num, query=qb.search, \
93                 show_discarded=qb.show_discarded, n=num, sort=qb.order_by) \
94                         if query.has_next else None
95         prev_url = url_for("todo.topics", page=query.prev_num, query=qb.search, \
96                 show_discarded=qb.show_discarded, n=num, sort=qb.order_by) \
97                         if query.has_prev else None
98
99         return render_template("todo/topics.html", topics=query.items, total=total, \
100                         topic_count=topic_count, query=qb.search, show_discarded=qb.show_discarded, \
101                         next_url=next_url, prev_url=prev_url, page=page, page_max=query.pages, \
102                         n=num, sort_by=qb.order_by)