Introduce form so that error is forwarded to the user

This commit is contained in:
Nico Schottelius 2020-12-04 23:45:26 +01:00
parent e037620ef3
commit 10078a324f
3 changed files with 19 additions and 3 deletions

View file

@ -1 +1,12 @@
from django import forms from django import forms
from .models import validate_ula_prefix, ULA
class ULAForm(forms.ModelForm):
class Meta:
model = ULA
fields = [ 'prefix', 'name', 'organization', 'website' ]
def clean_prefix(self):
prefix = self.cleaned_data['prefix']
validate_ula_prefix(prefix)

View file

@ -13,11 +13,14 @@ def validate_ula_prefix(prefix):
net = ipaddress.IPv6Network(net_str, strict=False) net = ipaddress.IPv6Network(net_str, strict=False)
if not net.subnet_of(ula_net): if not net.subnet_of(ula_net):
raise ValidationError( print("raising error here")
v = ValidationError(
_(f"Prefix {prefix} is not within ULA range ({ula_net})"), _(f"Prefix {prefix} is not within ULA range ({ula_net})"),
code='invalid', code='invalid',
params = {'prefix': prefix } params = {'prefix': prefix }
) )
print(v)
raise v
class User(AbstractUser): class User(AbstractUser):

View file

@ -10,7 +10,8 @@ import ipaddress
from django.conf import settings from django.conf import settings
from ipv6ula.models import * from .models import *
from .forms import ULAForm
class IndexView(ListView): class IndexView(ListView):
model = ULA model = ULA
@ -19,9 +20,10 @@ class IndexView(ListView):
class SubmitView(LoginRequiredMixin, CreateView): class SubmitView(LoginRequiredMixin, CreateView):
model = ULA model = ULA
fields = [ 'prefix', 'name', 'organization', 'website' ]
login_url = '/login/' login_url = '/login/'
success_url = '/' success_url = '/'
form_class = ULAForm
def form_valid(self, form): def form_valid(self, form):
form.instance.owner = self.request.user form.instance.owner = self.request.user