示例#1
0
def register(request):
    if request.method == "POST":
        uform = CreateUserForm(request.POST)
        pform = UserProfileForm(request.POST)
        if uform.is_valid() and pform.is_valid():
            # create the user and redirect to profile
            user = uform.save()
            profile = pform.save(commit=False)
            profile.user = user
            profile.save()
            # this would be usefull when the profile is 
            # allready created when post_save is triggered
            # profile = user.get_profile()
            # profile.address = pform.cleaned_data['address']
            # profile.save()
            
            # login the user automatically after registering
            user = authenticate(username=user.username, password=uform.cleaned_data['password'])
            login(request, user)
            
            return HttpResponseRedirect(reverse('fi_home'))
    else:
        uform = CreateUserForm()
        pform = UserProfileForm()

    ctx = {
        'user_form':uform,
        'profile_form':pform,
    }
            
    return render(request, 'accounts/register.html', ctx)
示例#2
0
def register(request):
    form = CreateUserForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')

    return render(request, "accounts/register.html", {"form": form})
示例#3
0
文件: views.py 项目: hakimkal/llin
def register(request):
    form = CreateUserForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
      
    return render(request, "accounts/register.html",{"form": form})
示例#4
0
 def test_invalid_incomplete(self):
     '''When form isn't completely filled out, form is not valid'''
     form = CreateUserForm(
         data={
             'user_name': 'test',
             'password': '******',
             'password_verify': '!@HesJwir3442@'
         })
     self.assertFalse(form.is_valid())
示例#5
0
    def test_register_form(self):
        """Test that the CreateUserForm is valid"""
        data = {
            "email": "*****@*****.**",
            "password1": "VendalTest",
            "password2": "VendalTest",
        }
        form = CreateUserForm(data)

        self.assertTrue(form.is_valid())
示例#6
0
 def test_vaild(self):
     '''Form is valid'''
     form = CreateUserForm(
         data={
             'user_name': 'test',
             'email': '*****@*****.**',
             'password': '******',
             'password_verify': '!@HesJwir3442@'
         })
     self.assertTrue(form.is_valid())
示例#7
0
 def test_email_required(self):
     """ An error occurs if a user does not register with an email"""
     data = {
         "email": "test",
         "password1": "VendalTest",
         "password2": "VendalTesto",
     }
     form = CreateUserForm(data)
     self.assertFalse(form.is_valid())
     self.assertIn("email", form.errors.keys())
     self.assertEqual(form.errors["email"][0],
                      "Enter a valid email address.")
示例#8
0
    def test_missing_password_confirmation(self):
        """An error occurs if the conformation password is missing"""
        data = {
            "email": "*****@*****.**",
            "password1": "VendalTest",
        }
        form = CreateUserForm(data)

        self.assertFalse(form.is_valid())
        self.assertIn("password2", form.errors.keys())
        self.assertEqual(form.errors["password2"][0],
                         "This field is required.")
示例#9
0
 def test_invalid_mismatch_passwords(self):
     '''When passwords don't match, form is invalid and throws error message'''
     form = CreateUserForm(
         data={
             'user_name': 'test',
             'email': '*****@*****.**',
             'password': '******',
             'password_verify': 'BLLLLEEEERRRG!!!'
         })
     self.assertFalse(form.is_valid())
     self.assertEqual(form['password_verify'].errors[0],
                      "Passwords did not match.")
示例#10
0
 def test_invalid_django_validation(self):
     '''When passwords don't pass django validation, form is invalid and throws error message'''
     form = CreateUserForm(
         data={
             'user_name': 'test',
             'email': '*****@*****.**',
             'password': '******',  #common password -- should fail
             'password_verify': 'password'
         })
     self.assertFalse(form.is_valid())
     self.assertEqual(form['password'].errors[0],
                      "This password is too common.")
示例#11
0
 def test_invalid_duplicate_user(self):
     '''When user already exists(case insensitive), form is invalid and throws error message'''
     User.objects.create_user('TeSt', 'pass')
     form = CreateUserForm(
         data={
             'user_name': 'tEsT',
             'email': '*****@*****.**',
             'password': '******',
             'password_verify': '!@HesJwir3442@'
         })
     self.assertFalse(form.is_valid())
     self.assertEqual(form['user_name'].errors[0],
                      "That user already exists!")
