Skip to content

Commit 112846c

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

File tree

12 files changed

+56
-112
lines changed

12 files changed

+56
-112
lines changed

django_select2/conf.py

+9-66
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

+2-2
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
@@ -293,7 +293,7 @@ def render(self, *args, **kwargs):
293293
return output
294294

295295
def _get_cache_key(self):
296-
return "%s%s" % (settings.SELECT2_CACHE_PREFIX, self.uuid)
296+
return f"{settings.SELECT2_CACHE_PREFIX}{self.uuid}"
297297

298298
def set_to_cache(self):
299299
"""

django_select2/static/django_select2/django_select2.js

+1-1
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'

django_select2/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_widget_or_404(self):
9292
except BadSignature:
9393
raise Http404('Invalid "field_id".')
9494
else:
95-
cache_key = "%s%s" % (settings.SELECT2_CACHE_PREFIX, key)
95+
cache_key = f"{settings.SELECT2_CACHE_PREFIX}{key}"
9696
widget_dict = cache.get(cache_key)
9797
if widget_dict is None:
9898
raise Http404("field_id not found")

docs/extra.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ the field in the form. The value represents the name of the field in the model (
3939
class AddressForm(forms.Form):
4040
country = forms.ModelChoiceField(
4141
queryset=Country.objects.all(),
42-
label=u"Country",
42+
label="Country",
4343
widget=ModelSelect2Widget(
4444
model=Country,
4545
search_fields=['name__icontains'],
@@ -48,7 +48,7 @@ the field in the form. The value represents the name of the field in the model (
4848
4949
city = forms.ModelChoiceField(
5050
queryset=City.objects.all(),
51-
label=u"City",
51+
label="City",
5252
widget=ModelSelect2Widget(
5353
model=City,
5454
search_fields=['name__icontains'],
@@ -72,7 +72,7 @@ Customize the form in a manner:
7272
class AddressForm(forms.Form):
7373
country = forms.ModelChoiceField(
7474
queryset=Country.objects.all(),
75-
label=u"Country",
75+
label="Country",
7676
widget=ModelSelect2Widget(
7777
search_fields=['name__icontains'],
7878
dependent_fields={'city': 'cities'},
@@ -81,7 +81,7 @@ Customize the form in a manner:
8181
8282
city = forms.ModelChoiceField(
8383
queryset=City.objects.all(),
84-
label=u"City",
84+
label="City",
8585
widget=ModelSelect2Widget(
8686
search_fields=['name__icontains'],
8787
dependent_fields={'country': 'country'},

docs/index.rst

+14-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ Install ``django-select2``::
88
python3 -m pip install django-select2
99

1010
Add ``django_select2`` to your ``INSTALLED_APPS`` in your project settings.
11+
Since version 8, please ensure that Django's admin app is enabled too:
12+
13+
.. code-block:: python
14+
15+
INSTALLED_APPS = [
16+
# other django apps…
17+
'django.contrib.admin',
18+
# other 3rd party apps…
19+
'django_select2',
20+
]
1121
1222
Add ``django_select`` to your URL root configuration:
1323

@@ -16,20 +26,20 @@ Add ``django_select`` to your URL root configuration:
1626
from django.urls import include, path
1727
1828
urlpatterns = [
19-
# other patterns
29+
# other patterns
2030
path("select2/", include("django_select2.urls")),
21-
# other patterns
31+
# other patterns
2232
]
2333
2434
2535
``django-select2`` requires a cache backend which is **persistent**
26-
across all application servers..
36+
across all application servers..
2737

2838
**This means that the** :class:`.DummyCache` **backend will not work!**
2939

3040
The default cache backend is :class:`.LocMemCache`, which is persistent
3141
across a single node. For projects with a single application server
32-
this will work fine, however you will run into issues when
42+
this will work fine, however you will run into issues when
3343
you scale up into multiple servers.
3444

3545
Below is an example setup using Redis, which is a solution that

example/example/settings.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,12 @@
117117

118118
CACHES = {
119119
"default": {
120-
"BACKEND": "django_redis.cache.RedisCache",
120+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
121121
"LOCATION": "redis://127.0.0.1:6379/1",
122-
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
123122
},
124123
"select2": {
125-
"BACKEND": "django_redis.cache.RedisCache",
124+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
126125
"LOCATION": "redis://127.0.0.1:6379/2",
127-
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
128126
},
129127
}
130128

example/example/templates/example/book_form.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
{% load static %}<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<title>Create Book</title>
@@ -14,7 +14,7 @@ <h1>Create a new Book</h1>
1414
{{ form.as_p }}
1515
<input type="submit">
1616
</form>
17-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
17+
<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}"></script>
1818
{{ form.media.js }}
1919
</body>
2020
</html>

example/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
-e ..
2-
django-redis
2+
redis

tests/test_forms.py

+16-23
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from selenium.webdriver.support.wait import WebDriverWait
1414

1515
from django_select2.cache import cache
16-
from django_select2.conf import settings
1716
from django_select2.forms import (
1817
HeavySelect2MultipleWidget,
1918
HeavySelect2Widget,
@@ -135,44 +134,44 @@ def test_empty_option(self, db):
135134
def test_i18n(self):
136135
translation.activate("de")
137136
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",
137+
"admin/js/vendor/select2/select2.full.min.js",
138+
"admin/js/vendor/select2/i18n/de.js",
140139
"django_select2/django_select2.js",
141140
)
142141

143142
translation.activate("en")
144143
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",
144+
"admin/js/vendor/select2/select2.full.min.js",
145+
"admin/js/vendor/select2/i18n/en.js",
147146
"django_select2/django_select2.js",
148147
)
149148

150149
translation.activate("00")
151150
assert tuple(Select2Widget().media._js) == (
152-
f"https://cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js",
151+
"admin/js/vendor/select2/select2.full.min.js",
153152
"django_select2/django_select2.js",
154153
)
155154

156-
translation.activate("sr-cyrl")
155+
translation.activate("sr-Cyrl")
157156
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",
157+
"admin/js/vendor/select2/select2.full.min.js",
158+
"admin/js/vendor/select2/i18n/sr-Cyrl.js",
160159
"django_select2/django_select2.js",
161160
)
162161

163162
pytest.importorskip("django", minversion="2.0.4")
164163

165164
translation.activate("zh-hans")
166165
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",
166+
"admin/js/vendor/select2/select2.full.min.js",
167+
"admin/js/vendor/select2/i18n/zh-CN.js",
169168
"django_select2/django_select2.js",
170169
)
171170

172171
translation.activate("zh-hant")
173172
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",
173+
"admin/js/vendor/select2/select2.full.min.js",
174+
"admin/js/vendor/select2/i18n/zh-TW.js",
176175
"django_select2/django_select2.js",
177176
)
178177

@@ -186,8 +185,8 @@ class TestSelect2AdminMixin:
186185
def test_media(self):
187186
translation.activate("en")
188187
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",
188+
"admin/js/vendor/select2/select2.full.min.js",
189+
"admin/js/vendor/select2/i18n/en.js",
191190
"django_select2/django_select2.js",
192191
)
193192

@@ -204,14 +203,8 @@ class TestSelect2MixinSettings:
204203
def test_default_media(self):
205204
sut = Select2Widget()
206205
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-
)
206+
assert "admin/js/vendor/select2/select2.full.min.js" in result
207+
assert "admin/css/vendor/select2/select2.min.css" in result
215208
assert "django_select2/django_select2.js" in result
216209

217210
def test_js_setting(self, settings):

tests/testapp/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"django.contrib.contenttypes",
1313
"django.contrib.sessions",
1414
"django.contrib.staticfiles",
15+
"django.contrib.admin",
1516
"django_select2",
1617
"tests.testapp",
1718
)

tests/testapp/templates/form.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
{% load static %}
2-
<!DOCTYPE html>
3-
<html>
1+
{% load static %}<!DOCTYPE html>
2+
<html lang="en">
43
<head>
54
{{ form.media.css }}
65
<style type="text/css">
@@ -15,7 +14,7 @@
1514
{{ form }}
1615
<input type="submit" value="Submit Form"/>
1716
</form>
18-
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
17+
<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}"></script>
1918
<script type="text/javascript">
2019
window.onerror = function (msg) {
2120
$("body").attr("JSError", msg);

0 commit comments

Comments
 (0)