devicehub-django/evidence/views.py

214 lines
6.1 KiB
Python
Raw Normal View History

import json
2024-11-11 17:57:20 +00:00
from django.contrib import messages
2024-10-03 10:49:07 +00:00
from urllib.parse import urlparse
from django.http import HttpResponse
2024-07-11 15:40:45 +00:00
from django.utils.translation import gettext_lazy as _
2024-10-03 10:49:07 +00:00
from django.shortcuts import get_object_or_404, redirect, Http404
2024-07-11 15:40:45 +00:00
from django.views.generic.base import TemplateView
2024-10-03 10:49:07 +00:00
from django.urls import reverse_lazy, resolve
2024-07-18 15:21:22 +00:00
from django.views.generic.edit import (
2024-10-03 10:49:07 +00:00
DeleteView,
2024-07-19 15:40:01 +00:00
FormView,
2024-07-18 15:21:22 +00:00
)
2024-07-13 13:27:26 +00:00
2024-07-31 17:24:22 +00:00
from dashboard.mixins import DashboardView, Http403
2024-10-03 10:49:07 +00:00
from evidence.models import Evidence, Annotation
2024-10-28 16:52:34 +00:00
from evidence.forms import (
UploadForm,
UserTagForm,
ImportForm,
EraseServerForm
)
2024-06-12 07:32:49 +00:00
2024-07-26 15:59:34 +00:00
class ListEvidencesView(DashboardView, TemplateView):
template_name = "evidences.html"
section = "evidences"
title = _("Evidences")
breadcrumb = "Evidences"
2024-07-11 15:40:45 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2024-07-31 11:28:46 +00:00
evidences = Evidence.get_all(self.request.user)
2024-07-11 15:40:45 +00:00
context.update({
2024-07-26 15:59:34 +00:00
'evidences': evidences,
2024-07-11 15:40:45 +00:00
})
return context
2024-07-13 13:27:26 +00:00
2024-07-30 17:38:04 +00:00
class UploadView(DashboardView, FormView):
template_name = "upload.html"
section = "evidences"
title = _("Upload Evidence")
breadcrumb = "Evidences / Upload"
success_url = reverse_lazy('evidence:list')
form_class = UploadForm
def form_valid(self, form):
form.save(self.request.user)
2024-11-11 17:57:20 +00:00
messages.success(self.request, _("Evidence uploaded successfully."))
2024-07-30 17:38:04 +00:00
response = super().form_valid(form)
return response
def form_invalid(self, form):
response = super().form_invalid(form)
return response
2024-07-31 17:24:22 +00:00
2024-08-05 11:32:52 +00:00
class ImportView(DashboardView, FormView):
template_name = "upload.html"
section = "evidences"
title = _("Import Evidence")
breadcrumb = "Evidences / Import"
success_url = reverse_lazy('evidence:list')
form_class = ImportForm
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
form.save()
2024-11-11 17:57:20 +00:00
messages.success(self.request, _("Evidence imported successfully."))
2024-08-05 11:32:52 +00:00
response = super().form_valid(form)
return response
def form_invalid(self, form):
response = super().form_invalid(form)
return response
class EvidenceView(DashboardView, FormView):
2024-07-31 17:24:22 +00:00
template_name = "ev_details.html"
section = "evidences"
title = _("Evidences")
breadcrumb = "Evidences / Details"
success_url = reverse_lazy('evidence:list')
form_class = UserTagForm
2024-07-31 17:24:22 +00:00
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = Evidence(self.pk)
2024-09-18 16:01:46 +00:00
if self.object.owner != self.request.user.institution:
2024-07-31 17:24:22 +00:00
raise Http403
self.object.get_annotations()
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
def get_form_kwargs(self):
self.pk = self.kwargs.get('pk')
kwargs = super().get_form_kwargs()
kwargs['uuid'] = self.pk
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
form.save(self.request.user)
response = super().form_valid(form)
return response
def form_invalid(self, form):
response = super().form_invalid(form)
return response
def get_success_url(self):
success_url = reverse_lazy('evidence:details', args=[self.pk])
return success_url
class DownloadEvidenceView(DashboardView, TemplateView):
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
evidence = Evidence(pk)
2024-09-18 16:01:46 +00:00
if evidence.owner != self.request.user.institution:
raise Http403()
evidence.get_doc()
data = json.dumps(evidence.doc)
response = HttpResponse(data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response
2024-10-03 10:49:07 +00:00
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.institution
)
2024-10-03 10:49:07 +00:00
self.object.delete()
return redirect(url_name, **kwargs_view)
2024-10-28 16:52:34 +00:00
class EraseServerView(DashboardView, FormView):
template_name = "ev_eraseserver.html"
section = "evidences"
title = _("Evidences")
breadcrumb = "Evidences / Details"
success_url = reverse_lazy('evidence:list')
form_class = EraseServerForm
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = Evidence(self.pk)
if self.object.owner != self.request.user.institution:
raise Http403
self.object.get_annotations()
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'object': self.object,
})
return context
def get_form_kwargs(self):
self.pk = self.kwargs.get('pk')
kwargs = super().get_form_kwargs()
kwargs['uuid'] = self.pk
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
form.save(self.request.user)
response = super().form_valid(form)
return response
def form_invalid(self, form):
response = super().form_invalid(form)
return response
def get_success_url(self):
success_url = reverse_lazy('evidence:details', args=[self.pk])
return success_url