Esempio n. 1
0
def api_user_add(request):
    response = {}
    email = get_param(request, "email")
    password = get_param(request, "password", strip=False)
    password_conf = get_param(request, "password_conf", strip=False)
    errors = {}
    if is_blank(email):
        errors["email"] = "Sorry! You need an e-mail address to sign up."
    elif not is_valid_email(email):
        errors["email"] = "Sorry! Please put a valid e-mail address."
    if is_blank(password):
        errors["password"] = "******"
    if is_blank(password_conf):
        errors["password_conf"] = "Please confirm your password."
    elif password != password_conf:
        errors["password_conf"] = "Oops! Your passwords do not match."
    try:
        User.objects.get(email=email)
        errors["email"] = "Sorry! This e-mail address is being used."
    except User.DoesNotExist:
        pass
    if len(errors.keys()) > 0:
        response["errors"] = errors
        return response, 200
    new_user = User(email=email)
    new_user.set_password(password)
    new_user.save()
    response["user"] = new_user.id
    user_login(request, new_user)
    return response, 201
Esempio n. 2
0
def api_user_add(request):
    response = {}
    email = get_param(request, 'email')
    password = get_param(request, 'password', strip=False)
    password_conf = get_param(request, 'password_conf', strip=False)
    errors = {}
    if is_blank(email):
        errors['email'] = 'Sorry! You need an e-mail address to sign up.'
    elif not is_valid_email(email):
        errors['email'] = 'Sorry! Please put a valid e-mail address.'
    if is_blank(password):
        errors['password'] = '******'
    if is_blank(password_conf):
        errors['password_conf'] = 'Please confirm your password.'
    elif password != password_conf:
        errors['password_conf'] = 'Oops! Your passwords do not match.'
    try:
        User.objects.get(email=email)
        errors['email'] = 'Sorry! This e-mail address is being used.'
    except User.DoesNotExist:
        pass
    if len(errors.keys()) > 0:
        response['errors'] = errors
        return response, 200
    new_user = User(email=email)
    new_user.set_password(password)
    new_user.save()
    response['user'] = new_user.id
    user_login(request, new_user)
    return response, 201
Esempio n. 3
0
def api_user_login(request):
    response = {}
    email = get_param(request, "email")
    password = get_param(request, "password", strip=False)
    errors = {}
    if is_blank(email):
        errors["email"] = "Sorry! You need an e-mail address to login."
    if is_blank(password):
        errors["password"] = "******"
    if not (is_blank(email) or is_blank(password)):
        user = authenticate(username=email, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
            else:
                errors["__all__"] = "Sorry! This account is inactive."
        else:
            errors["__all__"] = "Sorry! Your credentials are incorrect."
    if len(errors.keys()) > 0:
        response["errors"] = errors
        return response, 200
    return response, 200