Skip to content

Commit

Permalink
fix: Bad use of get_queryset for dashboards (#21726)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Apr 22, 2024
1 parent 594788d commit e4c5828
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
14 changes: 8 additions & 6 deletions posthog/api/dashboards/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,23 +398,25 @@ class DashboardsViewSet(
viewsets.ModelViewSet,
):
scope_object = "dashboard"
queryset = Dashboard.objects.order_by("name")
queryset = Dashboard.objects_including_soft_deleted.order_by("name")
permission_classes = [CanEditDashboard]

def get_serializer_class(self) -> Type[BaseSerializer]:
return DashboardBasicSerializer if self.action == "list" else DashboardSerializer

def get_queryset(self) -> QuerySet:
if (
queryset = super().get_queryset()

include_deleted = (
self.action == "partial_update"
and "deleted" in self.request.data
and not self.request.data.get("deleted")
and len(self.request.data) == 1
):
)

if not include_deleted:
# a dashboard can be un-deleted by patching {"deleted": False}
queryset = Dashboard.objects_including_soft_deleted
else:
queryset = super().get_queryset()
queryset = queryset.filter(deleted=False)

queryset = queryset.prefetch_related("sharingconfiguration_set").select_related(
"team__organization",
Expand Down
28 changes: 14 additions & 14 deletions posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@
INNER JOIN "posthog_team" ON ("posthog_dashboard"."team_id" = "posthog_team"."id")
INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboard"."created_by_id" = "posthog_user"."id")
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted"
AND "posthog_dashboard"."id" = 2)
LIMIT 21
'''
Expand Down Expand Up @@ -2615,8 +2615,8 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_dashboard"
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2)
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted")
'''
# ---
# name: TestDashboard.test_listing_dashboards_is_not_nplus1.55
Expand Down Expand Up @@ -2738,8 +2738,8 @@
INNER JOIN "posthog_team" ON ("posthog_dashboard"."team_id" = "posthog_team"."id")
INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboard"."created_by_id" = "posthog_user"."id")
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2)
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted")
ORDER BY "posthog_dashboard"."name" ASC
LIMIT 300
'''
Expand Down Expand Up @@ -6929,8 +6929,8 @@
INNER JOIN "posthog_team" ON ("posthog_dashboard"."team_id" = "posthog_team"."id")
INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboard"."created_by_id" = "posthog_user"."id")
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted"
AND "posthog_dashboard"."id" = 2)
LIMIT 21
'''
Expand Down Expand Up @@ -10687,8 +10687,8 @@
INNER JOIN "posthog_team" ON ("posthog_dashboard"."team_id" = "posthog_team"."id")
INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboard"."created_by_id" = "posthog_user"."id")
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted"
AND "posthog_dashboard"."id" = 2)
LIMIT 21
'''
Expand Down Expand Up @@ -11797,8 +11797,8 @@
'''
SELECT COUNT(*) AS "__count"
FROM "posthog_dashboard"
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2)
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted")
'''
# ---
# name: TestDashboard.test_retrieve_dashboard_list.3
Expand Down Expand Up @@ -11931,8 +11931,8 @@
INNER JOIN "posthog_team" ON ("posthog_dashboard"."team_id" = "posthog_team"."id")
INNER JOIN "posthog_organization" ON ("posthog_team"."organization_id" = "posthog_organization"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_dashboard"."created_by_id" = "posthog_user"."id")
WHERE (NOT ("posthog_dashboard"."deleted")
AND "posthog_dashboard"."team_id" = 2)
WHERE ("posthog_dashboard"."team_id" = 2
AND NOT "posthog_dashboard"."deleted")
ORDER BY "posthog_dashboard"."name" ASC
LIMIT 100
'''
Expand Down

0 comments on commit e4c5828

Please sign in to comment.