Use lists for tags instead of dicts

This commit is contained in:
grossmj committed 2026-02-23 23:26:42 +08:00
1 parent 66f32cecf2
commit bdcb6445c7
6 files changed
+28 -49

No files matched your search

+8 -18
View File
@@ -137,7 +137,7 @@ async def create_node(node_data: schemas.NodeCreate, project: Project = Depends(
)
def get_nodes(
project: Project = Depends(dep_project),
tags: Optional[List[str]] = Query(None, description="Filter by tags (e.g., tags=vendor:cisco&tags=model:7200)")
tags: Optional[List[str]] = Query(None, description="Filter by tags (e.g. tags=vendor:cisco&tags=model:7200)")
) -> List[schemas.Node]:
"""
Return all nodes belonging to a given project.
@@ -145,7 +145,7 @@ def get_nodes(
Required privilege: Node.Audit
Query Parameters:
- tags: Filter by tags in format "key:value". Multiple tags are ANDed together.
- tags: Filter by tags. Multiple tags are ANDed together.
Example: ?tags=vendor:cisco&tags=model:7200
"""
@@ -155,29 +155,19 @@ def get_nodes(
else:
nodes = [v.asdict() for v in project.nodes.values()]
# Filter by tags if provided
# Filter by tags if provided (all filter tags have to match the node tags)
if tags:
filtered_nodes = []
for node in nodes:
node_dict = node.asdict() if hasattr(node, 'asdict') else node
node_tags = node_dict.get("tags") or {}
# Check if all tag filters match
node_tags = node.get("tags") or []
match = True
for tag_filter in tags:
if ":" in tag_filter:
key, value = tag_filter.split(":", 1)
if node_tags.get(key) != value:
match = False
break
else:
# Check if key exists
if tag_filter not in node_tags:
match = False
break
if tag_filter not in node_tags:
match = False
break
if match:
filtered_nodes.append(node)
nodes = filtered_nodes
return filtered_nodes
return nodes
+7 -15
View File
@@ -170,7 +170,7 @@ async def get_templates(
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
current_user: schemas.User = Depends(get_current_active_user),
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository)),
tags: Optional[List[str]] = Query(None, description="Filter by tags (e.g., tags=vendor:cisco&tags=model:7200)")
tags: Optional[List[str]] = Query(None, description="Filter by tags (e.g. tags=vendor:cisco&tags=model:7200)")
) -> List[schemas.Template]:
"""
Return all templates.
@@ -178,30 +178,22 @@ async def get_templates(
Required privilege: Template.Audit
Query Parameters:
- tags: Filter by tags in format "key:value". Multiple tags are ANDed together.
- tags: Filter by tags. Multiple tags are ANDed together.
Example: ?tags=vendor:cisco&tags=model:7200
"""
templates = await TemplatesService(templates_repo).get_templates()
# Filter by tags if provided
# Filter by tags if provided (all filter tags have to match the node tags)
if tags:
filtered_templates = []
for template in templates:
template_tags = template.get("tags") or {}
# Check if all tag filters match
template_tags = template.get("tags") or []
match = True
for tag_filter in tags:
if ":" in tag_filter:
key, value = tag_filter.split(":", 1)
if template_tags.get(key) != value:
match = False
break
else:
# Check if key exists
if tag_filter not in template_tags:
match = False
break
if tag_filter not in template_tags:
match = False
break
if match:
filtered_templates.append(template)
templates = filtered_templates