From 27debfff8d2865dd99d33f3e468f76756c5a05b7 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 3 Sep 2022 22:58:44 +0200 Subject: [PATCH 1/2] API endpoint to get the locked status of a project --- gns3server/api/routes/controller/projects.py | 11 ++++++++++- gns3server/controller/project.py | 15 +++++++++++++++ tests/api/routes/controller/test_projects.py | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/gns3server/api/routes/controller/projects.py b/gns3server/api/routes/controller/projects.py index b8797e3c..9df7b9be 100644 --- a/gns3server/api/routes/controller/projects.py +++ b/gns3server/api/routes/controller/projects.py @@ -30,7 +30,7 @@ import logging log = logging.getLogger() -from fastapi import APIRouter, Depends, Request, Response, Body, HTTPException, status, WebSocket, WebSocketDisconnect +from fastapi import APIRouter, Depends, Request, Body, HTTPException, status, WebSocket, WebSocketDisconnect from fastapi.encoders import jsonable_encoder from fastapi.responses import StreamingResponse, FileResponse from websockets.exceptions import ConnectionClosed, WebSocketException @@ -395,6 +395,15 @@ async def duplicate_project( return new_project.asdict() +@router.get("/{project_id}/locked") +async def locked_project(project: Project = Depends(dep_project)) -> dict: + """ + Returns whether a project is locked or not + """ + + return {"result": project.locked} + + @router.post("/{project_id}/lock", status_code=status.HTTP_204_NO_CONTENT) async def lock_project(project: Project = Depends(dep_project)) -> None: """ diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 37446cae..7107429a 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -1144,6 +1144,21 @@ class Project: self.emit_notification("node.updated", node.asdict()) self.dump() + @property + @open_required + def locked(self): + """ + Check if all items in a project are locked and not + """ + + for drawing in self._drawings.values(): + if not drawing.locked: + return False + for node in self.nodes.values(): + if not node.locked: + return False + return True + def dump(self): """ Dump topology to disk diff --git a/tests/api/routes/controller/test_projects.py b/tests/api/routes/controller/test_projects.py index e94cc8fc..7b392742 100644 --- a/tests/api/routes/controller/test_projects.py +++ b/tests/api/routes/controller/test_projects.py @@ -511,6 +511,10 @@ async def test_lock_unlock(app: FastAPI, client: AsyncClient, project: Project, for node in project.nodes.values(): assert node.locked is True + response = await client.get(app.url_path_for("locked_project", project_id=project.id)) + assert response.status_code == status.HTTP_200_OK + assert response.json()["result"] is True + response = await client.post(app.url_path_for("unlock_project", project_id=project.id)) assert response.status_code == status.HTTP_204_NO_CONTENT From 2976e220dcc967b1a4ca609d0df8fbd7b33d56b0 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 3 Sep 2022 23:08:13 +0200 Subject: [PATCH 2/2] Return a boolean directly from API endpoint for project locked status --- gns3server/api/routes/controller/projects.py | 4 ++-- tests/api/routes/controller/test_projects.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gns3server/api/routes/controller/projects.py b/gns3server/api/routes/controller/projects.py index 9df7b9be..91021f64 100644 --- a/gns3server/api/routes/controller/projects.py +++ b/gns3server/api/routes/controller/projects.py @@ -396,12 +396,12 @@ async def duplicate_project( @router.get("/{project_id}/locked") -async def locked_project(project: Project = Depends(dep_project)) -> dict: +async def locked_project(project: Project = Depends(dep_project)) -> bool: """ Returns whether a project is locked or not """ - return {"result": project.locked} + return project.locked @router.post("/{project_id}/lock", status_code=status.HTTP_204_NO_CONTENT) diff --git a/tests/api/routes/controller/test_projects.py b/tests/api/routes/controller/test_projects.py index 7b392742..45b9398d 100644 --- a/tests/api/routes/controller/test_projects.py +++ b/tests/api/routes/controller/test_projects.py @@ -513,7 +513,7 @@ async def test_lock_unlock(app: FastAPI, client: AsyncClient, project: Project, response = await client.get(app.url_path_for("locked_project", project_id=project.id)) assert response.status_code == status.HTTP_200_OK - assert response.json()["result"] is True + assert response.json() is True response = await client.post(app.url_path_for("unlock_project", project_id=project.id)) assert response.status_code == status.HTTP_204_NO_CONTENT