Edit or Delete a CUSTOM_ID

This commit is contained in:
Cayo Puigdefabregas 2024-10-03 12:49:07 +02:00
parent 74d6126c0b
commit 13ba92a6fc
4 changed files with 66 additions and 6 deletions

View File

@ -55,7 +55,18 @@ class UserTagForm(forms.Form):
tag = forms.CharField(label=_("Tag"))
def __init__(self, *args, **kwargs):
self.pk = None
self.uuid = kwargs.pop('uuid', None)
instance = Annotation.objects.filter(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID'
).first()
if instance:
kwargs["initial"]["tag"] = instance.value
self.pk = instance.pk
super().__init__(*args, **kwargs)
def clean(self):
@ -63,12 +74,25 @@ class UserTagForm(forms.Form):
if not data:
return False
self.tag = data
self.instance = Annotation.objects.filter(
uuid=self.uuid,
type=Annotation.Type.SYSTEM,
key='CUSTOM_ID'
).first()
return True
def save(self, user, commit=True):
if not commit:
return
if self.instance:
if not self.tag:
self.instance.delete()
self.instance.value = self.tag
self.instance.save()
return
Annotation.objects.create(
uuid=self.uuid,
owner=user,
@ -103,12 +127,12 @@ class ImportForm(forms.Form):
for n in data_pd.keys():
if 'type' not in [x.lower() for x in data_pd[n]]:
raise ValidationError("You need a column with name 'type'")
for k, v in data_pd[n].items():
if k.lower() == "type":
if v not in Device.Types.values:
raise ValidationError("{} is not a valid device".format(v))
self.rows.append(data_pd[n])
return data

View File

@ -66,8 +66,17 @@
{% endif %}
{% bootstrap_form form %}
<div class="container">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
<div class="row">
<div class="col">
<a class="btn btn-grey" href="">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
{% if form.tag.value %}
<div class="col-1">
<a class="btn btn-yellow" href="{% url 'evidence:delete_annotation' form.pk %}">{% translate "Delete" %}</a>
</div>
{% endif %}
</div>
</div>
</form>
</div>

View File

@ -19,4 +19,5 @@ urlpatterns = [
path("import", views.ImportView.as_view(), name="import"),
path("<uuid:pk>", views.EvidenceView.as_view(), name="details"),
path("<uuid:pk>/download", views.DownloadEvidenceView.as_view(), name="download"),
path('annotation/<int:pk>/del', views.AnnotationDeleteView.as_view(), name='delete_annotation'),
]

View File

@ -1,15 +1,18 @@
import json
from urllib.parse import urlparse
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from django.shortcuts import get_object_or_404, redirect, Http404
from django.views.generic.base import TemplateView
from django.urls import reverse_lazy
from django.urls import reverse_lazy, resolve
from django.views.generic.edit import (
DeleteView,
FormView,
)
from dashboard.mixins import DashboardView, Http403
from evidence.models import Evidence
from evidence.models import Evidence, Annotation
from evidence.forms import UploadForm, UserTagForm, ImportForm
# from django.shortcuts import render
# from rest_framework import viewsets
@ -135,3 +138,26 @@ class DownloadEvidenceView(DashboardView, TemplateView):
response = HttpResponse(data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response
class AnnotationDeleteView(DashboardView, DeleteView):
model = Annotation
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
try:
referer = self.request.META["HTTP_REFERER"]
path_referer = urlparse(referer).path
resolver_match = resolve(path_referer)
url_name = resolver_match.view_name
kwargs_view = resolver_match.kwargs
except:
# if is not possible resolve the reference path return 404
raise Http404
self.object = get_object_or_404(self.model, pk=self.pk, owner=self.request.user)
self.object.delete()
return redirect(url_name, **kwargs_view)