This commit is contained in:
Sebastian Lohff 2017-03-26 05:23:00 +02:00
parent d544e3364f
commit ed5fee0b13
3 changed files with 75 additions and 18 deletions

View File

@ -84,25 +84,34 @@ def freeSubnet(request):
raise ValidationError("Given prefix length is out of range") raise ValidationError("Given prefix length is out of range")
except ValueError: except ValueError:
raise ValidationError("PrefixLen is not a number") raise ValidationError("PrefixLen is not a number")
nets = list(parentRange.getNetwork().subnets())
for subRange in parentRange.inetnum_set.all():
newNet = None
for net in nets:
if subRange.getNetwork().network_address in net:
newNet = net
if not newNet:
# critical error, we want a 500 here
raise ValueError("Subnet not in range")
nets.remove(newNet)
nets.extend(newNet.address_exclude(subRange.getNetwork()))
nets = sorted(nets)
usableNet = None usableNet = None
for net in nets: # FIXME: use first biggest usable netblock...
if net.prefixlen <= prefixLen: #biggestNet = parentRange.inetnum_set.order_by("-address")
usableNet = net #if biggestNet
break # candidateNet =
# and (biggestNet.getNetwork().broadcast_address.:
if not usableNet:
# search for free network
nets = list(parentRange.getNetwork().subnets())
for subRange in parentRange.inetnum_set.all():
newNet = None
for net in nets:
if subRange.getNetwork().network_address in net:
newNet = net
if not newNet:
# critical error, we want a 500 here
raise ValueError("Subnet not in range")
nets.remove(newNet)
nets.extend(newNet.address_exclude(subRange.getNetwork()))
nets = sorted(nets)
for net in nets:
if net.prefixlen <= prefixLen:
usableNet = net
break
if not usableNet: if not usableNet:
raise ValidationError("No space left in given range") raise ValidationError("No space left in given range")

34
whoisdb/formfields.py Normal file
View File

@ -0,0 +1,34 @@
from django import forms
class MultiTextInput(forms.widgets.Input):
input_type = "text"
def render(self, name, value, attrs=None):
if value is not None:
selectedOptions = []
for val in value:
for k, v in self.choices:
if val == k:
selectedOptions.append(v)
value = " ".join(selectedOptions)
return super(MultiTextInput, self).render(name, value, attrs)
def value_from_datadict(self, data, files, name):
values = list(filter(bool, map(lambda _x: _x.strip(), data.get(name).split(" "))))
result = []
for value in values:
# FIXME: using value here throws a weird error message at some point
# could be handled by overriding the messages in ChoiceField
# or... well, don't know
kId = value
for k, v in self.choices:
if v.lower() == value.lower():
kId = str(k)
break
result.append(kId)
return result

View File

@ -3,6 +3,7 @@ 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 from .validators import HandleValidatorWithSuffix, IP46CIDRValidator
from .formfields import MultiTextInput
import ipaddress import ipaddress
@ -69,7 +70,20 @@ class MntFormMixin(object):
class MntForm(WhoisObjectFormMixin, forms.ModelForm): class MntForm(WhoisObjectFormMixin, forms.ModelForm):
class Meta: class Meta:
model = Maintainer model = Maintainer
fields = ['handle', 'description', 'admin_c'] fields = ['handle', 'description', 'admin_c', 'auth']
widgets = {'auth': MultiTextInput()}
help_texts = {
'auth': 'Enter names of users which can edit this object (space separated; '
'and yes, validation is somewhat broken (values disappear on error - just reload))'
}
def __init__(self, *args, **kwargs):
super(MntForm, self).__init__(*args, **kwargs)
print(args, kwargs)
#if self._create:
# self.fields['auth'].text("noot")
class MntInitialForm(MntForm): class MntInitialForm(MntForm):