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

@ -5,8 +5,10 @@ from .models import Device
class DeviceFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr="icontains")
mac = django_filters.CharFilter(lookup_expr="icontains")
user = django_filters.CharFilter(field_name='user__last_name', lookup_expr="icontains")
user = django_filters.CharFilter(
field_name="user__last_name", lookup_expr="icontains"
)
class Meta:
model = Device
fields = "__all__"
fields = "__all__"

View File

@ -0,0 +1,20 @@
# Generated by Django 5.2 on 2025-04-25 08:42
import devices.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("devices", "0005_device_has_a_pending_payment"),
]
operations = [
migrations.AlterField(
model_name="device",
name="mac",
field=models.CharField(
max_length=255, validators=[devices.models.validate_mac_address]
),
),
]

View File

@ -1,11 +1,26 @@
from django.db import models
from django.utils import timezone
from api.models import User
import regex
from django.core.exceptions import ValidationError
def validate_mac_address(value):
if not regex.match(r"^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$", value):
raise ValidationError(
"This field accepts a valid MAC address in the format XX-XX-XX-XX-XX-XX using '-' as the separator."
)
return value
class Device(models.Model):
name = models.CharField(max_length=255)
mac = models.CharField(max_length=255)
mac = models.CharField(
max_length=255,
validators=[
validate_mac_address,
],
)
has_a_pending_payment = models.BooleanField(default=False)
reason_for_blocking = models.CharField(max_length=255, null=True, blank=True)
is_active = models.BooleanField(default=False)

View File

@ -7,14 +7,12 @@ from billing.models import Payment # Import the Payment model
class CreateDeviceSerializer(serializers.ModelSerializer):
name = serializers.CharField(required=True)
mac = serializers.CharField(required=True)
registered = serializers.BooleanField(required=True)
class Meta:
model = Device
fields = [
"name",
"mac",
"registered",
"blocked_by",
]
depth = 2

View File

@ -52,6 +52,11 @@ class DeviceListCreateAPIView(
return Response(
{"message": "Device with this mac address already exists."}, status=400
)
# Normalize MAC address to use "-" as separators
mac = re.sub(r"[^0-9A-Fa-f]", "-", mac).upper()
request.data["mac"] = mac
return super().create(request, *args, **kwargs)
def perform_create(self, serializer):