我正在使用 Django Social Auth (v0.7.22) 通过 Facebook 注册用户,并且工作正常。
我的疑问是如何为新用户收集额外数据:
**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()
我的疑惑:
user
是 None
用于检测新用户(不确定是否可以假设)。 **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/