django-rest-framework - 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 响应,并且我当前的实现是否适用于我的用例?
解决方案
推荐阅读
- postgresql - 用户级别的 postgres work_mem
- python - 灵活地将文件夹中的每个图像文件定义为 ImageTk 对象
- python - 从使用 Airflow KubernetesPodOperator 启动的 Pod 获取上下文
- ios - 如何在渲染回调中交错非交错的 AudioBufferList?
- javascript - 如何使用 Javascript 动态更新运输进度条?
- javascript - 如何使用节点 js 将 PDF 存储在 mysql 数据库中并表达
- r - 将数值向量缩放到指定的平均值、最小值和最大值
- c++ - 类方法不返回值
- postgresql - Rust serde_json::value::RawValue 给出错误“不满足 trait bound ... `r2d2_postgres::postgres::types::FromSql<'_>`”
- python - 例外:带有 Gradio 库的 MissingSchema