mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-27 22:03:58 +00:00
Refactor and enhance device management and authentication features
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 4m12s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 4m12s
- Updated the `reverse_dhivehi_string` function to correct the range for combining characters. - Added new device handling in the health check view and integrated the `add_new_devices_to_omada` task. - Improved date handling in `CreateTemporaryUserView` to ensure proper string conversion. - Enhanced OTP sending by converting mobile numbers to strings. - Implemented MAC address validation in the `Device` model using a custom validator. - Removed unnecessary fields from the `CreateDeviceSerializer`. - Normalized MAC address format in the `DeviceListCreateAPIView`. - Updated the `djangopasswordlessknox` package to improve code consistency and readability. - Added migration to enforce MAC address validation in the database.
This commit is contained in:
96
api/tasks.py
96
api/tasks.py
@ -14,6 +14,27 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
env.read_env(os.path.join(BASE_DIR, ".env"))
|
||||
PERSON_VERIFY_BASE_URL = env.str("PERSON_VERIFY_BASE_URL")
|
||||
OMADA_PROXY_API_KEY = env.str("OMADA_PROXY_API_KEY")
|
||||
OMADA_PROXY_URL = env("OMADA_PROXY_URL")
|
||||
OMADA_SITE_ID = env(
|
||||
"OMADA_SITE_ID",
|
||||
)
|
||||
OMADA_GROUP_ID = env(
|
||||
"OMADA_GROUP_ID",
|
||||
)
|
||||
|
||||
if not OMADA_SITE_ID:
|
||||
raise ValueError(
|
||||
"OMADA_SITE_ID is not set. Please set it in your environment variables."
|
||||
)
|
||||
if not OMADA_GROUP_ID:
|
||||
raise ValueError(
|
||||
"OMADA_GROUP_ID is not set. Please set it in your environment variables."
|
||||
)
|
||||
if not OMADA_PROXY_URL:
|
||||
raise ValueError(
|
||||
"OMADA_PROXY_URL is not set. Please set it in your environment variables."
|
||||
)
|
||||
|
||||
|
||||
@shared_task
|
||||
@ -54,6 +75,81 @@ def deactivate_expired_devices():
|
||||
}
|
||||
|
||||
|
||||
def get_existing_omada_devices():
|
||||
"""
|
||||
Get existing Omada devices from the database.
|
||||
:return: List of existing device names.
|
||||
"""
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{OMADA_PROXY_URL}/9fd0cffa3475a74ae4e4d37de0d12414/api/v2/sites/66dcddb804aa0d2978cf145f/setting/profiles/groups",
|
||||
headers={"X-API-Key": str(OMADA_PROXY_API_KEY)},
|
||||
)
|
||||
print("Response: ", response.status_code)
|
||||
data = response.json()
|
||||
existing_devices = []
|
||||
if "result" in data and len(data["result"]["data"]) > 0:
|
||||
last_entry = data["result"]["data"][-1]
|
||||
print("Last Entry: ", last_entry)
|
||||
if "macAddressList" in last_entry:
|
||||
existing_devices = last_entry["macAddressList"]
|
||||
print(existing_devices)
|
||||
return existing_devices
|
||||
except requests.RequestException as e:
|
||||
print(f"Error fetching existing devices: {e}")
|
||||
return []
|
||||
|
||||
|
||||
@shared_task
|
||||
def add_new_devices_to_omada(new_devices: list[dict]):
|
||||
"""
|
||||
Add new devices to Omada.
|
||||
:param new_devices: List of new device names to add.
|
||||
"""
|
||||
try:
|
||||
PAYLOAD = {
|
||||
"name": "REGISTERED_DEVICES",
|
||||
"type": 2,
|
||||
"resource": 0,
|
||||
"ipList": None,
|
||||
"ipv6List": None,
|
||||
"macAddressList": None,
|
||||
"portList": None,
|
||||
"countryList": None,
|
||||
"portType": None,
|
||||
"portMaskList": None,
|
||||
"domainNamePort": None,
|
||||
}
|
||||
existing_devices = get_existing_omada_devices()
|
||||
PAYLOAD["macAddressList"] = existing_devices
|
||||
print("Payload with existing devices: ", PAYLOAD)
|
||||
for device in new_devices:
|
||||
print("Device in loop: ", device)
|
||||
PAYLOAD["macAddressList"].append(
|
||||
{
|
||||
"macAddress": device["mac"],
|
||||
"name": device["name"],
|
||||
}
|
||||
)
|
||||
print("New Payload: ", PAYLOAD)
|
||||
print(
|
||||
f"{OMADA_PROXY_URL}/9fd0cffa3475a74ae4e4d37de0d12414/api/v2/sites/{OMADA_SITE_ID}/setting/profiles/groups/2/{OMADA_GROUP_ID}"
|
||||
)
|
||||
response = requests.patch(
|
||||
f"{OMADA_PROXY_URL}/9fd0cffa3475a74ae4e4d37de0d12414/api/v2/sites/{OMADA_SITE_ID}/setting/profiles/groups/2/{OMADA_GROUP_ID}",
|
||||
headers={"X-API-Key": str(OMADA_PROXY_API_KEY)},
|
||||
json=PAYLOAD,
|
||||
)
|
||||
print("Response: ", response.status_code)
|
||||
if response.status_code == 200:
|
||||
print("Devices successfully added.")
|
||||
print(response.json())
|
||||
else:
|
||||
print(f"Failed to add devices: {response.text}")
|
||||
except requests.RequestException as e:
|
||||
print(f"Error adding devices: {e}")
|
||||
|
||||
|
||||
def verify_user_with_person_api_task(user_id: int):
|
||||
"""
|
||||
Verify the user with the Person API.
|
||||
|
Reference in New Issue
Block a user