Skip to content

Commit

Permalink
feat: Allow for get_link_queryset method in model admin
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 28, 2024
1 parent c7c31fb commit ea83926
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions djangocms_link/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ def get_queryset(self):
"""Return queryset based on ModelAdmin.get_search_results()."""
try:
# django CMS 4.2+
qs = PageContent.admin_manager.filter(
language=self.language, title__icontains=self.term
qs = PageContent.admin_manager.filter(language=self.language).filter(
Q(title__icontains=self.term) | Q(menu_title__icontains=self.term)
).current_content().values_list("page_id", flat=True)
qs = Page.objects.filter(pk__in=qs).order_by("path")
if self.site:
qs = qs.filter(site_id=self.site)

Check warning on line 113 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L113

Added line #L113 was not covered by tests
except (AttributeError, FieldError):
# django CMS 3.11 - 4.1
qs = get_manager(PageContent, current_content=True).filter(
language=self.language, title__icontains=self.term
).values_list("page_id", flat=True)
qs = get_manager(PageContent, current_content=True).filter(language=self.language).filter(
Q(title__icontains=self.term) | Q(menu_title__icontains=self.term)
).values_list("page_id", flat=True)
qs = Page.objects.filter(pk__in=qs).order_by("node__path")
if self.site:
qs = qs.filter(node__site_id=self.site)
Expand All @@ -127,11 +127,16 @@ def add_admin_querysets(self, qs):
# hack: GrouperModelAdmin expects a language to be temporarily set
if isinstance(model_admin, GrouperModelAdmin): # pragma: no cover
model_admin.language = self.language
new_qs = model_admin.get_queryset(self.request)
if hasattr(model_admin.model, "site") and self.site:
new_qs = new_qs.filter(Q(site_id=self.site) | Q(site__isnull=True))
elif hasattr(model_admin.model, "sites") and self.site:
new_qs = new_qs.filter(sites__id=self.site)
if hasattr(model_admin, "get_link_queryset"):
# Allow model admins to define get_link_queryset to do additional
# filtering, sorting and potentially custom site selection
new_qs = model_admin.get_link_queryset(self.request, self.site)

Check warning on line 133 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L133

Added line #L133 was not covered by tests
else:
new_qs = model_admin.get_queryset(self.request)
if hasattr(model_admin.model, "site") and self.site:
new_qs = new_qs.filter(Q(site_id=self.site) | Q(site__isnull=True))
elif hasattr(model_admin.model, "sites") and self.site:
new_qs = new_qs.filter(sites__id=self.site)

Check warning on line 139 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L139

Added line #L139 was not covered by tests
new_qs, search_use_distinct = model_admin.get_search_results(
self.request, new_qs, self.term
)
Expand Down

0 comments on commit ea83926

Please sign in to comment.