django - 在 Django Social Auth 管道中收集额外的用户数据

标签 django django-socialauth

我正在使用 Django Social Auth (v0.7.22) 通过 Facebook 注册用户,并且工作正常。

我的疑问是如何为新用户收集额外数据:

  • 如何检测新用户?
  • 在哪里存储收集到的数据(在 Django session 中或通过管道 **kwargs 传递)?

  • 我的管道看起来像:
    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',
    )    
    

    第一个自定义函数只是收集 Facebook 个人资料图片:
    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()
    

    我的疑惑:
  • 我正在检查是否 userNone用于检测新用户(不确定是否可以假设)。
  • 我将头像元数据存储在管道的 **kwargs 中而不是使用 session ,可以吗?我什么时候应该使用 session 。

  • 其他自定义函数基于 Matias Aguirre example ,并使用 session
    用于存储新用户的用户名。
    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} 
    

    所以,我不确定何时使用 session 或“正确的习语”来解决我的问题。
    提前致谢。

    最佳答案

    Matias Aguirre (django socialauth creator) 在 DSA google mail list 中温柔地回答了我的问题

    他的回答:

    Collecting the data is easy as you saw, but where to put it depends on your project, what you plan to do with the data, and where you expect it to be available. For example, if you plan to put the avatar image on a user profile model, then you should get the profile for the current user and store it there, if you use a custom user with an avatar_url field, then you should fill that field after the user was created, if you plan to download the image and store it local to the server using the username (id, etc) as the filename, then just do it that way too.

    The session is usually used as a mechanism of communication with a view.

    Regarding the "user is new", there's a flag "is_new" set to True/False when the user is new or not, but it's not updated until the "create_user" is called.

    The pipeline entry "save_status_to_session" should be placed before the method that breaks the pipeline flow, in your case handle_new_user.



    谢谢大家。

    关于django - 在 Django Social Auth 管道中收集额外的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950616/

    相关文章:

    django - python 社交身份验证更改引荐来源地址

    python - Django Social Auth 没有重定向到下一个 URL?

    python - Ubuntu 上的 Django + Apache 部署

    django - 是否可以覆盖 Django 为 UniqueConstraint 错误显示的错误消息? (完整性错误)

    python - Django 模型继承与 OneToOne 字段

    python - 捕获 yyyy-mm-dd 日期的 Django url

    python - 在 Django 中使用 python-social-auth 重定向回网站时不可散列的类型

    django - 如何处理django-social-auth引发的异常?

    python - 使用 Django 将社区元素添加到网站

    python - raw_id_fields 不显示带有放大镜按钮的所有字段