Exemple #1
0
def register(request):
    if request.method == "POST":
        email = request.POST.get('email')
        username = request.POST.get('username')
        password = request.POST.get('password')
        if not validators.email_validator(
                email) and not validators.username_validator(email):
            response = {
                'msg': 'fail',
                'info': 'please check your email address or username.'
            }
        elif not validators.password_validator(password):
            response = {'msg': 'fail', 'info': 'password too short.'}
        else:
            try:
                user = models.UserProfile()
                user.username = username
                user.email = email
                user.set_password(password)
                user.save()
                response = {"msg": "success"}
            except IntegrityError as e:
                response = {"msg": "fail", "info": str(e)}
        return HttpResponse(json.dumps(response))
    return render(request, 'register.html')
Exemple #2
0
def login(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get('password')
        if not validators.email_validator(
                email) and not validators.username_validator(email):
            response = {
                'msg': 'fail',
                'info': 'please check your email address or username.'
            }
        elif not validators.password_validator(password):
            response = {'msg': 'fail', 'info': 'password too short.'}
        else:
            response = {'msg': 'success', 'info': ''}
            user = auth.authenticate(request,
                                     username=email,
                                     password=password)
            if user is not None:
                auth.login(request, user)
            else:
                response = {'msg': 'fail', 'info': 'password is invalid.'}
                try:
                    models.UserProfile.objects.get(username=email)
                except models.UserProfile.DoesNotExist:
                    try:
                        models.UserProfile.objects.get(email=email)
                    except models.UserProfile.DoesNotExist:
                        response['info'] = 'username or email not exist.'
        return HttpResponse(json.dumps(response))
    return render(request, 'login.html')
Exemple #3
0
def change_password(request):
    if request.is_ajax():
        old_pass = request.POST.get('oldPass')
        new_pass = request.POST.get('newPass')
        user = get_user(request)
        if not validators.password_validator(new_pass):
            res = {'msg': 'fail', 'info': 'invalid input.'}
        elif not user.check_password(old_pass) or user.check_password(new_pass):
            res = {'msg': 'fail', 'info': 'invalid password.'}
        else:
            user.set_password(new_pass)
            user.save()
            res = {'msg': 'success'}
        return HttpResponse(json.dumps(res))