diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 4fa27769..9b618d15 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -6,6 +6,8 @@ version: 2 build: os: ubuntu-20.04 + apt_packages: + - graphviz tools: python: "3.10" diff --git a/LICENSE b/LICENSE index 3f28e2b7..621b9067 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Johannes Maron +Copyright (c) 2022 Johannes Maron and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index 81cf3e09..93d87344 100644 --- a/README.rst +++ b/README.rst @@ -6,7 +6,7 @@ Django-Select2 |version| |coverage| |license| -Custom autocompelte fields for `Django`_. +Custom autocomplete fields for `Django`_. Documentation ------------- diff --git a/django_select2/conf.py b/django_select2/conf.py index 0f34f774..a704eb17 100644 --- a/django_select2/conf.py +++ b/django_select2/conf.py @@ -55,32 +55,32 @@ class Select2Conf(AppConf): It has set `select2_` as a default value, which you can change if needed. """ - JS = "admin/js/vendor/select2/select2.full.min.js" + JS = ["admin/js/vendor/select2/select2.full.min.js"] """ The URI for the Select2 JS file. By default this points to version shipped with Django. If you want to select the version of the JS library used, or want to serve it from the local 'static' resources, add a line to your settings.py like so:: - SELECT2_JS = 'assets/js/select2.min.js' + SELECT2_JS = ['assets/js/select2.min.js'] If you provide your own JS and would not like Django-Select2 to load any, change this setting to a blank string like so:: - SELECT2_JS = '' + SELECT2_JS = [] .. tip:: Change this setting to a local asset in your development environment to develop without an Internet connection. """ - CSS = "admin/css/vendor/select2/select2.min.css" + CSS = ["admin/css/vendor/select2/select2.min.css"] """ The URI for the Select2 CSS file. By default this points to version shipped with Django. If you want to select the version of the library used, or want to serve it from the local 'static' resources, add a line to your settings.py like so:: - SELECT2_CSS = 'assets/css/select2.css' + SELECT2_CSS = ['assets/css/select2.css'] If you want to add more css (usually used in select2 themes), add a line in settings.py like this:: @@ -93,7 +93,7 @@ class Select2Conf(AppConf): If you provide your own CSS and would not like Django-Select2 to load any, change this setting to a blank string like so:: - SELECT2_CSS = '' + SELECT2_CSS = [] .. tip:: Change this setting to a local asset in your development environment to develop without an Internet connection. diff --git a/django_select2/forms.py b/django_select2/forms.py index 4809d066..045693e1 100644 --- a/django_select2/forms.py +++ b/django_select2/forms.py @@ -127,9 +127,11 @@ def media(self): .. Note:: For more information visit https://docs.djangoproject.com/en/stable/topics/forms/media/#media-as-a-dynamic-property """ - select2_js = [settings.SELECT2_JS] if settings.SELECT2_JS else [] + select2_js = settings.SELECT2_JS if settings.SELECT2_JS else [] select2_css = settings.SELECT2_CSS if settings.SELECT2_CSS else [] + if isinstance(select2_js, str): + select2_js = [select2_js] if isinstance(select2_css, str): select2_css = [select2_css] @@ -227,6 +229,8 @@ class HeavySelect2Mixin: """Mixin that adds select2's AJAX options and registers itself on Django's cache.""" dependent_fields = {} + data_view = None + data_url = None def __init__(self, attrs=None, choices=(), **kwargs): """ @@ -247,8 +251,8 @@ def __init__(self, attrs=None, choices=(), **kwargs): self.uuid = str(uuid.uuid4()) self.field_id = signing.dumps(self.uuid) - self.data_view = kwargs.pop("data_view", None) - self.data_url = kwargs.pop("data_url", None) + self.data_view = kwargs.pop("data_view", self.data_view) + self.data_url = kwargs.pop("data_url", self.data_url) dependent_fields = kwargs.pop("dependent_fields", None) if dependent_fields is not None: @@ -600,7 +604,7 @@ def value_from_datadict(self, data, files, name): # You need to implement this method yourself, to ensure proper object creation. pks = self.queryset.filter(**{'pk__in': list(values)}).values_list('pk', flat=True) pks = set(map(str, pks)) - cleaned_values = list(values) + cleaned_values = list(pks) for val in values - pks: cleaned_values.append(self.queryset.create(title=val).pk) return cleaned_values diff --git a/example/example/migrations/0001_initial.py b/example/example/migrations/0001_initial.py index db45b1bb..a578fdce 100644 --- a/example/example/migrations/0001_initial.py +++ b/example/example/migrations/0001_initial.py @@ -6,7 +6,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/linter-requirements.txt b/linter-requirements.txt index c3083bcb..649a6a1d 100644 --- a/linter-requirements.txt +++ b/linter-requirements.txt @@ -1,5 +1,5 @@ bandit==1.7.4 -black==22.10.0 -flake8==5.0.4 -isort==5.10.1 -pydocstyle[toml]==6.1.1 +black==23.1.0 +flake8==6.0.0 +isort==5.12.0 +pydocstyle[toml]==6.3.0 diff --git a/tests/test_forms.py b/tests/test_forms.py index a3a0d8b5..c7ba75a1 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -269,7 +269,9 @@ def test_selected_option(self, db): in not_required_field.widget.render("primary_genre", 1) or '' in not_required_field.widget.render("primary_genre", 1) - ), (not_required_field.widget.render("primary_genre", 1)) + ), not_required_field.widget.render( + "primary_genre", 1 + ) def test_many_selected_option(self, db, genres): field = HeavySelect2MultipleWidgetForm().fields["genres"] diff --git a/tests/testapp/forms.py b/tests/testapp/forms.py index da9487c5..ed90f222 100644 --- a/tests/testapp/forms.py +++ b/tests/testapp/forms.py @@ -51,6 +51,14 @@ def label_from_instance(self, obj): return force_str(obj.title).upper() +class ArtistDataViewWidget(HeavySelect2Widget): + data_view = "heavy_data_1" + + +class PrimaryGenreDataUrlWidget(HeavySelect2Widget): + data_url = "/heavy_data_2/" + + class AlbumSelect2WidgetForm(forms.ModelForm): class Meta: model = models.Album @@ -143,11 +151,9 @@ class Select2WidgetForm(forms.Form): class HeavySelect2WidgetForm(forms.Form): - artist = forms.ChoiceField( - widget=HeavySelect2Widget(data_view="heavy_data_1"), choices=NUMBER_CHOICES - ) + artist = forms.ChoiceField(widget=ArtistDataViewWidget(), choices=NUMBER_CHOICES) primary_genre = forms.ChoiceField( - widget=HeavySelect2Widget(data_view="heavy_data_2"), + widget=PrimaryGenreDataUrlWidget(), required=False, choices=NUMBER_CHOICES, )