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

View File

@ -66,8 +66,17 @@
{% endif %} {% endif %}
{% bootstrap_form form %} {% bootstrap_form form %}
<div class="container"> <div class="container">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a> <div class="row">
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" /> <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> </div>
</form> </form>
</div> </div>

View File

@ -19,4 +19,5 @@ urlpatterns = [
path("import", views.ImportView.as_view(), name="import"), path("import", views.ImportView.as_view(), name="import"),
path("<uuid:pk>", views.EvidenceView.as_view(), name="details"), path("<uuid:pk>", views.EvidenceView.as_view(), name="details"),
path("<uuid:pk>/download", views.DownloadEvidenceView.as_view(), name="download"), 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 import json
from urllib.parse import urlparse
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _ 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.views.generic.base import TemplateView
from django.urls import reverse_lazy from django.urls import reverse_lazy, resolve
from django.views.generic.edit import ( from django.views.generic.edit import (
DeleteView,
FormView, FormView,
) )
from dashboard.mixins import DashboardView, Http403 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 evidence.forms import UploadForm, UserTagForm, ImportForm
# from django.shortcuts import render # from django.shortcuts import render
# from rest_framework import viewsets # from rest_framework import viewsets
@ -135,3 +138,26 @@ class DownloadEvidenceView(DashboardView, TemplateView):
response = HttpResponse(data, content_type="application/json") response = HttpResponse(data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json") response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response 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)