data.model.repository: add back search fields
This commit is contained in:
parent
2bdd3d4fa1
commit
d20ff785e6
2 changed files with 27 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta, datetime
|
||||||
from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError
|
from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError
|
||||||
from cachetools import ttl_cache
|
from cachetools import ttl_cache
|
||||||
|
@ -17,6 +18,7 @@ from util.itertoolrecipes import take
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
SEARCH_FIELDS = Enum("SearchFields", ["name", "description"])
|
||||||
|
|
||||||
|
|
||||||
def get_repo_kind_name(repo):
|
def get_repo_kind_name(repo):
|
||||||
|
@ -356,20 +358,26 @@ def get_app_repository(namespace_name, repository_name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_app_search(lookup, username=None, limit=50):
|
def get_app_search(lookup, search_fields=None, username=None, limit=50):
|
||||||
|
if search_fields is None:
|
||||||
|
search_fields = set([SEARCH_FIELDS.description.name])
|
||||||
return get_filtered_matching_repositories(lookup, filter_username=username,
|
return get_filtered_matching_repositories(lookup, filter_username=username,
|
||||||
|
search_fields=search_fields,
|
||||||
repo_kind='application', offset=0, limit=limit)
|
repo_kind='application', offset=0, limit=limit)
|
||||||
|
|
||||||
|
|
||||||
def get_filtered_matching_repositories(lookup_value, filter_username=None, repo_kind='image',
|
def get_filtered_matching_repositories(lookup_value, filter_username=None, repo_kind='image',
|
||||||
offset=0, limit=25):
|
offset=0, limit=25, search_fields=None):
|
||||||
""" Returns an iterator of all repositories matching the given lookup value, with optional
|
""" Returns an iterator of all repositories matching the given lookup value, with optional
|
||||||
filtering to a specific user. If the user is unspecified, only public repositories will
|
filtering to a specific user. If the user is unspecified, only public repositories will
|
||||||
be returned.
|
be returned.
|
||||||
"""
|
"""
|
||||||
|
if search_fields is None:
|
||||||
|
search_fields = set([SEARCH_FIELDS.description.name, SEARCH_FIELDS.name.name])
|
||||||
|
|
||||||
# Build the unfiltered search query.
|
# Build the unfiltered search query.
|
||||||
unfiltered_query = _get_sorted_matching_repositories(lookup_value, repo_kind=repo_kind,
|
unfiltered_query = _get_sorted_matching_repositories(lookup_value, repo_kind=repo_kind,
|
||||||
|
search_fields=search_fields,
|
||||||
include_private=filter_username is not None)
|
include_private=filter_username is not None)
|
||||||
|
|
||||||
# Add a filter to the iterator, if necessary.
|
# Add a filter to the iterator, if necessary.
|
||||||
|
@ -427,18 +435,29 @@ def _filter_repositories_visible_to_username(unfiltered_query, filter_username,
|
||||||
iteration_count = iteration_count + 1
|
iteration_count = iteration_count + 1
|
||||||
|
|
||||||
|
|
||||||
def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_private=False):
|
def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_private=False,
|
||||||
|
search_fields=None):
|
||||||
""" Returns a query of repositories matching the given lookup string, with optional inclusion of
|
""" Returns a query of repositories matching the given lookup string, with optional inclusion of
|
||||||
private repositories. Note that this method does *not* filter results based on visibility
|
private repositories. Note that this method does *not* filter results based on visibility
|
||||||
to users.
|
to users.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if search_fields is None:
|
||||||
|
search_fields = set([SEARCH_FIELDS.description.name, SEARCH_FIELDS.name.name])
|
||||||
|
|
||||||
|
# Always search at least on name (init clause)
|
||||||
|
clause = Repository.name.match(lookup_value)
|
||||||
|
|
||||||
|
if SEARCH_FIELDS.description.name in search_fields:
|
||||||
|
clause = Repository.description.match(lookup_value) | clause
|
||||||
|
|
||||||
last_week = datetime.now() - timedelta(weeks=1)
|
last_week = datetime.now() - timedelta(weeks=1)
|
||||||
|
|
||||||
query = (Repository
|
query = (Repository
|
||||||
.select(Repository, Namespace)
|
.select(Repository, Namespace)
|
||||||
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
||||||
.where(Repository.name.match(lookup_value) | Repository.description.match(lookup_value),
|
.where(clause,
|
||||||
Repository.kind == Repository.kind.get_id(repo_kind))
|
Repository.repo_kind == Repository.repo_kind.get_id(repo_kind))
|
||||||
.group_by(Repository.id, Namespace.id))
|
.group_by(Repository.id, Namespace.id))
|
||||||
|
|
||||||
if not include_private:
|
if not include_private:
|
||||||
|
|
|
@ -9,9 +9,12 @@ from data.oci_model import tag as tag_model
|
||||||
|
|
||||||
def list_packages_query(namespace=None, media_type=None, search_query=None, username=None):
|
def list_packages_query(namespace=None, media_type=None, search_query=None, username=None):
|
||||||
""" List and filter repository by search query. """
|
""" List and filter repository by search query. """
|
||||||
|
fields = [model.repository.SEARCH_FIELDS.name.name]
|
||||||
|
|
||||||
if search_query is not None:
|
if search_query is not None:
|
||||||
repositories = model.repository.get_app_search(search_query,
|
repositories = model.repository.get_app_search(search_query,
|
||||||
username=username,
|
username=username,
|
||||||
|
search_fields=fields,
|
||||||
limit=50)
|
limit=50)
|
||||||
repo_query = (Repository
|
repo_query = (Repository
|
||||||
.select(Repository, Namespace.username)
|
.select(Repository, Namespace.username)
|
||||||
|
|
Reference in a new issue