本文整理汇总了Python中social_auth.models.UserSocialAuth类的典型用法代码示例。如果您正苦于以下问题:Python UserSocialAuth类的具体用法?Python UserSocialAuth怎么用?Python UserSocialAuth使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserSocialAuth类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vkontakte_view
def vkontakte_view(request, *args, **kwargs):
if request.method == 'POST':
user = UserSocialAuth.create_user(username=request.POST['uid'])
user.first_name = request.POST['firstname']
user.last_name = request.POST['lastname']
user.backend = 'django.contrib.auth.backends.ModelBackend'
user.save()
social = UserSocialAuth.create_social_auth(user, user.username, 'vkontakte')
request.session['social_auth_last_login_backend'] = social.provider
login(request, user)
else:
try:
social_user = UserSocialAuth.objects.get(user__username=request.GET['viewer_id'])
social_user.user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, social_user.user)
except UserSocialAuth.DoesNotExist:
return render_to_response('vkontakte_auth.html', RequestContext(request))
return render_to_response('vkontakte_app.html',
RequestContext(request))
示例2: create_user
def create_user(backend, details, response, uid, username, user=None, *args,
**kwargs):
"""Create user. Depends on get_username pipeline."""
if user:
return {'user': user}
if not username:
return None
# Avoid hitting field max length
email = details.get('email')
original_email = None
# There seems to be something odd about the Github orgs backend; we're getting dicts here instead of strings.
if isinstance(email, dict):
email = email.get('email', None)
if email and UserSocialAuth.email_max_length() < len(email):
original_email = email
email = ''
return {
'user': UserSocialAuth.create_user(username=username, email=email),
'original_email': original_email,
'is_new': True
}
示例3: create_user
def create_user(backend, details, response, uid, username, user=None,
*args, **kwargs):
"""Create user. Depends on get_username pipeline."""
if user:
return {'user': user}
if not username:
return None
# Avoid hitting field max length
email = details.get('email')
original_email = None
if not email:
message = _("""your social account needs to have a verified email address in order to proceed.""")
raise AuthFailed(backend, message)
if email and UserSocialAuth.email_max_length() < len(email):
original_email = email
email = ''
return {
'user': UserSocialAuth.create_user(username=username, email=email,
sync_emailaddress=False),
'original_email': original_email,
'is_new': True
}
示例4: create_user
def create_user(backend, details, response, uid, username, user=None, *args,
**kwargs):
"""Create user. Depends on get_username pipeline."""
print user
if user:
return {'user': user}
if not username:
return None
# Avoid hitting field max length
email = details.get('email')
original_email = None
if type(email) is dict and email.get('email'):
email = email.get('email')
details['email'] = email
if email and UserSocialAuth.email_max_length() < len(email):
original_email = email
email = ''
return {
'user': UserSocialAuth.create_user(username=username, email=email),
'original_email': original_email,
'is_new': True
}
示例5: pre_process
def pre_process(self):
"""在处理命令前检查用户的状态。
- 先检查用户是否存在,不存在先保存用户。
- 再检查用户是否已在某个状态,如有,则把用户状态保存至实例。
"""
social = UserSocialAuth.objects.filter(provider='weixin',
uid=self.wxreq.FromUserName)
if social:
social = social[0]
self.user = social.user
else:
try:
user = User.objects.create_user('default_' +
str(random.randint(1, 10000)))
user.save()
user.username = 'wx_%d' % user.id
user.save()
self.user = user
social = UserSocialAuth(user=user, provider='weixin',
uid=self.wxreq.FromUserName)
social.save()
except:
log_err()
ss = UserState.objects.filter(user=social.user.username)
if ss:
self.wxstate = ss[0]
else:
self.wxstate = None
示例6: get_username
def get_username(details, user=None,
user_exists=UserSocialAuth.simple_user_exists,
*args, **kwargs):
"""Return an username for new user. Return current user username
if user was given.
"""
if user:
return {'username': user.username}
if details.get(USERNAME):
username = unicode(details[USERNAME])
else:
username = uuid4().get_hex()
uuid_length = setting('SOCIAL_AUTH_UUID_LENGTH', 16)
max_length = UserSocialAuth.username_max_length()
short_username = username[:max_length - uuid_length]
final_username = UserSocialAuth.clean_username(username[:max_length])
# Generate a unique username for current user using username
# as base but adding a unique hash at the end. Original
# username is cut to avoid any field max_length.
while user_exists(username=final_username):
username = short_username + uuid4().get_hex()[:uuid_length]
final_username = UserSocialAuth.clean_username(slugify(username[:max_length]))
return {'username': final_username}
示例7: get_username
def get_username(
user_exists=UserSocialAuth.simple_user_exists,
):
"""Return an username for new user. Return current user username
if user was given.
"""
uuid_length = setting('SOCIAL_AUTH_UUID_LENGTH', 16)
do_slugify = setting('SOCIAL_AUTH_SLUGIFY_USERNAMES', False)
username = uuid4().get_hex()
max_length = UserSocialAuth.username_max_length()
short_username = username[:max_length - uuid_length]
final_username = UserSocialAuth.clean_username(username[:max_length])
if do_slugify:
final_username = slugify(final_username)
# Generate a unique username for current user using username
# as base but adding a unique hash at the end. Original
# username is cut to avoid any field max_length.
while user_exists(username=final_username):
username = short_username + uuid4().get_hex()[:uuid_length]
username = username[:max_length]
final_username = UserSocialAuth.clean_username(username)
if do_slugify:
final_username = slugify(final_username)
print final_username
return final_username
示例8: disconnect
def disconnect(self, user, association_id=None):
"""Deletes current backend from user if associated.
Override if extra operations are needed.
"""
if association_id:
UserSocialAuth.get_social_auth_for_user(user).get(id=association_id).delete()
else:
UserSocialAuth.get_social_auth_for_user(user).filter(provider=self.AUTH_BACKEND.name).delete()
示例9: test_simple
def test_simple(self):
UserSocialAuth.create_social_auth(self.user, '1234', 'github')
self.login_as(self.user)
url = reverse('sentry-api-0-user-social-identities-index', kwargs={
'user_id': self.user.id,
})
response = self.client.get(url)
assert response.status_code == 200, response.content
assert len(response.data) == 1
assert response.data[0]['provider'] == 'github'
示例10: _googleAuth
def _googleAuth(user):
google_auth = UserSocialAuth()
google_auth.user = user
google_auth.provider = 'google'
google_auth.uid = user.email
google_auth.extra_data = '{}'
return google_auth
示例11: add_auth_id
def add_auth_id(self, auth_str):
"""
Add a social auth identifier for this user.
The `auth_str` should be in the format '{provider}:{uid}'
this is useful for adding multiple unique email addresses.
Example::
user = User.objects.get(username='foo')
user.add_auth_id('email:[email protected]')
"""
provider, uid = auth_str.split(':')
UserSocialAuth.create_social_auth(self, uid, provider)
示例12: getAssociation
def getAssociation(self, server_url, handle=None):
"""Return stored assocition"""
oid_associations = UserSocialAuth.get_oid_associations(server_url,
handle)
associations = [association
for assoc_id, association in oid_associations
if association.getExpiresIn() > 0]
expired = [assoc_id for assoc_id, association in oid_associations
if association.getExpiresIn() == 0]
if expired: # clear expired associations
UserSocialAuth.delete_associations(expired)
if associations: # return most recet association
return associations[0]
示例13: test_login_user
def test_login_user(self):
uid = randint(1000, 9000)
user = UserSocialAuth.create_user(username=uid)
user.first_name = 'test_firstname'
user.last_name = 'test_lastname'
user.save()
social = UserSocialAuth.create_social_auth(user, user.username, 'vkontakte')
response = self.client.get(reverse('vk_app'), {'viewer_id':uid})
self.assertEqual(response.status_code, 200)
self.assertTrue(response.context['user'].is_authenticated())
示例14: disconnect
def disconnect(self, user, association_id=None):
"""Deletes current backend from user if associated.
Override if extra operations are needed.
"""
name = self.AUTH_BACKEND.name
if UserSocialAuth.allowed_to_disconnect(user, name, association_id):
if association_id:
UserSocialAuth.get_social_auth_for_user(user)\
.get(id=association_id).delete()
else:
UserSocialAuth.get_social_auth_for_user(user)\
.filter(provider=name)\
.delete()
else:
raise NotAllowedToDisconnect()
示例15: get_username
def get_username(user=None, user_exists=UserSocialAuth.simple_user_exists,
*args, **kwargs):
if user:
return {'username': UserSocialAuth.user_username(user)}
prefix = setting('ACCOUNT_USERNAME_PREFIX', 'user')
max_length = UserSocialAuth.username_max_length()
uuid_length = setting('SOCIAL_AUTH_UUID_LENGTH', 16)
username = None
while username is None or user_exists(username=username):
username = prefix + uuid4().get_hex()[:uuid_length]
username = username[:max_length]
return {'username': username}