Login

Registration with confirmation email

Author:
catellar
Posted:
August 24, 2011
Language:
Python
Version:
1.3
Score:
0 (after 2 ratings)

This registers a users taking data from a submitted form, sends a confirmation email, activates the account when the confirmation link is clicked and logs the user in

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def register(request):
	if request.method == 'POST':
		captcha_error = ""
		captcha_response = captcha.submit(
		request.POST.get("recaptcha_challenge_field", None),
		request.POST.get("recaptcha_response_field", None),
		settings.RECAPTCHA_PRIVATE_KEY,
		request.META.get("REMOTE_ADDR", None))
		if not captcha_response.is_valid:
			captcha_error = "&error=%s" % captcha_response.error_code
			c = {}
			c.update(csrf(request))
			c['repetir'] = True
			c['header'] = "register"
			return render_to_response('register.html', c, context_instance=RequestContext(request))
		else:
			if error_register(request):
				c = {}
				c.update(csrf(request))
				c['repetir'] = True
				c['header'] = "register"
				return render_to_response('register.html', c, context_instance=RequestContext(request))
			else:
				username = clean_username(request.POST['user'])
				password = request.POST['password']
				email = request.POST['email']
				user = User.objects.create_user(username, email, password)
				user.is_active = False
				user.save()
				confirmation_code = ''.join(tehrandom.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(33))
				p = Profile(user=user, confirmation_code=confirmation_code)
				p.save()
				send_registration_confirmation(user)
				return HttpResponseRedirect('../../../../../')
	else:
		c = create_c(request)
		c['header'] = "register"
		return render_to_response('register.html', c, context_instance=RequestContext(request))

def send_registration_confirmation(user):
	p = user.get_profile()
	title = "Gsick account confirmation"
	content = "http://www.gsick.com/confirm/" + str(p.confirmation_code) + "/" + user.username
	send_mail(title, content, '[email protected]', [user.email], fail_silently=False)

def confirm(request, confirmation_code, username):
	try:
		user = User.objects.get(username=username)
		profile = user.get_profile()
		if profile.confirmation_code == confirmation_code and user.date_joined > (datetime.datetime.now()-datetime.timedelta(days=1)):
			user.is_active = True
			user.save()
			user.backend='django.contrib.auth.backends.ModelBackend' 
			auth_login(request,user)
		return HttpResponseRedirect('../../../../../')
	except:
		return HttpResponseRedirect('../../../../../')

def error_register(request):
	username = request.POST['user']
	password = request.POST['password'] 
	email = request.POST['email'] 
	if not clean_username(username):
		return True
	if username.replace(" ", "") == "" or password.replace(" ", "") == "":
		return True
	if len(username) > 15 or len(password) > 50:
		return True
	if not "@" in email:
		return True
	try:
		if User.objects.get(username=username):
			return True
	except:
		pass
		

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

haraway (on May 29, 2013):

What's creation_c ? (ok, one of your other snippet). What kind of captcha do you use ? What's tehrandom ?

#

Please login first before commenting.