Beispiel #1
0
def signup(request):
	"""
	signup page. Create a legacy account

	url : /signup/"

	templates: authopenid/signup.html, authopenid/confirm_email.txt
	"""
	action_signin = reverse('user_signin')

	next = request.GET.get('next', '')
	if not next or not is_valid_next_url(next):
		next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))

	form = RegistrationForm(initial={'next':next})
	form_signin = OpenidSigninForm(initial={'next':next})
	
	if request.POST:
		if 'blocal' in request.POST.keys():
			form = RegistrationForm(request.POST)
			if form.is_valid():
	
				next = form.cleaned_data.get('next', '')
				if not next or not is_valid_next_url(next):
					next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))
	
				user_ = User.objects.create_user( form.cleaned_data['username'],
						form.cleaned_data['email'], form.cleaned_data['password1'])
			   
				user_.backend = "django.contrib.auth.backends.ModelBackend"
				login(request, user_)
				
				# send email
				current_domain = Site.objects.get_current().domain
				subject = _("Welcome")
				message_template = loader.get_template(
						'authopenid/confirm_email.txt'
				)
				message_context = Context({ 
					'site_url': 'http://%s/' % current_domain,
					'username': form.cleaned_data['username'],
					'password': form.cleaned_data['password1'] 
				})
				message = message_template.render(message_context)
				if not settings.DEBUG:
					send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, 
							  [user_.email])
				
				return HttpResponseRedirect(next)
		
		elif 'bsignin' in request.POST.keys(): 
			# This chunk of code is repeated in signin() above
			# We can either leave this here, or refactor it to one access path as it was previously, 
			# but that would mean jumping all around the place (in views.py) just to trace the execution path.
			# So I'm inclined to have some duplicated code, but have a clearer execution path
			form_signin = OpenidSigninForm(request, request.POST)
			if form_signin.is_valid():
				next = form_signin.cleaned_data['next']
				if not next:
					next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))
				
				sreg_req = sreg.SRegRequest(optional=['nickname', 'email', 'language', 'country', 'timezone', 'fullname'])
				redirect_to = "%s%s?%s" % (
						get_url_host(request),
						reverse('user_complete_signin'), 
						urllib.urlencode({'next':next})
				)
				
				return HttpResponseRedirect(form_signin.get_sreg_redirect(sreg_req, redirect_to))			
	
	return render('authopenid/signup.html', {
		'regform': form,
		'oidform': form_signin,
		'action': request.path,
		'action_signin': action_signin,
		}, context_instance=RequestContext(request))
Beispiel #2
0
def signin(request):
	"""
	signin page. It manage the legacy authentification (user/password) 
	and authentification with openid.

	url: /signin/
	
	template : authopenid/signin.htm
	"""

	on_failure = signin_failure
	next = ''


	if request.GET.get('next') and is_valid_next_url(request.GET['next']):
		next = request.GET.get('next', '').strip()
	if not next or not is_valid_next_url(next):
		next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))

	form_signin = OpenidSigninForm(request, initial={'next':next})
	form_auth = OpenidAuthForm(initial={'next':next})

	if request.POST:   
		if 'bsignin' in request.POST.keys():
			form_signin = OpenidSigninForm(request, request.POST)
			if form_signin.is_valid():
				next = form_signin.cleaned_data['next']
				if not next:
					next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))
				
				sreg_req = sreg.SRegRequest(optional=['nickname', 'email', 'language', 'country', 'timezone', 'fullname'])
				redirect_to = "%s%s?%s" % (
						get_url_host(request),
						reverse('user_complete_signin'), 
						urllib.urlencode({'next':next})
				)
				
				return HttpResponseRedirect(form_signin.get_sreg_redirect(sreg_req, redirect_to))

		elif 'blogin' in request.POST.keys():
			# perform normal django authentification
			form_auth = OpenidAuthForm(request.POST)
			if form_auth.is_valid():
				user_ = form_auth.get_user()
				login(request, user_)

				next = form_auth.cleaned_data['next']
				if not next:
					next = getattr(settings, 'OPENID_REDIRECT_NEXT', reverse('library'))
					
				if request.is_ajax():
					return HttpResponse(json.dumps({'userid':user_.id, 'api_key': APIKey.objects.get(user=user_).key}))
				
				return HttpResponseRedirect(next)

			else:
				if request.is_ajax():
					return HttpResponseForbidden()

	return render('authopenid/signin.html', {
		'lform': form_auth,
		'oidform': form_signin,
		'action': request.path,
		'msg':	request.GET.get('msg',''),
		'signin_page': True,
		'sendpw_url': reverse('user_sendpw'),
	}, context_instance=RequestContext(request))