Skip to content

Commit

Permalink
View: Update workspace view to return updated details more quickly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Puerto committed Feb 16, 2024
1 parent 3eacfef commit 6910a10
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions src/user_workspaces_server/views/workspace_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def post(self, request):
raise ParseError("Workspace details not JSON.")

request_workspace_details = {
"files": [
{"name": file["name"]} for file in workspace_details.get("files", [])
],
"files": [{"name": file["name"]} for file in workspace_details.get("files", [])],
"symlinks": workspace_details.get("symlinks", []),
}

Expand Down Expand Up @@ -116,9 +114,7 @@ def post(self, request):
main_storage.set_ownership(
external_user_mapping.external_username, external_user_mapping
)
main_storage.set_ownership(
workspace.file_path, external_user_mapping, recursive=True
)
main_storage.set_ownership(workspace.file_path, external_user_mapping, recursive=True)

workspace.save()
except Exception:
Expand All @@ -134,9 +130,7 @@ def post(self, request):
"message": "Successful.",
"success": True,
"data": {
"workspace": model_to_dict(
workspace, models.Workspace.get_dict_fields()
)
"workspace": model_to_dict(workspace, models.Workspace.get_dict_fields())
},
}
)
Expand All @@ -154,9 +148,7 @@ def put(self, request, workspace_id, put_type=None):
)

try:
workspace = models.Workspace.objects.get(
id=workspace_id, user_id=request.user
)
workspace = models.Workspace.objects.get(id=workspace_id, user_id=request.user)
except models.Workspace.DoesNotExist:
raise NotFound(f"Workspace {workspace_id} not found for user.")

Expand All @@ -169,8 +161,6 @@ def put(self, request, workspace_id, put_type=None):
workspace.name = body.get("name", workspace.name)
workspace.description = body.get("description", workspace.description)

workspace.save()

workspace_details = body.get("workspace_details", {})

if not isinstance(workspace_details, dict):
Expand All @@ -183,11 +173,22 @@ def put(self, request, workspace_id, put_type=None):
workspace.file_path, external_user_mapping, recursive=True
)
except Exception:
logger.exception(
"Failure when creating symlink/files or setting ownership."
)
logger.exception("Failure when creating symlink/files or setting ownership.")
raise

# How do we ensure that the files/symlinks that are listed here are unique?
# Does it matter? We might see duplicates temporarily, but they'll be updated
# eventually as part of the async_task
workspace.workspace_details["current_workspace_details"]["files"].extend(
[{"name": file["name"]} for file in workspace_details.get("files", [])]
)

workspace.workspace_details["current_workspace_details"]["symlinks"].extend(
workspace_details.get("symlinks", [])
)

workspace.save()

async_task("user_workspaces_server.tasks.update_workspace", workspace.pk)

return JsonResponse({"message": "Update successful.", "success": True})
Expand Down Expand Up @@ -283,9 +284,7 @@ def put(self, request, workspace_id, put_type=None):
for file_index, file in request.FILES.items():
main_storage.create_file(workspace.file_path, file)

main_storage.set_ownership(
workspace.file_path, external_user_mapping, recursive=True
)
main_storage.set_ownership(workspace.file_path, external_user_mapping, recursive=True)

async_task("user_workspaces_server.tasks.update_workspace", workspace.pk)

Expand All @@ -295,9 +294,7 @@ def put(self, request, workspace_id, put_type=None):

def delete(self, request, workspace_id):
try:
workspace = models.Workspace.objects.get(
user_id=request.user, id=workspace_id
)
workspace = models.Workspace.objects.get(user_id=request.user, id=workspace_id)
except models.Workspace.DoesNotExist:
raise NotFound(f"Workspace {workspace_id} not found for user.")

Expand All @@ -319,9 +316,7 @@ def delete(self, request, workspace_id):
)

if not main_storage.is_valid_path(workspace.file_path):
logger.error(
f"Workspace {workspace_id} deletion failed due to invalid path"
)
logger.error(f"Workspace {workspace_id} deletion failed due to invalid path")
workspace.status = models.Workspace.Status.ERROR
workspace.save()
raise APIException(
Expand Down

0 comments on commit 6910a10

Please sign in to comment.