From 10078a324f73f4fc998343a73adbe7ecd27bb2f5 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 4 Dec 2020 23:45:26 +0100 Subject: [PATCH] Introduce form so that error is forwarded to the user --- ipv6ula/forms.py | 11 +++++++++++ ipv6ula/models.py | 5 ++++- ipv6ula/views.py | 6 ++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ipv6ula/forms.py b/ipv6ula/forms.py index e79048b..ebd3c1e 100644 --- a/ipv6ula/forms.py +++ b/ipv6ula/forms.py @@ -1 +1,12 @@ 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) diff --git a/ipv6ula/models.py b/ipv6ula/models.py index b408796..fb1f529 100644 --- a/ipv6ula/models.py +++ b/ipv6ula/models.py @@ -13,11 +13,14 @@ def validate_ula_prefix(prefix): net = ipaddress.IPv6Network(net_str, strict=False) 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})"), code='invalid', params = {'prefix': prefix } ) + print(v) + raise v class User(AbstractUser): diff --git a/ipv6ula/views.py b/ipv6ula/views.py index 70ae514..859e4e8 100644 --- a/ipv6ula/views.py +++ b/ipv6ula/views.py @@ -10,7 +10,8 @@ import ipaddress from django.conf import settings -from ipv6ula.models import * +from .models import * +from .forms import ULAForm class IndexView(ListView): model = ULA @@ -19,9 +20,10 @@ class IndexView(ListView): class SubmitView(LoginRequiredMixin, CreateView): model = ULA - fields = [ 'prefix', 'name', 'organization', 'website' ] + login_url = '/login/' success_url = '/' + form_class = ULAForm def form_valid(self, form): form.instance.owner = self.request.user