Exemple #1
0
def _update_user(user, facebook):
    '''
    Updates the user and his/her profile with the data from facebook
    '''
    #if you want to add fields to ur user model instead of the profile thats fine
    #partial support (everything except raw_data and facebook_id is included)
    facebook_data = facebook.facebook_registration_data()
    facebook_fields = [
        'facebook_name', 'facebook_profile_url', 'date_of_birth', 'about_me',
        'website_url', 'first_name', 'last_name'
    ]
    user_dirty = profile_dirty = False
    profile = user.get_profile()

    signals.facebook_pre_update.send(sender=get_profile_class(),
                                     profile=profile,
                                     facebook_data=facebook_data)

    profile_field_names = [f.name for f in profile._meta.fields]
    user_field_names = [f.name for f in user._meta.fields]

    #set the facebook id and make sure we are the only user with this id
    if facebook_data['facebook_id'] != profile.facebook_id:
        profile.facebook_id = facebook_data['facebook_id']
        profile_dirty = True
        #like i said, me and only me
        profile_class = get_profile_class()
        profile_class.objects.filter(facebook_id=profile.facebook_id)\
                             .exclude(user__id=user.id)\
                             .update(facebook_id=None)

    #update all fields on both user and profile
    for f in facebook_fields:
        facebook_value = facebook_data.get(f, False)
        if facebook_value:
            if f in profile_field_names and not getattr(profile, f, False):
                setattr(profile, f, facebook_value)
                profile_dirty = True
            elif f in user_field_names and not getattr(user, f, False):
                setattr(user, f, facebook_value)
                user_dirty = True

    #write the raw data in case we missed something
    if hasattr(profile, 'raw_data'):
        serialized_fb_data = json.dumps(facebook.facebook_profile_data())
        profile.raw_data = serialized_fb_data
        profile_dirty = True

    #save both models if they changed
    if user_dirty:
        user.save()
    if profile_dirty:
        profile.save()

    signals.facebook_post_update.send(sender=get_profile_class(),
                                      profile=profile,
                                      facebook_data=facebook_data)

    return user
Exemple #2
0
def _update_user(user, facebook):
    '''
    Updates the user and his/her profile with the data from facebook
    '''
    #if you want to add fields to ur user model instead of the profile thats fine
    #partial support (everything except raw_data and facebook_id is included)
    facebook_data = facebook.facebook_registration_data(username=False)
    facebook_fields = ['facebook_name', 'facebook_profile_url',
        'date_of_birth', 'about_me', 'website_url', 'first_name', 'last_name']
    user_dirty = profile_dirty = False
    profile = user.get_profile()
    
    signals.facebook_pre_update.send(sender=get_profile_class(),
        profile=profile, facebook_data=facebook_data)
    
    profile_field_names = [f.name for f in profile._meta.fields]
    user_field_names = [f.name for f in user._meta.fields]

    #set the facebook id and make sure we are the only user with this id
    if facebook_data['facebook_id'] != profile.facebook_id:
        logger.info('profile facebook id changed from %s to %s', repr(facebook_data['facebook_id']), repr(profile.facebook_id))
        profile.facebook_id = facebook_data['facebook_id']
        profile_dirty = True
        _remove_old_connections(profile.facebook_id, user.id)

    #update all fields on both user and profile
    for f in facebook_fields:
        facebook_value = facebook_data.get(f, False)
        if facebook_value:
            if f in profile_field_names and hasattr(profile, f) and not getattr(profile, f, False):
                logger.debug('profile field %s changed from %s to %s', f, getattr(profile, f), facebook_value)
                setattr(profile, f, facebook_value)
                profile_dirty = True
            elif f in user_field_names and hasattr(user, f) and not getattr(user, f, False):
                logger.debug('user field %s changed from %s to %s', f, getattr(user, f), facebook_value)
                setattr(user, f, facebook_value)
                user_dirty = True

    #write the raw data in case we missed something
    if hasattr(profile, 'raw_data'):
        serialized_fb_data = json.dumps(facebook.facebook_profile_data())
        if profile.raw_data != serialized_fb_data:
            logger.debug('profile raw data changed from %s to %s', profile.raw_data, serialized_fb_data)
            profile.raw_data = serialized_fb_data
            profile_dirty = True

    #save both models if they changed
    if user_dirty:
        user.save()
    if profile_dirty:
        profile.save()

    signals.facebook_post_update.send(sender=get_profile_class(),
        profile=profile, facebook_data=facebook_data)

    return user
