regluit/core/auth.py

62 lines
2.1 KiB
Python
Raw Normal View History

2013-03-18 18:56:27 +00:00
import logging
2013-06-03 16:31:39 +00:00
2015-04-08 02:55:30 +00:00
from social.pipeline.social_auth import (
social_user,
2013-06-03 16:31:39 +00:00
load_extra_data
)
2015-04-08 02:55:30 +00:00
from social.apps.django_app.default.models import UserSocialAuth
2013-06-03 16:31:39 +00:00
2013-03-18 18:56:27 +00:00
from regluit.core.models import TWITTER, FACEBOOK
logger = logging.getLogger(__name__)
def selectively_associate(backend, uid, user=None, *args, **kwargs):
"""Not using Facebook or Twitter to authenticate a user.
"""
2015-04-08 02:55:30 +00:00
logger.info('selectively_associate')
social_auth = UserSocialAuth.get_social_auth(backend.name, uid)
2013-03-18 18:56:27 +00:00
if backend.name in ('twitter', 'facebook'):
# not for authentication
2015-04-08 02:55:30 +00:00
return {'social_user': social_auth}
return social_user(backend, uid, user=None, *args, **kwargs)
2013-03-18 18:56:27 +00:00
def facebook_extra_values( user, extra_data):
try:
facebook_id = extra_data.get('id')
user.profile.facebook_id = facebook_id
if user.profile.avatar_source is None:
user.profile.avatar_source = FACEBOOK
user.profile.save()
return True
except Exception,e:
logger.error(e)
return False
def twitter_extra_values( user, extra_data):
try:
twitter_id = extra_data.get('screen_name')
profile_image_url = extra_data.get('profile_image_url_https')
user.profile.twitter_id = twitter_id
if user.profile.avatar_source is None or user.profile.avatar_source is TWITTER:
user.profile.pic_url = profile_image_url
if user.profile.avatar_source is None:
user.profile.avatar_source = TWITTER
user.profile.save()
return True
except Exception,e:
logger.error(e)
return False
def deliver_extra_data(backend, details, response, uid, user, social_user=None,
*args, **kwargs):
2015-04-08 02:55:30 +00:00
logger.info('deliver_extra_data')
2013-03-18 18:56:27 +00:00
pipeline_data = load_extra_data(backend, details, response, uid, user, social_user=None,
*args, **kwargs)
if backend.name is 'twitter':
twitter_extra_values( user, social_user.extra_data)
if backend.name is 'facebook':
facebook_extra_values( user, social_user.extra_data)