@@ -191,3 +200,27 @@ export class UserAjaxSelect extends AjaxSelect {
return `
${sanitize(item.display_name)}`;
}
}
+
+@registerComponent("group-ajax-select")
+export class GroupsAjaxSelect extends AjaxSelect {
+ protected valueField = "id";
+ protected labelField = "name";
+
+ protected async search(query: string): Promise
{
+ const resp = await groupSearchGroup({ query: { search: query } });
+ if (resp.data) {
+ return resp.data.results;
+ }
+ return [];
+ }
+
+ protected renderOption(item: GroupSchema, sanitize: typeof escape_html) {
+ return `
+ ${sanitize(item.name)}
+
`;
+ }
+
+ protected renderItem(item: GroupSchema, sanitize: typeof escape_html) {
+ return `${sanitize(item.name)}`;
+ }
+}
diff --git a/core/templates/core/widgets/autocomplete_select.jinja b/core/templates/core/widgets/autocomplete_select.jinja
index 30bc7eb5..ab91d766 100644
--- a/core/templates/core/widgets/autocomplete_select.jinja
+++ b/core/templates/core/widgets/autocomplete_select.jinja
@@ -7,4 +7,5 @@
{% endif %}{% endfor %}
+{% for sel in selected %}{{ sel }}{% endfor %}
{{ component }}>
\ No newline at end of file
diff --git a/core/views/widgets/select.py b/core/views/widgets/select.py
index 8bc7f5ea..a83ead7e 100644
--- a/core/views/widgets/select.py
+++ b/core/views/widgets/select.py
@@ -1,11 +1,27 @@
from django.contrib.staticfiles.storage import staticfiles_storage
+from django.db.models import Model
from django.forms import Select, SelectMultiple
+from ninja import ModelSchema
+
+from core.models import Group, User
+from core.schemas import GroupSchema, UserProfileSchema
class AutoCompleteSelectMixin:
component_name = "autocomplete-select"
template_name = "core/widgets/autocomplete_select.jinja"
- is_ajax = False
+ model: Model | None = None
+ schema: ModelSchema | None = None
+ pk = "id"
+
+ def __init__(self, attrs=None, choices=()):
+ if self.is_ajax:
+ choices = () # Avoid computing anything when in ajax mode
+ super().__init__(attrs=attrs, choices=choices)
+
+ @property
+ def is_ajax(self):
+ return self.model and self.schema
def optgroups(self, name, value, attrs=None):
"""Don't create option groups when doing ajax"""
@@ -27,6 +43,13 @@ class AutoCompleteSelectMixin:
staticfiles_storage.url("core/components/ajax-select.scss"),
],
}
+ if self.is_ajax:
+ context["selected"] = [
+ self.schema.from_orm(obj).json()
+ for obj in self.model.objects.filter(
+ **{f"{self.pk}__in": context["widget"]["value"]}
+ ).all()
+ ]
return context
@@ -38,9 +61,23 @@ class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ...
class AutoCompleteSelectUser(AutoCompleteSelectMixin, Select):
component_name = "user-ajax-select"
- is_ajax = True
+ model = User
+ schema = UserProfileSchema
class AutoCompleteSelectMultipleUser(AutoCompleteSelectMixin, SelectMultiple):
component_name = "user-ajax-select"
- is_ajax = True
+ model = User
+ schema = UserProfileSchema
+
+
+class AutoCompleteSelectGroup(AutoCompleteSelectMixin, Select):
+ component_name = "group-ajax-select"
+ model = Group
+ schema = GroupSchema
+
+
+class AutoCompleteSelectMultipleGroup(AutoCompleteSelectMixin, SelectMultiple):
+ component_name = "group-ajax-select"
+ model = Group
+ schema = GroupSchema
diff --git a/election/views.py b/election/views.py
index 1f189f41..422205fd 100644
--- a/election/views.py
+++ b/election/views.py
@@ -15,7 +15,7 @@ from core.views.forms import SelectDateTime
from core.views.widgets.markdown import MarkdownInput
from core.views.widgets.select import (
AutoCompleteSelect,
- AutoCompleteSelectMultiple,
+ AutoCompleteSelectMultipleGroup,
AutoCompleteSelectUser,
)
from election.models import Candidature, Election, ElectionList, Role, Vote
@@ -157,10 +157,10 @@ class ElectionForm(forms.ModelForm):
"candidature_groups",
]
widgets = {
- "edit_groups": AutoCompleteSelectMultiple,
- "view_groups": AutoCompleteSelectMultiple,
- "vote_groups": AutoCompleteSelectMultiple,
- "candidature_groups": AutoCompleteSelectMultiple,
+ "edit_groups": AutoCompleteSelectMultipleGroup,
+ "view_groups": AutoCompleteSelectMultipleGroup,
+ "vote_groups": AutoCompleteSelectMultipleGroup,
+ "candidature_groups": AutoCompleteSelectMultipleGroup,
}
start_date = forms.DateTimeField(