mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-27 15:53:56 +00:00
Refactor device blocking logic in DeviceBlockAPIView to utilize Omada BlockDeviceResponse for blocking in omada first
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 3m42s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 3m42s
This commit is contained in:
30
api/omada.py
30
api/omada.py
@ -1,5 +1,12 @@
|
|||||||
import requests
|
import requests
|
||||||
from apibase.env import env
|
from apibase.env import env
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlockDeviceResponse:
|
||||||
|
errorCode: int
|
||||||
|
msg: str
|
||||||
|
|
||||||
|
|
||||||
class Omada:
|
class Omada:
|
||||||
@ -92,7 +99,9 @@ class Omada:
|
|||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(f"Error adding devices to Omada: {e}")
|
print(f"Error adding devices to Omada: {e}")
|
||||||
|
|
||||||
def block_device(self, mac_address: str, operation: str = "block"):
|
def block_device(
|
||||||
|
self, mac_address: str, operation: str = "block"
|
||||||
|
) -> BlockDeviceResponse:
|
||||||
"""
|
"""
|
||||||
Block a device in Omada.
|
Block a device in Omada.
|
||||||
:param mac_address: The MAC address of the device to block.
|
:param mac_address: The MAC address of the device to block.
|
||||||
@ -103,10 +112,23 @@ class Omada:
|
|||||||
url,
|
url,
|
||||||
headers={"X-API-Key": str(self.api_key)},
|
headers={"X-API-Key": str(self.api_key)},
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
data = response.json()
|
||||||
data = response.json()
|
print("Response Data: ", data)
|
||||||
return data
|
if data["errorCode"] == 0:
|
||||||
|
return BlockDeviceResponse(
|
||||||
|
errorCode=data["errorCode"],
|
||||||
|
msg=data["msg"]
|
||||||
|
if "msg" in data
|
||||||
|
else "Device blocked successfully.",
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(f"Failed to block device: {response.text}")
|
print(f"Failed to block device: {response.text}")
|
||||||
|
return BlockDeviceResponse(
|
||||||
|
errorCode=data["errorCode"],
|
||||||
|
msg=data["msg"] if "msg" in data else "Failed to block device.",
|
||||||
|
)
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(f"Error blocking device in Omada: {e}")
|
print(f"Error blocking device in Omada: {e}")
|
||||||
|
return BlockDeviceResponse(
|
||||||
|
errorCode=-1, msg=f"Error blocking device in Omada: {e}"
|
||||||
|
)
|
||||||
|
@ -127,16 +127,22 @@ class DeviceBlockAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
return Response({"message": "Blocked field is required."}, status=400)
|
return Response({"message": "Blocked field is required."}, status=400)
|
||||||
if not isinstance(blocked, bool):
|
if not isinstance(blocked, bool):
|
||||||
return Response({"message": "Blocked field must be a boolean."}, status=400)
|
return Response({"message": "Blocked field must be a boolean."}, status=400)
|
||||||
instance.blocked = blocked
|
|
||||||
omada_client = Omada()
|
omada_client = Omada()
|
||||||
omada_client.block_device(
|
blocked = omada_client.block_device(
|
||||||
instance.mac, operation="block" if blocked else "unblock"
|
instance.mac, operation="block" if blocked else "unblock"
|
||||||
)
|
)
|
||||||
instance.save()
|
if blocked.errorCode == 0:
|
||||||
serializer = self.get_serializer(instance, data=request.data, partial=False)
|
instance.blocked = blocked
|
||||||
serializer.is_valid(raise_exception=True)
|
instance.save()
|
||||||
self.perform_update(serializer)
|
serializer = self.get_serializer(instance, data=request.data, partial=False)
|
||||||
return Response(serializer.data)
|
serializer.is_valid(raise_exception=True)
|
||||||
|
self.perform_update(serializer)
|
||||||
|
return Response(serializer.data)
|
||||||
|
else:
|
||||||
|
return Response(
|
||||||
|
{"message": blocked.msg},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DeviceDestroyAPIView(StaffEditorPermissionMixin, generics.DestroyAPIView):
|
class DeviceDestroyAPIView(StaffEditorPermissionMixin, generics.DestroyAPIView):
|
||||||
|
Reference in New Issue
Block a user