Add typing to get_components function and refactor
count_components to accept filter_conditions
This commit is contained in:
parent
b972e1a2de
commit
805ec1570d
2 changed files with 28 additions and 18 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
|
@ -11,6 +11,7 @@ from langflow.services.deps import get_settings_service, get_store_service
|
|||
from langflow.services.store.schema import (
|
||||
ComponentResponse,
|
||||
DownloadComponentResponse,
|
||||
ListComponentResponse,
|
||||
ListComponentResponseModel,
|
||||
StoreComponentCreate,
|
||||
TagResponse,
|
||||
|
|
@ -89,9 +90,11 @@ def get_components(
|
|||
):
|
||||
try:
|
||||
with user_data_context(api_key=store_api_Key, store_service=store_service):
|
||||
filter_conditions: List[Dict[str, Any]] = []
|
||||
result: List[ListComponentResponse] = []
|
||||
authorized = False
|
||||
try:
|
||||
authorized = False
|
||||
result = store_service.query_components(
|
||||
result, filter_conditions = store_service.query_components(
|
||||
api_key=store_api_Key,
|
||||
page=page,
|
||||
limit=limit,
|
||||
|
|
@ -108,16 +111,19 @@ def get_components(
|
|||
"You are not authorized to access this public resource"
|
||||
)
|
||||
try:
|
||||
comp_count = store_service.count_components(
|
||||
api_key=store_api_Key,
|
||||
filter_by_user=filter_by_user,
|
||||
is_component=is_component,
|
||||
)
|
||||
if result:
|
||||
comp_count = store_service.count_components(
|
||||
api_key=store_api_Key,
|
||||
filter_by_user=filter_by_user,
|
||||
filter_conditions=filter_conditions,
|
||||
)
|
||||
else:
|
||||
comp_count = 0
|
||||
except Exception:
|
||||
#! This should be removed once we fix the bug
|
||||
comp_count = 0
|
||||
|
||||
if store_api_Key:
|
||||
if store_api_Key and result:
|
||||
# Now, from the result, we need to get the components
|
||||
# the user likes and set the liked_by_user to True
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import json
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
|
||||
from uuid import UUID
|
||||
|
||||
import httpx
|
||||
|
|
@ -144,8 +144,11 @@ class StoreService(Service):
|
|||
filter_conditions.append({"is_component": {"_eq": is_component}})
|
||||
|
||||
if tags:
|
||||
# params["filter[tags][_in]"] = ",".join(tags)
|
||||
filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}})
|
||||
# tags_filter = {"tags": {"_or": []}}
|
||||
# for tag in tags:
|
||||
# tags_filter["tags"]["_or"].append({"tags_id": {"name": {"_eq": tag}}})
|
||||
# filter_conditions.append(tags_filter)
|
||||
|
||||
if date_from:
|
||||
# params["filter[date_updated][_gte]"] = date_from.isoformat()
|
||||
|
|
@ -183,10 +186,10 @@ class StoreService(Service):
|
|||
self,
|
||||
api_key: Optional[str] = None,
|
||||
filter_by_user: bool = False,
|
||||
is_component: Optional[bool] = None,
|
||||
filter_conditions: Optional[List[Dict[str, Any]]] = None,
|
||||
) -> int:
|
||||
params = {"aggregate": json.dumps({"count": "*"})}
|
||||
filter_conditions = []
|
||||
filter_conditions = [] if filter_conditions is None else filter_conditions
|
||||
if filter_by_user:
|
||||
params["deep"] = json.dumps(
|
||||
{
|
||||
|
|
@ -198,9 +201,6 @@ class StoreService(Service):
|
|||
else:
|
||||
filter_conditions.append({"status": {"_in": ["public", "Public"]}})
|
||||
|
||||
if is_component is not None:
|
||||
filter_conditions.append({"is_component": {"_eq": is_component}})
|
||||
|
||||
if filter_conditions:
|
||||
params["filter"] = json.dumps({"_and": filter_conditions})
|
||||
|
||||
|
|
@ -229,7 +229,7 @@ class StoreService(Service):
|
|||
fields: Optional[List[str]] = None,
|
||||
is_component: Optional[bool] = None,
|
||||
filter_by_user: bool = False,
|
||||
) -> Union[List[ListComponentResponse], List[Dict[str, int]]]:
|
||||
) -> Tuple[List[ListComponentResponse], List[Dict[str, Any]]]:
|
||||
params = {"page": page, "limit": limit}
|
||||
# ?aggregate[count]=likes
|
||||
params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields)
|
||||
|
|
@ -248,6 +248,10 @@ class StoreService(Service):
|
|||
|
||||
if tags:
|
||||
filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}})
|
||||
# tags_filter = {"tags": {"_or": []}}
|
||||
# for tag in tags:
|
||||
# tags_filter["tags"]["_or"].append({"tags_id": {"name": {"_eq": tag}}})
|
||||
# filter_conditions.append(tags_filter)
|
||||
|
||||
if is_component is not None:
|
||||
filter_conditions.append({"is_component": {"_eq": is_component}})
|
||||
|
|
@ -276,7 +280,7 @@ class StoreService(Service):
|
|||
# for component in results_objects:
|
||||
# if component.tags:
|
||||
# component.tags = [tags_id.tags_id for tags_id in component.tags]
|
||||
return results_objects
|
||||
return results_objects, filter_conditions
|
||||
|
||||
def get_liked_by_user_components(
|
||||
self, component_ids: List[UUID], api_key: str
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue