aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-08-25 18:50:05 +0100
committerrubenwardy <rw@rubenwardy.com>2018-08-25 18:50:05 +0100
commit0210a3e6017732a4588190e22f39ff1b4fbc2e8c (patch)
treeb77297478030dd8652645ec500ca551dbd9aa986
parent36000b159273d06aba3fb56eab2cd78099b7e54a (diff)
downloadcheatdb-0210a3e6017732a4588190e22f39ff1b4fbc2e8c.tar.xz
Add I'm feeling lucky
-rw-r--r--app/templates/packages/list.html1
-rw-r--r--app/views/packages/__init__.py47
2 files changed, 33 insertions, 15 deletions
diff --git a/app/templates/packages/list.html b/app/templates/packages/list.html
index 53ffac0..5a9e58c 100644
--- a/app/templates/packages/list.html
+++ b/app/templates/packages/list.html
@@ -9,6 +9,7 @@
{% if type %}<input type="hidden" name="type" value="{{ type }}" />{% endif %}
<input type="text" name="q" value="{{ query or ''}}" />
<input type="submit" value="Search" />
+ <input type="submit" name="lucky" value="I'm feeling lucky" />
<p>
Found {{ packages_count }} packages.
diff --git a/app/views/packages/__init__.py b/app/views/packages/__init__.py
index 62170f5..303ce47 100644
--- a/app/views/packages/__init__.py
+++ b/app/views/packages/__init__.py
@@ -47,10 +47,11 @@ class QueryBuilder:
if len(types) > 0:
title = ", ".join([type.value + "s" for type in types])
- self.title = title
- self.types = types
+ self.title = title
+ self.types = types
self.search = request.args.get("q")
-
+ self.lucky = "lucky" in request.args
+ self.limit = 1 if self.lucky else None
def buildPackageQuery(self):
query = Package.query.filter_by(soft_deleted=False, approved=True)
@@ -63,8 +64,25 @@ class QueryBuilder:
query = query.order_by(db.desc(Package.score))
+ if self.limit:
+ query = query.limit(self.limit)
+
return query
+ def buildTopicQuery(self):
+ topics = ForumTopic.query \
+ .filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
+ .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
+ .filter(ForumTopic.title.ilike('%' + self.search + '%'))
+
+ if len(self.types) > 0:
+ topics = topics.filter(ForumTopic.type.in_(self.types))
+
+ if self.limit:
+ topics = topics.limit(self.limit)
+
+ return topics
+
@menu.register_menu(app, ".mods", "Mods", order=11, endpoint_arguments_constructor=lambda: { 'type': 'mod' })
@menu.register_menu(app, ".games", "Games", order=12, endpoint_arguments_constructor=lambda: { 'type': 'game' })
@menu.register_menu(app, ".txp", "Texture Packs", order=13, endpoint_arguments_constructor=lambda: { 'type': 'txp' })
@@ -77,6 +95,15 @@ def packages_page():
query = qb.buildPackageQuery()
title = qb.title
+ if qb.lucky:
+ package = query.first()
+ if package:
+ return redirect(package.getDetailsURL())
+
+ topic = qb.buildTopicQuery().first()
+ if topic:
+ return redirect("https://forum.minetest.net/viewtopic.php?t=" + str(topic.topic_id))
+
page = int(request.args.get("page") or 1)
num = min(42, int(request.args.get("n") or 100))
query = query.paginate(page, num, True)
@@ -90,18 +117,8 @@ def packages_page():
if query.has_prev else None
topics = None
-
- search = request.args.get("q")
- if search and not query.has_next:
- topics = ForumTopic.query \
- .filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
- .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
- .filter(ForumTopic.title.ilike('%' + search + '%'))
-
- if len(qb.types) > 0:
- topics = topics.filter(ForumTopic.type.in_(qb.types))
-
- topics = topics.all()
+ if qb.search and not query.has_next:
+ topics = qb.buildTopicQuery().all()
tags = Tag.query.all()
return render_template("packages/list.html", \