【问题标题】:Python Social Auth: How to define a different username scheme?Python Social Auth:如何定义不同的用户名方案?
【发布时间】:2014-06-13 05:10:59
【问题描述】:

Python Social Auth 中的默认用户名生成方案是采用经过身份验证的社交网络的用户名,如果已经采用,则为其添加一些随机值(或者它是一个哈希?)。

无论如何,我想改变这种行为,我想定义自己的用户名生成方法。比如用户名+提供者+随机数。

想法?

【问题讨论】:

    标签: python django django-socialauth python-social-auth


    【解决方案1】:

    您只需将 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 之间的随机数。

    您可以查看social.pipeline.user.get_username on GitHub的源代码

    【讨论】:

    • 那里的 randrange 有问题,异常:异常序列项 2:预期的字符串或 Unicode,int found ,但我修复了它,现在它可以工作了,我可以完全控制用户名的生成。不错。
    • 啊,我的错,我忘记将 int 转换为 str。代码已更新。
    • 太棒了。谢谢它帮了我很多!
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多