data.model.repository: audited for repo_kind usage

This commit is contained in:
Jimmy Zelinskie 2017-03-20 19:05:55 -04:00
parent 074c1bc4a8
commit d2a4c9d05a

View file

@ -26,10 +26,11 @@ def get_public_repo_visibility():
return _basequery.get_public_repo_visibility() return _basequery.get_public_repo_visibility()
def create_repository(namespace, name, creating_user, visibility='private'): def create_repository(namespace, name, creating_user, visibility='private', repo_kind='image'):
private = Visibility.get(name=visibility) private = Visibility.get(name=visibility)
namespace_user = User.get(username=namespace) namespace_user = User.get(username=namespace)
repo = Repository.create(name=name, visibility=private, namespace_user=namespace_user) repo = Repository.create(name=name, visibility=private, namespace_user=namespace_user,
kind=Repository.kind.get_id(repo_kind))
admin = Role.get(name='admin') admin = Role.get(name='admin')
yesterday = datetime.now() - timedelta(days=1) yesterday = datetime.now() - timedelta(days=1)
@ -103,7 +104,7 @@ def _get_gc_expiration_policies():
def get_random_gc_policy(): def get_random_gc_policy():
""" Return a single random policy from the database to use when garbage collecting. """ Return a single random policy from the database to use when garbage collecting.
""" """
return random.choice(_get_gc_expiration_policies()) return random.choice(_get_gc_expiration_policies())
@ -259,7 +260,7 @@ def unstar_repository(user, repository):
raise DataModelException('Star not found.') raise DataModelException('Star not found.')
def get_user_starred_repositories(user): def get_user_starred_repositories(user, repo_kind='image'):
""" Retrieves all of the repositories a user has starred. """ """ Retrieves all of the repositories a user has starred. """
query = (Repository query = (Repository
.select(Repository, User, Visibility, Repository.id.alias('rid')) .select(Repository, User, Visibility, Repository.id.alias('rid'))
@ -268,7 +269,8 @@ def get_user_starred_repositories(user):
.join(User) .join(User)
.switch(Repository) .switch(Repository)
.join(Visibility) .join(Visibility)
.where(Star.user == user)) .where(Star.user == user,
Repository.kind == Repository.kind.get_id(repo_kind)))
return query return query
@ -302,8 +304,8 @@ def get_when_last_modified(repository_ids):
return last_modified_map return last_modified_map
def get_visible_repositories(username, namespace=None, include_public=False, start_id=None, def get_visible_repositories(username, namespace=None, repo_kind='image', include_public=False,
limit=None): start_id=None, limit=None):
""" Returns the repositories visible to the given user (if any). """ Returns the repositories visible to the given user (if any).
""" """
if not include_public and not username: if not include_public and not username:
@ -321,7 +323,7 @@ def get_visible_repositories(username, namespace=None, include_public=False, sta
# Note: We only need the permissions table if we will filter based on a user's permissions. # Note: We only need the permissions table if we will filter based on a user's permissions.
query = query.switch(Repository).distinct().join(RepositoryPermission, JOIN_LEFT_OUTER) query = query.switch(Repository).distinct().join(RepositoryPermission, JOIN_LEFT_OUTER)
query = _basequery.filter_to_repos_for_user(query, username, namespace, include_public, query = _basequery.filter_to_repos_for_user(query, username, namespace, repo_kind, include_public,
start_id=start_id) start_id=start_id)
if limit is not None: if limit is not None:
@ -330,14 +332,15 @@ def get_visible_repositories(username, namespace=None, include_public=False, sta
return query return query
def get_filtered_matching_repositories(lookup_value, filter_username=None, offset=0, limit=25): def get_filtered_matching_repositories(lookup_value, filter_username=None, repo_kind='image',
offset=0, limit=25):
""" 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.
""" """
# Build the unfiltered search query. # Build the unfiltered search query.
unfiltered_query = _get_sorted_matching_repositories(lookup_value, unfiltered_query = _get_sorted_matching_repositories(lookup_value, repo_kind=repo_kind,
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.
@ -395,7 +398,7 @@ 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, include_private=False): def _get_sorted_matching_repositories(lookup_value, repo_kind='image', include_private=False):
""" 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.
@ -405,7 +408,8 @@ def _get_sorted_matching_repositories(lookup_value, include_private=False):
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(Repository.name.match(lookup_value) | Repository.description.match(lookup_value),
Repository.kind == Repository.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:
@ -438,7 +442,8 @@ def repository_is_public(namespace_name, repository_name):
.join(Namespace, on=(Repository.namespace_user == Namespace.id)) .join(Namespace, on=(Repository.namespace_user == Namespace.id))
.switch(Repository) .switch(Repository)
.join(Visibility) .join(Visibility)
.where(Namespace.username == namespace_name, Repository.name == repository_name, .where(Namespace.username == namespace_name,
Repository.name == repository_name,
Visibility.name == 'public') Visibility.name == 'public')
.get()) .get())
return True return True
@ -461,7 +466,8 @@ def get_email_authorized_for_repo(namespace, repository, email):
.select(RepositoryAuthorizedEmail, Repository, Namespace) .select(RepositoryAuthorizedEmail, Repository, Namespace)
.join(Repository) .join(Repository)
.join(Namespace, on=(Repository.namespace_user == Namespace.id)) .join(Namespace, on=(Repository.namespace_user == Namespace.id))
.where(Namespace.username == namespace, Repository.name == repository, .where(Namespace.username == namespace,
Repository.name == repository,
RepositoryAuthorizedEmail.email == email) RepositoryAuthorizedEmail.email == email)
.get()) .get())
except RepositoryAuthorizedEmail.DoesNotExist: except RepositoryAuthorizedEmail.DoesNotExist:
@ -495,7 +501,7 @@ def confirm_email_authorization_for_repo(code):
return found return found
def list_popular_public_repos(action_count_threshold, time_span): def list_popular_public_repos(action_count_threshold, time_span, repo_kind='image'):
cutoff = datetime.now() - time_span cutoff = datetime.now() - time_span
return (Repository return (Repository
.select(Namespace.username, Repository.name) .select(Namespace.username, Repository.name)
@ -503,7 +509,8 @@ def list_popular_public_repos(action_count_threshold, time_span):
.switch(Repository) .switch(Repository)
.join(RepositoryActionCount) .join(RepositoryActionCount)
.where(RepositoryActionCount.date >= cutoff, .where(RepositoryActionCount.date >= cutoff,
Repository.visibility == get_public_repo_visibility()) Repository.visibility == get_public_repo_visibility(),
Repository.kind == Repository.kind.get_id(repo_kind))
.group_by(RepositoryActionCount.repository, Repository.name, Namespace.username) .group_by(RepositoryActionCount.repository, Repository.name, Namespace.username)
.having(fn.Sum(RepositoryActionCount.count) >= action_count_threshold) .having(fn.Sum(RepositoryActionCount.count) >= action_count_threshold)
.tuples()) .tuples())