def sign_up(request): error_message = None if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): # Create the user try: user = User.objects.create_user( form.cleaned_data['user_name'], form.cleaned_data['email'], form.cleaned_data['password']) except IntegrityError: error_message = "That username is not available." else: user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.save() # Create the user profile user_profile = UserProfile( user=user, tshirt=form.cleaned_data['tshirt'], diet=form.cleaned_data['diet'], description=form.cleaned_data['description'], notify_by_email=form.cleaned_data['notify_by_email']) user_profile.save() # Log the user in user = authenticate(username=user.username, password=form.cleaned_data['password']) login(request, user) return HttpResponseRedirect(reverse('users-profile', args=[user.username])) # Redirect after POST else: form = SignUpForm() env = common_env() env['form'] = form env['error_message'] = error_message return render(request, 'users/signup.html', env)
def test_image_url_generation(self): profile = UserProfile(user=User(email="*****@*****.**")) assert profile.image == ("http://gravatar.com/avatar/" "55502f40dc8b7c769880b10874abc9d0")
def sign_up(request): error_message = None backends = settings.AUTHENTICATION_BACKENDS form_class = SignUpForm create_user = True # the AD backend makes users on login and fills in name and email for us # so we don't need those fields on the form if 'hackday.auth.backend.ActiveDirectoryBackend' in backends: form_class = ActiveDirectorySignUpForm create_user = False if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): if create_user: # Create the user try: user = User.objects.create_user( form.cleaned_data['user_name'], form.cleaned_data['email'], form.cleaned_data['password']) except IntegrityError: error_message = "That username is not available." else: user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.save() else: user = authenticate(username=form.cleaned_data['user_name'], password=form.cleaned_data['password']) if not user: error_message = "Invalid username or password." if not error_message: # Create the user profile user_profile = UserProfile( user=user, alternate_email=form.cleaned_data['alternate_email'], phone=form.cleaned_data['phone'], tshirt=form.cleaned_data['tshirt'], diet=form.cleaned_data['diet'], location=form.cleaned_data['location'], description=form.cleaned_data['description'], notify_by_email=form.cleaned_data['notify_by_email'], dinner_required=form.cleaned_data['dinner_required'], breakfast_required=form.cleaned_data['breakfast_required']) try: user_profile.save() except IntegrityError: error_message = "That user has already registered." else: # Log the user in user = authenticate(username=user.username, password=form.cleaned_data['password']) login(request, user) return HttpResponseRedirect(reverse('users-profile', args=[user.username])) # Redirect after POST else: form = form_class() env = common_env() env['ldap_enabled'] = not create_user env['form'] = form env['error_message'] = error_message return render(request, 'users/signup.html', env)