update device view url

This commit is contained in:
Vratika 2024-12-10 10:40:30 +05:30
parent 93a65f691f
commit 606bbcd0f0
2 changed files with 88 additions and 2 deletions

View File

@ -1,6 +1,7 @@
from django.urls import path
from .import views
from .views import*
from .views import CheckDeviceAssociation
urlpatterns = [
@ -8,7 +9,9 @@ urlpatterns = [
path('edit_device/<str:device_unique_id>/', edit_device, name='edit_device'),
path('devices', device_list, name='devices'),
path('map_view', map_view, name='map_view'),
path('getLocation', views.getLocation, name='getLocation'),
path('getLocation', views.getLocation, name='getLocation'),#template dose not exsist
path('maps', maps, name='maps'),
path('update-device/', update_device, name='update_device'), # New route for updating device
]
path('device-data/<int:device_id>/', views.get_device_data, name='get_device_data'),
path('check-device/<int:device_id>/', CheckDeviceAssociation.as_view(), name='check_device_association'),
]

View File

@ -17,6 +17,20 @@ from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from Dashboard .models import DdosPrediction, Rensomware_TypePrediction, Rensomware_AuditPrediction
from malware .models import MalwarePredictionsDevice
import os
from django.conf import settings
from rest_framework.response import Response
from rest_framework.decorators import api_view
import pandas as pd
from rest_framework import status
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
@ -245,3 +259,72 @@ def update_device(request, device_id=None):
except json.JSONDecodeError:
return JsonResponse({"error": "Invalid JSON payload."}, status=400)
@api_view(['GET'])
def get_device_data(request, device_id):
# print('Entering get_device_data function')
# Retrieve DDoS, ransomware, and malware data for the given device
ddos_data = DdosPrediction.objects.filter(device_id=device_id).values('file_path', 'uploaded_at')
malware_data = MalwarePredictionsDevice.objects.filter(device_id=device_id).values('file_path', 'uploaded_at')
ransomware_type_data = Rensomware_TypePrediction.objects.filter(device_id=device_id).values('file_path', 'uploaded_at')
ransomware_audit_data = Rensomware_AuditPrediction.objects.filter(device_id=device_id).values('file_path', 'uploaded_at')
# print(f"Retrieved ddos_data: {list(ddos_data)}")
# print(f"Retrieved malware_data: {list(malware_data)}")
# print(f"Retrieved ransomware_type_data: {list(ransomware_type_data)}")
# print(f"Retrieved ransomware_audit_data: {list(ransomware_audit_data)}")
# Prepare the response data
response_data = {
'ddos_data': list(ddos_data),
'malware_data': list(malware_data), # Only includes file path and timestamp
'ransomware_type_data': list(ransomware_type_data),
'ransomware_audit_data': list(ransomware_audit_data)
}
# print("Prepared response data:")
# print(response_data)
# print("Exiting get_device_data function")
return Response(response_data)
class CheckDeviceAssociation(APIView):
"""
API to check if the given device is associated with the logged-in user.
"""
# Set the authentication and permission classes
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, device_id):
try:
# Get the logged-in user's profile
user_profile = UserProfile.objects.get(user=request.user)
# Check if the device is associated with the user's profile
device = Devices.objects.get(id=device_id, used_by=user_profile)
print(device)
return Response(
{"message": "Device is associated with the logged-in user."},
status=status.HTTP_200_OK
)
except UserProfile.DoesNotExist:
return Response(
{"error": "User profile not found."},
status=status.HTTP_404_NOT_FOUND
)
except Devices.DoesNotExist:
return Response(
{"error": "Device not found or not associated with the user."},
status=status.HTTP_404_NOT_FOUND
)