Remove ajax_select from sas

This commit is contained in:
Antoine Bartuccio 2024-10-21 10:30:35 +02:00
parent 8b419dcee6
commit afdc6b69df
5 changed files with 95 additions and 21 deletions

View File

@ -1,3 +1,6 @@
from typing import Annotated
from annotated_types import MinLen
from django.conf import settings
from django.db.models import F
from django.urls import reverse
@ -9,10 +12,11 @@ from ninja_extra.permissions import IsAuthenticated
from ninja_extra.schemas import PaginatedResponseSchema
from pydantic import NonNegativeInt
from core.api_permissions import CanView, IsInGroup, IsRoot
from core.api_permissions import CanAccessLookup, CanView, IsInGroup, IsRoot
from core.models import Notification, User
from sas.models import PeoplePictureRelation, Picture
from sas.models import Album, PeoplePictureRelation, Picture
from sas.schemas import (
AlbumSchema,
IdentifiedUserSchema,
ModerationRequestSchema,
PictureFilterSchema,
@ -22,6 +26,18 @@ from sas.schemas import (
IsSasAdmin = IsRoot | IsInGroup(settings.SITH_GROUP_SAS_ADMIN_ID)
@api_controller("/sas/album")
class AlbumController(ControllerBase):
@route.get(
"/search",
response=PaginatedResponseSchema[AlbumSchema],
permissions=[CanAccessLookup],
)
@paginate(PageNumberPaginationExtra, page_size=50)
def search_album(self, search: Annotated[str, MinLen(1)]):
return Album.objects.filter(name__icontains=search)
@api_controller("/sas/picture")
class PicturesController(ControllerBase):
@route.get(

View File

@ -1,14 +1,14 @@
from typing import Any
from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectMultipleField
from django import forms
from django.utils.translation import gettext_lazy as _
from core.models import User
from core.views import MultipleImageField
from core.views.forms import SelectDate
from sas.models import Album, PeoplePictureRelation, Picture, PictureModerationRequest
from core.views.widgets.select import AutoCompleteSelectMultipleGroup
from sas.models import Album, Picture, PictureModerationRequest
from sas.widgets.select import AutoCompleteSelectAlbum
class SASForm(forms.Form):
@ -62,34 +62,24 @@ class SASForm(forms.Form):
)
class RelationForm(forms.ModelForm):
class Meta:
model = PeoplePictureRelation
fields = ["picture"]
widgets = {"picture": forms.HiddenInput}
users = AutoCompleteSelectMultipleField(
"users", show_help_text=False, help_text="", label=_("Add user"), required=False
)
class PictureEditForm(forms.ModelForm):
class Meta:
model = Picture
fields = ["name", "parent"]
parent = make_ajax_field(Picture, "parent", "files", help_text="")
widgets = {"parent": AutoCompleteSelectAlbum}
class AlbumEditForm(forms.ModelForm):
class Meta:
model = Album
fields = ["name", "date", "file", "parent", "edit_groups"]
widgets = {
"parent": AutoCompleteSelectAlbum,
"edit_groups": AutoCompleteSelectMultipleGroup,
}
name = forms.CharField(max_length=Album.NAME_MAX_LENGTH, label=_("file name"))
date = forms.DateField(label=_("Date"), widget=SelectDate, required=True)
parent = make_ajax_field(Album, "parent", "files", help_text="")
edit_groups = make_ajax_field(Album, "edit_groups", "groups", help_text="")
recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False)

View File

@ -1,11 +1,24 @@
from datetime import datetime
from pathlib import Path
from django.urls import reverse
from ninja import FilterSchema, ModelSchema, Schema
from pydantic import Field, NonNegativeInt
from core.schemas import SimpleUserSchema, UserProfileSchema
from sas.models import Picture, PictureModerationRequest
from sas.models import Album, Picture, PictureModerationRequest
class AlbumSchema(ModelSchema):
class Meta:
model = Album
fields = ["id", "name"]
path: str
@staticmethod
def resolve_path(obj: Album) -> str:
return str(Path(obj.get_parent_path()) / obj.name)
class PictureFilterSchema(FilterSchema):

View File

@ -0,0 +1,30 @@
import { AjaxSelect } from "#core:core/components/ajax-select-base";
import { registerComponent } from "#core:utils/web-components";
import type { TomOption } from "tom-select/dist/types/types";
import type { escape_html } from "tom-select/dist/types/utils";
import { type AlbumSchema, albumSearchAlbum } from "#openapi";
@registerComponent("album-ajax-select")
export class AlbumAjaxSelect extends AjaxSelect {
protected valueField = "id";
protected labelField = "path";
protected searchField = ["path", "name"];
protected async search(query: string): Promise<TomOption[]> {
const resp = await albumSearchAlbum({ query: { search: query } });
if (resp.data) {
return resp.data.results;
}
return [];
}
protected renderOption(item: AlbumSchema, sanitize: typeof escape_html) {
return `<div class="select-item">
<span class="select-item-text">${sanitize(item.path)}</span>
</div>`;
}
protected renderItem(item: AlbumSchema, sanitize: typeof escape_html) {
return `<span>${sanitize(item.path)}</span>`;
}
}

25
sas/widgets/select.py Normal file
View File

@ -0,0 +1,25 @@
from django.forms import Select, SelectMultiple
from core.views.widgets.select import AutoCompleteSelectMixin
from sas.models import Album
from sas.schemas import AlbumSchema
class AutoCompleteSelectAlbum(AutoCompleteSelectMixin, Select):
component_name = "album-ajax-select"
model = Album
schema = AlbumSchema
js = [
"webpack/sas/components/ajax-select-index.ts",
]
class AutoCompleteSelectMultipleAlbum(AutoCompleteSelectMixin, SelectMultiple):
component_name = "album-ajax-select"
model = Album
schema = AlbumSchema
js = [
"webpack/sas/components/ajax-select-index.ts",
]