def save(self, request=None):
     self.user.email = self.cleaned_data.get('email')
     self.user.save()
     self.profile.user = self.user
     self.profile.save()
     socialregistrationuser_created.send( 
                     sender=None, 
                     user=self.user, 
                     profile=self.profile, 
                     request=request)
     return self.user
def facebook_setup(request, template='socialregistration/facebook_setup.html',
    form_class=FacebookUserForm, extra_context=dict()):
    """
    Setup view to create a username & set email address after authentication
    Clone of setup, special to do facebook stuff

    TODO:
        need to handle this scenario :
            - user logged in with facebook, but the email already exists - somewhat handled correctly right now,
              although not very convenient
    """
    try:
        social_user = request.session['socialregistration_user']
        social_profile = request.session['socialregistration_profile']
    except KeyError:
        return render_to_response(
            template, dict(error=True), context_instance=RequestContext(request))

    if not GENERATE_USERNAME or (not social_user.email):
        # Should go here so user can pick their email
        if not request.method == "POST":
            form = form_class(social_user, social_profile)
        else:
            social_user.username = generate_unique_username()
            form = form_class(social_user, social_profile, request.POST)

            if form.is_valid():
                form.save(request=request)
                user = form.profile.authenticate()
                login(request, user)

                signup_code = form.cleaned_data.get("signup_code", None)
                if type(signup_code) == type(SignupCode()):
                    signup_code.use(user)

                del request.session['socialregistration_user']
                del request.session['socialregistration_profile']

                return HttpResponseRedirect(_get_next(request))

        extra_context.update(dict(form=form))
        extra_context['user']=social_user
        extra_context['original_email']=request.session['socialregistration_email']

        return render_to_response(template, extra_context,
            context_instance=RequestContext(request))

    else:
        # Generate user and profile
        social_user.username = generate_unique_username()
        social_user.save()

        social_profile.user = social_user
        social_profile.save()

        socialregistrationuser_created.send( 
                        sender=None, 
                        user=social_user, 
                        profile=social_profile, 
                        request=request)


        # Authenticate and login
        user = social_profile.authenticate()
        login(request, user)

        # Clear & Redirect
        del request.session['socialregistration_user']
        del request.session['socialregistration_profile']
        del request.session['socialregistration_email']
        return HttpResponseRedirect(_get_next(request))