def signup(request): if request.method == 'POST': form = EmailUserCreationForm(request.POST) if form.is_valid(): form.save() next_url = request.GET.get('next', 'shop:index') return redirect(next_url) else: form = EmailUserCreationForm() return render(request, 'accounts/signup.html', {'form':form})
def create_user(self): """ Use the form to create a user and save it to the database """ params = { 'email':'*****@*****.**', 'password1':'test', 'password2':'test', 'first_name':'Test', 'last_name':'User', 'fbid': -1 } form = EmailUserCreationForm(params) user = form.save() # Check to make sure user creation worked self.assertIsNotNone(User.objects.get(email='*****@*****.**')) return user
def test_passwords_different(self): """ Tests that different passwords will not work. """ params = { 'email':'*****@*****.**', 'password1':'blah1', 'password2':'blah', 'first_name':'blah', 'last_name':'blah', 'fbid': -1 } form = EmailUserCreationForm(params) self.assertFalse(form.is_valid(), "Mis-matched passwords should not pass!") self.assertNotIn('email', form._errors) self.assertNotIn('password1', form._errors) self.assertNotIn('first_name', form._errors) self.assertNotIn('last_name', form._errors) self.assertNotIn('fbid_name', form._errors) self.assertEqual(form._errors['password2'][0], "The two password fields didn't match.")
def test_email_invalid(self): """ Tests that an invalid email address will not work. """ params = { 'email':'blah', 'password1':'blah', 'password2':'blah', 'first_name':'blah', 'last_name':'blah', 'fbid': -1 } form = EmailUserCreationForm(params) self.assertFalse(form.is_valid(), "Invalid email should not pass!") self.assertNotIn('password1', form._errors) self.assertNotIn('password2', form._errors) self.assertNotIn('first_name', form._errors) self.assertNotIn('last_name', form._errors) self.assertNotIn('fbid_name', form._errors) self.assertEqual(form._errors['email'][0], "Enter a valid e-mail address.")
def register(request): """ Handles user registration request """ template = 'accounts/register-1.html' template_context = {} if request.user.is_authenticated(): # They are already logged on, don't let them register again return redirect('/mypage') if request.POST: template = 'accounts/register-2.html' template_context['post_request'] = True if request.POST.get('signed_request'): # Post request received from first page (through Facebook API) signed_request = request.POST.get('signed_request') data = parse_signed_request(signed_request, settings.FACEBOOK_APP_SECRET) register_info = data['registration'] if 'name' in register_info: name_parts = register_info['name'].split(u' ') template_context['firstname'] = name_parts[0] template_context['lastname'] = name_parts[len(name_parts)-1] else: template_context['firstname'] = register_info['first_name'] template_context['lastname'] = register_info['last_name'] template_context['email'] = register_info['email'] valid = True if not isUniqueEmail(template_context['email']): valid = False template_context['used_email'] = True unique_email = isUniqueEmail(template_context['email']) template_context['fbid'] = -1 if 'user_id' in data: template_context['has_fbid'] = True template_context['redir_uri'] = settings.WEB_ROOT + '/connect' template_context['fbid'] = data['user_id'] if not isUniqueFbid(template_context['fbid']): valid = False template_context['used_fbid'] = True error_msg = "That Facebook account has already been registered \ with this app!" messages.add_message(request, messages.ERROR, error_msg) elif not unique_email: error_msg = 'The email address associated with that Facebook \ account is already being used! If you want to connect it \ to an existing account, please <a href="login">log in</a> \ and go to your dashboard to do so.' messages.add_message(request, messages.ERROR, error_msg, extra_tags='safe') elif not unique_email: error_msg = 'That email address is already being used! Are you \ trying to <a href="login">log in</a>?' messages.add_message(request, messages.ERROR, error_msg, extra_tags='safe') if not valid: template = 'accounts/register-1.html' else: # Post request received from second page form = EmailUserCreationForm(request.POST) if form.is_valid(): # All validation rules pass template_context['extra'] = 'SUCCESS' # Create new user new_user = form.save(request.POST.copy()) # Build activation key username = new_user.username salt = hashlib.sha224(str(random.random())).hexdigest()[:5] activation_key = hashlib.sha1(salt+username).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) # Create and save user and profile new_profile = new_user.get_profile() new_profile.activation_key = activation_key new_profile.key_expires = key_expires new_profile.save() # Send an email with the confirmation link (disabled for now) email = new_user.email email_subject = 'Your new EventHub account confirmation' email_template = get_template('accounts/email/register.txt') context = Context({ 'email' : email, 'web_root' : settings.WEB_ROOT, 'activation_key' : activation_key }) email_body = email_template.render(context) send_mail(email_subject, email_body, '*****@*****.**', [email]) # Redirect to 'My Page' after successful registration success_msg = "You have successfully registered for an EventHub \ account! Please check your email for your activation link so \ you can start using our site." messages.add_message(request, messages.SUCCESS, success_msg) return redirect('/login') else: # Form did not validate. Assuming email has been taken while user # was still on registration page error_msg = 'The email address "' + request.POST['email'] \ + '" has been taken while you were registering. You \ may already be registered for EventHub.' messages.add_message(request, messages.ERROR, error_msg) return redirect('/register') request_context = RequestContext(request, template_context) return render_to_response(template, request_context)