Skip to content

Commit

Permalink
chore: sync run folder when editing label
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwski committed Feb 23, 2023
1 parent c6ac56d commit c7bff46
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
16 changes: 16 additions & 0 deletions euphro_tools/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ def initialize_run_directory(project_name: str, run_name: str):
response.status_code,
response.text,
)


def rename_run_directory(project_name: str, run_name: str, new_run_name: str):
base_url = os.environ["EUPHROSYNE_TOOLS_API_URL"]
response = _make_request(
f"{base_url}/data/{project_name}/runs/{run_name}/rename/{new_run_name}"
)
if response is not None and not response.ok:
logger.error(
"Could not update run directory name from %s to %s of project %s. %s: %s",
run_name,
new_run_name,
project_name,
response.status_code,
response.text,
)
12 changes: 12 additions & 0 deletions euphro_tools/tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_make_request,
initialize_project_directory,
initialize_run_directory,
rename_run_directory,
)


Expand All @@ -32,6 +33,17 @@ def test_initialize_run_directory(requests_mock: MagicMock, monkeypatch: MonkeyP
)


@patch("euphro_tools.hooks.requests")
def test_rename_run_directory(requests_mock: MagicMock, monkeypatch: MonkeyPatch):
monkeypatch.setenv("EUPHROSYNE_TOOLS_API_URL", "http://euphro.tools")
rename_run_directory("project", "run", "newname")

requests_mock.post.assert_called_once()
assert requests_mock.post.call_args[0] == (
"http://euphro.tools/data/project/runs/run/rename/newname",
)


@patch("euphro_tools.hooks.requests")
@patch("euphro_tools.hooks.EuphroToolsAPIToken")
def test_make_request_has_auth_header(
Expand Down
4 changes: 3 additions & 1 deletion lab/admin/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.http.request import HttpRequest
from django.utils.translation import gettext_lazy as _

from euphro_tools.hooks import initialize_run_directory
from euphro_tools.hooks import initialize_run_directory, rename_run_directory

from ..forms import RunDetailsForm
from ..models import Project, Run
Expand Down Expand Up @@ -230,6 +230,8 @@ def save_model(self, request: Any, obj: Run, form: ModelForm, change: bool) -> N
super().save_model(request, obj, form, change)
if not change:
initialize_run_directory(obj.project.name, obj.label)
elif "label" in form.changed_data:
rename_run_directory(obj.project.name, form.initial["label"], obj.label)

def _get_project(self, request, object_id=None) -> Optional[Project]:
if object_id:
Expand Down
43 changes: 42 additions & 1 deletion lab/tests/admin/test_run_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,48 @@ def test_add_run_calls_init_directroy_hook(self, init_run_dir_mock):
form=MagicMock(),
change=False,
)
init_run_dir_mock.assert_called_once_with(run.label, run.project.name)
init_run_dir_mock.assert_called_once_with(run.project.name, run.label)

@patch("lab.admin.run.rename_run_directory")
def test_change_run_label_calls_hook(self, rename_run_dir_mock):
run = factories.RunFactory()
run.project.members.add(self.staff_user)
request = self.request_factory.post(
reverse(
"admin:lab_run_change",
args=[run.id],
),
data={
"label": "new-run-name",
"particle_type": "",
"energy_in_keV_Proton": "",
"energy_in_keV_Alpha particle": "",
"energy_in_keV_Deuton": "",
"beamline": "Microbeam",
"filters_for_detector_LE0": "",
"filters_for_detector_HE1": "",
"filters_for_detector_HE2": "",
"filters_for_detector_HE3": "",
"filters_for_detector_HE4": "",
"detector_IBIL_other": "",
"detector_FORS_other": "",
"detector_ERDA": "",
"detector_NRA": "",
"Run_run_object_groups-TOTAL_FORMS": "0",
"Run_run_object_groups-INITIAL_FORMS": "0",
},
)
request.user = self.staff_user
with patch.object(run, "save"):
run_admin = RunAdmin(Run, admin_site=AdminSite())
form_class = run_admin.get_form(request, obj=run, change=True)
form = form_class(request.POST, request.FILES, instance=run)
# Clean form to populate run instance
form.is_valid()
run_admin.save_model(request, run, form=form, change=True)
rename_run_dir_mock.assert_called_once_with(
run.project.name, form.initial["label"], "new-run-name"
)


class TestRunAdminViewAsAdmin(TestCase):
Expand Down

0 comments on commit c7bff46

Please sign in to comment.