o
    gf	                     @   sX   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ G dd deZdS )	    )settings)errorMessageFailure)redirect)SocialAuthBaseException)backend_settingget_backend_namec                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )SocialAuthExceptionMiddlewarea  Middleware that handles Social Auth AuthExceptions by providing the user
    with a message, logging an error, and redirecting to some next location.

    By default, the exception message itself is sent to the user and they are
    redirected to the location specified in the LOGIN_ERROR_URL setting.

    This middleware can be extended by overriding the get_message or
    get_redirect_uri methods, which each accept request and exception.
    c                 C   s   |  ||| _| ||rd S t|trft| j}| ||}| ||}dg}|r/|| zt	||d
|d W t|S  tye   |d|v rLdpMdd | 7 }|r`|d| 7 }Y t|S Y t|S w d S )Nzsocial-auth )
extra_tags?&zmessage=z	&backend=)get_backendbackendraise_exception
isinstancer   r   get_messageget_redirect_uriappendr   joinr   r   )selfrequest	exceptionbackend_namemessageurltags r   W/var/www/html/humari/django-venv/lib/python3.10/site-packages/social_auth/middleware.pyprocess_exception   s,   


z/SocialAuthExceptionMiddleware.process_exceptionc                 C   s*   t | dst|dd pt|dd | _| jS )Nr   )hasattrgetattrr   r   r   r   r   r   r   r   )   s
   

z)SocialAuthExceptionMiddleware.get_backendc                 C   s   | j }|o	t|dS )NSOCIAL_AUTH_RAISE_EXCEPTIONS)r   r   )r   r   r   r   r   r   r   r   /   s   z-SocialAuthExceptionMiddleware.raise_exceptionc                 C   s   t |S )N)unicoder"   r   r   r   r   4   s   z)SocialAuthExceptionMiddleware.get_messagec                 C   s"   | j d urt| j dptjS tjS )NSOCIAL_AUTH_BACKEND_ERROR_URL)r   r   r   LOGIN_ERROR_URLr"   r   r   r   r   7   s   
z.SocialAuthExceptionMiddleware.get_redirect_uriN)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r	   
   s    	r	   N)django.confr   django.contrib.messages.apir   r   django.shortcutsr   social_auth.exceptionsr   social_auth.utilsr   r   objectr	   r   r   r   r   <module>   s   