From 13ba92a6fcc08639c551dbc9b61f128a65fcfc3f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 3 Oct 2024 12:49:07 +0200 Subject: [PATCH] Edit or Delete a CUSTOM_ID --- evidence/forms.py | 28 ++++++++++++++++++++++++++-- evidence/templates/ev_details.html | 13 +++++++++++-- evidence/urls.py | 1 + evidence/views.py | 30 ++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/evidence/forms.py b/evidence/forms.py index ae2630d..da5760c 100644 --- a/evidence/forms.py +++ b/evidence/forms.py @@ -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 diff --git a/evidence/templates/ev_details.html b/evidence/templates/ev_details.html index 4c3fdbf..94c3058 100644 --- a/evidence/templates/ev_details.html +++ b/evidence/templates/ev_details.html @@ -66,8 +66,17 @@ {% endif %} {% bootstrap_form form %}
- {% translate "Cancel" %} - +
+ + {% if form.tag.value %} + + {% endif %} +
diff --git a/evidence/urls.py b/evidence/urls.py index d4dff01..87ccdd5 100644 --- a/evidence/urls.py +++ b/evidence/urls.py @@ -19,4 +19,5 @@ urlpatterns = [ path("import", views.ImportView.as_view(), name="import"), path("", views.EvidenceView.as_view(), name="details"), path("/download", views.DownloadEvidenceView.as_view(), name="download"), + path('annotation//del', views.AnnotationDeleteView.as_view(), name='delete_annotation'), ] diff --git a/evidence/views.py b/evidence/views.py index 1e02c7e..8794cef 100644 --- a/evidence/views.py +++ b/evidence/views.py @@ -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)