validation code - wrong place?

This commit is contained in:
Nico Schottelius 2020-12-04 22:52:32 +01:00
parent 2c00930b8b
commit 43019e8e15
2 changed files with 24 additions and 10 deletions

View File

@ -1,8 +1,9 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.auth import get_user_model
from django.db import models
from django.core.exceptions import ValidationError
import ipaddress
class User(AbstractUser):
@ -19,16 +20,12 @@ class ULA(models.Model):
organization = models.CharField(max_length=256)
website = models.URLField()
def save(self, *args, **kwargs):
"""
Ensure we only save the first IPv6 address of the network
"""
# def save(self, *args, **kwargs):
# """
# Ensure we only save the first IPv6 address of the network
# """
net_str = f"{self.prefix}/48"
net = ipaddress.IPv6Network(net_str, strict=False)
self.prefix = str(net[0])
super().save(*args, **kwargs)
# super().save(*args, **kwargs)
def __str__(self):
return f"{self.prefix}"

View File

@ -6,6 +6,8 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import logout
from django.shortcuts import redirect
import ipaddress
from django.conf import settings
from ipv6ula.models import *
@ -23,6 +25,21 @@ class SubmitView(LoginRequiredMixin, CreateView):
def form_valid(self, form):
form.instance.owner = self.request.user
prefix = form.cleaned_data['prefix']
ula_net = ipaddress.IPv6Network("fd00::/8")
net_str = f"{prefix}/48"
net = ipaddress.IPv6Network(net_str, strict=False)
if not net.subnet_of(ula_net):
raise ValidationError(f"Prefix {prefix} is not within ULA range ({ula_net})",
code='invalid',
params = {'prefix': 42 }
)
self.prefix = str(net[0])
return super(SubmitView, self).form_valid(form)
class LoginView(auth_views.LoginView):