示例#12
0
def registerPage(request):
    form = CreateUserForm()

    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            username = form.cleaned_data.get("username")
            messages.success(request, "Account was created for" + username)
            return redirect("login")

    context = {"form": form}
    return render(request, "accounts/register.html", context)
示例#13
0
 def test_invalid_duplicate_email(self):
     '''When email is duplicated from another account, form is invalid and throws error message'''
     User.objects.create_user('tester', '*****@*****.**', 'pass')
     form = CreateUserForm(
         data={
             'user_name': 'test',
             'email': '*****@*****.**',
             'password': '******',
             'password_verify': '!@HesJwir3442@'
         })
     self.assertFalse(form.is_valid())
     self.assertEqual(form['email'].errors[0],
                      "That email is already being used.")
示例#14
0
def create(request):
    if request.method == 'POST':
        form = CreateUserForm(request.POST, initial={'captcha': request.META['REMOTE_ADDR']})
        if form.is_valid():
            form.save()
            messages.success(request, _(u'Account created success!'))
            return redirect('accounts:login')
        messages.error(request, _(u'Please correct the error below.'))
    else:
        form = CreateUserForm()
    return {
        'form': form
    }
示例#15
0
    def test_password_dont_match(self):
        """Test that an error occurs when password don't match"""
        data = {
            "email": "*****@*****.**",
            "password1": "VendalTest",
            "password2": "VendalTesto",
        }
        form = CreateUserForm(data)

        self.assertFalse(form.is_valid())
        self.assertIn("password2", form.errors.keys())
        self.assertEqual(form.errors["password2"][0],
                         "The two password fields didn’t match.")
def registerPage(request):
    form = CreateUserForm(request.POST)
    if request.method == "POST":
        if form.is_valid():
            user = form.save()
            username = form.cleaned_data.get('username')
            group = Group.objects.get(name='customer')
            user.groups.add(group)
            Customer.objects.create(user)
            messages.success(request, f'Account Created for {username}')
            return redirect('login')
    context = {'form': form}
    return render(request, 'accounts/register.html', context)
示例#17
0
def register(request):
    """Register an account with the tracker"""
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            user = form.save()
            user = authenticate(username=form.cleaned_data['username'], 
                password=form.cleaned_data['password2'])
            login(request,user)
            return HttpResponseRedirect('/profile/')
    else:
        form = CreateUserForm()

    return render(request, 'register.html', {
        'form' : form,
    })
示例#18
0
 def post(self, request):
     form = CreateUserForm(request.POST)
     if form.is_valid():
         user = form.save(commit=False)
         user.set_password(form.cleaned_data['password1'])
         user.save()
         # u2 = User()
         # username = form.cleaned_data['username']
         # passwoed = form.cleaned_data['password1']
         # first_name = form.cleaned_data['first_name']
         # last_name = form.cleaned_data['last_name']
         # u2.username = username
         # u2.first_name=first_name
         # u2.last_name = last_name
         # u2.set_password(password)
         # u2.save()
         return redirect('login')
     return render(request, 'form.html', {'form': form})
示例#19
0
def register(request):
    if request.method == "POST":
        uform = CreateUserForm(request.POST)
        pform = UserProfileForm(request.POST)
        if uform.is_valid() and pform.is_valid():
            # create the user and redirect to profile
            user = uform.save()
            profile = pform.save(commit=False)
            profile.user = user
            profile.save()
            # this would be usefull when the profile is
            # allready created when post_save is triggered
            # profile = user.get_profile()
            # profile.address = pform.cleaned_data['address']
            # profile.save()

            # login the user automatically after registering
            user = authenticate(username=user.username,
                                password=uform.cleaned_data['password'])
            login(request, user)

            return HttpResponseRedirect(reverse('fi_home'))
    else:
        uform = CreateUserForm()
        pform = UserProfileForm()

    ctx = {
        'user_form': uform,
        'profile_form': pform,
    }

    return render(request, 'accounts/register.html', ctx)
示例#20
0
def update_profile(request, pk):
    user = request.user
    user_profile = UserProfile.objects.get(username=user.username)

    if request.method == "POST ":
        user_form = CreateUserForm(data=request.POST, instance=user)
        profile_form = UserProfileForm(data=request.POST,
                                       instance=user_profile)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            return HttpResponseRedirect(
                reverse_lazy('accounts:profile', kwargs={'pk': user.pk}))

        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = CreateUserForm(instance=user)
        profile_form = UserProfileForm(instance=user_profile)

    return render(request, 'accounts/update_profile.html', {
        'user_form': user_form,
        'profile_form': profile_form,
    })
