Refactor imports in admin.py and update MAC address handling in views.py to improve code clarity and error handling
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 3m31s

This commit is contained in:
2025-06-08 15:23:18 +05:00
parent 1b58ebd4db
commit 7e2f6699b1
2 changed files with 6 additions and 6 deletions

View File

@ -1,4 +1,5 @@
from attr import dataclass
from dataclasses import dataclass
from xmlrpc.client import Boolean
from rest_framework import generics, status
from rest_framework.response import Response
from django_filters.rest_framework import DjangoFilterBackend
@ -56,7 +57,7 @@ class DeviceListCreateAPIView(
{"message": "Device with this mac address already exists."}, status=400
)
mac_details = get_mac_address_details(mac)
if mac_details.vendor == "Unknown":
if not mac_details.ok:
return Response({"message": "MAC address vendor not found."}, status=400)
mac = re.sub(NORMALIZE_MAC_REGEX, "-", mac).upper()
@ -154,6 +155,7 @@ class MacResponse:
mac_address: str
vendor: str
detail: str | None = None
ok: Boolean = True
def get_mac_address_details(mac: str) -> MacResponse:
@ -167,6 +169,7 @@ def get_mac_address_details(mac: str) -> MacResponse:
mac_address=json_data.get("mac_address", mac),
vendor=json_data.get("vendor", ""),
detail=json_data.get("detail"),
ok=json_data.get("ok", True),
)
else:
return MacResponse(mac_address=mac, vendor="Unknown", detail=None)
return MacResponse(mac_address=mac, vendor="Unknown", detail=None, ok=False)