Skip to content

Commit

Permalink
Merge pull request django-cms#106 from gkmngrgn/78
Browse files Browse the repository at this point in the history
we started to support new django-select2 versions. django-cms#78
  • Loading branch information
FinalAngel authored Dec 21, 2016
2 parents efd3d08 + dc11432 commit d6c7747
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc
*.egg-info
*.egg/
.DS_Store
.idea/
.tox/
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

2.1.0 (unreleased)
==================

* Supported new django-select2 versions
* Remove unused ``UserSearchField``


2.0.3 (2016-11-22)
==================
Expand Down
56 changes: 9 additions & 47 deletions djangocms_link/fields.py
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
25 changes: 25 additions & 0 deletions djangocms_link/fields_select2.py
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)
17 changes: 17 additions & 0 deletions djangocms_link/fields_select2_legacy.py
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
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# requirements from setup.py
django-select2>=4.3,<5.0
django-select2>=5.0
# other requirements
djangocms-text-ckeditor
html5lib<0.99999999
Expand Down
6 changes: 3 additions & 3 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HELPER_SETTINGS = {
'INSTALLED_APPS': [
'django_select2',
Expand All @@ -14,11 +11,14 @@
}]
},
'LANGUAGE_CODE': 'en',
'DJANGOCMS_LINK_USE_SELECT2': True
}


def run():
from djangocms_helper import runner
runner.cms('djangocms_link')


if __name__ == '__main__':
run()
8 changes: 8 additions & 0 deletions tests/tests_field.py
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)

0 comments on commit d6c7747

Please sign in to comment.