Sith/core/views/widgets/select.py

112 lines
3.4 KiB
Python
Raw Normal View History

from collections.abc import Collection
from typing import Any
from django.contrib.staticfiles.storage import staticfiles_storage
from django.db.models import Model, QuerySet
from django.forms import Select, SelectMultiple
2024-10-20 11:33:44 +00:00
from ninja import ModelSchema
from pydantic import TypeAdapter
2024-10-20 11:33:44 +00:00
2024-10-20 21:25:56 +00:00
from core.models import Group, SithFile, User
from core.schemas import GroupSchema, SithFileSchema, UserProfileSchema
class AutoCompleteSelectMixin:
component_name = "autocomplete-select"
template_name = "core/widgets/autocomplete_select.jinja"
model: type[Model] | None = None
adapter: TypeAdapter[Collection[ModelSchema]] | None = None
2024-10-20 11:33:44 +00:00
pk = "id"
js = [
"bundled/core/components/ajax-select-index.ts",
]
css = [
2024-11-19 00:56:55 +00:00
"bundled/ajax-select-index.css",
"core/components/ajax-select.scss",
]
def get_queryset(self, pks: Collection[Any]) -> QuerySet:
return self.model.objects.filter(
**{
f"{self.pk}__in": [
pk
for pk in pks
if str(pk).isdigit() # We filter empty values for create views
]
}
).all()
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.adapter and self.model
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"
context["component"] = self.component_name
context["statics"] = {
"js": [staticfiles_storage.url(file) for file in self.js],
"css": [staticfiles_storage.url(file) for file in self.css],
}
2024-10-20 11:33:44 +00:00
if self.is_ajax:
context["initial"] = self.adapter.dump_json(
self.adapter.validate_python(
self.get_queryset(context["widget"]["value"])
)
).decode("utf-8")
return context
class AutoCompleteSelect(AutoCompleteSelectMixin, Select): ...
class AutoCompleteSelectMultiple(AutoCompleteSelectMixin, SelectMultiple): ...
class AutoCompleteSelectUser(AutoCompleteSelect):
component_name = "user-ajax-select"
2024-10-20 11:33:44 +00:00
model = User
adapter = TypeAdapter(list[UserProfileSchema])
class AutoCompleteSelectMultipleUser(AutoCompleteSelectMultiple):
component_name = "user-ajax-select"
2024-10-20 11:33:44 +00:00
model = User
adapter = TypeAdapter(list[UserProfileSchema])
2024-10-20 11:33:44 +00:00
class AutoCompleteSelectGroup(AutoCompleteSelect):
2024-10-20 11:33:44 +00:00
component_name = "group-ajax-select"
model = Group
adapter = TypeAdapter(list[GroupSchema])
2024-10-20 11:33:44 +00:00
class AutoCompleteSelectMultipleGroup(AutoCompleteSelectMultiple):
2024-10-20 11:33:44 +00:00
component_name = "group-ajax-select"
model = Group
adapter = TypeAdapter(list[GroupSchema])
2024-10-20 21:25:56 +00:00
class AutoCompleteSelectSithFile(AutoCompleteSelect):
2024-10-20 21:25:56 +00:00
component_name = "sith-file-ajax-select"
model = SithFile
adapter = TypeAdapter(list[SithFileSchema])
2024-10-20 21:25:56 +00:00
class AutoCompleteSelectMultipleSithFile(AutoCompleteSelectMultiple):
2024-10-20 21:25:56 +00:00
component_name = "sith-file-ajax-select"
model = SithFile
adapter = TypeAdapter(list[SithFileSchema])