feat: Add components_only parameter to filter flows by components in API endpoint (#3932)
* Add fixture for creating flow component in tests * Add test for reading flows with components only in test_database.py * Add `components_only` parameter to filter flows by components in API endpoint
This commit is contained in:
parent
6db15255a5
commit
35d81b7e34
3 changed files with 32 additions and 9 deletions
|
|
@ -126,6 +126,7 @@ def read_flows(
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
settings_service: "SettingsService" = Depends(get_settings_service),
|
settings_service: "SettingsService" = Depends(get_settings_service),
|
||||||
remove_example_flows: bool = False,
|
remove_example_flows: bool = False,
|
||||||
|
components_only: bool = False,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Retrieve a list of flows.
|
Retrieve a list of flows.
|
||||||
|
|
@ -135,7 +136,7 @@ def read_flows(
|
||||||
session (Session): The database session.
|
session (Session): The database session.
|
||||||
settings_service (SettingsService): The settings service.
|
settings_service (SettingsService): The settings service.
|
||||||
remove_example_flows (bool, optional): Whether to remove example flows. Defaults to False.
|
remove_example_flows (bool, optional): Whether to remove example flows. Defaults to False.
|
||||||
|
components_only (bool, optional): Whether to return only components. Defaults to False.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[Dict]: A list of flows in JSON format.
|
List[Dict]: A list of flows in JSON format.
|
||||||
|
|
@ -144,18 +145,22 @@ def read_flows(
|
||||||
try:
|
try:
|
||||||
auth_settings = settings_service.auth_settings
|
auth_settings = settings_service.auth_settings
|
||||||
if auth_settings.AUTO_LOGIN:
|
if auth_settings.AUTO_LOGIN:
|
||||||
flows = session.exec(
|
stmt = select(Flow).where(
|
||||||
select(Flow).where(
|
(Flow.user_id == None) | (Flow.user_id == current_user.id) # noqa
|
||||||
(Flow.user_id == None) | (Flow.user_id == current_user.id) # noqa
|
)
|
||||||
)
|
if components_only:
|
||||||
).all()
|
stmt = stmt.where(Flow.is_component == True) # noqa
|
||||||
|
flows = session.exec(stmt).all()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
flows = current_user.flows
|
flows = current_user.flows
|
||||||
|
|
||||||
flows = validate_is_component(flows) # type: ignore
|
flows = validate_is_component(flows) # type: ignore
|
||||||
|
if components_only:
|
||||||
|
flows = [flow for flow in flows if flow.is_component]
|
||||||
flow_ids = [flow.id for flow in flows]
|
flow_ids = [flow.id for flow in flows]
|
||||||
# with the session get the flows that DO NOT have a user_id
|
# with the session get the flows that DO NOT have a user_id
|
||||||
if not remove_example_flows:
|
if not remove_example_flows and not components_only:
|
||||||
try:
|
try:
|
||||||
folder = session.exec(select(Folder).where(Folder.name == STARTER_FOLDER_NAME)).first()
|
folder = session.exec(select(Folder).where(Folder.name == STARTER_FOLDER_NAME)).first()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import orjson
|
import orjson
|
||||||
import pytest
|
import pytest
|
||||||
|
from base.langflow.components.inputs.ChatInput import ChatInput
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
@ -420,6 +421,17 @@ def added_webhook_test(client, json_webhook_test, logged_in_headers):
|
||||||
client.delete(f"api/v1/flows/{response.json()['id']}", headers=logged_in_headers)
|
client.delete(f"api/v1/flows/{response.json()['id']}", headers=logged_in_headers)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def flow_component(client: TestClient, logged_in_headers):
|
||||||
|
chat_input = ChatInput()
|
||||||
|
graph = Graph(start=chat_input, end=chat_input)
|
||||||
|
graph_dict = graph.dump(name="Chat Input Component")
|
||||||
|
flow = FlowCreate(**graph_dict)
|
||||||
|
response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers)
|
||||||
|
assert response.status_code == 201
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def created_api_key(active_user):
|
def created_api_key(active_user):
|
||||||
hashed = get_password_hash("random_key")
|
hashed = get_password_hash("random_key")
|
||||||
|
|
@ -448,8 +460,6 @@ def get_simple_api_test(client, logged_in_headers, json_simple_api_test):
|
||||||
flow = FlowCreate(name="Simple API Test", data=data, description="Simple API Test")
|
flow = FlowCreate(name="Simple API Test", data=data, description="Simple API Test")
|
||||||
response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers)
|
response = client.post("api/v1/flows/", json=flow.model_dump(), headers=logged_in_headers)
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
assert response.json()["name"] == flow.name
|
|
||||||
assert response.json()["data"] == flow.data
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,14 @@ def test_read_flows(client: TestClient, json_flow: str, active_user, logged_in_h
|
||||||
assert len(response.json()) > 0
|
assert len(response.json()) > 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_flows_components_only(client: TestClient, flow_component: dict, logged_in_headers):
|
||||||
|
response = client.get("api/v1/flows/", headers=logged_in_headers, params={"components_only": True})
|
||||||
|
assert response.status_code == 200
|
||||||
|
names = [flow["name"] for flow in response.json()]
|
||||||
|
assert any("Chat Input Component" in name for name in names)
|
||||||
|
assert all(flow["is_component"] is True for flow in response.json()), [flow["name"] for flow in response.json()]
|
||||||
|
|
||||||
|
|
||||||
def test_read_flow(client: TestClient, json_flow: str, logged_in_headers):
|
def test_read_flow(client: TestClient, json_flow: str, logged_in_headers):
|
||||||
flow = orjson.loads(json_flow)
|
flow = orjson.loads(json_flow)
|
||||||
data = flow["data"]
|
data = flow["data"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue