5

I'm using Django Social Auth (v0.7.22) for registering users via Facebook, and that is working OK.

My doubt is how to collect extra data for new users:

  1. How to detect a new user?
  2. Where to store the collected data (in the Django session or pass it through pipeline **kwargs)?

My pipeline looks like:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.misc.save_status_to_session',
    ## My customs ...
    'myapp.pipeline.load_data_new_user',
    'myapp.pipeline.handle_new_user',
    'myapp.pipeline.username',
    ##
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
)    

The first custom function just collect the Facebook profile picture:

def load_data_new_user(backend, response, user, *args, **kwargs):
    if user is None:
        if backend.name == "facebook":
            try:
                url = "http://graph.facebook.com/%s/picture?width=200&height=200&redirect=false" % response['id']
                data = json.loads(urllib2.urlopen(url).read())['data']
                return {'avatar': data}
            except StandardError:
                return {'avatar': None}
        else:
            raise ValueError()

My doubts:

  1. I'm checking if user is None for detecting new users (not sure if it's OK to assume that).
  2. I'm storing the avatar metadata in the pipeline's **kwargs instead of use sessions, is it OK? When should I use session.

その他のカスタム関数はMatias Aguirre の例に基づいており、セッションを使用して新しいユーザーのユーザー名を保存します。

def handle_new_user(request, user, *args, **kwargs):
    if user is None and not request.session.get('saved_username'):
        return HttpResponseRedirect('/form/')


def username(request, user, *args, **kwargs):
    if user is not None:
        username = user.username
    else:
        username = request.session.get('saved_username')
    return {'username': username} 

そのため、問題を解決するためにセッションまたは「正しいイディオム」をいつ使用するかわかりません。前もって感謝します。

4

2 に答える 2

5

Matias Aguirre (django socialauth の作成者) が、DSA の Google メーリング リストで私の質問に優しく答えてくれました

彼の答え:

ご覧のとおり、データの収集は簡単ですが、データをどこに置くかは、プロジェクト、データで何をする予定か、データがどこで利用可能になるかによって異なります。たとえば、アバター画像をユーザー プロファイル モデルに配置する場合は、現在のユーザーのプロファイルを取得してそこに保存する必要があります。avatar_url フィールドを持つカスタム ユーザーを使用する場合は、その後にそのフィールドに入力する必要があります。イメージをダウンロードし、ユーザー名 (ID など) をファイル名として使用してサーバーのローカルに保存する場合は、そのようにします。

セッションは通常、ビューとの通信メカニズムとして使用されます。

「ユーザーが新規」に関しては、ユーザーが新規かどうかを True/False に設定するフラグ「is_new」がありますが、「create_user」が呼び出されるまで更新されません。

パイプライン エントリ「save_status_to_session」は、パイプライン フローを中断するメソッド (この場合は handle_new_user) の前に配置する必要があります。

みんな、ありがとう。

于 2013-04-13T14:05:23.000 に答える