Skip to content

Commit 8190eec

Browse files
committed
Change default Select2 version to Django's vendored version
1 parent 3781b67 commit 8190eec

File tree

4 files changed

+28
-91
lines changed

4 files changed

+28
-91
lines changed

django_select2/conf.py

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
__all__ = ("settings", "Select2Conf")
66

7+
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
8+
79

810
class Select2Conf(AppConf):
911
"""Settings for Django-Select2."""
1012

11-
LIB_VERSION = "4.0.12"
12-
"""Version of the Select2 library."""
13-
1413
CACHE_BACKEND = "default"
1514
"""
1615
Django-Select2 uses Django's cache to sure a consistent state across multiple machines.
@@ -56,11 +55,9 @@ class Select2Conf(AppConf):
5655
It has set `select2_` as a default value, which you can change if needed.
5756
"""
5857

59-
JS = "https://cdnjs.cloudflare.com/ajax/libs/select2/{version}/js/select2.min.js".format(
60-
version=LIB_VERSION
61-
)
58+
JS = "admin/js/vendor/select2/select2.full.min.js"
6259
"""
63-
The URI for the Select2 JS file. By default this points to the Cloudflare CDN.
60+
The URI for the Select2 JS file. By default this points to version shipped with Django.
6461
6562
If you want to select the version of the JS library used, or want to serve it from
6663
the local 'static' resources, add a line to your settings.py like so::
@@ -76,11 +73,9 @@ class Select2Conf(AppConf):
7673
develop without an Internet connection.
7774
"""
7875

79-
CSS = "https://cdnjs.cloudflare.com/ajax/libs/select2/{version}/css/select2.min.css".format(
80-
version=LIB_VERSION
81-
)
76+
CSS = "admin/css/vendor/select2/select2.min.css"
8277
"""
83-
The URI for the Select2 CSS file. By default this points to the Cloudflare CDN.
78+
The URI for the Select2 CSS file. By default this points to version shipped with Django.
8479
8580
If you want to select the version of the library used, or want to serve it from
8681
the local 'static' resources, add a line to your settings.py like so::
@@ -112,13 +107,9 @@ class Select2Conf(AppConf):
112107
.. tip:: When using other themes, you may need use select2 css and theme css.
113108
"""
114109

115-
I18N_PATH = (
116-
"https://cdnjs.cloudflare.com/ajax/libs/select2/{version}/js/i18n".format(
117-
version=LIB_VERSION
118-
)
119-
)
110+
I18N_PATH = "admin/js/vendor/select2/i18n"
120111
"""
121-
The base URI for the Select2 i18n files. By default this points to the Cloudflare CDN.
112+
The base URI for the Select2 i18n files. By default this points to version shipped with Django.
122113
123114
If you want to select the version of the I18N library used, or want to serve it from
124115
the local 'static' resources, add a line to your settings.py like so::
@@ -129,55 +120,7 @@ class Select2Conf(AppConf):
129120
develop without an Internet connection.
130121
"""
131122

