-
Notifications
You must be signed in to change notification settings - Fork 660
UN-3769 [FEAT] Sortable resource lists with co-owner ownership #2200
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
Open
kirtimanmishrazipstack
wants to merge
13
commits into
main
Choose a base branch
from
UN-3769-Show-co-owner-ownership-in-unstract-resource-list-views
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
45cafd5
UN-3769 [FEAT] Sortable resource list table with server-side sort, se…
kirtimanmishrazipstack 821fbee
UN-3769 [MISC] Merge main into UN-3769; keep ListView removed
kirtimanmishrazipstack 049a129
UN-3769 [FIX] Dedupe list fetch into shared helpers; fix stale-respon…
kirtimanmishrazipstack 4bfae22
UN-3769 [FIX] Collapse duplicated list-page preamble to clear duplica…
kirtimanmishrazipstack b76e2e2
UN-3769 [FIX] Gate list-fetch catch/finally on the request sequence
kirtimanmishrazipstack c435b06
UN-3769 [FIX] Show a retryable error on list-fetch failure; use codeP…
kirtimanmishrazipstack f6c0160
UN-3769 [FIX] Track "Me" in the Owned By cell by displayed owner, not…
kirtimanmishrazipstack db24151
UN-3769 [FIX] Gate delete-failure loading clear on the request sequence
kirtimanmishrazipstack 52d9d11
UN-3769 [FIX] Keep adapter delete out of the shared list-loading state
kirtimanmishrazipstack 5947bcc
UN-3769 [FIX] Refresh the current list view, not the params captured …
kirtimanmishrazipstack 83a460b
UN-3769 [FIX] Fix list fetch state handling and dead pagination
kirtimanmishrazipstack db133f5
UN-3769 [FIX] Address self-review on resource list views
kirtimanmishrazipstack 7c35db4
Merge branch 'main' into UN-3769-Show-co-owner-ownership-in-unstract-…
kirtimanmishrazipstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Shared list-query helpers for resource list endpoints. | ||
|
|
||
| Provides owner-inclusive ``?search`` and per-column sorting (name / owner / | ||
| created) with a ``pk__in`` re-wrap so managers ending in Postgres ``DISTINCT | ||
| ON`` can still be ordered by an arbitrary column. | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| from django.db.models import Model, Q, QuerySet | ||
| from rest_framework.request import Request | ||
|
|
||
| # ``name`` maps to the resource-specific name field each caller passes; | ||
| # owner/created are shared across every list endpoint. | ||
| OWNER_SORT_FIELD = "created_by__email" | ||
| CREATED_SORT_FIELD = "created_at" | ||
|
|
||
|
|
||
| def apply_search_and_sort( | ||
| queryset: QuerySet[Any], | ||
| *, | ||
| model: type[Model], | ||
| name_field: str, | ||
| request: Request, | ||
| select_related: tuple[str, ...] = (), | ||
| prefetch_related: tuple[str, ...] = (), | ||
| default_sort_by: str = "name", | ||
| ) -> QuerySet[Any]: | ||
| """Apply owner-inclusive ``?search`` and ``?sort_by``/``?order`` to a list | ||
| queryset. | ||
|
|
||
| ``sort_by`` is ``name`` | ``owner`` | ``created`` (default ``name``); | ||
| ``order`` is ``asc`` | ``desc`` (default ``asc``). The queryset is re-wrapped | ||
| via ``pk__in`` to drop any ``DISTINCT ON`` (so ordering by a non-distinct | ||
| column is legal) and a ``pk`` tiebreaker is appended for stable pagination. | ||
| ``select_related`` / ``prefetch_related`` are re-attached to the re-wrapped | ||
| queryset to keep the list free of N+1 owner/co-owner lookups. | ||
|
|
||
| Args: | ||
| queryset: The already org-scoped, ``for_user``-filtered list queryset. | ||
| model: The concrete resource model, used to re-wrap via ``pk__in``. | ||
| name_field: The resource's name column (e.g. ``adapter_name``). | ||
| request: DRF request carrying ``search`` / ``sort_by`` / ``order``. | ||
| select_related: FK joins to re-attach after the re-wrap. | ||
| prefetch_related: Reverse/M2M prefetches to re-attach after the re-wrap. | ||
| default_sort_by: Sort key used when ``?sort_by`` is absent. | ||
|
|
||
| Returns: | ||
| An ordered queryset ready for pagination. | ||
| """ | ||
| params = request.query_params | ||
|
|
||
| search = params.get("search") | ||
| if search: | ||
| queryset = queryset.filter( | ||
| Q(**{f"{name_field}__icontains": search}) | ||
| | Q(**{f"{OWNER_SORT_FIELD}__icontains": search}) | ||
| ) | ||
|
|
||
| sort_field = { | ||
| "name": name_field, | ||
| "owner": OWNER_SORT_FIELD, | ||
| "created": CREATED_SORT_FIELD, | ||
| }.get((params.get("sort_by") or default_sort_by).lower(), name_field) | ||
| order_prefix = "-" if (params.get("order") or "asc").lower() == "desc" else "" | ||
|
|
||
| # Ordering the source by ``pk`` keeps the DISTINCT ON (always the pk) valid | ||
| # while stripping the model's default ordering, so the outer query is free | ||
| # to sort by any column. | ||
| rewrapped = model.objects.filter(pk__in=queryset.order_by("pk").values("pk")) | ||
| if select_related: | ||
| rewrapped = rewrapped.select_related(*select_related) | ||
| if prefetch_related: | ||
| rewrapped = rewrapped.prefetch_related(*prefetch_related) | ||
| return rewrapped.order_by(f"{order_prefix}{sort_field}", "pk") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.