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

- 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:
2025-04-25 14:37:27 +05:00
parent 0f19f0c15c
commit 83db42cc60
24 changed files with 475 additions and 209 deletions

View File

@ -27,6 +27,8 @@ class IsStaffEditorPermission(permissions.DjangoModelPermissions):
# Check permissions based on the request method
perms = self.perms_map.get(request.method, [])
perms = [perm % {'app_label': app_label, 'model_name': model_name} for perm in perms]
perms = [
perm % {"app_label": app_label, "model_name": model_name} for perm in perms
]
return request.user.has_perms(perms)

View File

@ -13,7 +13,7 @@ if not api_url or not api_key:
)
def send_otp(mobile: str, otp: int, message: str):
def send_otp(mobile: str, otp: str, message: str):
if not api_url or not api_key:
logger.debug("Failed to send SMS. Missing SMS_API_URL or SMS_API_KEY.")
return False

View File

@ -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.

View File

@ -18,7 +18,7 @@ def reverse_dhivehi_string(input_str):
i = 0
while i < len(reversed_str):
# Check if current character is a combining character
if i + 1 < len(reversed_str) and "\u0300" <= reversed_str[i + 1] <= "\u036F":
if i + 1 < len(reversed_str) and "\u0300" <= reversed_str[i + 1] <= "\u036f":
# If next character is a combining mark, add it before the base character
corrected_chars.append(reversed_str[i + 1] + reversed_str[i])
i += 2

View File

@ -1,4 +1,5 @@
# django imports
import pprint
from django.contrib.auth import login
# rest_framework imports
@ -31,7 +32,8 @@ from typing import cast, Dict, Any
from django.core.mail import send_mail
from django.db.models import Q
from api.sms import send_otp
from .tasks import add, deactivate_expired_devices
from .tasks import add, add_new_devices_to_omada
from devices.models import Device
# local apps import
from .serializers import (
@ -60,7 +62,8 @@ class ErrorMessages:
@api_view(["GET"])
def healthcheck(request):
add.delay(1, 2)
deactivate_expired_devices.delay()
# devices = Device.objects.filter(is_active=False).values()
# add_new_devices_to_omada.delay(new_devices=list(devices))
return Response({"status": "Good"}, status=status.HTTP_200_OK)
@ -117,7 +120,7 @@ class CreateTemporaryUserView(generics.CreateAPIView):
current_date = timezone.now()
try:
dob = timezone.datetime.strptime(dob, "%Y-%m-%d").date()
dob = timezone.datetime.strptime(str(dob), "%Y-%m-%d").date()
except ValueError:
return Response(
{"message": "Invalid date format for DOB. Use YYYY-MM-DD."}, status=400
@ -193,7 +196,7 @@ class CreateTemporaryUserView(generics.CreateAPIView):
formatted_time = otp_expiry.strftime("%d/%m/%Y %H:%M:%S")
otp = temp_user.generate_otp()
send_otp(
temp_user.t_mobile,
str(temp_user.t_mobile),
otp,
f"Your Registration SARLink OTP: {otp}. \nExpires at {formatted_time}. \n\n- SAR Link",
)
@ -263,7 +266,7 @@ class VerifyOTPView(generics.GenericAPIView):
User.objects.create_user(
first_name=temp_user.t_first_name,
last_name=temp_user.t_last_name,
username=temp_user.t_username,
username=str(temp_user.t_username),
password="",
address=temp_user.t_address,
mobile=temp_user.t_mobile,