From db068aa9fd5051a5065949e2e981b5a827028ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Srokosz?= Date: Fri, 12 Jul 2024 18:03:13 +0200 Subject: [PATCH] Allow to set limit of tags returned by TagListResource (#960) --- mwdb/resources/tag.py | 11 +++++++++++ mwdb/schema/tag.py | 1 + mwdb/web/src/commons/api/index.tsx | 4 ++-- mwdb/web/src/components/ShowObject/common/TagForm.tsx | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/mwdb/resources/tag.py b/mwdb/resources/tag.py index 2ddd7b5e1..a5313f89c 100644 --- a/mwdb/resources/tag.py +++ b/mwdb/resources/tag.py @@ -42,6 +42,12 @@ def get(self): type: string description: Tag prefix required: false + - in: query + name: count + schema: + type: integer + description: Number of objects to return + required: false responses: 200: description: List of tags @@ -67,6 +73,11 @@ def get(self): tag_prefix = obj["query"] if tag_prefix: tags = tags.filter(Tag.tag.startswith(tag_prefix, autoescape=True)) + + tag_count = obj.get("count") + if tag_count: + tags = tags.limit(tag_count) + tags = tags.all() schema = TagItemResponseSchema(many=True) return schema.dump(tags) diff --git a/mwdb/schema/tag.py b/mwdb/schema/tag.py index 39d17f580..9c54e5687 100644 --- a/mwdb/schema/tag.py +++ b/mwdb/schema/tag.py @@ -19,6 +19,7 @@ def validate_tag(self, value): class TagListRequestSchema(Schema): query = fields.Str(missing="") + count = fields.Int() class TagRequestSchema(TagSchemaBase): diff --git a/mwdb/web/src/commons/api/index.tsx b/mwdb/web/src/commons/api/index.tsx index a3d4310cd..19140f136 100644 --- a/mwdb/web/src/commons/api/index.tsx +++ b/mwdb/web/src/commons/api/index.tsx @@ -267,9 +267,9 @@ function getObjectCount( }); } -function getTags(query: string): GetTagsResponse { +function getTags(query: string, count: number): GetTagsResponse { return axios.get(`/tag`, { - params: { query }, + params: { query, count }, }); } diff --git a/mwdb/web/src/components/ShowObject/common/TagForm.tsx b/mwdb/web/src/components/ShowObject/common/TagForm.tsx index c4b801e8c..acc7bd9f1 100644 --- a/mwdb/web/src/components/ShowObject/common/TagForm.tsx +++ b/mwdb/web/src/components/ShowObject/common/TagForm.tsx @@ -30,7 +30,7 @@ export function TagForm(props: Props) { return; } try { - const response = await api.getTags(value); + const response = await api.getTags(value, 20); setTags(response.data.map((t) => t.tag)); } catch (error) { context.setObjectError(error);