Edit or Delete a CUSTOM_ID
This commit is contained in:
parent
74d6126c0b
commit
13ba92a6fc
|
@ -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,
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue