40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
from cnr.models.package_base import get_media_type, manifest_media_type
|
|
from peewee import prefetch
|
|
|
|
from data import model, oci_model
|
|
from data.database import Repository, Namespace, Tag, ManifestListManifest
|
|
|
|
|
|
def list_packages_query(namespace=None, media_type=None, search_query=None, username=None):
|
|
""" List and filter repository by search query. """
|
|
if search_query is not None:
|
|
repositories = model.repository.get_app_search(search_query,
|
|
username=username,
|
|
limit=50)
|
|
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')))
|
|
|
|
if namespace:
|
|
repo_query = (repo_query
|
|
.where(Namespace.username == namespace))
|
|
|
|
tag_query = (Tag
|
|
.select()
|
|
.where(Tag.tag_kind == Tag.tag_kind.get_id('release'))
|
|
.order_by(Tag.lifetime_start))
|
|
|
|
if media_type:
|
|
tag_query = oci_model.tag.filter_tags_by_media_type(tag_query, media_type)
|
|
|
|
tag_query = oci_model.tag.tag_alive_oci(tag_query)
|
|
query = prefetch(repo_query, tag_query)
|
|
|
|
return query
|