From 8b419dcee6db52d5f7f18372bc8e8236c8cd38f2 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 20 Oct 2024 23:25:56 +0200 Subject: [PATCH] Remove ajax_select from core --- core/api.py | 4 +-- core/schemas.py | 7 +++++ .../core/components/ajax-select-index.ts | 27 +++++++++++++++++++ core/views/files.py | 19 +++++++------ core/views/widgets/select.py | 16 +++++++++-- 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/core/api.py b/core/api.py index 445d79d2..1662cb84 100644 --- a/core/api.py +++ b/core/api.py @@ -75,8 +75,8 @@ class SithFileController(ControllerBase): permissions=[CanAccessLookup], ) @paginate(PageNumberPaginationExtra, page_size=50) - def search_files(self, query: Annotated[str, annotated_types.MinLen(1)]): - return SithFile.objects.filter(is_in_sas=False).filter(name__icontains=query) + def search_files(self, search: Annotated[str, annotated_types.MinLen(1)]): + return SithFile.objects.filter(is_in_sas=False).filter(name__icontains=search) @api_controller("/group") diff --git a/core/schemas.py b/core/schemas.py index d5c46ea7..775cd6b0 100644 --- a/core/schemas.py +++ b/core/schemas.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import Annotated from annotated_types import MinLen @@ -50,6 +51,12 @@ class SithFileSchema(ModelSchema): model = SithFile fields = ["id", "name"] + path: str + + @staticmethod + def resolve_path(obj: SithFile) -> str: + return str(Path(obj.get_parent_path()) / obj.name) + class GroupSchema(ModelSchema): class Meta: diff --git a/core/static/webpack/core/components/ajax-select-index.ts b/core/static/webpack/core/components/ajax-select-index.ts index 4aabe69f..db9f4f21 100644 --- a/core/static/webpack/core/components/ajax-select-index.ts +++ b/core/static/webpack/core/components/ajax-select-index.ts @@ -4,8 +4,10 @@ import type { TomOption } from "tom-select/dist/types/types"; import type { escape_html } from "tom-select/dist/types/utils"; import { type GroupSchema, + type SithFileSchema, type UserProfileSchema, groupSearchGroup, + sithfileSearchFiles, userSearchUsers, } from "#openapi"; @@ -71,3 +73,28 @@ export class GroupsAjaxSelect extends AjaxSelect { return `${sanitize(item.name)}`; } } + +@registerComponent("sith-file-ajax-select") +export class SithFileAjaxSelect extends AjaxSelect { + protected valueField = "id"; + protected labelField = "path"; + protected searchField = ["path", "name"]; + + protected async search(query: string): Promise { + const resp = await sithfileSearchFiles({ query: { search: query } }); + if (resp.data) { + return resp.data.results; + } + return []; + } + + protected renderOption(item: SithFileSchema, sanitize: typeof escape_html) { + return `
+ ${sanitize(item.path)} +
`; + } + + protected renderItem(item: SithFileSchema, sanitize: typeof escape_html) { + return `${sanitize(item.path)}`; + } +} diff --git a/core/views/files.py b/core/views/files.py index 3df5c014..d70991b7 100644 --- a/core/views/files.py +++ b/core/views/files.py @@ -18,7 +18,6 @@ from urllib.parse import quote, urljoin # This file contains all the views that concern the page model from wsgiref.util import FileWrapper -from ajax_select import make_ajax_field from django import forms from django.conf import settings from django.core.exceptions import PermissionDenied @@ -39,6 +38,11 @@ from core.views import ( CanViewMixin, can_view, ) +from core.views.widgets.select import ( + AutoCompleteSelectMultipleGroup, + AutoCompleteSelectSithFile, + AutoCompleteSelectUser, +) from counter.utils import is_logged_in_counter @@ -217,14 +221,13 @@ class FileEditPropForm(forms.ModelForm): class Meta: model = SithFile fields = ["parent", "owner", "edit_groups", "view_groups"] + widgets = { + "parent": AutoCompleteSelectSithFile, + "owner": AutoCompleteSelectUser, + "edit_groups": AutoCompleteSelectMultipleGroup, + "view_groups": AutoCompleteSelectMultipleGroup, + } - parent = make_ajax_field(SithFile, "parent", "files", help_text="") - edit_groups = make_ajax_field( - SithFile, "edit_groups", "groups", help_text="", label=_("edit group") - ) - view_groups = make_ajax_field( - SithFile, "view_groups", "groups", help_text="", label=_("view group") - ) recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False) diff --git a/core/views/widgets/select.py b/core/views/widgets/select.py index cd951940..a02df82d 100644 --- a/core/views/widgets/select.py +++ b/core/views/widgets/select.py @@ -3,8 +3,8 @@ 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 +from core.models import Group, SithFile, User +from core.schemas import GroupSchema, SithFileSchema, UserProfileSchema class AutoCompleteSelectMixin: @@ -91,3 +91,15 @@ class AutoCompleteSelectMultipleGroup(AutoCompleteSelectMixin, SelectMultiple): component_name = "group-ajax-select" model = Group schema = GroupSchema + + +class AutoCompleteSelectSithFile(AutoCompleteSelectMixin, Select): + component_name = "sith-file-ajax-select" + model = SithFile + schema = SithFileSchema + + +class AutoCompleteSelectMultipleSithFile(AutoCompleteSelectMixin, SelectMultiple): + component_name = "sith-file-ajax-select" + model = SithFile + schema = SithFileSchema