validate prefix

This commit is contained in:
Nico Schottelius 2020-12-05 00:21:43 +01:00
parent 10078a324f
commit ccc7053eb4
3 changed files with 19 additions and 6 deletions

View file

@ -10,3 +10,5 @@ class ULAForm(forms.ModelForm):
def clean_prefix(self):
prefix = self.cleaned_data['prefix']
validate_ula_prefix(prefix)
return prefix

View file

@ -13,14 +13,24 @@ def validate_ula_prefix(prefix):
net = ipaddress.IPv6Network(net_str, strict=False)
if not net.subnet_of(ula_net):
print("raising error here")
v = ValidationError(
raise ValidationError(
_(f"Prefix {prefix} is not within ULA range ({ula_net})"),
code='invalid',
params = {'prefix': prefix }
params = {'prefix': net }
)
print(v)
raise v
try:
ULA.objects.get(prefix=str(net[0]))
except ULA.DoesNotExist:
pass
else:
raise ValidationError(
_(f"Prefix {net} is already registered"),
code='invalid',
params = {'prefix': net }
)
class User(AbstractUser):

View file

@ -21,9 +21,10 @@ class IndexView(ListView):
class SubmitView(LoginRequiredMixin, CreateView):
model = ULA
form_class = ULAForm
login_url = '/login/'
success_url = '/'
form_class = ULAForm
def form_valid(self, form):
form.instance.owner = self.request.user