示例#21
0
def user_signup(request):
    registered = False

    if request.method == 'POST':
        user_form = CreateUserForm(data=request.POST)
        profile_form = UserProfileForm(request.POST, request.FILES)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user  # one to one relationship

            if 'profile_pic' in request.FILES:
                print('profile pic is uploaded')
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()
            registered = True

        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = CreateUserForm()
        profile_form = UserProfileForm()

    return render(
        request, 'accounts/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
def registerPage(request):

    # if request.user.is_authenticated:
    #     return redirect('home')
    # else:
    form = CreateUserForm()

    if request.method == 'POST':

        form = CreateUserForm(request.POST)
        print(request.POST)
        if form.is_valid():
            # print('on line 21')
            user = form.save()
            # print('on line 23')
            username = form.cleaned_data.get('username')
            group = Group.objects.get(name='customer')
            user.groups.add(group)

            messages.add_message(request, messages.SUCCESS,
                                 'Account was created for ' + username)
            # messages.success(request, 'Account was created for ' + user)
            return redirect('login')

        else:
            context = {'form': form}
            return render(request, 'accounts/register.html', context)
    else:
        context = {'form': form}
        return render(request, 'accounts/register.html', context)
示例#23
0
def login_operator(request, username=None):

    redirect_to = request.GET.get(REDIRECT_FIELD_NAME,
                                  reverse(settings.LOGIN_REDIRECT_URL))
    form = CreateUserForm(request.POST or None)

    if username:

        user = get_object_or_404(User, username=username)

        if user.is_superuser:

            messages.error(
                request, "Il faut un mot de passe pour se connecter "
                "avec cet utilisateur ;-)")
            return redirect('login')

        user.backend = 'django.contrib.auth.backends.ModelBackend'
        contrib_login(request, user)

        return redirect(redirect_to)

    elif request.method == 'POST' and form.is_valid():

        username = form.cleaned_data['username']
        assert (not User.objects.filter(username=username).exists())

        user = User.objects.create_user(username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        contrib_login(request, user)

        return redirect(redirect_to)

    c = {
        'form': form,
        'operators': User.objects.filter(is_superuser=False, is_active=True),
        'next': request.GET.get(REDIRECT_FIELD_NAME, ''),
    }

    return render(request, 'accounts/login_operator.html', c)
示例#24
0
def register(request):
    form = CreateUserForm()
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, 'Account was created for User ' + username)
            return redirect('login')
    context = {'form': form}
    return render(request, 'accounts/register.html', context)
示例#25
0
def login_operator(request, username=None):

    redirect_to = request.GET.get(REDIRECT_FIELD_NAME,
            reverse(settings.LOGIN_REDIRECT_URL))
    form = CreateUserForm(request.POST or None)

    if username:

        user = get_object_or_404(User, username=username)

        if user.is_superuser:

            messages.error(request, "Il faut un mot de passe pour se connecter "
                                    "avec cet utilisateur ;-)")
            return redirect('login')

        user.backend = 'django.contrib.auth.backends.ModelBackend'
        contrib_login(request, user)

        return redirect(redirect_to)

    elif request.method == 'POST' and form.is_valid():

        username = form.cleaned_data['username']
        assert(not User.objects.filter(username=username).exists())

        user = User.objects.create_user(username)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        contrib_login(request, user)

        return redirect(redirect_to)

    c = {
        'form': form,
        'operators': User.objects.filter(is_superuser=False, is_active=True),
        'next': request.GET.get(REDIRECT_FIELD_NAME, ''),
    }

    return render(request, 'accounts/login_operator.html', c)
示例#26
0
def signUp(request):
	if request.user.is_authenticated:
		return redirect('post_list')
	else:
		form = CreateUserForm()
		if request.method == 'POST':
			form = CreateUserForm(request.POST)
			if form.is_valid():
				form.save()
				user = form.cleaned_data.get('username')
				return redirect('login')
		return render(request, 'registration/signup.html', {'form':form})
示例#27
0
def loginPage(request):

    form = CreateUserForm()
    context = {'form': form}
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password1')
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('index')
        else:
            messages.error(request, 'Username or Password Incorrect')
            return render(request, 'accounts/login.html', context)
    else:
        return render(request, 'accounts/login.html', context)
示例#28
0
def registerPage(request):
    if request.user.is_authenticated:
        return redirect('home')
    else:
        form=CreateUserForm()
        if request.method == 'POST':
            form = CreateUserForm(request.POST)
            if form.is_valid():
                form.save()
                user = form.cleaned_data.get('username')
                messages.success(request,'Account was created for ' + user)
                return redirect("login")

        context= {'form':form}
        return render(request,'accounts/register.html',context)
示例#29
0
def createCustomer(request):
    form = CreateUserForm()
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            #group = Group.objects.get(name='customer')
            #user.groups.add(group)
            #Customer.objects.create(
            #   user=user,
            #   name=user.username
            #)

            logger.info("customer created by admin")

            return redirect('signout')
    context = {'form': form}
    return render(request, 'accounts/create_customer.html', context)
示例#30
0
def registerPage(request):
    form = CreateUserForm()
    context = {'form': form}
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            password = form.cleaned_data['password2']
            password
            messages.success(request, 'Account created for ' + username)
            return redirect('login')
        else:
            messages.error(request, form.errors)
            # form = CreateUserForm()
            # context = {'form': form}
    return render(request, 'accounts/register.html', context)
示例#31
0
def signup(request):
    if request.method == "POST":
        user_form = CreateUserForm(request.POST)

        if user_form.is_valid():
            user = user_form.save()
            auth_login(request, user)  # 로그인 처리
            return redirect('accounts:signup_profile')

    elif request.method == "GET":
        user_form = CreateUserForm()

    return render(request, 'accounts/signup.html', {
        'user_form': user_form,
    })
示例#32
0
def register(request):
    """Register an account with the tracker"""
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            user = form.save()
            user = authenticate(username=form.cleaned_data['username'],
                                password=form.cleaned_data['password2'])
            login(request, user)
            return HttpResponseRedirect('/profile/')
    else:
        form = CreateUserForm()

    return render(request, 'register.html', {
        'form': form,
    })
示例#33
0
def signUp(request):
    form = CreateUserForm()
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            #user = form.save()
            form.save()
            #group = Group.objects.get(name='customer')
            #user.groups.add(group)
            #Customer.objects.create(
            #    user=user,
            #    name=user.username
            #)
            logger.info("customer registered successfully")
            return redirect('signin')
        else:
            logger.error("registeration failed")

    context = {'form': form}
    return render(request, 'accounts/signup.html', context)
示例#34
0
def register(request):
    register_form = CreateUserForm()
    registered = False
    if request.method == 'POST':
        register_form = CreateUserForm(request.POST)

        if register_form.is_valid():
            user = register_form.save()
            # user.set_password(user.password)
            group = Group.objects.get(name='Members')
            group.save()
            user.groups.add(group)
            user.save()
            user = register_form.cleaned_data.get('username')
            messages.success(request, "Successfully registered! " + user)
            registered = True
        else:
            print(register_form.errors)

    else:
        register_form = CreateUserForm()

    return render(request, 'registration/registration.html', {'register_form':register_form,
                                                             'registered': registered})
示例#35
0
文件: views.py 项目: sevenisalie/CRM
def register(request):
    #form = UserCreationForm()                      #bring in a user creation form, this is why we imported that dope django premade usercreate form above
    form = CreateUserForm()                             ##custom based in forms.py, always import!
    if request.method == 'POST':
        #form = UserCreationForm(request.POST)      #pass in the stuff entered into the form, into the django UserCreationForm method.
        form = CreateUserForm(request.POST)             ##custom
        if form.is_valid():
            form.save()                             #.save() user recreation form
            user = form.cleaned_data.get('username')            #once a form.is_valid() you can call on it's data as a string with the .cleaned_data.get(nameofformfield)
            messages.success(request, 'Account was created')            #after importing messages from django.contrib, this is how you make a custom message.  do this command. then throw it into the html template you want to render the message in
            return render(request, 'accounts/login.html')
        else:
            messages.warning(request, 'Account unable to be created. Make sure all the fields are accurate.')           #it turns out this messages shit only works if theres an else statement. dont know why, but throw one in there even if its useless

    context = {
    'form': form,
    }


    #.redirect to login page
    return render(request, 'accounts/register.html', context)