forked from django-cms/djangocms-link
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request django-cms#106 from gkmngrgn/78
we started to support new django-select2 versions. django-cms#78
- Loading branch information
Showing
8 changed files
with
70 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.pyc | ||
*.egg-info | ||
*.egg/ | ||
.DS_Store | ||
.idea/ | ||
.tox/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.conf import settings | ||
|
||
from distutils.version import LooseVersion | ||
|
||
ENABLE_SELECT2 = getattr( | ||
settings, | ||
'DJANGOCMS_LINK_USE_SELECT2', | ||
False | ||
) | ||
from django.conf import settings | ||
|
||
ENABLE_SELECT2 = getattr(settings, 'DJANGOCMS_LINK_USE_SELECT2', False) | ||
|
||
if ENABLE_SELECT2 and 'django_select2' in settings.INSTALLED_APPS: | ||
from django_select2.fields import AutoModelSelect2Field | ||
|
||
class PageSearchField(AutoModelSelect2Field): | ||
site = None | ||
search_fields = [ | ||
'title_set__title__icontains', | ||
'title_set__menu_title__icontains', | ||
'title_set__slug__icontains' | ||
] | ||
|
||
def get_queryset(self): | ||
from cms.models import Page | ||
if self.site: | ||
return Page.objects.drafts().on_site(self.site) | ||
else: | ||
return Page.objects.drafts() | ||
|
||
def security_check(self, request, *args, **kwargs): | ||
user = request.user | ||
if user and not user.is_anonymous() and user.is_staff: | ||
return True | ||
return False | ||
|
||
class UserSearchField(AutoModelSelect2Field): | ||
search_fields = [ | ||
'username__icontains', | ||
'firstname__icontains', | ||
'lastname__icontains' | ||
] | ||
|
||
def security_check(self, request, *args, **kwargs): | ||
user = request.user | ||
if user and not user.is_anonymous() and user.is_staff: | ||
return True | ||
return False | ||
import django_select2 | ||
|
||
def prepare_value(self, value): | ||
if not value: | ||
return None | ||
return super(UserSearchField, self).prepare_value(value) | ||
select2_version = LooseVersion(django_select2.__version__) | ||
if select2_version >= LooseVersion('5'): | ||
from djangocms_link.fields_select2 import Select2PageSearchField as PageSearchField | ||
else: | ||
from djangocms_link.fields_select2_legacy import Select2LegacyPageSearchField as PageSearchField | ||
else: | ||
from cms.forms.fields import PageSelectFormField as PageSearchField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from cms.models import Page | ||
from django import forms | ||
from django_select2.forms import ModelSelect2Widget | ||
|
||
|
||
class Select2PageSearchFieldMixin(object): | ||
search_fields = [ | ||
'title_set__title__icontains', | ||
'title_set__menu_title__icontains', | ||
'title_set__slug__icontains' | ||
] | ||
|
||
|
||
class Select2PageSelectWidget(Select2PageSearchFieldMixin, ModelSelect2Widget): | ||
def __init__(self, site=None, *args, **kwargs): | ||
self.site = site | ||
super(Select2PageSelectWidget, self).__init__(*args, **kwargs) | ||
|
||
def get_queryset(self): | ||
return Page.objects.drafts().on_site(self.site) if self.site is not None else Page.objects.drafts() | ||
|
||
|
||
class Select2PageSearchField(forms.ChoiceField): | ||
site = None | ||
widget = Select2PageSelectWidget(site=site) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from cms.models import Page | ||
from django_select2.fields import AutoModelSelect2Field | ||
|
||
|
||
class Select2LegacyPageSearchField(AutoModelSelect2Field): | ||
site = None | ||
search_fields = [ | ||
'title_set__title__icontains', | ||
'title_set__menu_title__icontains', | ||
'title_set__slug__icontains' | ||
] | ||
|
||
def get_queryset(self): | ||
return Page.objects.drafts().on_site(self.site) if self.site is not None else Page.objects.drafts() | ||
|
||
def security_check(self, request, *args, **kwargs): | ||
return request.user and not request.user.is_anonymous() and request.user.is_staff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from djangocms_helper.base_test import BaseTestCase | ||
from djangocms_link.fields import PageSearchField | ||
from djangocms_link.fields_select2 import Select2PageSearchField | ||
|
||
|
||
class FieldTestCase(BaseTestCase): | ||
def test_field_with_django_select2_extension(self): | ||
self.assertEqual(PageSearchField, Select2PageSearchField) |