Frage

The default username-generation scheme in Python Social Auth is to take the username of the auth'd social network, and if it is already taken, add some sorta random value to it (or is it a hash?).

Anyway, I want to change that behavior, I want to define my own username-generation method. Like for example username+provider+random.

Ideas?

War es hilfreich?

Lösung

All you need is to replace social.pipeline.user.get_username from SOCIAL_AUTH_PIPELINE with path to your own function returning generated username.

For example:

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

In above function I'm calling default get_username method from python-social-auth module and to result append provider name and random number between 0 and 1000.

You can check source code of social.pipeline.user.get_username on GitHub.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top