def test_invalid_passwd(self):
     user_register = User.objects.create_user(username="******",
                                              email="*****@*****.**",
                                              password="******")
     data = {
         'username': user_register.username,
         'email': user_register.email,
         'password': user_register.password,
     }
     form = RegisterForm(data=data)
     self.assertFalse(form.is_valid())
Example #2
0
def user_register(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')

    registerForm = RegisterForm()

    state = None
    if request.method == 'POST':
        registerForm = RegisterForm(request.POST, request.FILES)
        password = request.POST.get('password', '')
        repeat_password = request.POST.get('re_password', '')
        if password == '' or repeat_password == '':
            state = 'empty'
        elif password != repeat_password:
            state = 'repeat_error'
        else:
            username = request.POST.get('username', '')
            name = request.POST.get('name', '')
            email = request.POST.get('email', '')
            if User.objects.filter(username=username):
                state = 'user_exist'
            else:
                new_user = User.objects.create(username=username)
                new_user.set_password(password)
                new_user.save()

                new_reader = Reader.objects.create(user=new_user, name=name, phone=int(username),email=email)
                # new_reader.photo = request.FILES['photo']
                #存图片
                # img=request.FILES['photo']
                # f = open('library/static/media/images/'+img.name, 'wb')
                # for line in img.chunks():  # img.chunks()为可迭代对象
                #     f.write(line)
                #     f.close()

                new_reader.save()

                state = 'success'

                auth.login(request, new_user)

                context = {
                    'state': state,
                    'registerForm': registerForm,
                }
                return render(request, 'library/register.html', context)

    context = {
        'state': state,
        'registerForm': registerForm,
    }

    return render(request, 'library/register.html', context)
Example #3
0
def user_edit(request):
    """Backend for AJAX modification of user's email and password.

    POST:
    email  -- new email address
    pass0  -- (optional) old password
    pass1  -- (optional) new password
    pass2  -- (optional) new password repeated

    Renders JSON: (besides 'status' or 'err')
    """
    pass_mod = map(
        lambda f: f in request.POST and request.POST[f] != '',
        ['pass0', 'pass1', 'pass2'],
        )
    pass_none = not reduce(bool.__or__, pass_mod)
    pass_all = reduce(bool.__and__, pass_mod)
    myuser = request.user.myuser
    fake_register = {
        'username': '******',
        'password': '******',
        'password2': 'fake_pass',
        'email': request.POST['email'],
        'name': 'Fake User',
        }
    if pass_none:
        f = RegisterForm(fake_register)
        if f.is_valid():
            myuser.update_user(request.POST['email'])
        else:
            return render_JSON_Error('Invalid email address.')
    elif pass_all:
        fake_register['password'] = request.POST['pass1']
        fake_register['password2'] = request.POST['pass2']
        f = RegisterForm(fake_register)
        if f.is_valid():
            myuser.update_user(
                request.POST['email'],
                request.POST['pass0'],
                request.POST['pass1'],
                )
        else:
            if 'email' in f.errors:
                return render_JSON_Error('Invalid email address.')
            elif 'password' in f.errors:
                return render_JSON_Error('New password is too short.')
            elif 'password2' in f.errors:
                return render_JSON_Error('New passwords do not match.')
            else:
                return render_JSON_Error('Syntax error.', {'detail': f.errors})
    else:
        return render_JSON_Error('Password fields incomplete.')
    return render_JSON_OK({})
Example #4
0
def user_register(request):
    if request.user.is_authenticated:
        return HttpResponseRedirect('/')

    registerForm = RegisterForm()

    state = None
    if request.method == 'POST':
        registerForm = RegisterForm(request.POST, request.FILES)
        password = request.POST.get('password', '')
        repeat_password = request.POST.get('re_password', '')
        #判断两次输入密码是否一致
        if password == '' or repeat_password == '':
            state = 'empty'
        elif password != repeat_password:
            state = 'repeat_error'
        else:
            username = request.POST.get('username', '')
            name = request.POST.get('name', '')
            #判断用户名是否存在
            if User.objects.filter(username=username):
                state = 'user_exist'
            else:
                #不存在同名用户,那就创建一个用户,并保存密码
                new_user = User.objects.create(username=username)
                new_user.set_password(password)
                new_user.save()
                #创建用户的同时,即创建一个读者
                new_reader = Reader.objects.create(user=new_user,
                                                   name=name,
                                                   phone=int(username))
                new_reader.photo = request.FILES['photo']
                new_reader.save()
                state = 'success'

                #用户登录
                auth.login(request, new_user)
                #return HttpResponseRedirect('/')

                context = {
                    'state': state,
                    'registerForm': registerForm,
                }
                return render(request, 'library/register.html', context)

    context = {
        'state': state,
        'registerForm': registerForm,
    }

    return render(request, 'library/register.html', context)
Example #5
0
def user_register(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/')

    registerForm = RegisterForm()

    state = None
    if request.method == 'POST':
        registerForm = RegisterForm(request.POST, request.FILES)
        password = request.POST.get('password', '')
        repeat_password = request.POST.get('repeat_password', '')
        if password == '' or repeat_password == '':
            state = 'empty'
        elif password != repeat_password:
            state = 'repeat_error'
        else:
            username = request.POST.get('username', '')
            name = request.POST.get('name', '')
            if User.objects.filter(username=username):
                state = 'user_exist'
            else:
                new_user = User.objects.create(username=username)
                new_user.save()
                new_reader = Reader.objects.create(user=new_user,
                                                   name=name,
                                                   phone=int(username))
                new_reader.photo = request.FILES['photo']
                new_reader.save()
                state = 'success'

                auth.login(request, new_user)

                context = {
                    'state': state,
                    'registerForm': registerForm,
                }
                return render(request, 'library/register.html', context)

    context = {
        'state': state,
        'registerForm': registerForm,
    }

    return render(request, 'library/register.html', context)
Example #6
0
def register(request):
    """Backend for AJAX register.

    POST:
    username  -- username of the new user
    password  -- password of the new user
    password2 -- password confirm
    email     -- email of the new user
    name      -- name of the new user

    Renders JSON: (besides 'status' or 'err')
    username -- (on 'OK') username of the logged in user
    name     -- (on 'OK') name of the logged in user
    detail   -- (on 'Error') error list from form validation

    POST data will be validated by RegisterForm.
    """
    form = RegisterForm(request.POST)
    if form.is_valid():
        u = MyUser()
        try:
            u.register(
                form.cleaned_data['username'],
                form.cleaned_data['password'],
                form.cleaned_data['email'],
                form.cleaned_data['name'],
                )
            user = auth.authenticate(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password'],
                )
            assert user is not None
            auth.login(request, user)
            return render_JSON_OK({
                'username': user.username,
                'name': user.myuser.name,
                })
        except IntegrityError as err:
            return render_JSON_Error('Username taken.')
    return render_JSON_Error('Register syntax error.', {
        'detail': form.errors,
        })