def create_user(self, profile): if LINKED_ACCOUNTS_EMAIL_ASSOCIATION and profile.email: users = list(User.objects.filter(email=profile.email)) if users and len(users) == 1: user = users[0] profile.user = user profile.save() create_email(user) if LINKED_ACCOUNTS_ALLOW_LOGIN: self.login(profile) return self.success(profile) if LINKED_ACCOUNTS_AUTO_REGISTRATION: return self.auto_registration(profile) else: self.request.session[LINKED_ACCOUNTS_ID_SESSION] = profile.id return redirect( reverse('linked_accounts_register') + "?next=%s" % self.get_next_url() )
def auto_registration(self, profile): #no match, create a new user - but there may be duplicate user names. nickname = profile.username username = nickname user = None try: i=0 while True: User.objects.get(username=username) username=permute_name(nickname, i) i+=1 except User.DoesNotExist: #available name! user = User.objects.create_user(username, profile.email or '') create_email(user) profile.user = user profile.save() if LINKED_ACCOUNTS_ALLOW_LOGIN: self.login(profile) return self.success(profile)
def register(request, form_class=RegisterForm, template_name="linked_accounts/registration.html"): if not LINKED_ACCOUNTS_ALLOW_REGISTRATION: return redirect('linked_accounts_registration_closed') next_url = request.REQUEST.get('next', settings.LOGIN_REDIRECT_URL) try: profile_id = request.session[LINKED_ACCOUNTS_ID_SESSION] profile = LinkedAccount.objects.get(id=profile_id) except (KeyError, LinkedAccount.DoesNotExist): return redirect(next_url) initial_data = { 'username': profile.username } email = profile.email if email: initial_data['email'] = email if request.method == "POST": form = form_class(request.POST) if form.is_valid(): user = form.save(profile) create_email(user) user.backend = "linked_accounts.backends.LinkedAccountsBackend" auth.login(request, user) return redirect(next_url) else: form = form_class(initial=initial_data) return render( request, template_name, {'form': form, 'profile': profile, 'next': next_url} )