示例#1
0
def register(request):
	
	context_dict = {}
	context_dict['registered'] = False
	
	if request.method == 'POST':
		user_form = UserForm(data=request.POST)
		profile_form = ProfileForm(data=request.POST)
		
		if user_form.is_valid() and profile_form.is_valid():
			user = user_form.save()

			username = user_form.cleaned_data['username']
			email = user_form.cleaned_data['email']


			salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
			account_deletion_key = hashlib.sha1(salt+email).hexdigest()   
			
			#user.set_password(user.password)
			user.save()

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

			if 'picture' in request.FILES:
				profile.picture = request.FILES['picture']

			profile.save()
			context_dict['registered'] = True


			email_subject = 'From Simplenation, with love'
			email_body = "Dear %s, thanks for signing up. Welcome! \n\n We do NOT do email confirmation \n\n But you can DELETE your account HERE: http://dev.djangobook.com/simplenation/account_deletion/%s" % (username, account_deletion_key)
			send_mail(email_subject, email_body, '*****@*****.**',[email], fail_silently=False)

			new_user = authenticate(username = request.POST['username'], password = request.POST['password1'])
			login(request, new_user)

			if request.POST["next"] is not "":
				return HttpResponseRedirect(request.POST["next"])
			else:
				return HttpResponseRedirect('/simplenation/')
			#return HttpResponseRedirect('/simplenation/')
		else:
			print user_form.errors, profile_form.errors

	else:
		user_form = UserForm()
		profile_form = ProfileForm()

	context_dict['next'] = request.GET.get('next', '')
	context_dict['user_form'] = user_form
	context_dict['profile_form'] = profile_form

	return render(request, 'simplenation/registration_form.html', context_dict)
示例#2
0
def register_user_while_post(request):
	response = {}
	response['success'] = False
	
	if request.method == 'POST':
		user_form = UserForm(data=request.POST)
		profile_form = ProfileForm(data=request.POST)

		if not 'username' in request.POST:
			response['error_message'] = "Please enter username."
			response['user_form'] = user_form
			response['profile_form'] = profile_form
			return response

		if not 'email' in request.POST:
			response['error_message'] = "Please enter email."
			response['user_form'] = user_form
			response['profile_form'] = profile_form
			return response

		if not 'password1' in request.POST:
			response['error_message'] = "Please enter password."
			response['user_form'] = user_form
			response['profile_form'] = profile_form
			return response

		email_for_validation = request.POST['email']
		if not simplenation_email_validation(email_for_validation):
			response['error_message'] = "Please enter correct email."
			response['user_form'] = user_form
			response['profile_form'] = profile_form
			return response

		username_for_validation = request.POST['username']
		if not simplenation_username_validation(username_for_validation):
			response['error_message'] = "Username is a bit invalid, try something awesome like "+"'"+random.choice(awesomeUsernames)+"'"
			response['user_form'] = user_form
			response['profile_form'] = profile_form
			return response

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

			username = user_form.cleaned_data['username']
			email = user_form.cleaned_data['email']


			salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
			account_deletion_key = hashlib.sha1(salt+email).hexdigest()   
			
			user.save()

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

			if 'picture' in request.FILES:
				profile.picture = request.FILES['picture']

			profile.save()
			

			email_data = {
				'email': email,
				'domain': request.META['HTTP_HOST'],
				'site_name': SITE_NAME,
				'account_deletion_key': account_deletion_key,
				'receiver_username': username,
				'site_email': EMAIL_HOST_USER,
				'protocol': 'http',
			}
			subject_template_name='simplenation/registration_notification_subject.txt'
			email_template_name='simplenation/registration_notification_email.html'
			
			if not send_email(email_data, subject_template_name, email_template_name):
				response['error_message'] = "Couldn't send confirmation email."
				response['user_form'] = user_form
				response['profile_form'] = profile_form
				return response

			new_user = authenticate(username = request.POST['username'], password = request.POST['password1'])
			login(request, new_user)
			response['success'] = True
			response['user'] = new_user
			
		else:
			response['error_message'] = None

	else:
		user_form = UserForm()
		profile_form = ProfileForm()

	response['user_form'] = user_form
	response['profile_form'] = profile_form

	return response