2024-07-05 13:32:07 +00:00
|
|
|
from django.urls import resolve
|
2024-07-09 11:34:30 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2024-07-05 13:32:07 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.views.generic.base import TemplateView
|
2024-07-09 15:31:24 +00:00
|
|
|
from device.models import Device
|
2024-07-26 15:59:34 +00:00
|
|
|
from evidence.models import Annotation
|
2024-07-18 15:21:22 +00:00
|
|
|
from lot.models import LotTag
|
2024-07-05 13:32:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Http403(PermissionDenied):
|
|
|
|
status_code = 403
|
|
|
|
default_detail = _('Permission denied. User is not authenticated')
|
|
|
|
default_code = 'forbidden'
|
|
|
|
|
|
|
|
def __init__(self, details=None, code=None):
|
|
|
|
if details is not None:
|
|
|
|
self.detail = details or self.default_details
|
|
|
|
if code is not None:
|
|
|
|
self.code = code or self.default_code
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardView(LoginRequiredMixin):
|
|
|
|
login_url = "/login/"
|
|
|
|
template_name = "dashboard.html"
|
|
|
|
breadcrumb = ""
|
|
|
|
title = ""
|
|
|
|
subtitle = ""
|
|
|
|
section = ""
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'title': self.title,
|
|
|
|
'subtitle': self.subtitle,
|
|
|
|
'breadcrumb': self.breadcrumb,
|
|
|
|
# 'icon': self.icon,
|
|
|
|
'section': self.section,
|
|
|
|
'path': resolve(self.request.path).url_name,
|
|
|
|
'user': self.request.user,
|
2024-07-18 15:21:22 +00:00
|
|
|
'lot_tags': LotTag.objects.filter(owner=self.request.user)
|
2024-07-05 13:32:07 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2024-07-09 15:31:24 +00:00
|
|
|
def get_session_devices(self):
|
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
dev_ids = self.request.session.pop("devices", [])
|
2024-07-19 15:40:01 +00:00
|
|
|
|
|
|
|
self._devices = []
|
|
|
|
for x in Annotation.objects.filter(value__in=dev_ids).filter(owner=self.request.user).distinct():
|
|
|
|
self._devices.append(Device(id=x.value))
|
2024-07-09 15:31:24 +00:00
|
|
|
return self._devices
|
|
|
|
|
2024-07-05 13:32:07 +00:00
|
|
|
|
|
|
|
class DetailsMixin(DashboardView, TemplateView):
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.pk = kwargs['pk']
|
2024-07-19 15:40:01 +00:00
|
|
|
self.object = get_object_or_404(self.model, pk=self.pk, owner=self.request.user)
|
2024-07-05 13:32:07 +00:00
|
|
|
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
|
2024-07-09 11:34:30 +00:00
|
|
|
|
2024-07-15 14:23:14 +00:00
|
|
|
|
2024-07-09 11:34:30 +00:00
|
|
|
class InventaryMixin(DashboardView, TemplateView):
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2024-07-09 15:31:24 +00:00
|
|
|
dev_ids = dict(self.request.POST).get("devices", [])
|
|
|
|
self.request.session["devices"] = dev_ids
|
2024-07-09 11:34:30 +00:00
|
|
|
url = self.request.POST.get("url")
|
|
|
|
if url:
|
|
|
|
try:
|
|
|
|
resource = resolve(url)
|
2024-07-09 15:31:24 +00:00
|
|
|
if resource and dev_ids:
|
2024-07-09 11:34:30 +00:00
|
|
|
return redirect(url)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return super().get(request, *args, **kwargs)
|