3
(h๕  ใ               @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ G dd deZG d	d
 d
eZG dd deZG dd deZdS )้    )ฺsettings)ฺREDIRECT_FIELD_NAME)ฺredirect_to_login)ฺImproperlyConfiguredฺPermissionDenied)ฺsix)ฺ
force_textc               @   s@   e Zd ZdZdZdZdZeZdd Z	dd Z
d	d
 Zdd ZdS )ฺAccessMixinz^
    Abstract CBV mixin that gives access mixins the same customizable
    functionality.
    Nฺ Fc             C   s*   | j p
tj}|s"tdj| jjt|S )zK
        Override this method to override the login_url attribute.
        zr{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override {0}.get_login_url().)ฺ	login_urlr   Z	LOGIN_URLr   ฺformatฺ	__class__ฺ__name__r   )ฺselfr   ฉ r   ๚>/tmp/pip-install-q3hcpn_q/Django/django/contrib/auth/mixins.pyฺget_login_url   s    zAccessMixin.get_login_urlc             C   s   | j S )z[
        Override this method to override the permission_denied_message attribute.
        )ฺpermission_denied_message)r   r   r   r   ฺget_permission_denied_message   s    z)AccessMixin.get_permission_denied_messagec             C   s   | j S )zU
        Override this method to override the redirect_field_name attribute.
        )ฺredirect_field_name)r   r   r   r   ฺget_redirect_field_name%   s    z#AccessMixin.get_redirect_field_namec             C   s,   | j rt| j t| jj | j | j S )N)ฺraise_exceptionr   r   r   ฺrequestZget_full_pathr   r   )r   r   r   r   ฺhandle_no_permission+   s    z AccessMixin.handle_no_permission)r   ฺ
__module__ฺ__qualname__ฺ__doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r	   	   s   r	   c                   s    e Zd ZdZ fddZ  ZS )ฺLoginRequiredMixinzJ
    CBV mixin which verifies that the current user is authenticated.
    c                s(   |j js| j S tt| j|f||S )N)ฺuserฺis_authenticatedr   ฺsuperr   ฺdispatch)r   r   ฺargsฺkwargs)r   r   r   r!   5   s    zLoginRequiredMixin.dispatch)r   r   r   r   r!   ฺ__classcell__r   r   )r   r   r   1   s   r   c                   s4   e Zd ZdZdZdd Zdd Z fddZ  ZS )	ฺPermissionRequiredMixinz[
    CBV mixin which verifies that the current user has all specified
    permissions.
    Nc             C   s>   | j dkrtdj| jjt| j tjr4| j f}n| j }|S )zv
        Override this method to override the permission_required attribute.
        Must return an iterable.
        Nz|{0} is missing the permission_required attribute. Define {0}.permission_required, or override {0}.get_permission_required().)ฺpermission_requiredr   r   r   r   ฺ
isinstancer   ฺstring_types)r   ฺpermsr   r   r   ฺget_permission_requiredB   s    

z/PermissionRequiredMixin.get_permission_requiredc             C   s   | j  }| jjj|S )zT
        Override this method to customize the way permissions are checked.
        )r*   r   r   Z	has_perms)r   r)   r   r   r   ฺhas_permissionR   s    z&PermissionRequiredMixin.has_permissionc                s(   | j  s| j S tt| j|f||S )N)r+   r   r    r%   r!   )r   r   r"   r#   )r   r   r   r!   Y   s    z PermissionRequiredMixin.dispatch)	r   r   r   r   r&   r*   r+   r!   r$   r   r   )r   r   r%   ;   s
   r%   c                   s0   e Zd ZdZdd Zdd Z fddZ  ZS )ฺUserPassesTestMixinz
    CBV Mixin that allows you to define a test function which must return True
    if the current user can access the view.
    c             C   s   t dj| jjd S )Nz<{0} is missing the implementation of the test_func() method.)ฺNotImplementedErrorr   r   r   )r   r   r   r   ฺ	test_funce   s    zUserPassesTestMixin.test_funcc             C   s   | j S )zK
        Override this method to use a different test_func method.
        )r.   )r   r   r   r   ฺget_test_funcj   s    z!UserPassesTestMixin.get_test_funcc                s.   | j   }|s| j S tt| j|f||S )N)r/   r   r    r,   r!   )r   r   r"   r#   Zuser_test_result)r   r   r   r!   p   s    
zUserPassesTestMixin.dispatch)r   r   r   r   r.   r/   r!   r$   r   r   )r   r   r,   _   s   r,   N)Zdjango.confr   Zdjango.contrib.authr   Zdjango.contrib.auth.viewsr   Zdjango.core.exceptionsr   r   Zdjango.utilsr   Zdjango.utils.encodingr   ฺobjectr	   r   r%   r,   r   r   r   r   ฺ<module>   s   (
$