Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace a confusing SQL query with a slightly less confusing one #363

Merged
merged 1 commit into from Feb 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions registry/quilt_server/views.py
Expand Up @@ -1423,8 +1423,6 @@ def profile():
plan = None
have_cc = None

public_access = sa.orm.aliased(Access)

# Check for outstanding package sharing invitations
invitations = (
db.session.query(Invitation, Package)
Expand All @@ -1439,13 +1437,19 @@ def profile():
if invitations:
db.session.commit()

# We want to show only the packages owned by or explicitly shared with the user -
# but also show whether they're public, in case a package is both public and shared with the user.
# So do a "GROUP BY" to get the public info, then "HAVING" to filter out packages that aren't shared.
packages = (
db.session.query(Package, public_access.user.isnot(None))
db.session.query(Package, sa.func.bool_or(Access.user == PUBLIC))
.join(Package.access)
.filter(Access.user == g.auth.user)
.outerjoin(public_access, sa.and_(
Package.id == public_access.package_id, public_access.user == PUBLIC))
.order_by(Package.owner, Package.name)
.filter(Access.user.in_([g.auth.user, PUBLIC]))
.group_by(Package.id)
.order_by(
sa.func.lower(Package.owner),
sa.func.lower(Package.name)
)
.having(sa.func.bool_or(Access.user == g.auth.user))
.all()
)

Expand All @@ -1455,15 +1459,15 @@ def profile():
dict(
owner=package.owner,
name=package.name,
is_public=bool(is_public)
is_public=is_public,
)
for package, is_public in packages if package.owner == g.auth.user
],
shared=[
dict(
owner=package.owner,
name=package.name,
is_public=bool(is_public)
is_public=is_public,
)
for package, is_public in packages if package.owner != g.auth.user
],
Expand Down