2017-08-10 00:45:46 +00:00
|
|
|
from cnr.models.package_base import get_media_type, manifest_media_type
|
2017-03-23 01:51:28 +00:00
|
|
|
from peewee import prefetch
|
|
|
|
|
2017-03-23 04:21:21 +00:00
|
|
|
|
|
|
|
from data import model
|
2018-05-24 21:54:51 +00:00
|
|
|
from data.database import Repository, Namespace
|
2018-05-23 21:08:00 +00:00
|
|
|
from data.appr_model import tag as tag_model
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
def list_packages_query(models_ref, namespace=None, media_type=None, search_query=None,
|
|
|
|
username=None):
|
2017-03-23 01:51:28 +00:00
|
|
|
""" List and filter repository by search query. """
|
2018-05-24 21:54:51 +00:00
|
|
|
Tag = models_ref.Tag
|
2017-03-23 14:46:04 +00:00
|
|
|
|
2019-02-14 20:25:41 +00:00
|
|
|
if username and not search_query:
|
|
|
|
repositories = model.repository.get_visible_repositories(username,
|
|
|
|
kind_filter='application',
|
|
|
|
include_public=True,
|
|
|
|
namespace=namespace,
|
|
|
|
limit=50)
|
2017-06-05 20:59:56 +00:00
|
|
|
if not repositories:
|
|
|
|
return []
|
|
|
|
|
2017-03-23 01:51:28 +00:00
|
|
|
repo_query = (Repository
|
|
|
|
.select(Repository, Namespace.username)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
2019-02-14 20:25:41 +00:00
|
|
|
.where(Repository.id << [repo.rid for repo in repositories]))
|
|
|
|
|
|
|
|
if namespace:
|
|
|
|
repo_query = (repo_query
|
|
|
|
.where(Namespace.username == namespace))
|
2017-03-23 01:51:28 +00:00
|
|
|
else:
|
2019-02-14 20:25:41 +00:00
|
|
|
if search_query is not None:
|
|
|
|
fields = [model.repository.SEARCH_FIELDS.name.name]
|
|
|
|
repositories = model.repository.get_app_search(search_query,
|
|
|
|
username=username,
|
|
|
|
search_fields=fields,
|
|
|
|
limit=50)
|
|
|
|
if not repositories:
|
|
|
|
return []
|
|
|
|
|
|
|
|
repo_query = (Repository
|
|
|
|
.select(Repository, Namespace.username)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
|
|
|
.where(Repository.id << [repo.id for repo in repositories]))
|
|
|
|
else:
|
|
|
|
repo_query = (Repository
|
|
|
|
.select(Repository, Namespace.username)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
|
|
|
.where(Repository.visibility == model.repository.get_public_repo_visibility(),
|
|
|
|
Repository.kind == Repository.kind.get_id('application')))
|
2017-03-23 01:51:28 +00:00
|
|
|
|
2019-02-14 20:25:41 +00:00
|
|
|
if namespace:
|
|
|
|
repo_query = (repo_query
|
|
|
|
.where(Namespace.username == namespace))
|
2017-03-23 01:51:28 +00:00
|
|
|
|
|
|
|
tag_query = (Tag
|
|
|
|
.select()
|
|
|
|
.where(Tag.tag_kind == Tag.tag_kind.get_id('release'))
|
|
|
|
.order_by(Tag.lifetime_start))
|
|
|
|
|
|
|
|
if media_type:
|
2018-05-24 21:54:51 +00:00
|
|
|
tag_query = tag_model.filter_tags_by_media_type(tag_query, media_type, models_ref)
|
2017-03-23 01:51:28 +00:00
|
|
|
|
2018-05-24 21:54:51 +00:00
|
|
|
tag_query = tag_model.tag_is_alive(tag_query, Tag)
|
2017-03-23 01:51:28 +00:00
|
|
|
query = prefetch(repo_query, tag_query)
|
|
|
|
return query
|