2017-05-10 12:48:57 +00:00
|
|
|
from django import forms
|
2017-12-01 19:00:00 +00:00
|
|
|
from django.contrib import admin
|
2017-05-10 12:48:57 +00:00
|
|
|
from django.contrib.admin.widgets import AutocompleteSelect
|
|
|
|
from django.forms import ModelChoiceField
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
from django.utils import translation
|
|
|
|
|
2021-03-18 10:21:23 +00:00
|
|
|
from .models import Album, Band, ReleaseEvent, VideoStream
|
2017-05-10 12:48:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AlbumForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Album
|
|
|
|
fields = ["band", "featuring"]
|
|
|
|
widgets = {
|
|
|
|
"band": AutocompleteSelect(
|
2021-01-12 10:37:38 +00:00
|
|
|
Album._meta.get_field("band"),
|
2017-12-01 19:00:00 +00:00
|
|
|
admin.site,
|
2017-05-10 12:48:57 +00:00
|
|
|
attrs={"class": "my-class"},
|
|
|
|
),
|
|
|
|
"featuring": AutocompleteSelect(
|
2021-01-12 10:37:38 +00:00
|
|
|
Album._meta.get_field("featuring"),
|
2017-12-01 19:00:00 +00:00
|
|
|
admin.site,
|
2017-05-10 12:48:57 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class NotRequiredBandForm(forms.Form):
|
|
|
|
band = ModelChoiceField(
|
|
|
|
queryset=Album.objects.all(),
|
2017-12-01 19:00:00 +00:00
|
|
|
widget=AutocompleteSelect(
|
|
|
|
Album._meta.get_field("band").remote_field, admin.site
|
|
|
|
),
|
2017-05-10 12:48:57 +00:00
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class RequiredBandForm(forms.Form):
|
|
|
|
band = ModelChoiceField(
|
|
|
|
queryset=Album.objects.all(),
|
2017-12-01 19:00:00 +00:00
|
|
|
widget=AutocompleteSelect(
|
|
|
|
Album._meta.get_field("band").remote_field, admin.site
|
|
|
|
),
|
2017-05-10 12:48:57 +00:00
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-18 10:21:23 +00:00
|
|
|
class VideoStreamForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = VideoStream
|
|
|
|
fields = ["release_event"]
|
|
|
|
widgets = {
|
|
|
|
"release_event": AutocompleteSelect(
|
|
|
|
VideoStream._meta.get_field("release_event"),
|
|
|
|
admin.site,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 12:48:57 +00:00
|
|
|
@override_settings(ROOT_URLCONF="admin_widgets.urls")
|
|
|
|
class AutocompleteMixinTests(TestCase):
|
|
|
|
empty_option = '<option value=""></option>'
|
|
|
|
maxDiff = 1000
|
|
|
|
|
|
|
|
def test_build_attrs(self):
|
|
|
|
form = AlbumForm()
|
|
|
|
attrs = form["band"].field.widget.get_context(
|
|
|
|
name="my_field", value=None, attrs={}
|
|
|
|
)["widget"]["attrs"]
|
|
|
|
self.assertEqual(
|
|
|
|
attrs,
|
|
|
|
{
|
2018-03-15 14:55:34 +00:00
|
|
|
"class": "my-class admin-autocomplete",
|
2017-05-10 12:48:57 +00:00
|
|
|
"data-ajax--cache": "true",
|
2019-08-22 14:20:30 +00:00
|
|
|
"data-ajax--delay": 250,
|
2017-05-10 12:48:57 +00:00
|
|
|
"data-ajax--type": "GET",
|
2021-01-12 10:37:38 +00:00
|
|
|
"data-ajax--url": "/autocomplete/",
|
2017-05-10 12:48:57 +00:00
|
|
|
"data-theme": "admin-autocomplete",
|
|
|
|
"data-allow-clear": "false",
|
2021-01-12 10:37:38 +00:00
|
|
|
"data-app-label": "admin_widgets",
|
|
|
|
"data-field-name": "band",
|
|
|
|
"data-model-name": "album",
|
2021-09-20 18:56:54 +00:00
|
|
|
"data-placeholder": "",
|
|
|
|
"lang": "en",
|
2017-05-10 12:48:57 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-03-15 14:55:34 +00:00
|
|
|
def test_build_attrs_no_custom_class(self):
|
|
|
|
form = AlbumForm()
|
|
|
|
attrs = form["featuring"].field.widget.get_context(
|
|
|
|
name="name", value=None, attrs={}
|
|
|
|
)["widget"]["attrs"]
|
|
|
|
self.assertEqual(attrs["class"], "admin-autocomplete")
|
|
|
|
|
2017-05-10 12:48:57 +00:00
|
|
|
def test_build_attrs_not_required_field(self):
|
|
|
|
form = NotRequiredBandForm()
|
|
|
|
attrs = form["band"].field.widget.build_attrs({})
|
|
|
|
self.assertJSONEqual(attrs["data-allow-clear"], True)
|
|
|
|
|
|
|
|
def test_build_attrs_required_field(self):
|
|
|
|
form = RequiredBandForm()
|
|
|
|
attrs = form["band"].field.widget.build_attrs({})
|
|
|
|
self.assertJSONEqual(attrs["data-allow-clear"], False)
|
|
|
|
|
|
|
|
def test_get_url(self):
|
2021-01-12 10:37:38 +00:00
|
|
|
rel = Album._meta.get_field("band")
|
2017-12-01 19:00:00 +00:00
|
|
|
w = AutocompleteSelect(rel, admin.site)
|
2017-05-10 12:48:57 +00:00
|
|
|
url = w.get_url()
|
2021-01-12 10:37:38 +00:00
|
|
|
self.assertEqual(url, "/autocomplete/")
|
2017-05-10 12:48:57 +00:00
|
|
|
|
|
|
|
def test_render_options(self):
|
|
|
|
beatles = Band.objects.create(name="The Beatles", style="rock")
|
|
|
|
who = Band.objects.create(name="The Who", style="rock")
|
|
|
|
# With 'band', a ForeignKey.
|
2021-01-12 10:37:38 +00:00
|
|
|
form = AlbumForm(initial={"band": beatles.uuid})
|
2017-05-10 12:48:57 +00:00
|
|
|
output = form.as_table()
|
2021-01-12 10:37:38 +00:00
|
|
|
selected_option = (
|
|
|
|
'<option value="%s" selected>The Beatles</option>' % beatles.uuid
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2021-01-12 10:37:38 +00:00
|
|
|
option = '<option value="%s">The Who</option>' % who.uuid
|
2017-05-10 12:48:57 +00:00
|
|
|
self.assertIn(selected_option, output)
|
|
|
|
self.assertNotIn(option, output)
|
|
|
|
# With 'featuring', a ManyToManyField.
|
|
|
|
form = AlbumForm(initial={"featuring": [beatles.pk, who.pk]})
|
|
|
|
output = form.as_table()
|
|
|
|
selected_option = (
|
|
|
|
'<option value="%s" selected>The Beatles</option>' % beatles.pk
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2017-05-10 12:48:57 +00:00
|
|
|
option = '<option value="%s" selected>The Who</option>' % who.pk
|
|
|
|
self.assertIn(selected_option, output)
|
|
|
|
self.assertIn(option, output)
|
|
|
|
|
|
|
|
def test_render_options_required_field(self):
|
|
|
|
"""Empty option is present if the field isn't required."""
|
|
|
|
form = NotRequiredBandForm()
|
|
|
|
output = form.as_table()
|
|
|
|
self.assertIn(self.empty_option, output)
|
|
|
|
|
|
|
|
def test_render_options_not_required_field(self):
|
|
|
|
"""Empty option isn't present if the field isn't required."""
|
|
|
|
form = RequiredBandForm()
|
|
|
|
output = form.as_table()
|
|
|
|
self.assertNotIn(self.empty_option, output)
|
|
|
|
|
2021-03-18 10:21:23 +00:00
|
|
|
def test_render_options_fk_as_pk(self):
|
|
|
|
beatles = Band.objects.create(name="The Beatles", style="rock")
|
|
|
|
rubber_soul = Album.objects.create(name="Rubber Soul", band=beatles)
|
|
|
|
release_event = ReleaseEvent.objects.create(
|
|
|
|
name="Test Target", album=rubber_soul
|
|
|
|
)
|
|
|
|
form = VideoStreamForm(initial={"release_event": release_event.pk})
|
|
|
|
output = form.as_table()
|
|
|
|
selected_option = (
|
|
|
|
'<option value="%s" selected>Test Target</option>' % release_event.pk
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2021-03-18 10:21:23 +00:00
|
|
|
self.assertIn(selected_option, output)
|
|
|
|
|
2017-05-10 12:48:57 +00:00
|
|
|
def test_media(self):
|
|
|
|
rel = Album._meta.get_field("band").remote_field
|
|
|
|
base_files = (
|
|
|
|
"admin/js/vendor/jquery/jquery.min.js",
|
|
|
|
"admin/js/vendor/select2/select2.full.min.js",
|
|
|
|
# Language file is inserted here.
|
|
|
|
"admin/js/jquery.init.js",
|
|
|
|
"admin/js/autocomplete.js",
|
|
|
|
)
|
|
|
|
languages = (
|
|
|
|
("de", "de"),
|
2022-02-18 01:21:35 +00:00
|
|
|
# Subsequent language codes are used when the language code is not
|
|
|
|
# supported.
|
|
|
|
("de-at", "de"),
|
|
|
|
("de-ch-1901", "de"),
|
|
|
|
("en-latn-us", "en"),
|
|
|
|
("nl-nl-x-informal", "nl"),
|
|
|
|
("zh-hans-HK", "zh-CN"),
|
2017-05-10 12:48:57 +00:00
|
|
|
# Language with code 00 does not exist.
|
|
|
|
("00", None),
|
|
|
|
# Language files are case sensitive.
|
|
|
|
("sr-cyrl", "sr-Cyrl"),
|
2018-03-12 16:50:43 +00:00
|
|
|
("zh-hans", "zh-CN"),
|
|
|
|
("zh-hant", "zh-TW"),
|
2022-08-03 13:45:09 +00:00
|
|
|
(None, None),
|
2017-05-10 12:48:57 +00:00
|
|
|
)
|
|
|
|
for lang, select_lang in languages:
|
|
|
|
with self.subTest(lang=lang):
|
|
|
|
if select_lang:
|
|
|
|
expected_files = (
|
|
|
|
base_files[:2]
|
|
|
|
+ (("admin/js/vendor/select2/i18n/%s.js" % select_lang),)
|
|
|
|
+ base_files[2:]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
expected_files = base_files
|
|
|
|
with translation.override(lang):
|
2019-02-23 15:41:56 +00:00
|
|
|
self.assertEqual(
|
|
|
|
AutocompleteSelect(rel, admin.site).media._js,
|
|
|
|
list(expected_files),
|
|
|
|
)
|