From 606bbcd0f0c1bf156eb07fa35d09fc2fbd88bb0b Mon Sep 17 00:00:00 2001 From: Vratika Date: Tue, 10 Dec 2024 10:40:30 +0530 Subject: [PATCH] update device view url --- Device/urls.py | 7 +++-- Device/views.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/Device/urls.py b/Device/urls.py index fac8add..b950fa4 100644 --- a/Device/urls.py +++ b/Device/urls.py @@ -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//', 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//', views.get_device_data, name='get_device_data'), + path('check-device//', CheckDeviceAssociation.as_view(), name='check_device_association'), +] \ No newline at end of file diff --git a/Device/views.py b/Device/views.py index f6cd4fd..33594d6 100644 --- a/Device/views.py +++ b/Device/views.py @@ -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 + ) \ No newline at end of file