]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/todo/__init__.py
Fix broken state comparison due to enum ordering
[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 from sqlalchemy import or_
24
25 bp = Blueprint("todo", __name__)
26
27 @bp.route("/todo/", methods=["GET", "POST"])
28 @login_required
29 def view():
30         canApproveNew = Permission.APPROVE_NEW.check(current_user)
31         canApproveRel = Permission.APPROVE_RELEASE.check(current_user)
32         canApproveScn = Permission.APPROVE_SCREENSHOT.check(current_user)
33
34         packages = None
35         wip_packages = None
36         if canApproveNew:
37                 packages = Package.query.filter_by(state=PackageState.READY_FOR_REVIEW) \
38                         .order_by(db.desc(Package.created_at)).all()
39                 wip_packages = Package.query.filter(or_(Package.state==PackageState.WIP, Package.state==PackageState.CHANGES_NEEDED)) \
40                         .order_by(db.desc(Package.created_at)).all()
41
42         releases = None
43         if canApproveRel:
44                 releases = PackageRelease.query.filter_by(approved=False).all()
45
46         screenshots = None
47         if canApproveScn:
48                 screenshots = PackageScreenshot.query.filter_by(approved=False).all()
49
50         if not canApproveNew and not canApproveRel and not canApproveScn:
51                 abort(403)
52
53         if request.method == "POST":
54                 if request.form["action"] == "screenshots_approve_all":
55                         if not canApproveScn:
56                                 abort(403)
57
58                         PackageScreenshot.query.update({ "approved": True })
59                         db.session.commit()
60                         return redirect(url_for("todo.view"))
61                 else:
62                         abort(400)
63
64         topic_query = ForumTopic.query \
65                         .filter_by(discarded=False)
66
67         total_topics = topic_query.count()
68         topics_to_add = topic_query \
69                         .filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
70                         .count()
71
72         total_packages = Package.query.filter_by(state=PackageState.APPROVED).count()
73         total_to_tag = Package.query.filter_by(state=PackageState.APPROVED, tags=None).count()
74
75         unfulfilled_meta_packages = MetaPackage.query \
76                         .filter(~ MetaPackage.packages.any(state=PackageState.APPROVED)) \
77                         .filter(MetaPackage.dependencies.any(optional=False)) \
78                         .order_by(db.asc(MetaPackage.name)).count()
79
80         return render_template("todo/list.html", title="Reports and Work Queue",
81                 packages=packages, wip_packages=wip_packages, releases=releases, screenshots=screenshots,
82                 canApproveNew=canApproveNew, canApproveRel=canApproveRel, canApproveScn=canApproveScn,
83                 topics_to_add=topics_to_add, total_topics=total_topics, \
84                 total_packages=total_packages, total_to_tag=total_to_tag, \
85                 unfulfilled_meta_packages=unfulfilled_meta_packages)
86
87
88 @bp.route("/todo/topics/")
89 @login_required
90 def topics():
91         qb    = QueryBuilder(request.args)
92         qb.setSortIfNone("date")
93         query = qb.buildTopicQuery()
94
95         tmp_q = ForumTopic.query
96         if not qb.show_discarded:
97                 tmp_q = tmp_q.filter_by(discarded=False)
98         total = tmp_q.count()
99         topic_count = query.count()
100
101         page  = get_int_or_abort(request.args.get("page"), 1)
102         num   = get_int_or_abort(request.args.get("n"), 100)
103         if num > 100 and not current_user.rank.atLeast(UserRank.EDITOR):
104                 num = 100
105
106         query = query.paginate(page, num, True)
107         next_url = url_for("todo.topics", page=query.next_num, query=qb.search, \
108                 show_discarded=qb.show_discarded, n=num, sort=qb.order_by) \
109                         if query.has_next else None
110         prev_url = url_for("todo.topics", page=query.prev_num, query=qb.search, \
111                 show_discarded=qb.show_discarded, n=num, sort=qb.order_by) \
112                         if query.has_prev else None
113
114         return render_template("todo/topics.html", topics=query.items, total=total, \
115                         topic_count=topic_count, query=qb.search, show_discarded=qb.show_discarded, \
116                         next_url=next_url, prev_url=prev_url, page=page, page_max=query.pages, \
117                         n=num, sort_by=qb.order_by)
118
119
120 @bp.route("/todo/tags/")
121 @login_required
122 def tags():
123         qb    = QueryBuilder(request.args)
124         qb.setSortIfNone("score", "desc")
125         query = qb.buildPackageQuery()
126
127         tags = Tag.query.order_by(db.asc(Tag.title)).all()
128
129         return render_template("todo/tags.html", packages=query.all(), tags=tags)
130
131
132 @bp.route("/todo/metapackages/")
133 @login_required
134 def metapackages():
135         mpackages = MetaPackage.query \
136                         .filter(~ MetaPackage.packages.any(state=PackageState.APPROVED)) \
137                         .filter(MetaPackage.dependencies.any(optional=False)) \
138                         .order_by(db.asc(MetaPackage.name)).all()
139
140         return render_template("todo/metapackages.html", mpackages=mpackages)