Skip to content

Commit 1954bf5

Browse files
committed
Add SELECT2_LIB_VERSION setting
The new setting allows to change to default library version to a specific value without overriding all URLs individually.
1 parent 08ca1c1 commit 1954bf5

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

django_select2/conf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
class Select2Conf(AppConf):
99
"""Settings for Django-Select2."""
1010

11+
LIB_VERSION = '4.0.3'
12+
"""Version of the Select2 library."""
13+
1114
CACHE_BACKEND = 'default'
1215
"""
1316
Django-Select2 uses Django's cache to sure a consistent state across multiple machines.
@@ -53,7 +56,7 @@ class Select2Conf(AppConf):
5356
It has set `select2_` as a default value, which you can change if needed.
5457
"""
5558

56-
JS = '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js'
59+
JS = f'//cdnjs.cloudflare.com/ajax/libs/select2/{LIB_VERSION}/js/select2.min.js'
5760
"""
5861
The URI for the Select2 JS file. By default this points to the Cloudflare CDN.
5962
@@ -71,7 +74,7 @@ class Select2Conf(AppConf):
7174
develop without an Internet connection.
7275
"""
7376

74-
CSS = '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css'
77+
CSS = f'//cdnjs.cloudflare.com/ajax/libs/select2/{LIB_VERSION}/css/select2.min.css'
7578
"""
7679
The URI for the Select2 CSS file. By default this points to the Cloudflare CDN.
7780
@@ -89,7 +92,7 @@ class Select2Conf(AppConf):
8992
develop without an Internet connection.
9093
"""
9194

92-
I18N_PATH = '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n'
95+
I18N_PATH = f'//cdnjs.cloudflare.com/ajax/libs/select2/{LIB_VERSION}/js/i18n'
9396
"""
9497
The base URI for the Select2 i18n files. By default this points to the Cloudflare CDN.
9598

tests/test_forms.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from selenium.webdriver.support.wait import WebDriverWait
1515

1616
from django_select2.cache import cache
17+
from django_select2.conf import settings
1718
from django_select2.forms import (
1819
HeavySelect2MultipleWidget, HeavySelect2Widget, ModelSelect2TagWidget,
1920
ModelSelect2Widget, Select2Widget
@@ -104,44 +105,44 @@ def test_empty_option(self, db):
104105
def test_i18n(self):
105106
translation.activate('de')
106107
assert tuple(Select2Widget().media._js) == (
107-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
108-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/de.js',
108+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
109+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/de.js',
109110
'django_select2/django_select2.js'
110111
)
111112

112113
translation.activate('en')
113114
assert tuple(Select2Widget().media._js) == (
114-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
115-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/en.js',
115+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
116+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/en.js',
116117
'django_select2/django_select2.js'
117118
)
118119

119120
translation.activate('00')
120121
assert tuple(Select2Widget().media._js) == (
121-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
122+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
122123
'django_select2/django_select2.js'
123124
)
124125

125126
translation.activate('sr-cyrl')
126127
assert tuple(Select2Widget().media._js) == (
127-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
128-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/sr-Cyrl.js',
128+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
129+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/sr-Cyrl.js',
129130
'django_select2/django_select2.js'
130131
)
131132

132133
pytest.importorskip("django", minversion="2.0.4")
133134

134135
translation.activate('zh-hans')
135136
assert tuple(Select2Widget().media._js) == (
136-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
137-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/zh-CN.js',
137+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
138+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/zh-CN.js',
138139
'django_select2/django_select2.js'
139140
)
140141

141142
translation.activate('zh-hant')
142143
assert tuple(Select2Widget().media._js) == (
143-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js',
144-
'//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/i18n/zh-TW.js',
144+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js',
145+
f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/i18n/zh-TW.js',
145146
'django_select2/django_select2.js'
146147
)
147148

@@ -150,8 +151,8 @@ class TestSelect2MixinSettings(object):
150151
def test_default_media(self):
151152
sut = Select2Widget()
152153
result = sut.media.render()
153-
assert '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js' in result
154-
assert '//cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css' in result
154+
assert f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/js/select2.min.js' in result
155+
assert f'//cdnjs.cloudflare.com/ajax/libs/select2/{settings.SELECT2_LIB_VERSION}/css/select2.min.css' in result
155156
assert 'django_select2/django_select2.js' in result
156157

157158
def test_js_setting(self, settings):

0 commit comments

Comments
 (0)