From 20d21219f970f507efc315a5d66516b5d29bbc6e Mon Sep 17 00:00:00 2001 From: Tim Daniel Metzler Date: Mon, 20 Jan 2025 14:51:50 +0100 Subject: [PATCH] Fix renaming task --- e2xauthoring/models/task.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/e2xauthoring/models/task.py b/e2xauthoring/models/task.py index 55f566a..f3959dd 100644 --- a/e2xauthoring/models/task.py +++ b/e2xauthoring/models/task.py @@ -108,20 +108,7 @@ def create( return Task(name, pool, base_path, repo) - def remove(self): - task_path = self.path - assert os.path.exists( - task_path - ), f"Task {self.name} does not exist in pool {self.pool}" - shutil.rmtree(task_path) - self.repo.detach(self) - self.repo.update_status() - - def copy(self, new_name: str): - old_path = self.path - new_path = os.path.join(os.path.dirname(old_path), new_name) - assert not os.path.exists(new_path), f"Task {new_name} already exists" - shutil.copytree(old_path, new_path) + def copy_and_rename_notebook(self, new_path, new_name): shutil.move( os.path.join(new_path, f"{self.name}.ipynb"), os.path.join(new_path, f"{new_name}.ipynb"), @@ -129,6 +116,7 @@ def copy(self, new_name: str): nb = nbformat.read( os.path.join(new_path, f"{new_name}.ipynb"), as_version=nbformat.NO_CONVERT ) + self._rename_notebook(nb, self.name, new_name) for cell in nb.cells: if is_grade(cell): cell.source = cell.source.replace(self.name, new_name) @@ -136,6 +124,22 @@ def copy(self, new_name: str): cell.metadata.nbgrader.grade_id.replace(self.name, new_name) ) nbformat.write(nb, os.path.join(new_path, f"{new_name}.ipynb")) + + def remove(self): + task_path = self.path + assert os.path.exists( + task_path + ), f"Task {self.name} does not exist in pool {self.pool}" + shutil.rmtree(task_path) + self.repo.detach(self) + self.repo.update_status() + + def copy(self, new_name: str): + old_path = self.path + new_path = os.path.join(os.path.dirname(old_path), new_name) + assert not os.path.exists(new_path), f"Task {new_name} already exists" + shutil.copytree(old_path, new_path) + self.copy_and_rename_notebook(new_path, new_name) self.repo.update_status() return Task(new_name, self.pool, self.base_path, self.repo) @@ -144,6 +148,7 @@ def rename(self, new_name: str): new_path = os.path.join(os.path.dirname(old_path), new_name) assert not os.path.exists(new_path), f"Task {new_name} already exists" shutil.move(old_path, new_path) + self.copy_and_rename_notebook(new_path, new_name) self.path = new_path self.name = new_name self.repo.update_status()