diff --git a/data/model/_basequery.py b/data/model/_basequery.py index bc5ab9748..59de01a3b 100644 --- a/data/model/_basequery.py +++ b/data/model/_basequery.py @@ -1,4 +1,4 @@ -from peewee import Clause, SQL, fn +from peewee import fn from cachetools import lru_cache from data.model import DataModelException @@ -6,18 +6,6 @@ from data.database import (Repository, User, Team, TeamMember, RepositoryPermiss Namespace, Visibility, ImageStorage, Image, db_for_update) -def prefix_search(field, prefix_query): - """ Returns the wildcard match for searching for the given prefix query. """ - # Escape the known wildcard characters. - prefix_query = (prefix_query - .replace('!', '!!') - .replace('%', '!%') - .replace('_', '!_') - .replace('[', '![')) - - return field ** Clause(prefix_query + '%', SQL("ESCAPE '!'")) - - def get_existing_repository(namespace_name, repository_name, for_update=False): query = (Repository .select(Repository, Namespace) diff --git a/data/model/label.py b/data/model/label.py index 166acedec..467eca86f 100644 --- a/data/model/label.py +++ b/data/model/label.py @@ -4,7 +4,7 @@ from cachetools import lru_cache from data.database import Label, TagManifestLabel, MediaType, LabelSourceType, db_transaction from data.model import InvalidLabelKeyException, InvalidMediaTypeException, DataModelException -from data.model._basequery import prefix_search +from data.text import prefix_search from util.validation import validate_label_key from util.validation import is_json diff --git a/data/model/repository.py b/data/model/repository.py index 11bcafcf1..5621266dc 100644 --- a/data/model/repository.py +++ b/data/model/repository.py @@ -12,6 +12,7 @@ from data.database import (Repository, Namespace, RepositoryTag, Star, Image, Im Role, RepositoryAuthorizedEmail, TagManifest, DerivedStorageForImage, Label, TagManifestLabel, db_for_update, get_epoch_timestamp, db_random_func, db_concat_func) +from data.text import prefix_search logger = logging.getLogger(__name__) diff --git a/data/model/team.py b/data/model/team.py index 64d877c5b..738a27407 100644 --- a/data/model/team.py +++ b/data/model/team.py @@ -1,6 +1,7 @@ from data.database import Team, TeamMember, TeamRole, User, TeamMemberInvite, RepositoryPermission from data.model import (DataModelException, InvalidTeamException, UserAlreadyInTeam, InvalidTeamMemberException, user, _basequery) +from data.text import prefix_search from util.validation import validate_username from peewee import fn, JOIN_LEFT_OUTER from util.morecollections import AttrDict @@ -137,7 +138,7 @@ def add_or_invite_to_team(inviter, team, user_obj=None, email=None, requires_inv def get_matching_user_teams(team_prefix, user_obj, limit=10): - team_prefix_search = _basequery.prefix_search(Team.name, team_prefix) + team_prefix_search = prefix_search(Team.name, team_prefix) query = (Team .select() .join(User) @@ -163,7 +164,7 @@ def get_organization_team(orgname, teamname): def get_matching_admined_teams(team_prefix, user_obj, limit=10): - team_prefix_search = _basequery.prefix_search(Team.name, team_prefix) + team_prefix_search = prefix_search(Team.name, team_prefix) admined_orgs = (_basequery.get_user_organizations(user_obj.username) .switch(Team) .join(TeamRole) @@ -182,7 +183,7 @@ def get_matching_admined_teams(team_prefix, user_obj, limit=10): def get_matching_teams(team_prefix, organization): - team_prefix_search = _basequery.prefix_search(Team.name, team_prefix) + team_prefix_search = prefix_search(Team.name, team_prefix) query = Team.select().where(team_prefix_search, Team.organization == organization) return query.limit(10) diff --git a/data/model/user.py b/data/model/user.py index ddb27400d..5672dc6ac 100644 --- a/data/model/user.py +++ b/data/model/user.py @@ -18,6 +18,7 @@ from data.model import (DataModelException, InvalidPasswordException, InvalidRob InvalidUsernameException, InvalidEmailAddressException, TooManyLoginAttemptsException, db_transaction, notification, config, repository, _basequery) +from data.text import prefix_search from util.names import format_robot_username, parse_robot_username from util.validation import (validate_username, validate_email, validate_password, INVALID_PASSWORD_MESSAGE) @@ -259,10 +260,10 @@ def get_matching_robots(name_prefix, username, limit=10): prefix_checks = False for org in admined_orgs: - org_search = _basequery.prefix_search(User.username, org.username + '+' + name_prefix) + org_search = prefix_search(User.username, org.username + '+' + name_prefix) prefix_checks = prefix_checks | org_search - user_search = _basequery.prefix_search(User.username, username + '+' + name_prefix) + user_search = prefix_search(User.username, username + '+' + name_prefix) prefix_checks = prefix_checks | user_search return User.select().where(prefix_checks).limit(limit) @@ -562,7 +563,7 @@ def get_user_or_org_by_customer_id(customer_id): def get_matching_user_namespaces(namespace_prefix, username, limit=10): - namespace_search = _basequery.prefix_search(Namespace.username, namespace_prefix) + namespace_search = prefix_search(Namespace.username, namespace_prefix) base_query = (Namespace .select() .distinct() @@ -573,12 +574,12 @@ def get_matching_user_namespaces(namespace_prefix, username, limit=10): return _basequery.filter_to_repos_for_user(base_query, username).limit(limit) def get_matching_users(username_prefix, robot_namespace=None, organization=None, limit=20): - user_search = _basequery.prefix_search(User.username, username_prefix) + user_search = prefix_search(User.username, username_prefix) direct_user_query = (user_search & (User.organization == False) & (User.robot == False)) if robot_namespace: robot_prefix = format_robot_username(robot_namespace, username_prefix) - robot_search = _basequery.prefix_search(User.username, robot_prefix) + robot_search = prefix_search(User.username, robot_prefix) direct_user_query = ((robot_search & (User.robot == True)) | direct_user_query) query = (User