Skip to content

Commit

Permalink
Fix renaming task
Browse files Browse the repository at this point in the history
  • Loading branch information
tmetzl committed Jan 20, 2025
1 parent 89f9b43 commit 20d2121
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions e2xauthoring/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,38 @@ 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"),
)
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)
cell.metadata.nbgrader.grade_id = (
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)

Expand All @@ -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()
Expand Down

0 comments on commit 20d2121

Please sign in to comment.