mirror of
https://github.com/langgenius/dify.git
synced 2026-09-08 11:04:27 +08:00
feat: add creators filter param when querying snippets list.
This commit is contained in:
parent
cf7cd3ac03
commit
eaa660e12f
@ -1,6 +1,6 @@
|
|||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field, field_validator
|
||||||
|
|
||||||
|
|
||||||
class SnippetListQuery(BaseModel):
|
class SnippetListQuery(BaseModel):
|
||||||
@ -10,6 +10,19 @@ class SnippetListQuery(BaseModel):
|
|||||||
limit: int = Field(default=20, ge=1, le=100)
|
limit: int = Field(default=20, ge=1, le=100)
|
||||||
keyword: str | None = None
|
keyword: str | None = None
|
||||||
is_published: bool | None = Field(default=None, description="Filter by published status")
|
is_published: bool | None = Field(default=None, description="Filter by published status")
|
||||||
|
creators: list[str] | None = Field(default=None, description="Filter by creator account IDs")
|
||||||
|
|
||||||
|
@field_validator("creators", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def parse_creators(cls, value: object) -> list[str] | None:
|
||||||
|
"""Normalize creators filter from query string or list input."""
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
if isinstance(value, str):
|
||||||
|
return [creator.strip() for creator in value.split(",") if creator.strip()] or None
|
||||||
|
if isinstance(value, list):
|
||||||
|
return [str(creator).strip() for creator in value if str(creator).strip()] or None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class IconInfo(BaseModel):
|
class IconInfo(BaseModel):
|
||||||
|
|||||||
@ -67,6 +67,7 @@ class CustomizedSnippetsApi(Resource):
|
|||||||
limit=query.limit,
|
limit=query.limit,
|
||||||
keyword=query.keyword,
|
keyword=query.keyword,
|
||||||
is_published=query.is_published,
|
is_published=query.is_published,
|
||||||
|
creators=query.creators,
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -49,6 +49,7 @@ class SnippetService:
|
|||||||
limit: int = 20,
|
limit: int = 20,
|
||||||
keyword: str | None = None,
|
keyword: str | None = None,
|
||||||
is_published: bool | None = None,
|
is_published: bool | None = None,
|
||||||
|
creators: list[str] | None = None,
|
||||||
) -> tuple[Sequence[CustomizedSnippet], int, bool]:
|
) -> tuple[Sequence[CustomizedSnippet], int, bool]:
|
||||||
"""
|
"""
|
||||||
Get paginated list of snippets with optional search.
|
Get paginated list of snippets with optional search.
|
||||||
@ -58,6 +59,7 @@ class SnippetService:
|
|||||||
:param limit: Number of items per page
|
:param limit: Number of items per page
|
||||||
:param keyword: Optional search keyword for name/description
|
:param keyword: Optional search keyword for name/description
|
||||||
:param is_published: Optional filter by published status (True/False/None for all)
|
:param is_published: Optional filter by published status (True/False/None for all)
|
||||||
|
:param creators: Optional filter by creator account IDs
|
||||||
:return: Tuple of (snippets list, total count, has_more flag)
|
:return: Tuple of (snippets list, total count, has_more flag)
|
||||||
"""
|
"""
|
||||||
stmt = (
|
stmt = (
|
||||||
@ -74,6 +76,9 @@ class SnippetService:
|
|||||||
if is_published is not None:
|
if is_published is not None:
|
||||||
stmt = stmt.where(CustomizedSnippet.is_published == is_published)
|
stmt = stmt.where(CustomizedSnippet.is_published == is_published)
|
||||||
|
|
||||||
|
if creators:
|
||||||
|
stmt = stmt.where(CustomizedSnippet.created_by.in_(creators))
|
||||||
|
|
||||||
# Get total count
|
# Get total count
|
||||||
count_stmt = select(func.count()).select_from(stmt.subquery())
|
count_stmt = select(func.count()).select_from(stmt.subquery())
|
||||||
total = db.session.scalar(count_stmt) or 0
|
total = db.session.scalar(count_stmt) or 0
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user