Skip to content

Commit

Permalink
Add ability to update input source tag in UI #708
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Feb 1, 2024
1 parent 413e27a commit 1d460da
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
21 changes: 21 additions & 0 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from django import forms
from django.apps import apps
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError

from taggit.forms import TagField
Expand Down Expand Up @@ -183,6 +184,26 @@ def save(self, project):
return project


class EditInputSourceTagForm(forms.Form):
input_source_uuid = forms.CharField(
widget=forms.widgets.HiddenInput,
required=True,
)
tag = forms.CharField(
widget=forms.TextInput(attrs={"class": "input"}),
)

def save(self, project):
input_source_uuid = self.cleaned_data["input_source_uuid"]
try:
input_source = project.inputsources.get(uuid=input_source_uuid)
except (ValidationError, ObjectDoesNotExist):
return

input_source.update(tag=self.cleaned_data["tag"])
return input_source


class ArchiveProjectForm(forms.Form):
remove_input = forms.BooleanField(
label="Remove inputs",
Expand Down
39 changes: 39 additions & 0 deletions scanpipe/templates/scanpipe/modals/edit_input_tag_modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div id="edit-input-tag-modal" class="modal">
<div class="modal-background"></div>
<form method="post">{% csrf_token %}
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title">Edit tag</p>
<button class="delete" type="button" aria-label="close"></button>
</header>
<section class="modal-card-body">
{{ edit_input_tag_from.input_source_uuid }}
<div class="field">
<label class="label has-text-weight-semibold" for="{{ edit_input_tag_from.tag.id_for_label }}">{{ edit_input_tag_from.tag.label }}</label>
<div class="control">
{{ edit_input_tag_from.tag }}
</div>
</div>
</section>
<footer class="modal-card-foot">
<button id="edit-input-tag-button" name="edit-input-tag-submit" class="button is-link is-no-close" type="submit">Update tag</button>
<button class="button" type="button">Close</button>
</footer>
</div>
</form>
</div>
<script>
document.addEventListener("openModal", function(event) {
const modal_id = event.detail.modal;
if (modal_id !== "edit-input-tag-modal") return;

const $modal = document.getElementById(modal_id);
const $input_source_uuid_input = $modal.querySelector("input#id_input_source_uuid");
const $tag_input = $modal.querySelector("input#id_tag");

const input_source_uuid = event.detail.$button.dataset.inputSourceUuid;
const tag_value = event.detail.$button.dataset.tagValue;
$input_source_uuid_input.value = input_source_uuid;
$tag_input.value = tag_value;
});
</script>
3 changes: 3 additions & 0 deletions scanpipe/templates/scanpipe/panels/project_inputs.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
{% if project.can_change_inputs and input_source.uuid %}
<a class="modal-button is-grey-link is-clickable ml-1" data-target="modal-inputs-delete" aria-haspopup="true" data-url="{% url 'project_delete_input' project.slug input_source.uuid %}" data-filename="{{ input_source.filename }}" href="#"><i class="fa-regular fa-trash-can"></i></a>
{% endif %}
<a class="modal-button is-grey-link is-clickable ml-1" data-input-source-uuid="{{ input_source.uuid }}" data-tag-value="{{ input_source.tag }}" data-target="edit-input-tag-modal" aria-haspopup="true">
<span class="icon width-1 height-1"><i class="fa-solid fa-tag"></i></span>
</a>
</div>
</div>
{% endfor %}
Expand Down
4 changes: 3 additions & 1 deletion scanpipe/templates/scanpipe/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</span>
{% include 'scanpipe/includes/project_labels.html' with labels=labels only %}
<a class="modal-button is-size-7" data-target="add-labels-modal" aria-haspopup="true">
Edit labels
{% if labels %}Edit{% else %}Add{% endif %} labels
</a>
</div>
</div>
Expand Down Expand Up @@ -163,6 +163,7 @@
{% include 'scanpipe/modals/run_modal.html' %}
{% include 'scanpipe/modals/clone_modal.html' %}
{% include "scanpipe/modals/add_labels_modal.html" %}
{% include "scanpipe/modals/edit_input_tag_modal.html" %}
{% endblock %}

{% block scripts %}
Expand All @@ -187,6 +188,7 @@
onSubmitOverlay("#add-pipeline-modal form");
onSubmitOverlay("#add-inputs-modal form");
onSubmitOverlay("#add-labels-modal form");
onSubmitOverlay("#edit-input-tag-modal form");
onSubmitOverlay("#clone-modal form");
</script>
{% endblock %}
9 changes: 7 additions & 2 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
from scanpipe.forms import AddLabelsForm
from scanpipe.forms import AddPipelineForm
from scanpipe.forms import ArchiveProjectForm
from scanpipe.forms import EditInputSourceTagForm
from scanpipe.forms import ProjectCloneForm
from scanpipe.forms import ProjectForm
from scanpipe.forms import ProjectSettingsForm
Expand Down Expand Up @@ -695,6 +696,7 @@ def get_context_data(self, **kwargs):
"add_pipeline_form": AddPipelineForm(),
"add_inputs_form": AddInputsForm(),
"add_labels_form": AddLabelsForm(),
"edit_input_tag_from": EditInputSourceTagForm(),
"project_clone_form": ProjectCloneForm(project),
"project_resources_url": project_resources_url,
"license_clarity": license_clarity,
Expand Down Expand Up @@ -725,13 +727,16 @@ def post(self, request, *args, **kwargs):
form_class = AddLabelsForm
success_message = "Label(s) added."
error_message = "Label addition error."
elif "edit-input-tag-submit" in request.POST:
form_class = EditInputSourceTagForm
success_message = "Tag updated."
error_message = "Tag update error."
else:
raise Http404

form_kwargs = {"data": request.POST, "files": request.FILES}
form = form_class(**form_kwargs)
if form.is_valid():
form.save(project)
if form.is_valid() and form.save(project):
messages.success(request, success_message)
else:
messages.error(request, error_message)
Expand Down

0 comments on commit 1d460da

Please sign in to comment.