【发布时间】:2014-06-13 05:10:59
【问题描述】:
Python Social Auth 中的默认用户名生成方案是采用经过身份验证的社交网络的用户名,如果已经采用,则为其添加一些随机值(或者它是一个哈希?)。
无论如何,我想改变这种行为,我想定义自己的用户名生成方法。比如用户名+提供者+随机数。
想法?
【问题讨论】:
标签: python django django-socialauth python-social-auth
Python Social Auth 中的默认用户名生成方案是采用经过身份验证的社交网络的用户名,如果已经采用,则为其添加一些随机值(或者它是一个哈希?)。
无论如何,我想改变这种行为,我想定义自己的用户名生成方法。比如用户名+提供者+随机数。
想法?
【问题讨论】:
标签: python django django-socialauth python-social-auth
您只需将 SOCIAL_AUTH_PIPELINE 中的 social.pipeline.user.get_username 替换为您自己的函数的路径,返回生成的用户名。
例如:
project/myapp/settings.py
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'project.myapp.utils.get_username',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
)
project/myapp/utils.py:
from social.pipeline.user import get_username as social_get_username
from random import randrange
def get_username(strategy, details, user=None, *args, **kwargs):
result = social_get_username(strategy, details, user=user, *args, **kwargs)
result['username'] = '-'.join([
result['username'], strategy.backend.name, str(randrange(0, 1000))
])
return result
在上面的函数中,我从python-social-auth 模块调用默认的get_username 方法,结果追加提供者名称和0 到1000 之间的随机数。
【讨论】: