Skip to content

Commit

Permalink
Optimize Page queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Nov 1, 2024
1 parent e56d0a6 commit e30fcec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion djangocms_link/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.0.0'
__version__ = '5.0.0a1'
33 changes: 23 additions & 10 deletions djangocms_link/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.conf import settings
from django.contrib import admin
from django.core.exceptions import FieldError, PermissionDenied
from django.db.models import Q
from django.db.models import OuterRef, Q, Subquery
from django.http import JsonResponse
from django.urls import path
from django.views.generic.list import BaseListView
Expand Down Expand Up @@ -65,10 +65,21 @@ def get(self, request, *args, **kwargs):

def get_reference(self, request):
try:
model, pk = request.GET.get("g").split(":")
app, model = model.split(".")
model_str, pk = request.GET.get("g").split(":")
app, model = model_str.split(".")
model = apps.get_model(app, model)
obj = get_manager(model).get(pk=pk)
model_admin = self.admin_site._registry.get(model)
if model_str == "cms.page" or model_admin is None:
obj = get_manager(model).get(pk=pk)
if model_str == "cms.page":
language = get_language_from_request(request)
obj.__link_text__ = obj.get_admin_content(language).title
return JsonResponse(self.serialize_result(obj))

if hasattr(model_admin, "get_link_queryset"):
obj = model_admin.get_link_queryset(self.request, None).get(pk=pk)

Check warning on line 80 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L80

Added line #L80 was not covered by tests
else:
obj = model_admin.get_queryset(self.request).get(pk=pk)
return JsonResponse(self.serialize_result(obj))
except Exception as e:
return JsonResponse({"error": str(e)})
Expand Down Expand Up @@ -98,7 +109,7 @@ def serialize_result(self, obj):
"""
return {
"id": f"{obj._meta.app_label}.{obj._meta.model_name}:{obj.pk}",
"text": str(obj),
"text": getattr(obj, "__link_text__", str(obj)) or str(obj),
"url": obj.get_absolute_url(),
}

Expand All @@ -108,16 +119,18 @@ def get_queryset(self):
# django CMS 4.2+
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")
).current_content()
qs = (Page.objects.filter(pk__in=qs.values_list("page_id", flat=True)).order_by("path")
.annotate(__link_text__=Subquery(qs.filter(page_id=OuterRef("pk")).values("title")[:1])))
if self.site:
qs = qs.filter(site_id=self.site)

Check warning on line 126 in djangocms_link/admin.py

View check run for this annotation

Codecov / codecov/patch

djangocms_link/admin.py#L126

Added line #L126 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).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")
)
qs = (Page.objects.filter(pk__in=qs.values_list("page_id", flat=True)).order_by("node__path")
.annotate(__link_text__=Subquery(qs.filter(page_id=OuterRef("pk")).values("title")[:1])))
if self.site:
qs = qs.filter(node__site_id=self.site)
return list(qs)
Expand Down Expand Up @@ -196,7 +209,7 @@ def get_urls(self):
]

def url_view(self, request):
return AdminUrlsView.as_view(admin_site=self)(request)
return AdminUrlsView.as_view(admin_site=self.admin_site)(request)


admin.site.register(models.Link, LinkAdmin)

0 comments on commit e30fcec

Please sign in to comment.