Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ami/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def visible_for_user(self, user: User | AnonymousUser) -> QuerySet:
if not is_anonymous:
filter_condition |= Q(owner=user) | Q(members=user)

return self.filter(filter_condition).distinct()
if is_anonymous:
return self.filter(filter_condition)

visible_project_ids = self.filter(filter_condition).order_by().values_list("pk", flat=True)
return self.filter(pk__in=visible_project_ids)
Comment on lines +72 to +73

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 50a4909: anonymous project visibility now returns the simple draft=False filter directly, avoiding the unnecessary self-referential primary-key subquery.


# For models related to Project
project_accessor = model.get_project_accessor()
Expand All @@ -85,7 +89,8 @@ def visible_for_user(self, user: User | AnonymousUser) -> QuerySet:
if not is_anonymous:
filter_condition |= Q(**{f"{project_field}owner": user}) | Q(**{f"{project_field}members": user})

return self.filter(filter_condition).distinct()
visible_object_ids = self.filter(filter_condition).order_by().values_list("pk", flat=True)
return self.filter(pk__in=visible_object_ids)


class BaseModel(models.Model):
Expand Down
9 changes: 9 additions & 0 deletions ami/main/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,15 @@ def test_deployment_list_draft_project(self):
ids = [d["id"] for d in response.data["results"]]
assert self.deployment.pk not in ids

def test_visible_for_user_deduplicates_captures_in_a_primary_key_subquery(self):
self.project.members.add(self.outsider)
queryset = SourceImage.objects.visible_for_user(self.owner)

self.assertIn(self.deployment.captures.first(), queryset)
capture_ids = list(queryset.values_list("pk", flat=True))
self.assertEqual(len(capture_ids), len(set(capture_ids)))
self.assertNotIn("SELECT DISTINCT", str(queryset.query))
Comment thread
coderabbitai[bot] marked this conversation as resolved.

def test_visible_for_user_across_all_models(self):
all_users = {
"superuser": self.superuser,
Expand Down