devicehub-django/device/views.py

181 lines
5.9 KiB
Python
Raw Normal View History

2024-07-15 14:23:14 +00:00
import json
2024-07-05 13:32:07 +00:00
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import (
CreateView,
UpdateView,
2024-07-19 15:40:01 +00:00
FormView,
2024-07-05 13:32:07 +00:00
)
2024-07-15 14:23:14 +00:00
from django.views.generic.base import TemplateView
2024-07-19 15:40:01 +00:00
from dashboard.mixins import DashboardView
2024-07-30 11:37:08 +00:00
from evidence.models import Annotation
from evidence.xapian import search
2024-07-18 15:21:22 +00:00
from lot.models import LotTag
from device.models import Device
2024-07-19 15:40:01 +00:00
from device.forms import DeviceFormSet
2024-07-05 13:32:07 +00:00
2024-07-19 15:40:01 +00:00
class NewDeviceView(DashboardView, FormView):
2024-07-05 13:32:07 +00:00
template_name = "new_device.html"
title = _("New Device")
breadcrumb = "Device / New Device"
2024-07-22 11:48:10 +00:00
success_url = reverse_lazy('device:add')
2024-07-19 15:40:01 +00:00
form_class = DeviceFormSet
2024-07-05 13:32:07 +00:00
def form_valid(self, form):
2024-07-23 13:37:40 +00:00
form.save(self.request.user)
2024-07-05 13:32:07 +00:00
response = super().form_valid(form)
return response
2024-07-18 15:21:22 +00:00
2024-07-22 11:48:10 +00:00
def form_invalid(self, form):
response = super().form_invalid(form)
return response
2024-07-05 13:32:07 +00:00
2024-07-19 15:40:01 +00:00
# class AddToLotView(DashboardView, FormView):
# template_name = "list_lots.html"
# title = _("Add to lots")
# breadcrumb = "lot / add to lots"
# success_url = reverse_lazy('dashboard:unassigned_devices')
# form_class = LotsForm
# def get_context_data(self, **kwargs):
# context = super().get_context_data(**kwargs)
# lots = Lot.objects.filter(owner=self.request.user)
# lot_tags = LotTag.objects.filter(owner=self.request.user)
# context.update({
# 'lots': lots,
# 'lot_tags':lot_tags,
# })
# return context
# def get_form(self):
# form = super().get_form()
# form.fields["lots"].queryset = Lot.objects.filter(owner=self.request.user)
# return form
# def form_valid(self, form):
# form.devices = self.get_session_devices()
# form.save()
# response = super().form_valid(form)
# return response
2024-07-08 10:59:32 +00:00
class EditDeviceView(DashboardView, UpdateView):
template_name = "new_device.html"
title = _("Update Device")
breadcrumb = "Device / Update Device"
success_url = reverse_lazy('dashboard:unassigned_devices')
2024-07-19 15:40:01 +00:00
model = Annotation
2024-07-08 10:59:32 +00:00
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.object = get_object_or_404(self.model, pk=pk)
self.success_url = reverse_lazy('device:details', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs
2024-07-05 13:32:07 +00:00
2024-07-19 15:40:01 +00:00
class DetailsView(DashboardView, TemplateView):
2024-07-15 14:23:14 +00:00
template_name = "details.html"
title = _("Device")
breadcrumb = "Device / Details"
2024-07-19 15:40:01 +00:00
model = Annotation
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = Device(id=self.pk)
return super().get(request, *args, **kwargs)
2024-07-15 14:23:14 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2024-07-18 15:21:22 +00:00
self.object.initial()
2024-09-18 16:01:46 +00:00
lot_tags = LotTag.objects.filter(owner=self.request.user.institution)
2024-07-15 14:23:14 +00:00
context.update({
2024-07-19 15:40:01 +00:00
'object': self.object,
2024-07-30 11:37:08 +00:00
'snapshot': self.object.get_last_evidence(),
2024-07-18 15:21:22 +00:00
'lot_tags': lot_tags,
2024-07-15 14:23:14 +00:00
})
return context
2024-07-05 13:32:07 +00:00
2024-10-08 05:26:48 +00:00
class DeviceWebView(DashboardView, TemplateView):
template_name = "device_web.html"
2024-10-08 06:02:42 +00:00
title = _("Device Website")
2024-10-08 05:26:48 +00:00
breadcrumb = "Device / Web"
model = Annotation
def get(self, request, *args, **kwargs):
self.pk = kwargs['pk']
self.object = Device(id=self.pk)
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
self.object.initial()
lot_tags = LotTag.objects.filter(owner=self.request.user.institution)
context.update({
'object': self.object,
'snapshot': self.object.get_last_evidence(),
'lot_tags': lot_tags,
})
return context
2024-07-18 15:21:22 +00:00
class AddAnnotationView(DashboardView, CreateView):
2024-07-30 11:37:08 +00:00
template_name = "new_annotation.html"
2024-07-18 15:21:22 +00:00
title = _("New annotation")
breadcrumb = "Device / New annotation"
2024-07-05 13:32:07 +00:00
success_url = reverse_lazy('dashboard:unassigned_devices')
2024-07-18 15:21:22 +00:00
model = Annotation
fields = ("key", "value")
2024-07-05 13:32:07 +00:00
2024-07-18 15:21:22 +00:00
def form_valid(self, form):
2024-09-18 16:01:46 +00:00
form.instance.owner = self.request.user.institution
2024-07-30 11:37:08 +00:00
form.instance.uuid = self.annotation.uuid
2024-07-18 15:21:22 +00:00
form.instance.type = Annotation.Type.USER
response = super().form_valid(form)
return response
2024-07-08 10:59:32 +00:00
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
2024-09-18 16:01:46 +00:00
institution = self.request.user.institution
2024-07-30 11:37:08 +00:00
self.annotation = Annotation.objects.filter(
2024-09-18 16:01:46 +00:00
owner=institution, value=pk, type=Annotation.Type.SYSTEM
2024-07-30 11:37:08 +00:00
).first()
if not self.annotation:
2024-09-18 16:01:46 +00:00
get_object_or_404(Annotation, pk=0, owner=institution)
2024-07-18 15:21:22 +00:00
self.success_url = reverse_lazy('device:details', args=[pk])
2024-07-08 10:59:32 +00:00
kwargs = super().get_form_kwargs()
2024-07-05 13:32:07 +00:00
return kwargs
2024-07-18 15:21:22 +00:00
2024-07-30 11:37:08 +00:00
class AddDocumentView(DashboardView, CreateView):
template_name = "new_annotation.html"
title = _("New Document")
breadcrumb = "Device / New document"
success_url = reverse_lazy('dashboard:unassigned_devices')
model = Annotation
fields = ("key", "value")
def form_valid(self, form):
2024-09-18 16:01:46 +00:00
form.instance.owner = self.request.user.institution
2024-07-30 11:37:08 +00:00
form.instance.uuid = self.annotation.uuid
form.instance.type = Annotation.Type.DOCUMENT
response = super().form_valid(form)
return response
2024-06-12 07:32:49 +00:00
2024-07-30 11:37:08 +00:00
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
2024-09-18 16:01:46 +00:00
institution = self.request.user.institution
2024-07-30 11:37:08 +00:00
self.annotation = Annotation.objects.filter(
2024-09-18 16:01:46 +00:00
owner=institution, value=pk, type=Annotation.Type.SYSTEM
2024-07-30 11:37:08 +00:00
).first()
if not self.annotation:
2024-09-18 16:01:46 +00:00
get_object_or_404(Annotation, pk=0, owner=institution)
2024-07-30 11:37:08 +00:00
self.success_url = reverse_lazy('device:details', args=[pk])
kwargs = super().get_form_kwargs()
return kwargs