From 5b0ea19070af5369f43910bdfa1ca905ee554051 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 6 Jul 2016 02:56:30 +0200 Subject: [PATCH] Validateur pour le login/pseudo --- users/models.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/users/models.py b/users/models.py index dacb9855..a6a29168 100644 --- a/users/models.py +++ b/users/models.py @@ -1,6 +1,7 @@ from django.db import models from django.forms import ModelForm, Form from django import forms +import re from django.utils import timezone @@ -15,6 +16,15 @@ def remove_user_room(room): user.room = None user.save() +def linux_user_validator(login): + """ Validation du pseudo pour respecter les contraintes unix""" + UNIX_LOGIN_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$") + if not UNIX_LOGIN_PATTERN.match(login): + raise forms.ValidationError( + ", ce pseudo ('%(label)s') contient des carractères interdits", + params={'label': login}, + ) + class User(models.Model): STATE_ACTIVE = 0 STATE_DEACTIVATED = 1 @@ -27,7 +37,7 @@ class User(models.Model): name = models.CharField(max_length=255) surname = models.CharField(max_length=255) - pseudo = models.CharField(max_length=255, unique=True) + pseudo = models.CharField(max_length=255, unique=True, help_text="Doit contenir uniquement des lettres, chiffres, ou tirets", validators=[linux_user_validator]) email = models.EmailField() school = models.ForeignKey('School', on_delete=models.PROTECT, null=False, blank=False) comment = models.CharField(help_text="Commentaire, promo", max_length=255, blank=True)