Skip to content

Commit

Permalink
Simplified manager retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Oct 27, 2024
1 parent 0b6e979 commit c3a3c1f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
13 changes: 3 additions & 10 deletions djangocms_link/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from cms.utils import get_language_from_request

from . import models
from .helpers import get_manager


_version = int(__version__.split(".")[0])
Expand Down Expand Up @@ -67,10 +68,7 @@ def get_reference(self, request):
model, pk = request.GET.get("g").split(":")
app, model = model.split(".")
model = apps.get_model(app, model)
if hasattr(model, "admin_manager"): # pragma: no cover
obj = model.admin_manager.get(pk=pk)
else:
obj = model.objects.get(pk=pk)
obj = get_manager(model).get(pk=pk)
return JsonResponse(self.serialize_result(obj))
except Exception as e:
return JsonResponse({"error": str(e)})
Expand Down Expand Up @@ -115,12 +113,7 @@ def get_queryset(self):
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
if hasattr(PageContent, "admin_manager"): # V4
qs = PageContent.admin_manager.filter(
language=self.language, title__icontains=self.term
).current_content().values_list("page_id", flat=True)
else: # V3
qs = PageContent.objects.filter(
qs = get_manager(PageContent, current_content=True).filter(
language=self.language, title__icontains=self.term
).values_list("page_id", flat=True)
qs = Page.objects.filter(pk__in=qs).order_by("node__path")
Expand Down
6 changes: 3 additions & 3 deletions djangocms_link/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def get_queryset_manager(base: models.QuerySet | models.Manager) -> models.Query
return base


def get_manager(model: models.Model) -> models.Manager:
if hasattr(model, "admin_manager"):
return model.admin_manager
def get_manager(model: models.Model, current_content: bool = False) -> models.Manager:
if hasattr(model, "admin_manager"): # pragma: no cover
return model.admin_manager.current_content() if current_content else model.admin_manager
return model.objects


Expand Down

0 comments on commit c3a3c1f

Please sign in to comment.