Skip to content

Commit

Permalink
#777 fix workspace clone
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Jul 28, 2023
1 parent 33f67c9 commit 97178bb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from flask.views import MethodView
from flask_sqlalchemy import Pagination

from workspaces.service.crud_service import BaseModelService, NotAuthorized
from workspaces.utils import dao_entity2dict
from workspaces.service.crud_service import BaseModelService, NotAuthorized, NotFoundException


class BaseModelView(MethodView):
Expand Down Expand Up @@ -51,6 +50,8 @@ def get(self, id_):
obj = self.service.get(id_)
except NotAuthorized:
return "Access to the requested resources not authorized", 401
except NotFoundException:
return f"{self.service.repository} with id {id_} not found.", 404
if obj is None:
return f"{self.service.repository} with id {id_} not found.", 404
if isinstance(obj, dict):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
WorkspaceService,
WorkspaceresourceService,
TagService,
NotFoundException
)


from workspaces.persistence.crud_persistence import OSBRepositoryRepository
from workspaces.utils import dao_entity2dict
from workspaces.controllers.crud.base_model_controller import BaseModelView


Expand All @@ -36,6 +35,8 @@ def get(self, id_, *args, **kwargs):
osbrepository_ext = self.service.get(id_)
except NotAuthorized:
return "Access to the requested resources not authorized", 401
except NotFoundException:
return f"{self.service.repository} with id {id_} not found.", 404
if osbrepository_ext is None:
return f"{self.service.repository} with id {id_} not found.", 404

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ def set_timestamp_updated(self, obj):
if hasattr(obj, "timestamp_updated"):
setattr(obj, "timestamp_updated", func.now())
return obj

def clone(self, id):
"""Clone an object from the repository."""
obj = self._get(id)
db.session.expunge(obj)
from sqlalchemy.orm.session import make_transient
make_transient(obj)
obj.id = None
return obj

def save(self, obj):
obj = self.set_timestamp_updated(obj)
Expand Down
20 changes: 13 additions & 7 deletions applications/workspaces/server/workspaces/service/crud_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def rm_null_values(dikt):
class NotAuthorized(Exception):
pass

class NotFoundException(Exception):
pass

class NotAllowed(Exception):
pass
Expand Down Expand Up @@ -211,20 +213,22 @@ def clone(self, workspace_id):
self.check_max_num_workspaces_per_user(user_id)
from workspaces.service.workflow import clone_workspaces_content
with db.session.no_autoflush:
workspace = self.get(workspace_id)
workspace = self.repository.clone(workspace_id)
if workspace is None:
raise Exception(
raise NotFoundException(
f"Cannot clone workspace with id {workspace_id}: not found.")

workspace.user = None
workspace.id = None
workspace.name = f"Clone of {workspace.name}"
workspace.publicable = False
workspace.featured = False
workspace.timestamp_created = None
cloned = self.to_dao(workspace.to_dict())
workspace.resources = []




cloned = self.repository.post(cloned)
cloned = self.repository.post(workspace)

create_volume(name=self.get_pvc_name(cloned.id),
size=self.get_workspace_volume_size(workspace))
Expand Down Expand Up @@ -435,8 +439,10 @@ def to_dao(cls, ws_dict: dict) -> TWorkspaceResourceEntity:
if "origin" in ws_dict:
wro_dao_dict = dict(ws_dict.get("origin"))
ws_dict.update({"origin": json.dumps(wro_dao_dict)})

workspace_resource = super().to_dao(ws_dict)
if 'path' in ws_dict:
ws_dict['folder'] = ws_dict['path']
del ws_dict['path']
workspace_resource: TWorkspaceResourceEntity = super().to_dao(ws_dict)
if not workspace_resource.resource_type or workspace_resource.resource_type == "u":
origin = json.loads(workspace_resource.origin)
workspace_resource.resource_type = guess_resource_type(
Expand Down

0 comments on commit 97178bb

Please sign in to comment.