Add ajax user widget and remove ajax_select from elections

This commit is contained in:
Antoine Bartuccio 2024-10-19 22:00:58 +02:00
parent 8be8328830
commit ce4f57bd8f
3 changed files with 35 additions and 8 deletions

View File

@ -3,8 +3,8 @@
<link-once rel="stylesheet" type="text/css" href="{{ css }}" defer></link-once> <link-once rel="stylesheet" type="text/css" href="{{ css }}" defer></link-once>
{% endfor %} {% endfor %}
<autocomplete-select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %} <{{ component }} name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}
<optgroup label="{{ group_name }}">{% endif %}{% for widget in group_choices %} <optgroup label="{{ group_name }}">{% endif %}{% for widget in group_choices %}
{% include widget.template_name %}{% endfor %}{% if group_name %} {% include widget.template_name %}{% endfor %}{% if group_name %}
</optgroup>{% endif %}{% endfor %} </optgroup>{% endif %}{% endfor %}
</autocomplete-select> </{{ component }}>

View File

@ -81,10 +81,19 @@ class MarkdownInput(Textarea):
class AutoCompleteSelectMixin: class AutoCompleteSelectMixin:
component_name = "autocomplete-select"
template_name = "core/widgets/autocomplete_select.jinja" template_name = "core/widgets/autocomplete_select.jinja"
is_ajax = False
def optgroups(self, name, value, attrs=None):
"""Don't create option groups when doing ajax"""
if self.is_ajax:
return []
return super().optgroups(name, value, attrs=attrs)
def get_context(self, name, value, attrs): def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs) context = super().get_context(name, value, attrs)
context["component"] = self.component_name
context["statics"] = { context["statics"] = {
"js": staticfiles_storage.url( "js": staticfiles_storage.url(
"webpack/core/components/ajax-select-index.ts" "webpack/core/components/ajax-select-index.ts"
@ -105,6 +114,16 @@ class AutoCompleteSelect(AutoCompleteSelectMixin, Select): ...
class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ... class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ...
class AutoCompleteSelectUser(AutoCompleteSelectMixin, Select):
component_name = "user-ajax-select"
is_ajax = True
class AutoCompleteSelectMultipleUser(AutoCompleteSelectMixin, SelectMultiple):
component_name = "user-ajax-select"
is_ajax = True
class NFCTextInput(TextInput): class NFCTextInput(TextInput):
template_name = "core/widgets/nfc.jinja" template_name = "core/widgets/nfc.jinja"

View File

@ -1,6 +1,5 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from ajax_select.fields import AutoCompleteSelectField
from django import forms from django import forms
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.db import transaction from django.db import transaction
@ -13,7 +12,9 @@ from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateVi
from core.views import CanCreateMixin, CanEditMixin, CanViewMixin from core.views import CanCreateMixin, CanEditMixin, CanViewMixin
from core.views.forms import ( from core.views.forms import (
AutoCompleteSelect,
AutoCompleteSelectMultiple, AutoCompleteSelectMultiple,
AutoCompleteSelectUser,
MarkdownInput, MarkdownInput,
SelectDateTime, SelectDateTime,
) )
@ -54,11 +55,15 @@ class CandidateForm(forms.ModelForm):
class Meta: class Meta:
model = Candidature model = Candidature
fields = ["user", "role", "program", "election_list"] fields = ["user", "role", "program", "election_list"]
widgets = {"program": MarkdownInput} labels = {
"user": _("User to candidate"),
user = AutoCompleteSelectField( }
"users", label=_("User to candidate"), help_text=None, required=True widgets = {
) "program": MarkdownInput,
"user": AutoCompleteSelectUser,
"role": AutoCompleteSelect,
"election_list": AutoCompleteSelect,
}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
election_id = kwargs.pop("election_id", None) election_id = kwargs.pop("election_id", None)
@ -100,6 +105,7 @@ class RoleForm(forms.ModelForm):
class Meta: class Meta:
model = Role model = Role
fields = ["title", "election", "description", "max_choice"] fields = ["title", "election", "description", "max_choice"]
widgets = {"election": AutoCompleteSelect}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
election_id = kwargs.pop("election_id", None) election_id = kwargs.pop("election_id", None)
@ -123,6 +129,7 @@ class ElectionListForm(forms.ModelForm):
class Meta: class Meta:
model = ElectionList model = ElectionList
fields = ("title", "election") fields = ("title", "election")
widgets = {"election": AutoCompleteSelect}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
election_id = kwargs.pop("election_id", None) election_id = kwargs.pop("election_id", None)
@ -320,6 +327,7 @@ class CandidatureCreateView(CanCreateMixin, CreateView):
"""Verify that the selected user is in candidate group.""" """Verify that the selected user is in candidate group."""
obj = form.instance obj = form.instance
obj.election = Election.objects.get(id=self.election.id) obj.election = Election.objects.get(id=self.election.id)
obj.user = obj.user if hasattr(obj, "user") else self.request.user
if (obj.election.can_candidate(obj.user)) and ( if (obj.election.can_candidate(obj.user)) and (
obj.user == self.request.user or self.can_edit obj.user == self.request.user or self.can_edit
): ):