Validate MAC addresses
This commit is contained in:
1 parent
6454d05c32
commit
d50b5b5bce
2 files changed
+13
-3
No files matched your search
+2
-2
@@ -2,7 +2,7 @@ from django.db import models
|
||||
from django.db.models.signals import post_save, pre_save, pre_delete
|
||||
from django.dispatch import receiver
|
||||
|
||||
from validators import validate_hostname
|
||||
from validators import validate_hostname, validate_macaddr
|
||||
|
||||
import ipaddr
|
||||
import datetime
|
||||
@@ -556,7 +556,7 @@ class Host(models.Model):
|
||||
|
||||
class Interface(models.Model):
|
||||
name = models.CharField(max_length=128)
|
||||
macaddr = models.CharField(max_length=17)
|
||||
macaddr = models.CharField(max_length=17, validators=[validate_macaddr])
|
||||
pxe_filename = models.CharField(max_length=64, blank=True)
|
||||
dhcp_client = models.BooleanField(default=False)
|
||||
host = models.ForeignKey(Host)
|
||||
|
||||
+11
-1
@@ -1,5 +1,15 @@
|
||||
from django.core.validators import RegexValidator
|
||||
import re
|
||||
|
||||
# http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
||||
hostname_re = re.compile(r'^(?!-)[-a-z0-9]+(?<!-)$', re.IGNORECASE)
|
||||
validate_hostname = RegexValidator(hostname_re, u'Enter a valid hostname', 'invalid')
|
||||
validate_hostname = RegexValidator(
|
||||
regex=hostname_re,
|
||||
message=u'Enter a valid hostname',
|
||||
code='invalid')
|
||||
|
||||
macaddr_re = re.compile(r'^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$', re.IGNORECASE)
|
||||
validate_macaddr = RegexValidator(
|
||||
regex=macaddr_re,
|
||||
message=u'Enter a valid MAC address',
|
||||
code='invalid')
|
||||
Reference in new issue
Block a user