move validator to model

This commit is contained in:
Nico Schottelius 2020-12-04 23:21:33 +01:00
parent 43019e8e15
commit e037620ef3
4 changed files with 53 additions and 25 deletions

View file

@ -0,0 +1,19 @@
# Generated by Django 3.1.4 on 2020-12-04 22:01
from django.db import migrations, models
import ipv6ula.models
class Migration(migrations.Migration):
dependencies = [
('ipv6ula', '0002_auto_20201204_2019'),
]
operations = [
migrations.AlterField(
model_name='ula',
name='prefix',
field=models.GenericIPAddressField(protocol='IPv6', unique=True, validators=[ipv6ula.models.validate_ula_prefix]),
),
]

View file

@ -2,8 +2,22 @@ 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
from django.utils.translation import gettext_lazy as _
import ipaddress
def validate_ula_prefix(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': prefix }
)
class User(AbstractUser):
@ -15,17 +29,21 @@ class ULA(models.Model):
on_delete=models.CASCADE
)
prefix = models.GenericIPAddressField(protocol='IPv6', unique=True)
prefix = models.GenericIPAddressField(protocol='IPv6', unique=True, validators=[validate_ula_prefix])
name = models.CharField(max_length=256)
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
"""
# super().save(*args, **kwargs)
net_str = f"{self.prefix}/48"
net = ipaddress.IPv6Network(net_str, strict=False)
self.prefix = str(net[0])
super().save(*args, **kwargs)
def __str__(self):
return f"{self.prefix}"

View file

@ -6,11 +6,17 @@
<div class="row">
<div class="col">
All ULA prefixes are /48 networks. Simply add the first IP address
(without any netmask, for instance fd23:2323:2323::).
<p>
All ULA prefixes are /48 networks. Simply add the first IP address
(without any netmask, for instance fd23:2323:2323::).
You can choose the name of your liking and an optional
organization name.
</p>
<p>
ULA prefixes must be part of the fd00::/8 network.
</p>
You can choose the name of your liking and an optional
organization name.
</div>
<div class="col">

View file

@ -25,21 +25,6 @@ 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):