Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix git and api calls #23

Merged
merged 9 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion e2xauthoring/models/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Pool(Observer):
def __init__(self, name: str, base_path: str):
self.name = name
self.base_path = base_path
self.path = os.path.join(base_path, name)
self.path = os.path.realpath(os.path.join(base_path, name))
self.repo = GitRepoFactory.get_instance(self.path)
self.repo.attach(self)
self._is_version_controlled = self.repo.is_version_controlled
Expand Down Expand Up @@ -65,6 +65,11 @@ def rename(self, new_name: str):
os.rename(self.path, new_path)
self.name = new_name
self.path = new_path

self.repo = GitRepoFactory.get_instance(self.path)
for task in self.tasks.values():
task.repo = self.repo

self.repo.update_status()

def turn_into_repository(self):
Expand Down
40 changes: 24 additions & 16 deletions e2xauthoring/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from typing import Dict

import nbformat
from e2xcore.utils.nbgrader_cells import get_points, is_grade, new_read_only_cell
from e2xcore.utils.nbgrader_cells import (
get_points,
is_grade,
is_nbgrader_cell,
new_read_only_cell,
)
from jupyter_client.kernelspec import KernelSpecManager

from ..dataclasses import GitStatus, TaskRecord
Expand Down Expand Up @@ -53,7 +58,7 @@ class Task(Observer):
def __init__(self, name: str, pool: str, base_path: str, repo: GitRepo):
self.name = name
self.pool = pool
self.path = os.path.join(base_path, pool, name)
self.path = os.path.realpath(os.path.join(base_path, pool, name))
self.base_path = base_path
self.repo = repo
self.repo.attach(self)
Expand Down Expand Up @@ -117,25 +122,27 @@ def remove(self):
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 _rename_notebook(self, path: str, old_name: str, new_name: str):
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
os.path.join(path, f"{old_name}.ipynb"),
os.path.join(path, f"{new_name}.ipynb"),
)
notebook_path = os.path.join(path, f"{new_name}.ipynb")
nb = nbformat.read(notebook_path, as_version=nbformat.NO_CONVERT)
for cell in nb.cells:
if is_grade(cell):
cell.source = cell.source.replace(self.name, new_name)
if is_nbgrader_cell(cell):
cell.source = cell.source.replace(old_name, new_name)
cell.metadata.nbgrader.grade_id = (
cell.metadata.nbgrader.grade_id.replace(self.name, new_name)
cell.metadata.nbgrader.grade_id.replace(old_name, new_name)
)
nbformat.write(nb, os.path.join(new_path, f"{new_name}.ipynb"))
nbformat.write(nb, notebook_path)

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._rename_notebook(new_path, self.name, new_name)
self.repo.update_status()
return Task(new_name, self.pool, self.base_path, self.repo)

Expand All @@ -144,6 +151,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._rename_notebook(new_path, self.name, new_name)
self.path = new_path
self.name = new_name
self.repo.update_status()
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export const API = {
rename: (old_name, new_name) =>
requests.put(POOL_API_ROOT, {
action: "rename",
old_name: old_name,
name: old_name,
new_name: new_name,
}),
copy: (old_name, new_name) =>
requests.put(POOL_API_ROOT, {
action: "copy",
old_name: old_name,
name: old_name,
new_name: new_name,
}),
turn_into_repository: (pool) =>
Expand Down
5 changes: 4 additions & 1 deletion packages/app/src/components/alerts/GitAuthorNotSetAlert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ export default function GitAuthorNotSetAlert() {
setAuthorIsSet(!!author);
});
}, []);
const handleAuthorSet = () => {
setAuthorIsSet(true);
};
return (
<>
{authorIsSet ? (
<></>
) : (
<Alert severity="warning">
<AlertTitle>Git Author is not set</AlertTitle>
<SetGitAuthorDialog />
<SetGitAuthorDialog onAuthorSet={handleAuthorSet} />
</Alert>
)}
</>
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/components/dialogs/SetGitAuthorDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import API from "@e2xauthoring/api";
import { FormDialogWithoutButton } from "./form-dialogs";
import { FormikTextField } from "../forms/form-components";

export default function SetGitAuthorDialog() {
export default function SetGitAuthorDialog({ onAuthorSet }) {
const [open, setOpen] = React.useState(false);
const formik = useFormik({
initialValues: {
Expand All @@ -24,11 +24,13 @@ export default function SetGitAuthorDialog() {
email: yup.string().email().required(),
}),
onSubmit: (values) => {
API.git.setAuthor(values.name, values.email).then((res) => {
console.log(values);
API.git_author.set(values.name, values.email).then((res) => {
if (!res["success"]) {
alert(res["message"]);
} else {
setOpen(false);
onAuthorSet();
}
});
},
Expand Down
Loading