Skip to content

Commit

Permalink
fix: Dropped django-select2 dependency (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun authored Mar 21, 2024
1 parent 7b97985 commit 6a6ee2a
Show file tree
Hide file tree
Showing 25 changed files with 438 additions and 462 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
Changelog
=========

1.3.0 (2024-03-21)
==================

* feat: Add abstract base model `AbstractFrontendUIItem` by @fsbraun in https://github.com/django-cms/djangocms-frontend/pull/195
* feat: Add icons for selected text-enabled plugins by @fsbraun in https://github.com/django-cms/djangocms-frontend/pull/195
* fix: Correct site used when using Link plugin within a static placholder in django CMS 3.x by @fsbraun
* fix: removed Nav Container plugin and fixed Navigation Link plugin by @fsbraun in https://github.com/django-cms/djangocms-frontend/pull/192
* fix: Remove `{% spaceless %}` around `{% block "content" %}` by @fsbraun in https://github.com/django-cms/djangocms-frontend/pull/188
* fix: Improved fieldset layout for Django 4.2+ by @fsbraun in https://github.com/django-cms/djangocms-frontend/pull/185
* fix: Dropped django-select2 dependency


1.2.2 (2024-01-13)
==================

Expand Down
2 changes: 1 addition & 1 deletion djangocms_frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
13. Github actions will publish the new package to pypi
"""

__version__ = "1.2.2"
__version__ = "1.3.0"
75 changes: 50 additions & 25 deletions djangocms_frontend/contrib/link/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import json
from types import SimpleNamespace

from django import apps, forms
from django.conf import settings as django_settings
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
from django.contrib.admin.widgets import SELECT2_TRANSLATIONS, AutocompleteMixin
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import models
from django.db.models.fields.related import ManyToOneRel
from django.urls import reverse
from django.utils.encoding import force_str
from django.utils.translation import get_language
from django.utils.translation import gettext as _
from django_select2.forms import HeavySelect2Widget, Select2Widget

# from djangocms_link.validators import IntranetURLValidator
from entangled.forms import EntangledModelForm
Expand Down Expand Up @@ -56,30 +59,9 @@ def __init__(self, *args, **kwargs):
)


class Select2jqWidget(HeavySelect2Widget if MINIMUM_INPUT_LENGTH else Select2Widget):
"""Make jQuery available to Select2 widget"""

class Select2jqWidget(AutocompleteMixin, forms.Select):
empty_label = _("Select a destination")

@property
def media(self):
extra = ".min"
i18n_name = SELECT2_TRANSLATIONS.get(get_language())
i18n_file = (
("admin/js/vendor/select2/i18n/%s.js" % i18n_name,) if i18n_name else ()
)
return forms.Media(
js=("admin/js/vendor/select2/select2.full%s.js" % extra,)
+ i18n_file
+ ("djangocms_frontend/js/django_select2.js",),
css={
"screen": (
"admin/css/vendor/select2/select2%s.css" % extra,
"djangocms_frontend/css/select2.css",
),
},
)

def __init__(self, *args, **kwargs):
if MINIMUM_INPUT_LENGTH:
if "attrs" in kwargs:
Expand All @@ -88,9 +70,52 @@ def __init__(self, *args, **kwargs):
)
else:
kwargs["attrs"] = {"data-minimum-input-length": MINIMUM_INPUT_LENGTH}
kwargs.setdefault("data_view", "dcf_autocomplete:ac_view")
kwargs.setdefault("admin_site", None)
kwargs.setdefault(
"field",
SimpleNamespace(name="", model=SimpleNamespace(
_meta=SimpleNamespace(app="djangocms_frontend", label="link")
))
) # Fake field properties for autocomplete field (unused by link)
super().__init__(*args, **kwargs)

def get_url(self):
return reverse("dcf_autocomplete:ac_view")

def build_attrs(self, base_attrs, extra_attrs=None):
"""
Set select2's AJAX attributes.
Attributes can be set using the html5 data attribute.
Nested attributes require a double dash as per
https://select2.org/configuration/data-attributes#nested-subkey-options
"""
attrs = super(forms.Select, self).build_attrs(base_attrs, extra_attrs=extra_attrs)
attrs.setdefault("class", "")
i18n_name = getattr(self, "i18n_name", SELECT2_TRANSLATIONS.get(get_language())) # Django 3.2 compat
attrs.update(
{
"data-ajax--cache": "true",
"data-ajax--delay": 250,
"data-ajax--type": "GET",
"data-ajax--url": self.get_url(),
"data-theme": "admin-autocomplete",
"data-app-label": "app",
"data-model-name": "model",
"data-field-name": "field",
"data-allow-clear": json.dumps(not self.is_required),
"data-placeholder": "", # Allows clearing of the input.
"lang": i18n_name,
"class": attrs["class"]
+ (" " if attrs["class"] else "")
+ "admin-autocomplete",
}
)
return attrs

def optgroups(self, name, value, attr=None):
return super(forms.Select, self).optgroups(name, value)


class SmartLinkField(forms.ChoiceField):
widget = Select2jqWidget
Expand Down
4 changes: 3 additions & 1 deletion djangocms_frontend/contrib/link/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ def get_object_for_value(value):
return None


def get_link_choices(request, term="", lang=None, nbsp=""):
def get_link_choices(request, term="", lang=None, nbsp=None):
global _querysets

if nbsp is None:
nbsp = "" if term else "\u2000"
available_objects = []
# Now create our list of cms pages
type_id = ContentType.objects.get_for_model(Page).id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

{% block extrahead %}
{{ block.super }}
<style> {# Bugfix for Django admin styling #}
@media screen {
.change-form .select2-dropdown {
background: var(--body-bg);
border: 1px solid var(--border-color);
}
}
</style>
<script src="{% static 'djangocms_frontend/js/bundle.link.js' %}"></script>
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion djangocms_frontend/contrib/link/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get(self, request, *args, **kwargs):

# TODO Check permissions
# ======================
self.term = kwargs.get("term", request.GET.get("term", ""))
self.term = kwargs.get("term", request.GET.get("term", "")).strip()
results = get_link_choices(request, self.term)
return JsonResponse(
{
Expand Down
Binary file modified djangocms_frontend/locale/ar/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 6a6ee2a

Please sign in to comment.