diff --git a/Device/urls.py b/Device/urls.py index b950fa4..9c3ce1f 100644 --- a/Device/urls.py +++ b/Device/urls.py @@ -13,5 +13,5 @@ urlpatterns = [ 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'), + 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 33594d6..49083f7 100644 --- a/Device/views.py +++ b/Device/views.py @@ -298,33 +298,61 @@ def get_device_data(request, device_id): class CheckDeviceAssociation(APIView): """ - API to check if the given device is associated with the logged-in user. + API to check if the given device is associated with the user based on + user ID, device MAC address, and unique ID, and return the required responses. """ - # Set the authentication and permission classes - authentication_classes = [TokenAuthentication] - permission_classes = [IsAuthenticated] + # Remove authentication and permission classes + authentication_classes = [] + permission_classes = [] - def get(self, request, device_id): + def post(self, request): try: - # Get the logged-in user's profile - user_profile = UserProfile.objects.get(user=request.user) + # Extract the required parameters from the request body + user_id = request.data.get("user_id") + mac_address = request.data.get("mac_address") + unique_id = request.data.get("unique_id") - # Check if the device is associated with the user's profile - device = Devices.objects.get(id=device_id, used_by=user_profile) - print(device) + # Validate the presence of required parameters + if not user_id or not mac_address or not unique_id: + return Response( + {"error": "user_id, mac_address, and unique_id are required."}, + status=status.HTTP_400_BAD_REQUEST + ) + # Check if the user exists + try: + user_profile = UserProfile.objects.get(user_id=user_id) + print(user_profile) + except UserProfile.DoesNotExist: + # User does not exist + return Response( + {"info": "User does not exist. Proceeding to send OTP."}, + status=status.HTTP_404_NOT_FOUND + ) + + # Check if the device exists and is associated with the user + try: + device = Devices.objects.get( + used_by=user_profile, + mac_address=mac_address, + unique_id=unique_id + ) + print(device) + # Device is already registered + return Response( + {"message": "Device already registered."}, + status=status.HTTP_200_OK + ) + except Devices.DoesNotExist: + # User exists but device is not registered + return Response( + {"message": "User exists but device is not registered."}, + status=status.HTTP_201_CREATED + ) + + except Exception as e: 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 + {"error": f"An unexpected error occurred: {str(e)}"}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR ) \ No newline at end of file