validations.py 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
'''
This module contains Validation functions
'''
import re

from modules.signup.exceptions import SignUpError
from modules.signup.utils import (
    clean_email,
    clean_full_name)


def validate_email(user_email):
    '''
    Validating Email provided to check for proper mail format and
    only alphabets
    '''
    user_email = clean_email(user_email)
    if not re.match(r'[^@]+@[^@]+\.[^@]+', user_email):
        raise SignUpError('Incorrect Format')
    user_email = user_email.strip(' ').split('.')
    for parts in user_email:
        parts = parts.split('@')
        if not all(part.isalpha() for part in parts):
            raise SignUpError('Incorrect Format')
    return True


def validate_first_pass(first_pass):
    '''
    Validating whether or not the first password is submitted by
    the user
    '''
    if not first_pass:
        raise SignUpError('Enter password')
    elif len(first_pass) < 6:
        raise SignUpError('Password is short')
    return True


def validate_confirm_pass(first_pass, confirm_pass):
    '''
    Validating whether both the password submitted by the user
    match each other
    '''
    if first_pass != confirm_pass:
        raise SignUpError('Password is not same')
    return True


def validate_full_name(full_name):
    '''
    Validating whether Full Name is provided by user and is in
    correct alphabet format
    '''
    full_name = clean_full_name(full_name)
    if not full_name:
        raise SignUpError('Enter Full Name')
    for name in full_name.split():
        if not name.isalpha():
            raise SignUpError('Incorrect Format')
    return True