Skip to content

Commit

Permalink
Exclusion for ?search parameter in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbiggs committed Feb 17, 2025
1 parent 08c052b commit 2d0d2cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
62 changes: 35 additions & 27 deletions etna/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from wagtail.contrib.redirects.models import Redirect
from wagtail.images.api.v2.views import ImagesAPIViewSet
from wagtail.models import Page, PageViewRestriction, Site
from wagtail.search.backends.database.postgres.postgres import PostgresSearchResults
from wagtail_headless_preview.models import PagePreview
from wagtailmedia.api.views import MediaAPIViewSet

Expand Down Expand Up @@ -51,36 +52,43 @@ def listing_view(self, request):
self.check_query_parameters(queryset)
queryset = self.filter_queryset(queryset)

# TODO: Investigate a way to not use PostgresSearchResults here
# when using the `?search` parameter (currently throws error)
if "include_aliases" not in request.GET:
alias_pages = queryset.filter(alias_of_id__isnull=False).values(
"id", "alias_of_id"
)
original_ids = set(
queryset.filter(alias_of_id__isnull=True).values_list("id", flat=True)
)
alias_ids = set(page["id"] for page in alias_pages)
alias_of_ids = alias_pages.values_list("alias_of_id", flat=True)

# Exclude any pages with matching alias_of_ids - aliases of the same original page
for alias_of_id in alias_of_ids:
alias_pages_with_same_id = alias_pages.filter(alias_of_id=alias_of_id)
if alias_pages_with_same_id.count() > 1:
first_page_id = alias_pages_with_same_id.order_by("depth").first()[
"id"
]
queryset = queryset.exclude(
id__in=alias_pages_with_same_id.values_list(
"id", flat=True
).exclude(id=first_page_id)
if not isinstance(queryset, PostgresSearchResults):
alias_pages = queryset.filter(alias_of_id__isnull=False).values(
"id", "alias_of_id"
)
original_ids = set(
queryset.filter(alias_of_id__isnull=True).values_list(
"id", flat=True
)
)
alias_ids = set(page["id"] for page in alias_pages)
alias_of_ids = alias_pages.values_list("alias_of_id", flat=True)

# Exclude any pages with matching alias_of_ids - aliases of the same original page
for alias_of_id in alias_of_ids:
alias_pages_with_same_id = alias_pages.filter(
alias_of_id=alias_of_id
)
if alias_pages_with_same_id.count() > 1:
first_page_id = alias_pages_with_same_id.order_by(
"depth"
).first()["id"]
queryset = queryset.exclude(
id__in=alias_pages_with_same_id.values_list(
"id", flat=True
).exclude(id=first_page_id)
)

# Exclude any pages that are aliases of pages in the current queryset
for page in alias_pages:
if (
page["alias_of_id"] in original_ids
or page["alias_of_id"] in alias_ids
):
queryset = queryset.exclude(id=page["id"])
# Exclude any pages that are aliases of pages in the current queryset
for page in alias_pages:
if (
page["alias_of_id"] in original_ids
or page["alias_of_id"] in alias_ids
):
queryset = queryset.exclude(id=page["id"])

queryset = self.paginate_queryset(queryset)
serializer = DefaultPageSerializer(queryset, many=True)
Expand Down
4 changes: 2 additions & 2 deletions sass/tna-toolkit/bootstrap/_custom-forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@
border-color: $custom-select-focus-border-color;
outline: 0;
@if $enable-shadows {
box-shadow: $custom-select-box-shadow,
$custom-select-focus-box-shadow;
box-shadow:
$custom-select-box-shadow, $custom-select-focus-box-shadow;
} @else {
box-shadow: $custom-select-focus-box-shadow;
}
Expand Down
12 changes: 7 additions & 5 deletions sass/tna-toolkit/bootstrap/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ $transition-collapse: height 0.35s ease !default;
// Font, line-height, and color for body text, headings, and more.

// stylelint-disable value-keyword-case
$font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol", "Noto Color Emoji" !default;
$font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", "Courier New", monospace !default;
$font-family-sans-serif:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji" !default;
$font-family-monospace:
SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New",
monospace !default;
$font-family-base: $font-family-sans-serif !default;
// stylelint-enable value-keyword-case

Expand Down

0 comments on commit 2d0d2cd

Please sign in to comment.