From e3afa4b2ea23c4d789b5eae86bc528c3dccfc05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Fri, 10 Nov 2023 19:00:09 +0100 Subject: [PATCH] validate mesh node address --- .../0011_meshnode_address_validate.py | 30 +++++++++++++++++++ src/c3nav/mesh/models.py | 9 +++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/c3nav/mesh/migrations/0011_meshnode_address_validate.py diff --git a/src/c3nav/mesh/migrations/0011_meshnode_address_validate.py b/src/c3nav/mesh/migrations/0011_meshnode_address_validate.py new file mode 100644 index 00000000..bc110441 --- /dev/null +++ b/src/c3nav/mesh/migrations/0011_meshnode_address_validate.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.1 on 2023-11-10 17:53 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("mesh", "0010_otaupdate_otaupdaterecipient"), + ] + + operations = [ + migrations.AlterField( + model_name="meshnode", + name="address", + field=models.CharField( + max_length=17, + primary_key=True, + serialize=False, + validators=[ + django.core.validators.RegexValidator( + code="invalid_macaddress", + message="Must be a lower-case mac address", + regex="^([a-f0-9]{2}:){5}[a-f0-9]{2}$", + ) + ], + verbose_name="mac address", + ), + ), + ] diff --git a/src/c3nav/mesh/models.py b/src/c3nav/mesh/models.py index 757ca3b7..b6014d56 100644 --- a/src/c3nav/mesh/models.py +++ b/src/c3nav/mesh/models.py @@ -7,6 +7,7 @@ from operator import attrgetter from typing import Any, Mapping, Optional, Self from django.contrib.auth import get_user_model +from django.core.validators import RegexValidator from django.db import NotSupportedError, models from django.db.models import Q, UniqueConstraint from django.utils import timezone @@ -199,7 +200,13 @@ class MeshNode(models.Model): """ A nesh node. Any node. """ - address = models.CharField(_('mac address'), max_length=17, primary_key=True) + address = models.CharField(_('mac address'), max_length=17, primary_key=True, + validators=[RegexValidator( + regex='^([a-f0-9]{2}:){5}[a-f0-9]{2}$', + message='Must be a lower-case mac address', + code='invalid_macaddress' + )]) + name = models.CharField(_('name'), max_length=32, null=True, blank=True) first_seen = models.DateTimeField(_('first seen'), auto_now_add=True) uplink = models.ForeignKey('MeshUplink', models.PROTECT, null=True,