Просмотр исходного кода

Documented validators defined so far by Misago

Rafał Pitoń 11 лет назад
Родитель
Сommit
2181c527c8
2 измененных файлов с 131 добавлено и 52 удалено
  1. 80 0
      docs/developers/validators.rst
  2. 51 52
      misago/users/validators.py

+ 80 - 0
docs/developers/validators.rst

@@ -27,9 +27,89 @@ misago.users.validators
 =======================
 
 
+validate_email
+--------------
+
+:py:class:`misago.users.validators.validate_email`
+
+Function that takes email address and runs content, availability and ban check validation in this order via calling dedicated validators.
+
+
+validate_email_banned
+---------------------
+
+:py:class:`misago.users.validators.validate_email_banned`
+
+Function that accepts email string as its only argument and raises Validation error if it's banned.
+
+
+validate_email_content
+----------------------
+
+:py:class:`misago.users.validators.validate_email_content`
+
+Callable instance of :py:class:`django.core.validators.EmailValidator` that checks if email address has valid structure and contents.
+
+
+validate_password
+-----------------
+
+:py:class:`misago.users.validators.validate_password`
+
+Function that takes plaintext password and runs length and complexity validation in this order via calling dedicated validators.
+
+
+validate_password_complexity
+----------------------------
+
+:py:class:`misago.users.validators.validate_password_complexity`
+
+Validates password complexity against tests specified in ``password_complexity`` setting.
+
+
+validate_password_length
+------------------------
+
+:py:class:`misago.users.validators.validate_password_length`
+
+Validates password length and raises ValidationError if specified plaintext password is shorter than ``password_length_min``.
+
+
 validate_username
 -----------------
 
 :py:class:`misago.users.validators.validate_username`
 
 Function that takes username and runs content, length, availability and ban check validation in this order via calling dedicated validators.
+
+
+validate_username_available
+---------------------------
+
+:py:class:`misago.users.validators.validate_username_available`
+
+Function that accepts username string as its only argument and raises ValidationError if it's already taken.
+
+
+validate_username_banned
+------------------------
+
+:py:class:`misago.users.validators.validate_username_banned`
+
+Function that accepts username string as its only argument and raises Validation error if it's banned.
+
+
+validate_username_content
+-------------------------
+
+:py:class:`misago.users.validators.validate_username_content`
+
+Function that accepts username string as its only argument and raises Validation error if username contains disallowed characters (eg. those that are not matched by ``[0-9a-z]+`` regex).
+
+
+validate_username_length
+------------------------
+
+:py:class:`misago.users.validators.validate_username_length`
+
+Function that accepts username string as its only argument and raises Validation error if it's shorter than ``username_length_min`` setting or longer than ``username_length_max`` setting.

+ 51 - 52
misago/users/validators.py

@@ -1,59 +1,13 @@
 import re
 from django.core.exceptions import ValidationError
-from django.core.validators import validate_email as validate_email_contents
+from django.core.validators import validate_email as validate_email_content
 from django.utils.translation import ungettext, ugettext_lazy as _
 from django.contrib.auth import get_user_model
 from misago.conf import settings
 
 
-USERNAME_REGEX = re.compile(r'^[0-9a-z]+$', re.IGNORECASE)
-
-
-def validate_username_available(value):
-    User = get_user_model()
-
-    try:
-        User.objects.get_by_username(value)
-    except User.DoesNotExist:
-        pass
-    else:
-        raise ValidationError(_("This username is not available."))
-
-
-def validate_username_banned(value):
-    """TODO for when bans will be reimplemented from 0.5"""
-
-
-def validate_username_content(value):
-    if not USERNAME_REGEX.match(value):
-        raise ValidationError(
-            _("Username can only contain latin alphabet letters and digits."))
-
-
-def validate_username_length(value):
-    if len(value) < settings.username_length_min:
-        message = ungettext(
-            'Username must be at least one character long.',
-            'Username must be at least %(length)d characters long.',
-            settings.username_length_min)
-        message = message % {'length': settings.username_length_min}
-        raise ValidationError(message)
-
-    if len(value) > settings.username_length_max:
-        message = ungettext(
-            "Username cannot be longer than one characters.",
-            "Username cannot be longer than %(length)d characters.",
-            settings.username_length_max)
-        message = message % {'length': settings.username_length_max}
-        raise ValidationError(message)
-
-
-def validate_username(value):
-    """shortcut function that does complete validation of username"""
-    validate_username_content(value)
-    validate_username_length(value)
-    validate_username_available(value)
-    validate_username_banned(value)
+ALPHANUMERICS_RE = re.compile('[\W_]+', re.UNICODE)
+USERNAME_RE = re.compile(r'^[0-9a-z]+$', re.IGNORECASE)
 
 
 def validate_email_available(value):
@@ -73,7 +27,7 @@ def validate_email_banned(value):
 
 def validate_email(value):
     """shortcut function that does complete validation of email"""
-    validate_email_contents(value)
+    validate_email_content(value)
     validate_email_available(value)
     validate_email_banned(value)
 
@@ -95,8 +49,6 @@ def _validate_password_case(value):
             _("Password must contain characters with different cases."))
 
 
-ALPHANUMERICS_RE = re.compile('[\W_]+', re.UNICODE)
-
 
 def _validate_password_special(value):
     alphanumerics_len = len(ALPHANUMERICS_RE.sub('', value))
@@ -133,3 +85,50 @@ def validate_password(value):
     """shortcut function that does complete validation of password"""
     validate_password_length(value)
     validate_password_complexity(value)
+
+
+def validate_username_available(value):
+    User = get_user_model()
+
+    try:
+        User.objects.get_by_username(value)
+    except User.DoesNotExist:
+        pass
+    else:
+        raise ValidationError(_("This username is not available."))
+
+
+def validate_username_banned(value):
+    """TODO for when bans will be reimplemented from 0.5"""
+
+
+def validate_username_content(value):
+    if not USERNAME_RE.match(value):
+        raise ValidationError(
+            _("Username can only contain latin alphabet letters and digits."))
+
+
+def validate_username_length(value):
+    if len(value) < settings.username_length_min:
+        message = ungettext(
+            'Username must be at least one character long.',
+            'Username must be at least %(length)d characters long.',
+            settings.username_length_min)
+        message = message % {'length': settings.username_length_min}
+        raise ValidationError(message)
+
+    if len(value) > settings.username_length_max:
+        message = ungettext(
+            "Username cannot be longer than one characters.",
+            "Username cannot be longer than %(length)d characters.",
+            settings.username_length_max)
+        message = message % {'length': settings.username_length_max}
+        raise ValidationError(message)
+
+
+def validate_username(value):
+    """shortcut function that does complete validation of username"""
+    validate_username_content(value)
+    validate_username_length(value)
+    validate_username_available(value)
+    validate_username_banned(value)