From 0d7f4a97ce7d73a988d237b8e7c0dbf2ffa74b3b Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 6 Jul 2016 02:03:52 +0200 Subject: [PATCH] Validateur pour le nom de l'interface --- machines/models.py | 29 +++++++++++++++++++++++++++-- re2o/settings.py | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/machines/models.py b/machines/models.py index 0276785c..f8d243d4 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1,8 +1,33 @@ from django.db import models -from django.forms import ModelForm, Form +from django.forms import ModelForm, Form, ValidationError from macaddress.fields import MACAddressField from users.models import User +from django.conf import settings +import re + +def full_domain_validator(hostname): + """ Validation du nom de domaine, extensions dans settings, prefixe pas plus long que 63 caractères """ + HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(? 63: + raise ValidationError( + ", le nom de domaine '%(label)s' est trop long (maximum de 63 caractères).", + params={'label': hostname}, + ) + if not HOSTNAME_LABEL_PATTERN.match(hostname): + raise ValidationError( + ", ce nom de domaine '%(label)s' contient des carractères interdits.", + params={'label': hostname}, + ) class Machine(models.Model): user = models.ForeignKey('users.User', on_delete=models.PROTECT) @@ -26,7 +51,7 @@ class Interface(models.Model): mac_address = MACAddressField(integer=False, unique=True) machine = models.ForeignKey('Machine', on_delete=models.PROTECT) details = models.CharField(max_length=255, blank=True) - dns = models.CharField(help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter de points", max_length=255, unique=True) + dns = models.CharField(help_text="Obligatoire et unique, doit se terminer en %s et ne pas comporter d'autres points" % ", ".join(settings.ALLOWED_EXTENSIONS), max_length=255, unique=True, validators=[full_domain_validator]) def __str__(self): return self.dns diff --git a/re2o/settings.py b/re2o/settings.py index 8d15d917..51604a9d 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -12,7 +12,7 @@ https://docs.djangoproject.com/en/1.8/ref/settings/ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os -from .settings_local import SECRET_KEY, DATABASES, DEBUG, ALLOWED_HOSTS +from .settings_local import SECRET_KEY, DATABASES, DEBUG, ALLOWED_HOSTS, ALLOWED_EXTENSIONS BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))