From 8e28570fb409f038a34ea22813ffab94e4262dc8 Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Thu, 8 Oct 2020 18:22:46 +0200
Subject: [PATCH] Add select2 to remaining forms and new template for
 django-material

---
 aleksis/core/forms.py                         | 20 +++++++++++++-----
 .../templates/core/group/child_groups.html    | 13 ++++++++++--
 .../core/templates/core/person/accounts.html  |  9 +++++++-
 aleksis/core/templates/core/person/edit.html  |  5 ++++-
 .../fields/django_select2_select2widget.html  | 21 +++++++++++++++++++
 5 files changed, 59 insertions(+), 9 deletions(-)
 create mode 100644 aleksis/core/templates/material/fields/django_select2_select2widget.html

diff --git a/aleksis/core/forms.py b/aleksis/core/forms.py
index 28b8b9456..86dfed9da 100644
--- a/aleksis/core/forms.py
+++ b/aleksis/core/forms.py
@@ -5,7 +5,7 @@ from django.contrib.auth import get_user_model
 from django.core.exceptions import ValidationError
 from django.utils.translation import gettext_lazy as _
 
-from django_select2.forms import ModelSelect2MultipleWidget, Select2Widget
+from django_select2.forms import ModelSelect2MultipleWidget, ModelSelect2Widget, Select2Widget
 from dynamic_preferences.forms import PreferenceForm
 from material import Fieldset, Layout, Row
 
@@ -24,7 +24,7 @@ class PersonAccountForm(forms.ModelForm):
     class Meta:
         model = Person
         fields = ["last_name", "first_name", "user"]
-        widgets = {"user": Select2Widget}
+        widgets = {"user": Select2Widget(attrs={"class": "browser-default"})}
 
     new_user = forms.CharField(required=False)
 
@@ -107,9 +107,19 @@ class EditPersonForm(ExtensibleForm):
             "primary_group",
         ]
         widgets = {
-            "user": Select2Widget,
-            "primary_group": Select2Widget,
-            "guardians": Select2Widget,
+            "user": Select2Widget(attrs={"class": "browser-default"}),
+            "primary_group": ModelSelect2Widget(
+                search_fields=["name__icontains", "short_name__icontains"],
+                attrs={"data-minimum-input-length": 0, "class": "browser-default"}
+            ),
+            "guardians": ModelSelect2Widget(
+                search_fields=[
+                    "first_name__icontains",
+                    "last_name__icontains",
+                    "short_name__icontains",
+                ],
+                attrs={"data-minimum-input-length": 0, "class": "browser-default"}
+            ),
         }
 
     new_user = forms.CharField(
diff --git a/aleksis/core/templates/core/group/child_groups.html b/aleksis/core/templates/core/group/child_groups.html
index bb5a429f6..589f8a6be 100644
--- a/aleksis/core/templates/core/group/child_groups.html
+++ b/aleksis/core/templates/core/group/child_groups.html
@@ -2,7 +2,14 @@
 
 {% extends "core/base.html" %}
 
-{% load i18n material_form %}
+{% load i18n material_form any_js %}
+
+
+{% block extra_head %}
+    {{ form.media.css }}
+    {% include_css "select2-materialize" %}
+{% endblock %}
+
 
 {% block browser_title %}{% blocktrans %}Assign child groups to groups{% endblocktrans %}{% endblock %}
 {% block page_title %}
@@ -119,7 +126,7 @@
 
       {% csrf_token %}
 
-      {% include "components/chips.html" with form_field=form.child_groups %}
+      {% form form=form %}{% endform %}
 
       <p class="left">
         {% if page.has_previous %}
@@ -151,4 +158,6 @@
       </p>
     </form>
   {% endif %}
+  {% include_js "select2-materialize" %}
+  {{ form.media.js }}
 {% endblock %}
diff --git a/aleksis/core/templates/core/person/accounts.html b/aleksis/core/templates/core/person/accounts.html
index 672b089aa..03725518d 100644
--- a/aleksis/core/templates/core/person/accounts.html
+++ b/aleksis/core/templates/core/person/accounts.html
@@ -2,7 +2,12 @@
 
 {% extends "core/base.html" %}
 
-{% load i18n %}
+{% load i18n any_js %}
+
+{% block extra_head %}
+  {{ persons_accounts_formset.media.css }}
+  {% include_css "select2-materialize" %}
+{% endblock %}
 
 {% block browser_title %}{% blocktrans %}Link persons to accounts{% endblocktrans %}{% endblock %}
 {% block page_title %}
@@ -55,4 +60,6 @@
       {% blocktrans %}Update{% endblocktrans %}
     </button>
   </form>
+  {% include_js "select2-materialize" %}
+  {{ persons_accounts_formset.media.js }}
 {% endblock %}
diff --git a/aleksis/core/templates/core/person/edit.html b/aleksis/core/templates/core/person/edit.html
index 261249f68..3bf16ca35 100644
--- a/aleksis/core/templates/core/person/edit.html
+++ b/aleksis/core/templates/core/person/edit.html
@@ -2,10 +2,11 @@
 
 {% extends "core/base.html" %}
 
-{% load material_form i18n %}
+{% load material_form i18n any_js %}
 
 {% block extra_head %}
   {{ edit_person_form.media }}
+  {% include_css "select2-materialize" %}
 {% endblock %}
 
 {% block browser_title %}{% blocktrans %}Edit person{% endblocktrans %}{% endblock %}
@@ -19,5 +20,7 @@
     {% form form=edit_person_form %}{% endform %}
     {% include "core/partials/save_button.html" %}
   </form>
+  {% include_js "select2-materialize" %}
+  {{ edit_group_form.media.js }}
 
 {% endblock %}
diff --git a/aleksis/core/templates/material/fields/django_select2_select2widget.html b/aleksis/core/templates/material/fields/django_select2_select2widget.html
new file mode 100644
index 000000000..1b19d8ec8
--- /dev/null
+++ b/aleksis/core/templates/material/fields/django_select2_select2widget.html
@@ -0,0 +1,21 @@
+{% load l10n material_form material_form_internal %}
+{% part bound_field.field %}<{{ field.widget.component|default:'dmc-select' }}>
+  <label>{{ bound_field.label }}</label>
+  <div class="row">
+    <div{% attrs bound_field 'group' %}
+        id="id_{{ bound_field.html_name }}_container"
+        class="input-field col s12{% if field.required %} required{% endif %}{% if bound_field.errors %} has-error{% endif %}"
+        style="margin-top: 0"
+    {% endattrs %}>
+        {% part field prefix %}{% endpart %}
+        {% part field control %}
+        {{ bound_field }}
+        {% endpart %}
+        {% part field help_text %}{% if field.help_text %}
+            <div class="help-block">{{ bound_field.help_text|safe }}</div>
+        {% endif %}{% endpart %}{% part field errors %}
+        {% if bound_field.errors %}
+            {% include  'material/field_errors.html' %}
+        {% endif %}{% endpart %}{{ hidden_initial }}
+    </div>
+</div></{{ field.widget.component|default:'dmc-select' }}>{% endpart %}
-- 
GitLab