From fae6d3717a5068c532b6c6eeed99d19715b8c611 Mon Sep 17 00:00:00 2001 From: amickan Date: Fri, 1 Dec 2023 08:58:35 +0100 Subject: [PATCH 1/2] Check if image exists before deleting civs --- .../static/archives/js/archive_item_update.js | 10 +++++++ app/grandchallenge/archives/tasks.py | 4 ++- .../archives/archive_items_list.html | 1 + .../templates/archives/archive_items_row.html | 2 +- app/tests/archives_tests/test_views.py | 26 ++++++++++++------- 5 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 app/grandchallenge/archives/static/archives/js/archive_item_update.js diff --git a/app/grandchallenge/archives/static/archives/js/archive_item_update.js b/app/grandchallenge/archives/static/archives/js/archive_item_update.js new file mode 100644 index 0000000000..1b97d0775f --- /dev/null +++ b/app/grandchallenge/archives/static/archives/js/archive_item_update.js @@ -0,0 +1,10 @@ +$('#ajaxDataTable').on( 'init.dt', function() { + allSelectElements = document.querySelectorAll('[id^="interfaceSelect"]'); + allSelectElements.forEach(function(elem) { + elem.addEventListener("change", loadUpdateView); + }); +}); + +function loadUpdateView(source) { + window.location.href = source.target.value +} diff --git a/app/grandchallenge/archives/tasks.py b/app/grandchallenge/archives/tasks.py index 8547f7b5ef..986ceb30f6 100644 --- a/app/grandchallenge/archives/tasks.py +++ b/app/grandchallenge/archives/tasks.py @@ -133,7 +133,9 @@ def update_archive_item_values(*, archive_item_pk, civ_pks_to_add): ).values_list("pk", flat=True): civ_pks_to_remove.append(civ_pk) # for images, check if there are any CIVs with the provided image - if civ.interface.is_image_kind: + # this is necessary to enable updating the interface + # of a given image via the API + if civ.interface.is_image_kind and civ.image: if instance.values.filter(image=civ.image).exists(): for civ_pk in instance.values.filter( image=civ.image diff --git a/app/grandchallenge/archives/templates/archives/archive_items_list.html b/app/grandchallenge/archives/templates/archives/archive_items_list.html index 109787ecf9..61c278d684 100644 --- a/app/grandchallenge/archives/templates/archives/archive_items_list.html +++ b/app/grandchallenge/archives/templates/archives/archive_items_list.html @@ -30,4 +30,5 @@

Items for {{ archive.title }}

{% block script %} {{ block.super }} {% include 'workstations/partials/session-control.html' %} + {% endblock %} diff --git a/app/grandchallenge/archives/templates/archives/archive_items_row.html b/app/grandchallenge/archives/templates/archives/archive_items_row.html index 03877967d8..fdc30d66d9 100644 --- a/app/grandchallenge/archives/templates/archives/archive_items_row.html +++ b/app/grandchallenge/archives/templates/archives/archive_items_row.html @@ -18,7 +18,7 @@ - {% for interface in interfaces %} {% if interface not in object_interfaces %} diff --git a/app/tests/archives_tests/test_views.py b/app/tests/archives_tests/test_views.py index 4bf9cd1284..2e0423813f 100644 --- a/app/tests/archives_tests/test_views.py +++ b/app/tests/archives_tests/test_views.py @@ -720,9 +720,14 @@ def test_archive_item_add_image( item = ArchiveItemFactory(archive=archive) editor = UserFactory() archive.add_editor(editor) - ci = ComponentInterfaceFactory( + ci_img = ComponentInterfaceFactory( kind=InterfaceKind.InterfaceKindChoices.IMAGE ) + ci_value = ComponentInterfaceFactory( + kind=InterfaceKind.InterfaceKindChoices.BOOL + ) + civ_value = ComponentInterfaceValueFactory(interface=ci_value, value=True) + item.values.add(civ_value) upload = create_upload_from_file( file_path=RESOURCE_PATH / "image10x10x10.mha", creator=editor, @@ -735,17 +740,18 @@ def test_archive_item_add_image( method=client.post, reverse_kwargs={ "pk": item.pk, - "interface_slug": ci.slug, + "interface_slug": ci_img.slug, "archive_slug": archive.slug, }, user=editor, follow=True, data={ - ci.slug: upload.pk, - f"WidgetChoice-{ci.slug}": WidgetChoices.IMAGE_UPLOAD.name, + ci_img.slug: upload.pk, + f"WidgetChoice-{ci_img.slug}": WidgetChoices.IMAGE_UPLOAD.name, }, ) assert response.status_code == 200 + assert item.values.count() == 2 assert "image10x10x10.mha" == item.values.first().image.name old_civ = item.values.first() @@ -757,14 +763,14 @@ def test_archive_item_add_image( method=client.post, reverse_kwargs={ "pk": item.pk, - "interface_slug": ci.slug, + "interface_slug": ci_img.slug, "archive_slug": archive.slug, }, user=editor, follow=True, data={ - ci.slug: old_civ.image.pk, - f"WidgetChoice-{ci.slug}": WidgetChoices.IMAGE_SEARCH.name, + ci_img.slug: old_civ.image.pk, + f"WidgetChoice-{ci_img.slug}": WidgetChoices.IMAGE_SEARCH.name, }, ) assert response.status_code == 200 @@ -781,14 +787,14 @@ def test_archive_item_add_image( method=client.post, reverse_kwargs={ "pk": item.pk, - "interface_slug": ci.slug, + "interface_slug": ci_img.slug, "archive_slug": archive.slug, }, user=editor, follow=True, data={ - ci.slug: image.pk, - f"WidgetChoice-{ci.slug}": WidgetChoices.IMAGE_SEARCH.name, + ci_img.slug: image.pk, + f"WidgetChoice-{ci_img.slug}": WidgetChoices.IMAGE_SEARCH.name, }, ) assert response.status_code == 200 From 478ac792af73bad6b020cc78ae8aaf21ed937b46 Mon Sep 17 00:00:00 2001 From: amickan Date: Fri, 1 Dec 2023 16:11:20 +0100 Subject: [PATCH 2/2] Turn js file into module --- .../js/{archive_item_update.js => archive_item_update.mjs} | 0 .../archives/templates/archives/archive_items_list.html | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename app/grandchallenge/archives/static/archives/js/{archive_item_update.js => archive_item_update.mjs} (100%) diff --git a/app/grandchallenge/archives/static/archives/js/archive_item_update.js b/app/grandchallenge/archives/static/archives/js/archive_item_update.mjs similarity index 100% rename from app/grandchallenge/archives/static/archives/js/archive_item_update.js rename to app/grandchallenge/archives/static/archives/js/archive_item_update.mjs diff --git a/app/grandchallenge/archives/templates/archives/archive_items_list.html b/app/grandchallenge/archives/templates/archives/archive_items_list.html index 61c278d684..9891289fd6 100644 --- a/app/grandchallenge/archives/templates/archives/archive_items_list.html +++ b/app/grandchallenge/archives/templates/archives/archive_items_list.html @@ -30,5 +30,5 @@

Items for {{ archive.title }}

{% block script %} {{ block.super }} {% include 'workstations/partials/session-control.html' %} - + {% endblock %}