2

I'm trying to implement a partial pipeline with rest framework which checks if a user have an email or not. If there's no email, it should return a json response with a message and a api endpoint to where user can post email data. After posting an email, i want pipeline to execute normally from where it paused.

PIPELINE STEPS

# 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 Pipeline:

@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

Having tried partial pipeline for the first time, i can get upto here.

Endpoint to 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')
        })

So basiclly my question is how can i return a JSON response while i pause a pipeline and is my current implementation current for my use case ?

Surya Bhusal
  • 520
  • 2
  • 8
  • 28

0 Answers0