2024-10-19 23:05:34 +00:00
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
2024-10-20 11:33:44 +00:00
|
|
|
from django.db.models import Model
|
2024-10-19 23:05:34 +00:00
|
|
|
from django.forms import Select, SelectMultiple
|
2024-10-20 11:33:44 +00:00
|
|
|
from ninja import ModelSchema
|
|
|
|
|
|
|
|
from core.models import Group, User
|
|
|
|
from core.schemas import GroupSchema, UserProfileSchema
|
2024-10-19 23:05:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoCompleteSelectMixin:
|
|
|
|
component_name = "autocomplete-select"
|
|
|
|
template_name = "core/widgets/autocomplete_select.jinja"
|
2024-10-20 11:33:44 +00:00
|
|
|
model: Model | None = None
|
|
|
|
schema: ModelSchema | None = None
|
|
|
|
pk = "id"
|
|
|
|
|
2024-10-20 11:40:59 +00:00
|
|
|
js = [
|
|
|
|
"webpack/core/components/ajax-select-index.ts",
|
|
|
|
]
|
|
|
|
css = [
|
|
|
|
"webpack/core/components/ajax-select-index.css",
|
|
|
|
"core/components/ajax-select.scss",
|
|
|
|
]
|
|
|
|
|
2024-10-20 11:33:44 +00:00
|
|
|
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
|
2024-10-19 23:05:34 +00:00
|
|
|
|
|
|
|
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):
|
|
|
|
context = super().get_context(name, value, attrs)
|
2024-10-20 15:37:51 +00:00
|
|
|
context["widget"]["attrs"]["autocomplete"] = "off"
|
2024-10-19 23:05:34 +00:00
|
|
|
context["component"] = self.component_name
|
|
|
|
context["statics"] = {
|
2024-10-20 11:40:59 +00:00
|
|
|
"js": [staticfiles_storage.url(file) for file in self.js],
|
|
|
|
"css": [staticfiles_storage.url(file) for file in self.css],
|
2024-10-19 23:05:34 +00:00
|
|
|
}
|
2024-10-20 11:33:44 +00:00
|
|
|
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()
|
|
|
|
]
|
2024-10-19 23:05:34 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class AutoCompleteSelect(AutoCompleteSelectMixin, Select): ...
|
|
|
|
|
|
|
|
|
|
|
|
class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ...
|
|
|
|
|
|
|
|
|
|
|
|
class AutoCompleteSelectUser(AutoCompleteSelectMixin, Select):
|
|
|
|
component_name = "user-ajax-select"
|
2024-10-20 11:33:44 +00:00
|
|
|
model = User
|
|
|
|
schema = UserProfileSchema
|
2024-10-19 23:05:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoCompleteSelectMultipleUser(AutoCompleteSelectMixin, SelectMultiple):
|
|
|
|
component_name = "user-ajax-select"
|
2024-10-20 11:33:44 +00:00
|
|
|
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
|