2024-07-15 14:23:14 +00:00
import json
2024-11-16 18:26:45 +00:00
import logging
2024-10-22 06:20:26 +00:00
from django . http import JsonResponse
2024-07-15 14:23:14 +00:00
2024-10-10 09:44:23 +00:00
from django . http import Http404
2024-07-05 13:32:07 +00:00
from django . urls import reverse_lazy
2024-11-15 21:16:45 +00:00
from django . contrib import messages
from django . shortcuts import get_object_or_404 , redirect , Http404
2024-07-05 13:32:07 +00:00
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-11-15 21:16:45 +00:00
DeleteView ,
2024-07-05 13:32:07 +00:00
)
2024-07-15 14:23:14 +00:00
from django . views . generic . base import TemplateView
2024-12-06 20:48:06 +00:00
from action . models import StateDefinition , State
2024-10-04 15:32:53 +00:00
from dashboard . mixins import DashboardView , Http403
2024-12-10 19:53:07 +00:00
from evidence . models import UserProperty , SystemProperty
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-11-16 18:26:45 +00:00
device_logger = logging . getLogger ( ' device_log ' )
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-09-26 01:32:26 +00:00
success_url = reverse_lazy ( ' dashboard:unassigned_devices ' )
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-11-12 17:04:59 +00:00
model = SystemProperty
2024-07-08 10:59:32 +00:00
def get_form_kwargs ( self ) :
pk = self . kwargs . get ( ' pk ' )
2024-10-04 15:32:53 +00:00
self . object = get_object_or_404 (
self . model ,
pk = pk ,
owner = self . request . user . institution
)
2024-07-08 10:59:32 +00:00
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-11-12 17:04:59 +00:00
model = SystemProperty
2024-07-19 15:40:01 +00:00
def get ( self , request , * args , * * kwargs ) :
self . pk = kwargs [ ' pk ' ]
self . object = Device ( id = self . pk )
2024-09-25 17:04:29 +00:00
if not self . object . last_evidence :
raise Http404
2024-10-04 15:32:53 +00:00
if self . object . owner != self . request . user . institution :
raise Http403
2024-10-22 05:35:47 +00:00
2024-07-19 15:40:01 +00:00
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-10-29 07:23:43 +00:00
self . object . initial ( )
2024-09-18 16:01:46 +00:00
lot_tags = LotTag . objects . filter ( owner = self . request . user . institution )
2024-12-06 20:48:06 +00:00
last_evidence = self . object . get_last_evidence ( ) ,
uuid = self . object . last_uuid ( )
2024-07-15 14:23:14 +00:00
context . update ( {
2024-07-19 15:40:01 +00:00
' object ' : self . object ,
2024-12-06 20:48:06 +00:00
' snapshot ' : last_evidence ,
2024-07-18 15:21:22 +00:00
' lot_tags ' : lot_tags ,
2024-12-05 21:31:30 +00:00
" state_definitions " : StateDefinition . objects . filter ( institution = self . request . user . institution ) . order_by ( ' order ' ) ,
2024-12-06 22:19:49 +00:00
" device_states " : State . objects . filter ( snapshot_uuid = uuid ) . order_by ( ' -date ' ) ,
2024-07-15 14:23:14 +00:00
} )
return context
2024-07-05 13:32:07 +00:00
2024-10-22 07:18:59 +00:00
class PublicDeviceWebView ( TemplateView ) :
2024-10-08 05:26:48 +00:00
template_name = " device_web.html "
def get ( self , request , * args , * * kwargs ) :
self . pk = kwargs [ ' pk ' ]
self . object = Device ( id = self . pk )
2024-11-05 09:19:24 +00:00
2024-10-30 15:00:23 +00:00
if not self . object . last_evidence :
raise Http404
2024-11-05 09:19:24 +00:00
2024-10-22 06:20:26 +00:00
if self . request . headers . get ( ' Accept ' ) == ' application/json ' :
return self . get_json_response ( )
2024-10-08 05:26:48 +00:00
return super ( ) . get ( request , * args , * * kwargs )
def get_context_data ( self , * * kwargs ) :
context = super ( ) . get_context_data ( * * kwargs )
self . object . initial ( )
context . update ( {
2024-10-23 06:47:40 +00:00
' object ' : self . object
2024-10-08 05:26:48 +00:00
} )
return context
2024-11-05 09:19:24 +00:00
@property
def public_fields ( self ) :
return {
2024-10-23 06:47:40 +00:00
' id ' : self . object . id ,
' shortid ' : self . object . shortid ,
' uuids ' : self . object . uuids ,
' hids ' : self . object . hids ,
2024-11-06 16:21:48 +00:00
' components ' : self . remove_serial_number_from ( self . object . components ) ,
2024-10-22 06:20:26 +00:00
}
2024-11-05 09:19:24 +00:00
@property
def authenticated_fields ( self ) :
return {
2024-11-06 07:24:10 +00:00
' serial_number ' : self . object . serial_number ,
2024-11-05 09:19:24 +00:00
' components ' : self . object . components ,
}
2024-11-06 16:21:48 +00:00
def remove_serial_number_from ( self , components ) :
2024-11-06 07:24:10 +00:00
for component in components :
if ' serial_number ' in component :
del component [ ' SerialNumber ' ]
return components
2024-11-05 09:19:24 +00:00
def get_device_data ( self ) :
data = self . public_fields
if self . request . user . is_authenticated :
data . update ( self . authenticated_fields )
return data
def get_json_response ( self ) :
device_data = self . get_device_data ( )
return JsonResponse ( device_data )
2024-10-22 06:20:26 +00:00
2024-10-08 05:26:48 +00:00
2024-11-12 14:47:32 +00:00
class AddUserPropertyView ( DashboardView , CreateView ) :
template_name = " new_user_property.html "
title = _ ( " New User Property " )
breadcrumb = " Device / New Property "
2024-07-05 13:32:07 +00:00
success_url = reverse_lazy ( ' dashboard:unassigned_devices ' )
2024-11-12 17:04:59 +00:00
model = UserProperty
2024-07-18 15:21:22 +00:00
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-10-04 15:32:53 +00:00
form . instance . user = self . request . user
2024-11-15 23:13:39 +00:00
form . instance . uuid = self . property . uuid
2024-12-10 19:53:07 +00:00
form . instance . type = UserProperty . Type . USER
2024-11-16 18:26:45 +00:00
messages . success ( self . request , _ ( " User property successfully added. " ) )
2024-12-09 21:38:09 +00:00
device_logger . info (
f " Created user property (key= ' { form . instance . key } ' , value= ' { form . instance . value } ' ) by user { self . request . user } , for evidence uuid: { self . property . uuid } . "
)
2024-07-18 15:21:22 +00:00
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-11-16 18:26:45 +00:00
self . property = SystemProperty . objects . filter (
2024-10-10 09:44:23 +00:00
owner = institution ,
2024-10-04 15:32:53 +00:00
value = pk ,
2024-07-30 11:37:08 +00:00
) . first ( )
2024-10-22 05:35:47 +00:00
2024-11-15 23:13:39 +00:00
if not self . property :
2024-10-10 09:44:23 +00:00
raise Http404
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-11-15 22:11:05 +00:00
class UpdateUserPropertyView ( DashboardView , UpdateView ) :
template_name = " new_user_property.html "
title = _ ( " Update User Property " )
breadcrumb = " Device / Update Property "
model = UserProperty
fields = ( " key " , " value " )
2024-11-15 22:38:33 +00:00
def get_form_kwargs ( self ) :
pk = self . kwargs . get ( ' pk ' )
user_property = get_object_or_404 ( UserProperty , pk = pk , owner = self . request . user . institution )
2024-11-15 23:13:39 +00:00
if not user_property :
2024-11-15 22:38:33 +00:00
raise Http404
kwargs = super ( ) . get_form_kwargs ( )
kwargs [ ' instance ' ] = user_property
return kwargs
2024-11-15 22:11:05 +00:00
def form_valid ( self , form ) :
2024-11-16 18:26:45 +00:00
old_key = self . object . key
old_value = self . object . value
new_key = form . cleaned_data [ ' key ' ]
new_value = form . cleaned_data [ ' value ' ]
form . instance . owner = self . request . user . institution
2024-11-15 22:11:05 +00:00
form . instance . user = self . request . user
2024-12-10 19:53:07 +00:00
form . instance . type = UserProperty . Type . USER
2024-11-15 22:11:05 +00:00
response = super ( ) . form_valid ( form )
messages . success ( self . request , _ ( " User property updated successfully. " ) )
2024-11-16 18:26:45 +00:00
device_logger . info (
f " Updated property from (key= ' { old_key } ' , value= ' { old_value } ' ) to (key= ' { new_key } ' , value= ' { new_value } ' ) by user { self . request . user } . "
)
2024-11-15 22:11:05 +00:00
return response
def get_success_url ( self ) :
2024-11-16 18:26:45 +00:00
return self . request . META . get ( ' HTTP_REFERER ' , reverse_lazy ( ' device:details ' , args = [ self . object . pk ] ) )
2024-11-15 22:11:05 +00:00
2024-11-15 21:16:45 +00:00
class DeleteUserPropertyView ( DashboardView , DeleteView ) :
model = UserProperty
def post ( self , request , * args , * * kwargs ) :
self . pk = kwargs [ ' pk ' ]
referer = request . META . get ( ' HTTP_REFERER ' )
if not referer :
raise Http404 ( " No referer header found " )
self . object = get_object_or_404 (
self . model ,
pk = self . pk ,
owner = self . request . user . institution
)
2024-11-16 18:26:45 +00:00
old_value = self . object . key
2024-11-15 21:16:45 +00:00
self . object . delete ( )
2024-11-16 18:26:45 +00:00
device_logger . info ( f " Deleted property with key ' { old_value } ' by user { self . request . user } . " )
2024-11-15 21:16:45 +00:00
messages . success ( self . request , _ ( " User property deleted successfully. " ) )
# Redirect back to the original URL
return redirect ( referer )
2024-07-18 15:21:22 +00:00
2024-07-30 11:37:08 +00:00
class AddDocumentView ( DashboardView , CreateView ) :
2024-11-15 23:13:39 +00:00
template_name = " new_user_property.html "
2024-07-30 11:37:08 +00:00
title = _ ( " New Document " )
breadcrumb = " Device / New document "
success_url = reverse_lazy ( ' dashboard:unassigned_devices ' )
2024-12-10 19:53:07 +00:00
model = UserProperty
2024-07-30 11:37:08 +00:00
fields = ( " key " , " value " )
def form_valid ( self , form ) :
2024-09-18 16:01:46 +00:00
form . instance . owner = self . request . user . institution
2024-10-04 15:32:53 +00:00
form . instance . user = self . request . user
2024-11-15 23:13:39 +00:00
form . instance . uuid = self . property . uuid
2024-12-10 19:53:07 +00:00
form . instance . type = UserProperty . Type . DOCUMENT
2024-07-30 11:37:08 +00:00
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-11-15 23:13:39 +00:00
self . property = SystemProperty . objects . filter (
2024-10-10 09:44:23 +00:00
owner = institution ,
value = pk ,
2024-07-30 11:37:08 +00:00
) . first ( )
2024-10-10 09:44:23 +00:00
2024-11-15 23:13:39 +00:00
if not self . property :
2024-10-10 09:44:23 +00:00
raise Http404
2024-07-30 11:37:08 +00:00
self . success_url = reverse_lazy ( ' device:details ' , args = [ pk ] )
kwargs = super ( ) . get_form_kwargs ( )
return kwargs