Collecting extra user data in the pipeline

735 views
Skip to first unread message

Odahi

unread,
Apr 11, 2013, 9:48:31 AM4/11/13
to django-so...@googlegroups.com

Hello, I'm using DSA for registering users via Facebook, and that is working OK. 

My doubt is how to collect extra data for new users:

 1. How to detect a new user?
 2. Where to store the collected data (in the Django session or pass it through pipeline **kwargs)?


My pipeline looks like:


SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.misc.save_status_to_session',
    ## My customs ...
    'myapp.pipeline.load_data_new_user',
    'myapp.pipeline.handle_new_user',
    'myapp.pipeline.username',
    ##
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
)    
 


The first custom function just collect the Facebook profile picture:
 

def load_data_new_user(backend, response, user, *args, **kwargs):
    if user is None:
        if backend.name == "facebook":
            try:
                url = "http://graph.facebook.com/%s/picture?width=200&height=200&redirect=false" % response['id']
                data = json.loads(urllib2.urlopen(url).read())['data']
                return {'avatar': data}
            except StandardError:
                return {'avatar': None}
        else:
            raise ValueError()



My doubts:

 1. I'm checking if user is None for detecting new users (not sure if it's OK to assume that). 
 2. I'm storing the avatar metadata in the pipeline's **kwargs instead of use sessions, is it OK? When should I use session.


The other custom functions are based on the Matias Aguirre example, and use sessions 
for storing the username of new users.


def handle_new_user(request, user, *args, **kwargs):
    if user is None and not request.session.get('saved_username'):
        return HttpResponseRedirect('/form/')


def username(request, user, *args, **kwargs):
    if user is not None:
        username = user.username
    else:
        username = request.session.get('saved_username')
    print username
    return {'username': username} 


So, I'm not sure when to use sessions or the "correct idiom" for resolve my problem.
Thanks in advance.





Matías Aguirre

unread,
Apr 11, 2013, 12:29:42 PM4/11/13
to django-social-auth
Hi Odahi,

Collecting the data is easy as you saw, but where to put it depends on your
project, what you plan to do with the data, and where you expect it to be
available. For example, if you plan to put the avatar image on a user profile
model, then you should get the profile for the current user and store it there,
if you use a custom user with an avatar_url field, then you should fill that
field after the user was created, if you plan to download the image and store
it local to the server using the username (id, etc) as the filename, then just
do it that way too.

The session is usually used as a mechanism of communication with a view.

Regarding the "user is new", there's a flag "is_new" set to True/False when the
user is new or not, but it's not updated until the "create_user" is called.

The pipeline entry "save_status_to_session" should be placed before the method
that breaks the pipeline flow, in your case handle_new_user.

Hope this answers your questions,
Matías

Excerpts from Odahi's message of 2013-04-11 10:48:31 -0300:
--
Matías Aguirre (matias...@gmail.com)

Odahi

unread,
Apr 11, 2013, 1:27:03 PM4/11/13
to django-so...@googlegroups.com

Thanks Matias, and yes, you've answered my question. 
The last time I used socialauth was 1 month ago and I forgot some important things.

One question more: Is SOCIAL_AUTH_NEW_USER_REDIRECT_URL really necesary?
I mean, seems that the same functionality can be done using the pipeline, 
and it's a bit confusing for me.

Many thanks.

Matías Aguirre

unread,
Apr 11, 2013, 1:35:55 PM4/11/13
to django-social-auth
That setting is not mandatory, it's a simple tweak to send the new users to
a different place, probably some landing page with details about your site, or
confirmation page where you say that an email was sent to them and the account
needs to be confirmed, etc. I don't recommended to use the pipeline for that
functionality since it breaks the pipeline flow.

Excerpts from Odahi's message of 2013-04-11 14:27:03 -0300:
> > Matías Aguirre (matias...@gmail.com <javascript:>)
> >
>
--
Matías Aguirre (matias...@gmail.com)

Boboc Sabin

unread,
Nov 25, 2013, 1:13:28 PM11/25/13
to django-so...@googlegroups.com
Hello !
Can you show a more detailed example based on Custom User Model. I have a Custom User model(subclassing AbstractUser, not AbstractBaseUser), but I don't know how to populate it with extra data.
Right now I'm getting out extra-data(faceobook) like this: 
    bday = request.user.social_auth.get(provider='facebook').extra_data['birthday']                                               
    hometown = request.user.social_auth.get(provider='facebook').extra_data['hometown']                                           
    location = request.user.social_auth.get(provider='facebook').extra_data['location']                                           
    locale = request.user.social_auth.get(provider='facebook').extra_data['locale']          
I didn;'t find much documentation based on CustomUserModel.
Thank you !

Reply all
Reply to author
Forward
0 new messages