def _update_user(user, facebook):
    '''
    Updates the user and his/her profile with the data from facebook
    '''
    #if you want to add fields to ur user model instead of the profile thats fine
    #partial support (everything except raw_data and facebook_id is included)
    facebook_data = facebook.facebook_registration_data()
    facebook_fields = ['facebook_name', 'facebook_profile_url',
        'date_of_birth', 'about_me', 'website_url', 'first_name', 'last_name']
    user_dirty = profile_dirty = False
    profile = user.get_profile()
    
    signals.facebook_pre_update.send(sender=get_profile_class(),
        profile=profile, facebook_data=facebook_data)
    
    profile_field_names = [f.name for f in profile._meta.fields]
    user_field_names = [f.name for f in user._meta.fields]

    #set the facebook id and make sure we are the only user with this id
    if facebook_data['facebook_id'] != profile.facebook_id:
        profile.facebook_id = facebook_data['facebook_id']
        profile_dirty = True
        #like i said, me and only me
        profile_class = get_profile_class()
        profile_class.objects.filter(facebook_id=profile.facebook_id)\
                             .exclude(user__id=user.id)\
                             .update(facebook_id=None)

    #update all fields on both user and profile
    for f in facebook_fields:
        facebook_value = facebook_data.get(f, False)
        if facebook_value:
            if f in profile_field_names and not getattr(profile, f, False):
                setattr(profile, f, facebook_value)
                profile_dirty = True
            elif f in user_field_names and not getattr(user, f, False):
                setattr(user, f, facebook_value)
                user_dirty = True

    #write the raw data in case we missed something
    if hasattr(profile, 'raw_data'):
        serialized_fb_data = json.dumps(facebook.facebook_profile_data())
        profile.raw_data = serialized_fb_data
        profile_dirty = True

    #save both models if they changed
    if user_dirty:
        user.save()
    if profile_dirty:
        profile.save()

    signals.facebook_post_update.send(sender=get_profile_class(),
        profile=profile, facebook_data=facebook_data)

    return user
Exemple #4
0
def _register_user(request, facebook, profile_callback=None):
    '''
    Creates a new user and authenticates
    The registration form handles the registration and validation
    Other data on the user profile is updates afterwards
    '''
    if not facebook.is_authenticated():
        raise ValueError(
            'Facebook needs to be authenticated for connect flows')

    #get the backend on new registration systems, or none if we are on an older version
    backend = get_registration_backend()

    #gets the form class specified in FACEBOOK_REGISTRATION_FORM
    form_class = get_form_class(backend, request)

    facebook_data = facebook.facebook_registration_data()

    data = request.POST.copy()
    for k, v in facebook_data.items():
        if not data.get(k):
            data[k] = v

    if request.REQUEST.get('force_registration_hard'):
        data['email'] = data['email'].replace('@', '+%s@' % randint(0, 100000))

    form = form_class(data=data,
                      files=request.FILES,
                      initial={'ip': request.META['REMOTE_ADDR']})

    if not form.is_valid():
        error = facebook_exceptions.IncompleteProfileError(
            'Facebook data %s '
            'gave error %s' % (facebook_data, form.errors))
        error.form = form
        raise error

    #for new registration systems use the backends methods of saving
    if backend:
        new_user = backend.register(request, **form.cleaned_data)
    else:
        # For backward compatibility, if django-registration form is used
        try:
            new_user = form.save(profile_callback=profile_callback)
        except TypeError:
            new_user = form.save()

    signals.facebook_user_registered.send(sender=get_profile_class(),
                                          user=new_user,
                                          facebook_data=facebook_data)

    #update some extra data not yet done by the form
    new_user = _update_user(new_user, facebook)

    # IS this the correct way for django 1.3? seems to require the backend
    # attribute for some reason
    new_user.backend = 'django_facebook.auth_backends.FacebookBackend'
    auth.login(request, new_user)

    return new_user
def _register_user(request, facebook, profile_callback=None):
    '''
    Creates a new user and authenticates
    The registration form handles the registration and validation
    Other data on the user profile is updates afterwards
    '''
    if not facebook.is_authenticated():
        raise ValueError('Facebook needs to be authenticated for connect flows')
    
    
    
    #get the backend on new registration systems, or none if we are on an older version
    backend = get_registration_backend()

    #gets the form class specified in FACEBOOK_REGISTRATION_FORM
    form_class = get_form_class(backend, request)

    facebook_data = facebook.facebook_registration_data()

    data = request.POST.copy()
    for k, v in facebook_data.items():
        if not data.get(k):
            data[k] = v

    if request.REQUEST.get('force_registration_hard'):
        data['email'] = data['email'].replace('@', '+%s@' % randint(0, 100000))

    form = form_class(data=data, files=request.FILES,
        initial={'ip': request.META['REMOTE_ADDR']})

    if not form.is_valid():
        error = facebook_exceptions.IncompleteProfileError('Facebook data %s '
            'gave error %s' % (facebook_data, form.errors))
        error.form = form
        raise error

    #for new registration systems use the backends methods of saving
    if backend:
        new_user = backend.register(request, **form.cleaned_data)
    else:
        # For backward compatibility, if django-registration form is used
        try:
            new_user = form.save(profile_callback=profile_callback)
        except TypeError:
            new_user = form.save()
    
    signals.facebook_user_registered.send(sender=get_profile_class(),
        user=new_user, facebook_data=facebook_data)
    
    #update some extra data not yet done by the form
    new_user = _update_user(new_user, facebook)

    # IS this the correct way for django 1.3? seems to require the backend
    # attribute for some reason
    new_user.backend = 'django_facebook.auth_backends.FacebookBackend'
    auth.login(request, new_user)

    return new_user
Exemple #6
0
def _remove_old_connections(facebook_id, current_user_id=None):
    '''
    Removes the facebook id for profiles with the specified facebook id
    which arent the current user id
    '''
    profile_class = get_profile_class()
    other_facebook_accounts = profile_class.objects.filter(facebook_id=facebook_id)
    if current_user_id:
        other_facebook_accounts = other_facebook_accounts.exclude(user__id=current_user_id)
    updated = other_facebook_accounts.update(facebook_id=None)