Skip to content

Commit 9dca384

Browse files
dawoodhqbenflexcompute
authored andcommitted
feat: add rename method to v2 assets (#1333)
* feat: add rename method to v2 assets (Folder, Project, SurfaceMesh, VolumeMesh, Case, etc) * fix: raise rename method to AssetBase class * fix: remove self return after renaming asset * fix: remove return docstring + missed self returns
1 parent 24c3bf7 commit 9dca384

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

flow360/cloud/flow360_requests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,17 @@ def _validate_force_creation_config(self):
235235

236236

237237
class MoveToFolderRequestV2(Flow360RequestsV2):
238-
"""v2 request implementation for moving folder"""
238+
"""Data model for moving folder using v2 endpoint"""
239239

240240
name: Optional[str] = pd_v2.Field(default=None, description="folder to move name")
241241
tags: List[str] = pd_v2.Field(default=[], description="folder tags")
242242
parent_folder_id: str = pd_v2.Field(alias="parentFolderId", default="ROOT.FLOW360")
243+
244+
245+
class RenameAssetRequestV2(Flow360RequestsV2):
246+
"""
247+
Data model for renaming an asset (folder, project, surface mesh, volume mesh,
248+
or case (other request fields, like folder to move to, already have implementations)
249+
"""
250+
251+
name: str = pd_v2.Field(description="case to rename")

flow360/component/case.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
import pydantic.v1 as pd_v1
1515

1616
from .. import error_messages
17-
from ..cloud.flow360_requests import MoveCaseItem, MoveToFolderRequest
17+
from ..cloud.flow360_requests import (
18+
MoveCaseItem,
19+
MoveToFolderRequest,
20+
RenameAssetRequestV2,
21+
)
1822
from ..cloud.rest_api import RestApi
1923
from ..cloud.s3_utils import CloudFileNotFoundError
2024
from ..exceptions import Flow360RuntimeError, Flow360ValidationError, Flow360ValueError
@@ -668,6 +672,19 @@ def move_to_folder(self, folder: Folder):
668672
)
669673
return self
670674

675+
def rename(self, new_name: str):
676+
"""
677+
Rename the current case.
678+
679+
Parameters
680+
----------
681+
new_name : str
682+
The new name for the case.
683+
"""
684+
RestApi(CaseInterfaceV2.endpoint).patch(
685+
RenameAssetRequestV2(name=new_name).dict(), method=self.id
686+
)
687+
671688
@classmethod
672689
def _interface(cls):
673690
return CaseInterface

flow360/component/project.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from PrettyPrint import PrettyPrintTree
1515
from pydantic import PositiveInt
1616

17-
from flow360.cloud.flow360_requests import LengthUnitType
17+
from flow360.cloud.flow360_requests import LengthUnitType, RenameAssetRequestV2
1818
from flow360.cloud.rest_api import RestApi
1919
from flow360.component.case import Case
2020
from flow360.component.geometry import Geometry
@@ -1211,6 +1211,19 @@ def refresh_project_tree(self):
12111211
"""Refresh the local project tree by fetching the latest project tree from cloud."""
12121212
return self._get_tree_from_cloud()
12131213

1214+
def rename(self, new_name: str):
1215+
"""
1216+
Rename the current project.
1217+
1218+
Parameters
1219+
----------
1220+
new_name : str
1221+
The new name for the project.
1222+
"""
1223+
RestApi(ProjectInterface.endpoint).patch(
1224+
RenameAssetRequestV2(name=new_name).dict(), method=self.id
1225+
)
1226+
12141227
def print_project_tree(self, line_width: int = 30, is_horizontal: bool = True):
12151228
"""Print the project tree to the terminal.
12161229

flow360/component/simulation/folder.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
import pydantic as pd
1212

13-
from ...cloud.flow360_requests import MoveToFolderRequestV2, NewFolderRequest
13+
from ...cloud.flow360_requests import (
14+
MoveToFolderRequestV2,
15+
NewFolderRequest,
16+
RenameAssetRequestV2,
17+
)
1418
from ...cloud.rest_api import RestApi
1519
from ...exceptions import Flow360ValueError
1620
from ...log import log
@@ -133,6 +137,19 @@ def move_to_folder(self, folder: Folder):
133137
)
134138
return self
135139

140+
def rename(self, new_name: str):
141+
"""
142+
Rename the current folder.
143+
144+
Parameters
145+
----------
146+
new_name : str
147+
The new name for the folder.
148+
"""
149+
RestApi(FolderInterfaceV2.endpoint).patch(
150+
RenameAssetRequestV2(name=new_name).dict(), method=self.id
151+
)
152+
136153
@classmethod
137154
def _interface(cls):
138155
return FolderInterface

flow360/component/simulation/web/asset_base.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pydantic import ValidationError
1212
from requests.exceptions import HTTPError
1313

14-
from flow360.cloud.flow360_requests import LengthUnitType
14+
from flow360.cloud.flow360_requests import LengthUnitType, RenameAssetRequestV2
1515
from flow360.cloud.rest_api import RestApi
1616
from flow360.component.interfaces import BaseInterface, ProjectInterface
1717
from flow360.component.resource_base import (
@@ -83,6 +83,19 @@ def solver_version(self):
8383
"""
8484
return self.info.solver_version
8585

86+
def rename(self, new_name: str):
87+
"""
88+
Rename the current asset.
89+
90+
Parameters
91+
----------
92+
new_name : str
93+
The new name for the asset.
94+
"""
95+
RestApi(self._interface_class.endpoint).patch(
96+
RenameAssetRequestV2(name=new_name).dict(), method=self.id
97+
)
98+
8699
@classmethod
87100
# pylint: disable=protected-access
88101
def _from_meta(cls, meta: AssetMetaBaseModelV2):

0 commit comments

Comments
 (0)