def openid_registration(request, template_name='registration/registration_form.html'): registration_form = OpenIDRegistrationFormUniqueUser(request.POST or None) try: openid_response = request.session['openid_response'] except KeyError: return HttpResponseNotAllowed(_(u'No openid_response object for this session!')) # If the user is already logged on, then link this new UserOpenID to their User if request.user.is_authenticated(): for backend in get_backends(): if type(backend) == OpenIDBackend: backend.associate_openid(request.user, openid_response) # Clear the openid_response from the session so it can't be used to create another account del request.session['openid_response'] return HttpResponseRedirect(reverse('preferences')) try: attributes = auth._extract_user_details(openid_response) registration_form.fields['username'].initial = attributes['nickname'] registration_form.fields['email'].initial = attributes['email'] except KeyError: pass if registration_form.is_valid(): user = authenticate(openid_response=openid_response, username=registration_form.cleaned_data.get('username', ''), create_user=True) # Clear the openid_response from the session so it can't be used to create another account del request.session['openid_response'] if user is not None: email = registration_form.cleaned_data.get('email') if email: user.email = email if getattr(settings, 'MODERATE_NEW_USERS', False): user.is_active = False user.save() user.get_profile().save() if user.is_active: login(request, user) return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) else: if not getattr(settings, 'MODERATE_NEW_USERS', False): return HttpResponseNotAllowed(_(u'Disabled account')) else: return render_to_response("registration/moderated.html", {'user': user,}, context_instance=RequestContext(request)) else: return HttpResponseNotAllowed(_(u'Unknown user')) return render_to_response(template_name, {'form' : registration_form}, context_instance=RequestContext(request))
def openid_registration(request, template_name='registration/registration_form.html'): registration_form = OpenIDRegistrationFormUniqueUser(request.POST or None) try: openid_response = request.session['openid_response'] except KeyError: return HttpResponseNotAllowed(_(u'No openid_response object for this session!')) try: attributes = auth._extract_user_details(openid_response) registration_form.fields['username'].initial = attributes['nickname'] registration_form.fields['email'].initial = attributes['email'] except KeyError: pass if registration_form.is_valid(): user = authenticate(openid_response=openid_response, username=registration_form.cleaned_data.get('username', ''), create_user=True) # Clear the openid_response from the session so it can't be used to create another account del request.session['openid_response'] if user is not None: email = registration_form.cleaned_data.get('email') if email: user.email = email user.save() user.get_profile().save() if user.is_active: login(request, user) return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) else: return HttpResponseNotAllowed(_(u'Disabled account')) else: return HttpResponseNotAllowed(_(u'Unknown user')) return render_to_response(template_name, {'form' : registration_form}, context_instance=RequestContext(request))
def openid_registration(request, template_name='registration/registration_form.html'): registration_form = OpenIDRegistrationFormUniqueUser(request.POST or None) try: openid_response = request.session['openid_response'] except KeyError: return HttpResponseNotAllowed( _(u'No openid_response object for this session!')) # If the user is already logged on, then link this new UserOpenID to their User if request.user.is_authenticated(): for backend in get_backends(): if type(backend) == OpenIDBackend: backend.associate_openid(request.user, openid_response) # Clear the openid_response from the session so it can't be used to create another account del request.session['openid_response'] return HttpResponseRedirect(reverse('preferences')) try: attributes = auth._extract_user_details(openid_response) registration_form.fields['username'].initial = attributes['nickname'] registration_form.fields['email'].initial = attributes['email'] except KeyError: pass if registration_form.is_valid(): user = authenticate(openid_response=openid_response, username=registration_form.cleaned_data.get( 'username', ''), create_user=True) # Clear the openid_response from the session so it can't be used to create another account del request.session['openid_response'] if user is not None: email = registration_form.cleaned_data.get('email') if email: user.email = email if getattr(settings, 'MODERATE_NEW_USERS', False): user.is_active = False user.save() user.get_profile().save() if user.is_active: login(request, user) return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) else: if not getattr(settings, 'MODERATE_NEW_USERS', False): return HttpResponseNotAllowed(_(u'Disabled account')) else: return render_to_response( "registration/moderated.html", { 'user': user, }, context_instance=RequestContext(request)) else: return HttpResponseNotAllowed(_(u'Unknown user')) return render_to_response(template_name, {'form': registration_form}, context_instance=RequestContext(request))