132-
I18N_AVAILABLE_LANGUAGES = [
133-
"ar",
134-
"az",
135-
"bg",
136-
"ca",
137-
"cs",
138-
"da",
139-
"de",
140-
"el",
141-
"en",
142-
"es",
143-
"et",
144-
"eu",
145-
"fa",
146-
"fi",
147-
"fr",
148-
"gl",
149-
"he",
150-
"hi",
151-
"hr",
152-
"hu",
153-
"id",
154-
"is",
155-
"it",
156-
"ja",
157-
"km",
158-
"ko",
159-
"lt",
160-
"lv",
161-
"mk",
162-
"ms",
163-
"nb",
164-
"nl",
165-
"pl",
166-
"pt-BR",
167-
"pt",
168-
"ro",
169-
"ru",
170-
"sk",
171-
"sr-Cyrl",
172-
"sr",
173-
"sv",
174-
"th",
175-
"tr",
176-
"uk",
177-
"vi",
178-
"zh-CN",
179-
"zh-TW",
180-
]
123+
I18N_AVAILABLE_LANGUAGES = list(SELECT2_TRANSLATIONS.values())
181124
"""
182125
List of available translations.
183126

django_select2/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
have to be pre-rendered onto the page
1717
and JavaScript would be used to search
1818
through them. Said that, they are also one
19-
the most easiest to use. They are a
19+
the easiest to use. They are a
2020
drop-in-replacement for Django's default
2121
select widgets.
2222

django_select2/static/django_select2/django_select2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
module.exports = factory(require('jquery'))
77
} else {
88
// Browser globals
9-
factory(jQuery)
9+
factory(jQuery || window.django.jQuery)
1010
}
1111
}(function ($) {
1212
'use strict'

tests/test_forms.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Iterable
44

55
import pytest
6+
from django.contrib.admin.widgets import get_select2_language
67
from django.db.models import QuerySet
78
from django.urls import reverse
89
from django.utils import translation
@@ -13,7 +14,6 @@
1314
from selenium.webdriver.support.wait import WebDriverWait
1415

1516
from django_select2.cache import cache
16-
from django_select2.conf import settings
1717
from django_select2.forms import (
1818
HeavySelect2MultipleWidget,
1919
HeavySelect2Widget,
@@ -135,44 +135,44 @@ def test_empty_option(self, db):
135135
def test_i18n(self):
136136
translation.activate("de")
137137
assert tuple(Select2Widget().media._js) == (
138-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
139-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/de.js",
138+
"admin/js/vendor/select2/select2.full.min.js",
139+
"admin/js/vendor/select2/i18n/de.js",
140140
"django_select2/django_select2.js",
141141
)
142142

143143
translation.activate("en")
144144
assert tuple(Select2Widget().media._js) == (
145-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
146-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/en.js",
145+
"admin/js/vendor/select2/select2.full.min.js",
146+
"admin/js/vendor/select2/i18n/en.js",
147147
"django_select2/django_select2.js",
148148
)
149149

150150
translation.activate("00")
151151
assert tuple(Select2Widget().media._js) == (
152-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
152+
"admin/js/vendor/select2/select2.full.min.js",
153153
"django_select2/django_select2.js",
154154
)
155155

156-
translation.activate("sr-cyrl")
156+
translation.activate("sr-Cyrl")
157157
assert tuple(Select2Widget().media._js) == (
158-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
159-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/sr-Cyrl.js",
158+
"admin/js/vendor/select2/select2.full.min.js",
159+
"admin/js/vendor/select2/i18n/sr-Cyrl.js",
160160
"django_select2/django_select2.js",
161161
)
162162

163163
pytest.importorskip("django", minversion="2.0.4")
164164

165165
translation.activate("zh-hans")
166166
assert tuple(Select2Widget().media._js) == (
167-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
168-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/zh-CN.js",
167+
"admin/js/vendor/select2/select2.full.min.js",
168+
"admin/js/vendor/select2/i18n/zh-CN.js",
169169
"django_select2/django_select2.js",
170170
)
171171

172172
translation.activate("zh-hant")
173173
assert tuple(Select2Widget().media._js) == (
174-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
175-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/zh-TW.js",
174+
"admin/js/vendor/select2/select2.full.min.js",
175+
"admin/js/vendor/select2/i18n/zh-TW.js",
176176
"django_select2/django_select2.js",
177177
)
178178

@@ -186,8 +186,8 @@ class TestSelect2AdminMixin:
186186
def test_media(self):
187187
translation.activate("en")
188188
assert tuple(Select2AdminMixin().media._js) == (
189-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
190-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/en.js",
189+
"admin/js/vendor/select2/select2.full.min.js",
190+
"admin/js/vendor/select2/i18n/en.js",
191191
"django_select2/django_select2.js",
192192
)
193193

@@ -204,14 +204,8 @@ class TestSelect2MixinSettings:
204204
def test_default_media(self):
205205
sut = Select2Widget()
206206
result = sut.media.render()
207-
assert (
208-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js"
209-
in result
210-
)
211-
assert (
212-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/css/select2.min.css"
213-
in result
214-
)
207+
assert "admin/js/vendor/select2/select2.full.min.js" in result
208+
assert "admin/css/vendor/select2/select2.min.css" in result
215209
assert "django_select2/django_select2.js" in result
216210

217211
def test_js_setting(self, settings):

0 commit comments

Comments
 (0)