首页 > 解决方案 > Python Social Auth Partial Pipeline 返回 JSON 响应

问题描述

我正在尝试使用休息框架实现部分管道,以检查用户是否有电子邮件。如果没有电子邮件,它应该返回一个 json 响应,其中包含一条消息和一个 api 端点,用户可以在其中发布电子邮件数据。发布电子邮件后,我希望管道从暂停的地方正常执行。

管道步骤

# Social Auth Pipelines
SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'semesteronedjango.oauth_pipeline.require_email',  # partial pipeline
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

部分管道:

@partial
def require_email(strategy, details, user=None, is_new=False, *args, **kwargs):
    
    if kwargs.get('ajax') or user and user.email:
        return
    elif is_new and not details.get('email'):
        email = strategy.request_data().get('email')
        if email:
            details['email'] = email
        else:
            current_partial = kwargs.get('current_partial')
            // Here i want to return a response containing link where user can post email

第一次尝试部分管道后,我可以到达这里。

发布电子邮件的端点:

@api_view(["GET", "POST"])
def require_email(request):
    strategy = load_strategy()
    partial_token = request.GET.get('partial_token')
    partial = strategy.partial_load(partial_token)
    if request.method == 'POST':
        partial.backend.strategy.session['email'] = request.POST.get('email')
        partial.backend.strategy.session['email_set'] = True
        return redirect('social:complete', backend=partial.backend)
    else:

        return Response({
            'email_required': True,
            'current_email': strategy.session_get('email'),
            'partial_backend_name': partial.backend,
            'partial_token': partial_token,
            'uid': partial.kwargs.get('uid')
        })

所以基本上我的问题是我如何在暂停管道时返回 JSON 响应,并且我当前的实现是否适用于我的用例?

标签: django-rest-frameworkpipelinepython-social-auth

解决方案


推荐阅读