Commit 7de3e400 authored by Toralf Neby berge's avatar Toralf Neby berge
Browse files

Add check if macaddr is unique in the selected subnet

parent 54180d6b
from django import forms
from django.core.exceptions import ValidationError
from django.forms import ModelChoiceField
from mdb.models import Interface, Ip4Address
......@@ -15,6 +16,16 @@ class Ip4ModelChoiceField(ModelChoiceField):
class InterfaceForm(forms.ModelForm):
model = Interface
def clean(self):
macaddr = self.cleaned_data['macaddr']
ip4address = self.cleaned_data['ip4address']
if Interface.objects.filter(macaddr=macaddr).exists():
for interface in Interface.objects.filter(macaddr=macaddr):
subnet = interface.ip4address.subnet
if subnet == ip4address.subnet and interface != self.instance:
raise ValidationError('Macaddr already in this subnet')
def __init__(self, *args, **kwargs):
super(InterfaceForm, self).__init__(*args, **kwargs)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment