Refactor API response model and add filter
conditions to list_components and count_components methods
This commit is contained in:
parent
4b8349d131
commit
cd71943b43
3 changed files with 64 additions and 27 deletions
|
|
@ -9,7 +9,7 @@ from langflow.services.deps import (
|
||||||
from langflow.services.store.schema import (
|
from langflow.services.store.schema import (
|
||||||
ComponentResponse,
|
ComponentResponse,
|
||||||
DownloadComponentResponse,
|
DownloadComponentResponse,
|
||||||
ListComponentResponse,
|
ListComponentResponseModel,
|
||||||
StoreComponentCreate,
|
StoreComponentCreate,
|
||||||
TagResponse,
|
TagResponse,
|
||||||
UsersLikesResponse,
|
UsersLikesResponse,
|
||||||
|
|
@ -70,7 +70,7 @@ def create_component(
|
||||||
raise HTTPException(status_code=400, detail=str(exc))
|
raise HTTPException(status_code=400, detail=str(exc))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/components/", response_model=List[ListComponentResponse])
|
@router.get("/components/", response_model=ListComponentResponseModel)
|
||||||
def list_components(
|
def list_components(
|
||||||
filter_by_user: bool = Query(False),
|
filter_by_user: bool = Query(False),
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
|
|
@ -81,6 +81,7 @@ def list_components(
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
with user_data_context(store_api_Key, store_service):
|
with user_data_context(store_api_Key, store_service):
|
||||||
|
authorized = False
|
||||||
result = store_service.query_components(
|
result = store_service.query_components(
|
||||||
api_key=store_api_Key,
|
api_key=store_api_Key,
|
||||||
page=page,
|
page=page,
|
||||||
|
|
@ -89,17 +90,28 @@ def list_components(
|
||||||
is_component=is_component,
|
is_component=is_component,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not store_api_Key:
|
comp_count = store_service.count_components(
|
||||||
return result
|
api_key=store_api_Key,
|
||||||
|
filter_by_user=filter_by_user,
|
||||||
|
is_component=is_component,
|
||||||
|
)
|
||||||
|
|
||||||
|
if store_api_Key:
|
||||||
# Now, from the result, we need to get the components
|
# Now, from the result, we need to get the components
|
||||||
# the user likes and set the liked_by_user to True
|
# the user likes and set the liked_by_user to True
|
||||||
|
try:
|
||||||
result = update_components_with_user_data(
|
result = update_components_with_user_data(
|
||||||
result, store_service, store_api_Key
|
result, store_service, store_api_Key
|
||||||
)
|
)
|
||||||
return result
|
authorized = True
|
||||||
|
except Exception:
|
||||||
|
# If we get an error here, it means the user is not authorized
|
||||||
|
authorized = False
|
||||||
|
return ListComponentResponseModel(
|
||||||
|
results=result, authorized=authorized, count=comp_count
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise HTTPException(status_code=400, detail=str(exc))
|
raise HTTPException(status_code=500, detail=str(exc))
|
||||||
|
|
||||||
|
|
||||||
@router.get("/components/count", response_model=dict)
|
@router.get("/components/count", response_model=dict)
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,12 @@ class ListComponentResponse(BaseModel):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class ListComponentResponseModel(BaseModel):
|
||||||
|
count: int
|
||||||
|
authorized: bool
|
||||||
|
results: Optional[List[ListComponentResponse]]
|
||||||
|
|
||||||
|
|
||||||
class DownloadComponentResponse(BaseModel):
|
class DownloadComponentResponse(BaseModel):
|
||||||
id: UUID
|
id: UUID
|
||||||
name: Optional[str]
|
name: Optional[str]
|
||||||
|
|
|
||||||
|
|
@ -129,19 +129,25 @@ class StoreService(Service):
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter_conditions = []
|
||||||
|
|
||||||
if status:
|
if status:
|
||||||
params["filter[status]"] = status
|
filter_conditions.append({"status": {"_eq": status}})
|
||||||
if is_component:
|
if is_component is not None:
|
||||||
params["filter[is_component][_eq]"] = is_component
|
# params["filter[is_component][_eq]"] = is_component
|
||||||
|
filter_conditions.append({"is_component": {"_eq": is_component}})
|
||||||
|
|
||||||
if tags:
|
if tags:
|
||||||
params["filter[tags][_in]"] = ",".join(tags)
|
# params["filter[tags][_in]"] = ",".join(tags)
|
||||||
|
filter_conditions.append({"tags": {"tags_id": {"name": {"_in": tags}}}})
|
||||||
|
|
||||||
if date_from:
|
if date_from:
|
||||||
params["filter[date_updated][_gte]"] = date_from.isoformat()
|
# params["filter[date_updated][_gte]"] = date_from.isoformat()
|
||||||
|
filter_conditions.append({"date_updated": {"_gte": date_from.isoformat()}})
|
||||||
|
|
||||||
if date_to:
|
if date_to:
|
||||||
params["filter[date_updated][_lte]"] = date_to.isoformat()
|
# params["filter[date_updated][_lte]"] = date_to.isoformat()
|
||||||
|
filter_conditions.append({"date_updated": {"_lte": date_to.isoformat()}})
|
||||||
|
|
||||||
if sort:
|
if sort:
|
||||||
params["sort"] = ",".join(sort)
|
params["sort"] = ",".join(sort)
|
||||||
|
|
@ -158,7 +164,11 @@ class StoreService(Service):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
params["filter"] = json.dumps({"status": {"_eq": "public"}})
|
# params["filter"] = json.dumps({"status": {"_eq": "public"}})
|
||||||
|
filter_conditions.append({"status": {"_in": ["public", "Public"]}})
|
||||||
|
|
||||||
|
if filter_conditions:
|
||||||
|
params["filter"] = json.dumps({"_and": filter_conditions})
|
||||||
|
|
||||||
results = self._get(self.components_url, api_key, params)
|
results = self._get(self.components_url, api_key, params)
|
||||||
return [ComponentResponse(**component) for component in results]
|
return [ComponentResponse(**component) for component in results]
|
||||||
|
|
@ -170,6 +180,7 @@ class StoreService(Service):
|
||||||
is_component: Optional[bool] = None,
|
is_component: Optional[bool] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
params = {"aggregate": json.dumps({"count": "*"})}
|
params = {"aggregate": json.dumps({"count": "*"})}
|
||||||
|
filter_conditions = []
|
||||||
if filter_by_user:
|
if filter_by_user:
|
||||||
params["deep"] = json.dumps(
|
params["deep"] = json.dumps(
|
||||||
{
|
{
|
||||||
|
|
@ -179,10 +190,14 @@ class StoreService(Service):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
params["filter"] = json.dumps({"status": {"_in": ["public", "Public"]}})
|
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})
|
||||||
|
|
||||||
if is_component:
|
|
||||||
params["filter[is_component][_eq]"] = is_component
|
|
||||||
results = self._get(self.components_url, api_key, params)
|
results = self._get(self.components_url, api_key, params)
|
||||||
return results[0].get("count", 0)
|
return results[0].get("count", 0)
|
||||||
|
|
||||||
|
|
@ -198,9 +213,10 @@ class StoreService(Service):
|
||||||
params = {"page": page, "limit": limit}
|
params = {"page": page, "limit": limit}
|
||||||
# ?aggregate[count]=likes
|
# ?aggregate[count]=likes
|
||||||
params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields)
|
params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields)
|
||||||
|
filter_conditions = []
|
||||||
|
|
||||||
if is_component:
|
if is_component is not None:
|
||||||
params["filter[is_component][_eq]"] = is_component
|
filter_conditions.append({"is_component": {"_eq": is_component}})
|
||||||
|
|
||||||
# Only public components or the ones created by the user
|
# Only public components or the ones created by the user
|
||||||
# check for "public" or "Public"
|
# check for "public" or "Public"
|
||||||
|
|
@ -209,11 +225,13 @@ class StoreService(Service):
|
||||||
|
|
||||||
if filter_by_user and api_key:
|
if filter_by_user and api_key:
|
||||||
user_data = user_data_var.get()
|
user_data = user_data_var.get()
|
||||||
params["filter"] = json.dumps({"user_created": {"_eq": user_data["id"]}})
|
# params["filter"] = json.dumps({"user_created": {"_eq": user_data["id"]}})
|
||||||
|
filter_conditions.append({"user_created": {"_eq": user_data["id"]}})
|
||||||
else:
|
else:
|
||||||
params["filter"] = params["filter"] = json.dumps(
|
filter_conditions.append({"status": {"_in": ["public", "Public"]}})
|
||||||
{"status": {"_in": ["public", "Public"]}}
|
|
||||||
)
|
if filter_conditions:
|
||||||
|
params["filter"] = json.dumps({"_and": filter_conditions})
|
||||||
|
|
||||||
results = self._get(self.components_url, api_key, params)
|
results = self._get(self.components_url, api_key, params)
|
||||||
results_objects = [ListComponentResponse(**component) for component in results]
|
results_objects = [ListComponentResponse(**component) for component in results]
|
||||||
|
|
@ -318,6 +336,7 @@ class StoreService(Service):
|
||||||
|
|
||||||
def get_component_likes_count(self, api_key: str, component_id: str) -> int:
|
def get_component_likes_count(self, api_key: str, component_id: str) -> int:
|
||||||
url = f"{self.components_url}/{component_id}"
|
url = f"{self.components_url}/{component_id}"
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
"fields": ",".join(["id", "count(liked_by)"]),
|
"fields": ",".join(["id", "count(liked_by)"]),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue