@ -2,8 +2,8 @@ from django import forms
from django . db . models import Case , When , IntegerField
from django . db . models import Case , When , IntegerField
from . models import Maintainer , Contact , InetNum , ASBlock , ASNumber
from . models import Maintainer , Contact , InetNum , ASBlock , ASNumber
from . validators import HandleValidatorWithSuffix , IP46CIDRValidator
import re
import ipaddress
import ipaddress
@ -15,31 +15,36 @@ class WhoisObjectFormMixin(object):
instance = getattr ( self , ' instance ' , None )
instance = getattr ( self , ' instance ' , None )
if instance and instance . pk :
if instance and instance . pk :
self . _create = False
self . _create = False
self . fields [ ' handle ' ] . widget . attrs [ ' readonly ' ] = True
#self.fields['handle'].disabled = True
else :
else :
self . _create = True
self . _create = True
self . fields [ ' handle ' ] . help_text = " Handle for this object in uppercase with a suffix of - %s " % instance . handleSuffix
# only show users contacts and already present contacts
# only show users contacts and already present contacts
mnts = self . _user . maintainer_set . all ( )
mnts = self . _user . maintainer_set . all ( )
if ' admin_c ' in self . fields :
if ' admin_c ' in self . fields :
self . fields [ ' admin_c ' ] . queryset = Contact . getMntQueryset ( mnts , self . instance , " admin_c " )
self . fields [ ' admin_c ' ] . queryset = Contact . getMntQueryset ( mnts , self . instance , " admin_c " )
def clean_handle ( self ) :
def clean_handle ( self ) :
if not self . _create :
HandleValidatorWithSuffix ( self . instance . handleSuffix ) ( self . cleaned_data [ ' handle ' ] )
return self . instance . handle
return self . cleaned_data [ ' handle ' ]
else :
return self . cleaned_data [ ' handle ' ]
def clean ( self ) :
def clean ( self ) :
cleaned_data = super ( WhoisObjectFormMixin , self ) . clean ( )
cleaned_data = super ( WhoisObjectFormMixin , self ) . clean ( )
if cleaned_data . get ( " handle " ) == " AUTO " and not self . errors :
if cleaned_data . get ( " handle " ) == " AUTO " and not self . errors :
cleaned_data [ ' handle ' ] = self . _meta . model . genGenericHandle ( cleaned_data . get ( " name " ) )
name = cleaned_data . get ( " name " )
if name is None :
name = self . _user . username
cleaned_data [ ' handle ' ] = self . _meta . model . genGenericHandle ( name )
return cleaned_data
return cleaned_data
class MntFormMixin ( object ) :
class MntFormMixin ( object ) :
protectedFields = [ ]
protectedFields = [ ]
def __init__ ( self , lower = False , * args , * * kwargs ) :
def __init__ ( self , lower = False , * args , * * kwargs ) :
super ( MntFormMixin , self ) . __init__ ( * args , * * kwargs )
super ( MntFormMixin , self ) . __init__ ( * args , * * kwargs )
@ -60,6 +65,7 @@ class MntFormMixin(object):
if " mnt_lower " in self . fields :
if " mnt_lower " in self . fields :
self . fields [ " mnt_lower " ] . queryset = mntQs
self . fields [ " mnt_lower " ] . queryset = mntQs
class MntForm ( WhoisObjectFormMixin , forms . ModelForm ) :
class MntForm ( WhoisObjectFormMixin , forms . ModelForm ) :
class Meta :
class Meta :
model = Maintainer
model = Maintainer
@ -80,7 +86,8 @@ class ContactForm(WhoisObjectFormMixin, forms.ModelForm):
def __init__ ( self , * args , * * kwargs ) :
def __init__ ( self , * args , * * kwargs ) :
super ( ContactForm , self ) . __init__ ( * args , * * kwargs )
super ( ContactForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' mnt_by ' ] . queryset = Maintainer . objects . filter ( auth = self . _user ) . distinct ( )
if " mnt_by " in self . fields :
self . fields [ ' mnt_by ' ] . queryset = Maintainer . objects . filter ( auth = self . _user ) . distinct ( )
class ContactInitialForm ( ContactForm ) :
class ContactInitialForm ( ContactForm ) :
@ -91,7 +98,7 @@ class ContactInitialForm(ContactForm):
class InetNumForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
class InetNumForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
prefix = forms . CharField ( )
prefix = forms . CharField ( )
protectedFields = [ ' handle' , ' protocol' , ' parent_range ' , ' mnt_by ' , ' prefix ' ]
protectedFields = [ ' protocol' , ' parent_range ' , ' mnt_by ' , ' prefix ' ]
class Meta :
class Meta :
model = InetNum
model = InetNum
@ -114,9 +121,9 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
def clean_prefix ( self ) :
def clean_prefix ( self ) :
# make sure this is a subnet we're getting
# make sure this is a subnet we're getting
net = self . cleaned_data [ ' prefix ' ]
net = self . cleaned_data [ ' prefix ' ] . lower ( )
if not re . match ( r " [0-9:.]+/[0-9]+ " , net ) :
IP46CIDRValidator ( net )
raise forms . ValidationError ( " Address needs to be a subnet in the format of ip/cidr " )
try :
try :
net = ipaddress . ip_network ( net )
net = ipaddress . ip_network ( net )
except ValueError as e :
except ValueError as e :
@ -126,9 +133,9 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
def clean_parent_range ( self ) :
def clean_parent_range ( self ) :
parent_range = self . cleaned_data . get ( ' parent_range ' , None )
parent_range = self . cleaned_data . get ( ' parent_range ' , None )
# allow parent range to be unset for already present objects
# allow parent range to be unset for already present objects
if not parent_range and ( self . _create or not self . _create and self . instance . parent_range ) :
if not parent_range and ( self . _create or not self . _create and self . instance . parent_range ) :
raise forms . ValidationError ( " Parent range must be set " )
raise forms . ValidationError ( " Parent range must be set " )
if not self . _create and parent_range :
if not self . _create and parent_range :
@ -171,8 +178,9 @@ class InetNumForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
return cleaned_data
return cleaned_data
class ASBlockForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
class ASBlockForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
protectedFields = [ ' handle' , ' parent_block' , ' asBegin ' , ' asEnd ' , ' mnt_by ' ]
protectedFields = [ ' parent_block' , ' asBegin ' , ' asEnd ' , ' mnt_by ' ]
# FIXME: Filter blocks
# FIXME: Filter blocks
class Meta :
class Meta :
@ -190,6 +198,23 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
mnts = self . _user . maintainer_set . all ( )
mnts = self . _user . maintainer_set . all ( )
self . fields [ ' parent_block ' ] . queryset = ASBlock . getMntQueryset ( mnts , self . instance , " parent_block " )
self . fields [ ' parent_block ' ] . queryset = ASBlock . getMntQueryset ( mnts , self . instance , " parent_block " )
def clean_parent_block ( self ) :
parent_block = self . cleaned_data . get ( ' parent_block ' , None )
# allow parent range to be unset for already present objects
if not parent_block and ( self . _create or not self . _create and self . instance . parent_block ) :
raise forms . ValidationError ( " Parent block must be set " )
if not self . _create and parent_block :
# make sure we don't have circular dependencies
obj = parent_block
while obj . parent_block :
if obj . pk == self . instance . pk :
raise forms . ValidationError ( " No circular dependencies allowed " )
obj = obj . parent_block
return parent_block
def clean ( self ) :
def clean ( self ) :
cleaned_data = super ( ASBlockForm , self ) . clean ( )
cleaned_data = super ( ASBlockForm , self ) . clean ( )
@ -198,7 +223,7 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
asEnd = cleaned_data [ ' asEnd ' ]
asEnd = cleaned_data [ ' asEnd ' ]
parent = cleaned_data [ ' parent_block ' ]
parent = cleaned_data [ ' parent_block ' ]
# check if somebody is already using this block
# check if somebody is already using this block
# check if in range
# check if in range
if asBegin > asEnd :
if asBegin > asEnd :
raise forms . ValidationError ( " AS beginning must be smaller or equal to AS end " )
raise forms . ValidationError ( " AS beginning must be smaller or equal to AS end " )
@ -207,7 +232,6 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
if parent . asnumber_set . count ( ) > 0 :
if parent . asnumber_set . count ( ) > 0 :
raise forms . ValidationError ( " The parent AS block is already references by following AS number objects: %s " % ( " , " . join ( map ( lambda _x : _x . handle , parent . asnumber_set . all ( ) ) ) , ) )
raise forms . ValidationError ( " The parent AS block is already references by following AS number objects: %s " % ( " , " . join ( map ( lambda _x : _x . handle , parent . asnumber_set . all ( ) ) ) , ) )
# check if same range
# check if same range
if not ( asBegin > = parent . asBegin and asEnd < = parent . asEnd ) :
if not ( asBegin > = parent . asBegin and asEnd < = parent . asEnd ) :
raise forms . ValidationError ( " AS beginning and end must be inside the range of the parent AS block " )
raise forms . ValidationError ( " AS beginning and end must be inside the range of the parent AS block " )
@ -221,14 +245,14 @@ class ASBlockForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
continue
continue
if block . asBegin < = asBegin < = block . asEnd or block . asBegin < = asEnd < = block . asEnd or \
if block . asBegin < = asBegin < = block . asEnd or block . asBegin < = asEnd < = block . asEnd or \
asBegin < = block . asBegin < = asEnd or asBegin < = block . asEnd < = asEnd :
asBegin < = block . asBegin < = asEnd or asBegin < = block . asEnd < = asEnd :
raise forms . ValidationError ( " Block overlaps with block %s " % block . handle )
raise forms . ValidationError ( " Block overlaps with block %s " % block . handle )
return cleaned_data
return cleaned_data
class ASNumberForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
class ASNumberForm ( MntFormMixin , WhoisObjectFormMixin , forms . ModelForm ) :
protectedFields = [ ' handle' , ' asblock' , ' number ' , ' mnt_by ' ]
protectedFields = [ ' asblock' , ' number ' , ' mnt_by ' ]
class Meta :
class Meta :
model = ASNumber
model = ASNumber
@ -267,4 +291,3 @@ class ASNumberForm(MntFormMixin, WhoisObjectFormMixin, forms.ModelForm):
# has already other asblock?
# has already other asblock?
if block . asblock_set . count ( ) > 0 :
if block . asblock_set . count ( ) > 0 :
raise forms . ValidationError ( " The given AS block is already references by following sub AS blocks: %s " % ( " , " . join ( map ( lambda _x : _x . handle , block . asblock_set . all ( ) ) ) , ) )
raise forms . ValidationError ( " The given AS block is already references by following sub AS blocks: %s " % ( " , " . join ( map ( lambda _x : _x . handle , block . asblock_set . all ( ) ) ) , ) )