def signup(request): """User registration view. **Context** ``form`` An instance of UserCreationForm. **Template:** :template:`users/signup.html` """ if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password1'] data = { 'timezone': form.cleaned_data['timezone'] or 'UTC', 'username': form.cleaned_data['username'], } user = User.objects.create_user(email=email, password=password, **data) signin(request, user) return HttpResponseRedirect('/') else: form = UserCreationForm() context = {'form': form} return render(request, 'users/signup.html', context)
def post(self, request, invitation_hash): signup_invitation = self.get_invitation(request, invitation_hash) if not signup_invitation: return render(request, 'registration/invalid_signup.html', {'hash': invitation_hash}) form = UserCreationForm(request.POST) if request.POST.get("email") != signup_invitation.email_invited: messages.error( request, "You must sign up with the invited email: {}".format( signup_invitation.email_invited)) return render( request, self.template_name, { 'form': form, 'categories': Category.objects.all(), 'invitation': signup_invitation }) if form.is_valid(): form.save(chosen_categories=request.POST.getlist('categories'), signup_invitation=signup_invitation) user = authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password1')) login(request, user) return redirect(self.redirect_to)
def signup_view(request, *args, **kwargs): if request.user.is_authenticated(): return redirect('home') form = UserCreationForm(request.POST or None) # form.helper = FormHelper(form) # form.helper.form_class = "form-horizontal" # form.helper.label_class = "col-lg-4" # form.helper.field_class = "col-lg-8" if form.is_valid(): user = form.save() if user.is_active: #logger.debug('SIGNUP %s', [user, user.username, user.password, request.POST["password"], form.cleaned_data["password"]]) user = authenticate( username=user.username, password=form.cleaned_data["password"]) #logger.debug('SIGNUP %s', [user]) login(request, user) #messages.success(request, 'Sign in succeeded.') # return redirect(request.REQUEST.get('next', 'users:profile')) return redirect('users:profile') else: messages.warning(request, 'Sorry, your user account is inactive.') return render(request, "signup.html", {'form': form, 'next': request.REQUEST.get('next', '/')})
def register(request): if request.user.is_authenticated(): return redirect("users.views.profile") if request.POST: creation_form = UserCreationForm(request.POST) profile_form = UserRegistrationForm(request.POST) if creation_form.is_valid() and profile_form.is_valid(): assert creation_form.cleaned_data.get("password1") == creation_form.cleaned_data.get("password2") new_user = creation_form.save(commit = False) new_profile = profile_form.save(commit = False) new_user.save() new_profile.user = new_user new_profile.save() return login(request) else: creation_form = UserCreationForm() profile_form = UserRegistrationForm() return render(request, "register.html", { "creation_form": creation_form, "profile_form": profile_form })
class UserCreationTestCase(TestCase): def setUp(self): self.test_user = User.objects.create( username='******', email='*****@*****.**', password='******' ) self.bad_form = UserCreationForm({ 'username': '******', 'password1': 'password', 'password2': 'password', }) self.good_form = UserCreationForm({ 'username': '******', 'password1': 'password', 'password2': 'password', }) def test_username_good(self): self.assertTrue(self.good_form.is_valid()) def test_clean_username_bad(self): self.assertFalse(self.bad_form.is_valid())
def test_form_with_valid_data(self): form = UserCreationForm(self.data) self.assertTrue(form.is_valid()) form.save() self.assertEqual( User.objects.all().filter(email=self.data['email']).count(), 1)
def test_create_duplicate(self): """Ensure an existing email address results in an error""" form = UserCreationForm(data={ 'email': '*****@*****.**', 'password1': 'new', 'password2': 'new', }) self.assertFalse(form.is_valid())
def test_create_icase_duplicate(self): """Ensure email validation is case insensitive""" form = UserCreationForm(data={ 'email': '*****@*****.**', 'password1': 'new', 'password2': 'new', }) self.assertFalse(form.is_valid())
def post(self, request): form = UserCreationForm(request.POST) if form.is_valid(): form.save() messages.success( request, f'Your account has been created! You are now able to log in') return redirect('blog-home')
def test_create_unique(self): """Ensure basic data validates""" form = UserCreationForm(data={ 'email': '*****@*****.**', 'password1': 'pass', 'password2': 'pass', }) self.assertTrue(form.is_valid())
def test_invalid_noPwd(self): #test for no password data = { 'first_name': 'valid', 'last_name': 'user', 'email': '*****@*****.**' } form = UserCreationForm(data) self.assertFalse(form.is_valid())
def test_invalid_email(self): data = { 'email': 'testuser', 'password1': 'Pa$sw0rd', 'password2': 'Pa$sw0rd', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertIn(_('Enter a valid email address.'), form['email'].errors)
def test_userCreationForm_valid(self): form = UserCreationForm(data={ 'username': '******', 'email': '*****@*****.**', 'password1': 'hellotest1', 'password2': 'hellotest1', 'phone':'123' },instance = CustomUser.objects.create(username = '******', email = '*****@*****.**', phone='123345')) self.assertTrue(form.is_valid())
def test_password2_matches_passwords1(self): form = UserCreationForm(data={ 'username': '******', 'email': '*****@*****.**', 'password1': 'hellotest1', 'password2': 'hellotestdiff', 'phone':'1234' },instance = CustomUser.objects.create(username = '******', email = '*****@*****.**', phone='23456')) self.assertFalse(form.is_valid())
def test_if_email_unique(self): form = UserCreationForm(data={ 'username': '******', 'email': '*****@*****.**', 'password1': 'Password1', 'password2': 'Password1', 'phone':'12345' },instance = CustomUser.objects.create(username = '******', email = '*****@*****.**', phone='123455')) self.assertTrue(form.is_valid())
def test_mismatching_passwords(self): form = UserCreationForm( data={ 'email': '*****@*****.**', 'username': '******', 'password1': 'MISMATCHING_PASSWORDS', 'password2': 'mismatching_passwords' }) self.assertFalse(form.is_valid()) self.assertTrue('password2' not in form.clean())
def test_valid_user_are_saved(self): data = { 'email': '*****@*****.**', 'password1': 'Pa$sw0rd', 'password2': 'Pa$sw0rd', } form = UserCreationForm(data) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual(repr(user), '<%s: [email protected]>' % get_user_model().__name__)
def test_password_is_not_saved_raw(self): raw_password = '******' data = { 'email': '*****@*****.**', 'password1': raw_password, 'password2': raw_password, } form = UserCreationForm(data) user = form.save() self.assertNotEqual(raw_password, user.password)
def test_valid_noFirstLast(self): #tests for valid -- no first and last data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'Testcasepwd', 'password2': 'Testcasepwd' } form = UserCreationForm(data=data) self.assertTrue(form.is_valid())
def test_invalid_noEmail(self): #test for no email data = { 'first_name': 'valid', 'last_name': 'user', 'password1': 'Testcasepwd', 'password2': 'Testcasepwd' } form = UserCreationForm(data) self.assertFalse(form.is_valid())
def test_clean_username(self): # A user with proto_user params does not exist yet. proto_user = UserFactory.build() form = UserCreationForm( { "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, } ) assert form.is_valid() assert form.clean_username() == proto_user.username # Creating a user. form.save() # The user with proto_user params already exists, # hence cannot be created. form = UserCreationForm( { "username": proto_user.username, "password1": proto_user._password, "password2": proto_user._password, } ) assert not form.is_valid() assert len(form.errors) == 1 assert "username" in form.errors
def test_password_missmatch(self): """Check that a form with different passwords is not valid.""" data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'test1', 'password2': 'test2' } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertTrue('password2' in form.errors)
def test_bad_username_provided(self): for unsupported_char in """\/!?@#$%^&*()[]{}<>=+~|:;"'""": form = UserCreationForm( data={ 'email': '*****@*****.**', 'username': '******' + unsupported_char, 'password1': 'A_VERY-$trong.p@assworD', 'password2': 'A_VERY-$trong.p@assworD' }) self.assertFalse(form.is_valid()) self.assertTrue('username' not in form.clean())
def test_invalid_noUser(self): #test for no username data = { 'first_name': 'valid', 'last_name': 'user', 'password1': 'Testcasepwd', 'password2': 'Testcasepwd', 'email': '*****@*****.**' } form = UserCreationForm(data) self.assertFalse(form.is_valid())
def test_password_verification(self): # The verification password is incorrect. data = { 'email': '*****@*****.**', 'password1': 'Pa$sw0rd1', 'password2': 'Pa$sw0rd2', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['password2'].errors, [force_text(form.error_messages['password_mismatch'])])
def test_valid_all(self): #tests for valid all inputs data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'valid', 'last_name': 'user', 'password1': 'Testcasepwd', 'password2': 'Testcasepwd' } form = UserCreationForm(data=data) self.assertTrue(form.is_valid())
def test_password_verification(self): # The verification password is incorrect. data = { 'email': '*****@*****.**', 'password1': 'Pa$sw0rd1', 'password2': 'Pa$sw0rd2', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual( form['password2'].errors, [force_text(form.error_messages['password_mismatch'])])
def test_user_already_exists(self): get_user_model().objects.create_user('*****@*****.**', 'Pa$sw0rd') data = { 'email': '*****@*****.**', 'password1': 'Pa$sw0rd', 'password2': 'Pa$sw0rd', } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form['email'].errors, [force_text(form.error_messages['duplicate_email'])])
def test_valid_user_are_saved(self): data = { 'email': '*****@*****.**', 'password1': 'Pa$sw0rd', 'password2': 'Pa$sw0rd', } form = UserCreationForm(data) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual( repr(user), '<%s: [email protected]>' % get_user_model().__name__)
def test_dupplicated_username(self): """Check that a form with an existing username is not valid.""" User.objects.create(username='******').save() data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'test', 'password2': 'test' } form = UserCreationForm(data) self.assertFalse(form.is_valid()) self.assertTrue('username' in form.errors)
def register(request): form = UserCreationForm(request.POST if request.method == 'POST' else None) for key in form.fields.keys(): form.fields[key].widget.attrs.update({'class': 'form-control'}) if request.method == 'POST': if form.is_valid(): user = form.save() login(request, user) return redirect('users_dashboard') return render(request, 'users/register.html', {'form': form})
def register_view(request): if request.method != 'POST': form = UserCreationForm() else: form = UserCreationForm(data=request.POST) if form.create(request): return redirect('home') # Render context = regular_context(request.user) context['form'] = form return render(request, 'register.html', context)
def test_user_with_email_already_exists(self): User.objects.create_user('*****@*****.**', 'user1', 'A_VERY-$trong.p@assworD') form = UserCreationForm( data={ 'email': '*****@*****.**', 'username': '******', 'password1': 'A_VERY-$trong.p@assworD', 'password2': 'A_VERY-$trong.p@assworD' }) self.assertFalse(form.is_valid()) self.assertTrue('email' not in form.clean())
def test_valid(self): """Check a valid form.""" data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'test', 'password2': 'test' } form = UserCreationForm(data) self.assertTrue(form.is_valid()) form.save() self.assertEqual(User.objects.count(), 1) self.assertEqual(Registration.objects.count(), 1) self.assertEqual(len(mail.outbox), 1)
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() user = form.instance user.email = form.cleaned_data['email'] #add user to group Users user.groups.add(Group.objects.get(name='Users')) user.save() #p = user.get_profile() p = UserProfile() p.user = user #cleaned_data value is available as unicode string p.gender = form.cleaned_data['gender'] == 'True' p.save() #login user - after saving user object, password contains just hash, #that's why we use the original password from the form user = authenticate(username=user.username, password=form.clean_password2()) if user: login(request, user) return HttpResponseRedirect(reverse('home')) else: form = UserCreationForm() return render_to_response('auth/register.html', {'form': form}, context_instance=RequestContext(request))
def create_user_account(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() primary_email = form.cleaned_data['primary_email'] password = form.cleaned_data['password1'] user = authenticate(username=primary_email, password=password) login(request, user) return redirect('/') else: form = UserCreationForm() return render(request, 'users/create_user_account.html', {'form': form})
def register(request): """ Shows and processes the registration form """ if request.method == 'POST': registration_form = UserCreationForm(request.POST) if registration_form.is_valid(): registration_form.save() return HttpResponseRedirect(reverse('login')) else: registration_form = UserCreationForm() return render(request, 'users/register.html', {'registration_form': registration_form})
def add(request): error = False if request.method == "POST": form = UserCreationForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] User.objects.create_user(username, email, password, first_name=first_name, last_name=last_name) messages.success(request, 'Account created. You can now log in.') return render(request, 'global/index.html', locals()) else: form = UserCreationForm() return render(request, 'users/add.html', locals())
def user_new(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save(verify_email_address=True, request=request) messages.add_message(request, messages.INFO, _("An email has been sent to you. Please " "check it to veiry your email.")) return HttpResponseRedirect('/') else: form = UserCreationForm() context = { 'form': form, } return render_to_response('accounts/user_new.html', context, context_instance=RequestContext(request))
def setUp(self): self.test_user = User.objects.create( username='******', email='*****@*****.**', password='******' ) self.bad_form = UserCreationForm({ 'username': '******', 'password1': 'password', 'password2': 'password', }) self.good_form = UserCreationForm({ 'username': '******', 'password1': 'password', 'password2': 'password', })
def test_creating_user(self): form = UserCreationForm(self.valid_data) self.assertIsInstance(form.save(), User)