2020-07-17 22:18:27 +02:00
|
|
|
from .models import db, PackageType, Package, ForumTopic, License, MinetestRelease, PackageRelease, User, Tag, Tags, ContentWarning
|
2020-05-19 17:39:39 +02:00
|
|
|
from .utils import isNo, isYes, get_int_or_abort
|
2019-01-08 18:17:36 +01:00
|
|
|
from sqlalchemy.sql.expression import func
|
|
|
|
from flask import abort
|
2019-01-28 22:31:08 +01:00
|
|
|
from sqlalchemy import or_
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
class QueryBuilder:
|
|
|
|
title = None
|
|
|
|
types = None
|
|
|
|
search = None
|
|
|
|
|
|
|
|
def __init__(self, args):
|
|
|
|
title = "Packages"
|
|
|
|
|
|
|
|
# Get request types
|
|
|
|
types = args.getlist("type")
|
|
|
|
types = [PackageType.get(tname) for tname in types]
|
|
|
|
types = [type for type in types if type is not None]
|
|
|
|
if len(types) > 0:
|
|
|
|
title = ", ".join([type.value + "s" for type in types])
|
|
|
|
|
2020-04-07 19:23:06 +02:00
|
|
|
# Get tags types
|
|
|
|
tags = args.getlist("tag")
|
|
|
|
tags = [Tag.query.filter_by(name=tname).first() for tname in tags]
|
|
|
|
tags = [tag for tag in tags if tag is not None]
|
|
|
|
|
2020-07-15 20:06:00 +02:00
|
|
|
# Hide
|
2019-02-03 13:59:33 +01:00
|
|
|
hide_flags = args.getlist("hide")
|
|
|
|
|
2020-07-17 22:18:27 +02:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
self.title = title
|
|
|
|
self.types = types
|
2020-04-07 19:23:06 +02:00
|
|
|
self.tags = tags
|
2020-03-28 19:09:20 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
self.random = "random" in args
|
2019-11-21 22:43:58 +01:00
|
|
|
self.lucky = "lucky" in args
|
2019-01-08 18:17:36 +01:00
|
|
|
self.limit = 1 if self.lucky else None
|
2019-03-29 20:45:29 +01:00
|
|
|
self.order_by = args.get("sort")
|
2020-07-15 16:44:59 +02:00
|
|
|
self.order_dir = args.get("order") or "desc"
|
2020-03-28 19:09:20 +01:00
|
|
|
|
2020-07-17 22:18:27 +02:00
|
|
|
self.hide_nonfree = "nonfree" in hide_flags
|
|
|
|
self.hide_flags = set(hide_flags)
|
|
|
|
self.hide_flags.discard("nonfree")
|
|
|
|
|
2020-03-28 19:09:20 +01:00
|
|
|
# Filters
|
|
|
|
|
|
|
|
self.search = args.get("q")
|
2020-06-03 18:17:21 +02:00
|
|
|
self.minetest_version = args.get("engine_version")
|
|
|
|
self.protocol_version = get_int_or_abort(args.get("protocol_version"))
|
2020-03-28 19:09:20 +01:00
|
|
|
self.author = args.get("author")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-03-29 20:45:29 +01:00
|
|
|
self.show_discarded = isYes(args.get("show_discarded"))
|
|
|
|
self.show_added = args.get("show_added")
|
|
|
|
if self.show_added is not None:
|
|
|
|
self.show_added = isYes(self.show_added)
|
|
|
|
|
2019-01-08 18:37:33 +01:00
|
|
|
if self.search is not None and self.search.strip() == "":
|
|
|
|
self.search = None
|
|
|
|
|
2020-07-15 16:44:59 +02:00
|
|
|
def setSortIfNone(self, name, dir="desc"):
|
2019-03-29 20:45:29 +01:00
|
|
|
if self.order_by is None:
|
|
|
|
self.order_by = name
|
2020-07-15 01:21:20 +02:00
|
|
|
self.order_dir = dir
|
2019-03-29 20:45:29 +01:00
|
|
|
|
2019-03-13 15:37:20 +01:00
|
|
|
def getMinetestVersion(self):
|
2020-06-03 18:17:21 +02:00
|
|
|
if not self.protocol_version and not self.minetest_version:
|
2019-03-13 15:37:20 +01:00
|
|
|
return None
|
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
return MinetestRelease.get(self.minetest_version, self.protocol_version)
|
2019-03-13 15:37:20 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
def buildPackageQuery(self):
|
2020-07-18 02:22:52 +02:00
|
|
|
query = None
|
|
|
|
if self.order_by == "last_release":
|
|
|
|
query = db.session.query(Package).select_from(PackageRelease).join(Package) \
|
|
|
|
.filter_by(soft_deleted=False, approved=True)
|
|
|
|
else:
|
|
|
|
query = Package.query.filter_by(soft_deleted=False, approved=True)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if len(self.types) > 0:
|
|
|
|
query = query.filter(Package.type.in_(self.types))
|
|
|
|
|
2019-01-08 18:37:33 +01:00
|
|
|
if self.search:
|
2020-07-12 18:56:06 +02:00
|
|
|
query = query.search(self.search, sort=self.order_by is None)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if self.random:
|
|
|
|
query = query.order_by(func.random())
|
|
|
|
else:
|
|
|
|
to_order = None
|
2020-07-12 18:52:15 +02:00
|
|
|
if self.order_by is None and self.search:
|
|
|
|
pass
|
2020-07-12 18:56:06 +02:00
|
|
|
elif self.order_by is None or self.order_by == "score":
|
2019-01-08 18:17:36 +01:00
|
|
|
to_order = Package.score
|
2020-07-10 23:32:52 +02:00
|
|
|
elif self.order_by == "name":
|
|
|
|
to_order = Package.name
|
|
|
|
elif self.order_by == "title":
|
|
|
|
to_order = Package.title
|
|
|
|
elif self.order_by == "downloads":
|
|
|
|
to_order = Package.downloads
|
|
|
|
elif self.order_by == "created_at" or self.order_by == "date":
|
2019-01-08 18:17:36 +01:00
|
|
|
to_order = Package.created_at
|
2020-07-18 02:48:21 +02:00
|
|
|
elif self.order_by == "approved_at" or self.order_by == "date":
|
|
|
|
to_order = Package.approved_at
|
2020-07-18 02:22:52 +02:00
|
|
|
elif self.order_by == "last_release":
|
|
|
|
to_order = PackageRelease.releaseDate
|
2019-01-08 18:17:36 +01:00
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
2020-07-12 18:56:06 +02:00
|
|
|
if to_order:
|
|
|
|
if self.order_dir == "asc":
|
|
|
|
to_order = db.asc(to_order)
|
|
|
|
elif self.order_dir == "desc":
|
|
|
|
to_order = db.desc(to_order)
|
|
|
|
else:
|
|
|
|
abort(400)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-07-12 18:56:06 +02:00
|
|
|
query = query.order_by(to_order)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2020-03-28 19:09:20 +01:00
|
|
|
if self.author:
|
|
|
|
author = User.query.filter_by(username=self.author).first()
|
|
|
|
if not author:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
query = query.filter_by(author=author)
|
|
|
|
|
2020-04-07 19:23:06 +02:00
|
|
|
for tag in self.tags:
|
|
|
|
query = query.filter(Package.tags.any(Tag.id == tag.id))
|
|
|
|
|
2020-07-17 22:18:27 +02:00
|
|
|
if "android_default" in self.hide_flags:
|
|
|
|
query = query.filter(~ Package.content_warnings.any())
|
|
|
|
else:
|
|
|
|
for flag in self.hide_flags:
|
|
|
|
warning = ContentWarning.query.filter_by(name=flag).first()
|
|
|
|
if warning:
|
|
|
|
query = query.filter(~ Package.content_warnings.any(ContentWarning.id == warning.id))
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if self.hide_nonfree:
|
|
|
|
query = query.filter(Package.license.has(License.is_foss == True))
|
|
|
|
query = query.filter(Package.media_license.has(License.is_foss == True))
|
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
if self.protocol_version or self.minetest_version:
|
2019-03-13 15:37:20 +01:00
|
|
|
version = self.getMinetestVersion()
|
2020-06-03 18:17:21 +02:00
|
|
|
if version:
|
|
|
|
query = query.join(Package.releases) \
|
|
|
|
.filter(PackageRelease.approved==True) \
|
|
|
|
.filter(or_(PackageRelease.min_rel_id==None, PackageRelease.min_rel_id<=version.id)) \
|
|
|
|
.filter(or_(PackageRelease.max_rel_id==None, PackageRelease.max_rel_id>=version.id))
|
2019-01-28 22:31:08 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if self.limit:
|
|
|
|
query = query.limit(self.limit)
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
2019-03-29 20:45:29 +01:00
|
|
|
def buildTopicQuery(self, show_added=False):
|
|
|
|
query = ForumTopic.query
|
|
|
|
|
|
|
|
if not self.show_discarded:
|
|
|
|
query = query.filter_by(discarded=False)
|
|
|
|
|
|
|
|
show_added = self.show_added == True or (self.show_added is None and show_added)
|
|
|
|
if not show_added:
|
|
|
|
query = query.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id))
|
|
|
|
|
|
|
|
if self.order_by is None or self.order_by == "name":
|
|
|
|
query = query.order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
|
|
|
|
elif self.order_by == "views":
|
|
|
|
query = query.order_by(db.desc(ForumTopic.views))
|
2020-07-10 23:32:52 +02:00
|
|
|
elif self.order_by == "created_at" or self.order_by == "date":
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.order_by(db.asc(ForumTopic.created_at))
|
2019-01-08 18:37:33 +01:00
|
|
|
|
|
|
|
if self.search:
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.filter(ForumTopic.title.ilike('%' + self.search + '%'))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if len(self.types) > 0:
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.filter(ForumTopic.type.in_(self.types))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if self.limit:
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.limit(self.limit)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-03-29 20:45:29 +01:00
|
|
|
return query
|