def changeopenid_success(request, identity_url, openid_response): openid_ = from_openid_response(openid_response) is_exist = True try: uassoc = UserAssociation.objects.get(openid_url__exact=identity_url) except: is_exist = False if not is_exist: try: uassoc = UserAssociation.objects.get( user__username__exact=request.user.username ) uassoc.openid_url = identity_url uassoc.save() except: uassoc = UserAssociation(user=request.user, openid_url=identity_url) uassoc.save() elif uassoc.user.username != request.user.username: return changeopenid_failure(request, _('This openid is already associated with another account.')) request.session['openids'] = [] request.session['openids'].append(openid_) msg = _("Openid %s associated with your account." % identity_url) redirect = "%s?msg=%s" % ( reverse('user_account_settings'), urlquote_plus(msg)) return HttpResponseRedirect(redirect)
def register(request): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. url : /complete/ template : authopenid/complete.html """ is_redirect = False next = request.GET.get('next', '').strip() if not next or not is_valid_next_url(next): next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/') openid_ = request.session.get('openid', None) if not openid_: return HttpResponseRedirect(reverse('user_signin') + next) nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') form1 = OpenidRegisterForm(initial={ 'next': next, 'username': nickname, 'email': email, }) form2 = OpenidVerifyForm(initial={ 'next': next, 'username': nickname, }) if request.POST: just_completed = False if 'bnewaccount' in request.POST.keys(): form1 = OpenidRegisterForm(request.POST) if form1.is_valid(): next = form1.cleaned_data['next'] if not next: next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/') is_redirect = True tmp_pwd = User.objects.make_random_password() user_ = User.objects.create_user(form1.cleaned_data['username'], form1.cleaned_data['email'], tmp_pwd) # make association with openid uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() # login user_.backend = "django.contrib.auth.backends.ModelBackend" login(request, user_) elif 'bverify' in request.POST.keys(): form2 = OpenidVerifyForm(request.POST) if form2.is_valid(): is_redirect = True next = form2.cleaned_data['next'] if not next: next = getattr(settings, 'OPENID_REDIRECT_NEXT', '/') user_ = form2.get_user() uassoc = UserAssociation(openid_url=str(openid_), user_id=user_.id) uassoc.save() login(request, user_) # redirect, can redirect only if forms are valid. if is_redirect: return HttpResponseRedirect(next) return render('authopenid/complete.html', { 'form1': form1, 'form2': form2, 'action': reverse('user_register'), 'nickname': nickname, 'email': email }, context_instance=RequestContext(request))