Skip to content

Commit

Permalink
chore: extract 'django cms 4' compatibility function to reuse it betw…
Browse files Browse the repository at this point in the history
…een fields
  • Loading branch information
filipweidemann committed Jul 22, 2024
1 parent f5d89f0 commit b49a8d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 5 additions & 8 deletions djangocms_link/fields_select2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from django_select2.forms import ModelSelect2Widget

from djangocms_link.helpers import get_queryset_manager


class Select2PageSearchFieldMixin:
search_fields = [
Expand All @@ -28,15 +30,10 @@ def build_attrs(self, base_attrs, extra_attrs=None):

def get_queryset(self):
# django CMS < 4
if hasattr(Page.objects, "drafts"):
if self.site:
return Page.objects.drafts().on_site(self.site)
return Page.objects.drafts()

# django CMS >= 4
base_queryset = get_queryset_manager(Page.objects)
if self.site:
return Page.objects.on_site(self.site)
return Page.objects.all()
return base_queryset.on_site(self.site)
return base_queryset.all()

# we need to implement jQuery ourselves, see #180
class Media:
Expand Down
4 changes: 3 additions & 1 deletion djangocms_link/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from djangocms_attributes_field.widgets import AttributesWidget

from djangocms_link.helpers import get_queryset_manager

from .fields import PageSearchField
from .models import Link

Expand All @@ -18,7 +20,7 @@ def for_site(self, site):
# current site
# this will work for PageSelectFormField
from cms.models import Page
self.fields['internal_link'].queryset = Page.objects.on_site(site)
self.fields['internal_link'].queryset = get_queryset_manager(Page.objects).on_site(site)
# set the current site as a internal_link field instance attribute
# this will be used by the field later to properly set up the queryset
# this will work for PageSearchField
Expand Down
4 changes: 4 additions & 0 deletions djangocms_link/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def get_queryset_manager(base):
if hasattr(base, "drafts"):
return base.drafts()
return base

0 comments on commit b49a8d4

Please sign in to comment.