def delete_project(request): try: username = request.data.get("username") user = User(username=username, password=None) user = user.find() project_id = request.data.get("project_id") store_obj = Store(user) err, exception = store_obj.delete(project_id) if err: raise Exception(exception) if project_id.startswith("shared_"): project_dir = os.path.join( store_obj.rootpath, request.data.get("owner"), project_id.replace("shared_", ""), ) with open(project_dir + os.sep + "meta.json", "r") as f: metadata = json.load(f) if metadata.get("shared_with", None): if username in metadata.get("shared_with"): metadata["shared_with"].remove(username) with open(project_dir + os.sep + "meta.json", "w") as f: json.dump(metadata, f) status, success, message = 200, True, "Project Deleted Successfully" except Exception as e: status, success, message = 500, False, "Project could not be deleted" return JsonResponse({"success": success, "message": message}, status=status)
def test_delete(): user = MockUser().mock_user() store = Store(user) store.rootpath = os.path.expanduser(tempdir + "/.autodl/") store.path = posixpath.join(store.rootpath, store.user.get("username")) project_path = store.create(project="test_project_dir") state, result = store.delete(project="test_project_dir") assert os.path.exists(project_path) == False shared_with_user = MockUser().mock_user() shared_with = shared_with_user.get("username") test_path = store.rootpath + shared_with + "/shared_test_project.json" file = "shared_test_project.json" with open(posixpath.join(store.path, file), "w") as fp: pass state, result = store.delete(project="shared_test_project.json") assert os.path.exists(test_path) == False project_path_none = store.create(project=None) state, result = store.delete(project=None) assert os.path.exists(project_path_none) == False
def delete_project(request): try: username = request.data.get("username") user = User(username=username, password=None) user = user.find() project_id = request.data.get("project_id") store_obj = Store(user) err, exception = store_obj.delete(project_id) if err: raise Exception(exception) status, success, message = 200, True, "Project Deleted Successfully" except Exception as e: status, success, message = 500, False, str(e) return JsonResponse({ "success": success, "message": message }, status=status)