o
    MfU                     @   sb  d dl Z d dlmZ 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 d d	lmZ d d
lmZ d dlmZmZmZ d dlmZ d dlmZmZmZmZ 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, d dl-m.Z. d dl/m0Z0 e Z1G dd de2Z3G dd de2Z4G dd dZ5G dd de Z6e6 Z7dS )    N)update_wrapper)WeakSet)apps)settings)
ModelAdminactions)AutocompleteJsonView)REDIRECT_FIELD_NAME)ImproperlyConfigured)	ModelBase)Http404HttpResponsePermanentRedirectHttpResponseRedirect)TemplateResponse)NoReverseMatchResolver404resolvereverse)
LazyObject)import_string)capfirst)gettextgettext_lazy)never_cache)no_append_slash)csrf_protect)JavaScriptCatalogc                   @      e Zd ZdS )AlreadyRegisteredN__name__
__module____qualname__ r#   r#   \/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/contrib/admin/sites.pyr          r   c                   @   r   )NotRegisteredNr   r#   r#   r#   r$   r&   !   r%   r&   c                   @   sL  e Zd ZdZedZedZedZdZdZ	dZ
dZdZdZdZdZdZdZdZd=d
dZdd Zd>ddZdd Zdd Zd>ddZdd Zdd Zedd Zdd Zd?dd Zd!d" Zed#d$ Z d%d& Z!d>d'd(Z"d>d)d*Z#d>d+d,Z$e%d>d-d.Z&e%d>d/d0Z'd1d2 Z(e)d3d4 Z*d>d5d6Z+d7d8 Z,e%d>d9d:Z-d>d;d<Z.dS )@	AdminSiteal  
    An AdminSite object encapsulates an instance of the Django admin application, ready
    to be hooked in to your URLconf. Models are registered with the AdminSite using the
    register() method, and the get_urls() method can then be used to access Django view
    functions that present a full admin interface for the collection of registered
    models.
    zDjango site adminzDjango administrationzSite administration/T-Nadminc                 C   s2   i | _ || _dtji| _| j | _t|  d S )Ndelete_selected)		_registrynamer   r+   _actionscopy_global_actions	all_sitesaddselfr-   r#   r#   r$   __init__H   s
   zAdminSite.__init__c                 C   sZ   |du rt  }t|}g }dd | j D }|D ]}|jjj|v r*||	  q|S )zl
        Run the system checks on all ModelAdmins, except if they aren't
        customized at all.
        Nc                 s   s    | ]
}|j tur|V  qd S N)	__class__r   ).0or#   r#   r$   	<genexpr>Y   s    z"AdminSite.check.<locals>.<genexpr>)
r   get_app_configssetr,   valuesmodel_meta
app_configextendcheck)r4   app_configserrorsmodeladmins
modeladminr#   r#   r$   rB   O   s   zAdminSite.checkc                 K   s   |pt }t|tr|g}|D ]X}|jjrtd|j || jv rJt| j| }d|j }|	dr@|dt
dd| 7 }t||d| 7 }t||jjsf|r^t|d< td	|j |f|}||| | j|< qd
S )a  
        Register the given model(s) with the given admin class.

        The model(s) should be Model classes, not instances.

        If an admin class isn't given, use ModelAdmin (the default admin
        options). If keyword arguments are given -- e.g., list_display --
        apply them as options to the admin class.

        If a model is already registered, raise AlreadyRegistered.

        If a model is abstract, raise ImproperlyConfigured.
        z@The model %s is abstract, so it cannot be registered with admin.z#The model %s is already registered z.ModelAdminz
in app %r.z\.ModelAdmin$ zwith %r.r!   z%sAdminN)r   
isinstancer   r?   abstractr
   r    r,   strendswithresubr   swappedtype)r4   model_or_iterableadmin_classoptionsr>   registered_adminmsgr#   r#   r$   register_   s.   



zAdminSite.registerc                 C   s>   t |tr|g}|D ]}|| jvrtd|j | j|= q
dS )zs
        Unregister the given model(s).

        If a model isn't already registered, raise NotRegistered.
        zThe model %s is not registeredN)rH   r   r,   r&   r    )r4   rP   r>   r#   r#   r$   
unregister   s   


zAdminSite.unregisterc                 C   s
   || j v S )zM
        Check if a model class is registered with this `AdminSite`.
        )r,   )r4   r>   r#   r#   r$   is_registered   s   
zAdminSite.is_registeredc                 C   s"   |p|j }|| j|< || j|< dS )z>
        Register an action to be available globally.
        N)r    r.   r0   )r4   actionr-   r#   r#   r$   
add_action   s   

zAdminSite.add_actionc                 C   s   | j |= dS )zY
        Disable a globally-registered action. Raise KeyError for invalid names.
        N)r.   r3   r#   r#   r$   disable_action   s   zAdminSite.disable_actionc                 C   s
   | j | S )z
        Explicitly get a registered global action whether it's enabled or
        not. Raise KeyError for invalid names.
        )r0   r3   r#   r#   r$   
get_action      
zAdminSite.get_actionc                 C   s
   | j  S )zM
        Get all the enabled actions as an iterable of (name, func).
        )r.   itemsr4   r#   r#   r$   r      r\   zAdminSite.actionsc                 C   s   |j jo|j jS )z|
        Return True if the given HttpRequest has permission to view
        *at least one* page in the admin site.
        )user	is_activeis_staffr4   requestr#   r#   r$   has_permission   s   zAdminSite.has_permissionFc                    s8    fdd}|st |}tddst|}t|S )a  
        Decorator to create an admin view attached to this ``AdminSite``. This
        wraps the view and provides permission checking by calling
        ``self.has_permission``.

        You'll want to use this from within ``AdminSite.get_urls()``:

            class MyAdminSite(AdminSite):

                def get_urls(self):
                    from django.urls import path

                    urls = super().get_urls()
                    urls += [
                        path('my_view/', self.admin_view(some_view))
                    ]
                    return urls

        By default, admin_views are marked non-cacheable using the
        ``never_cache`` decorator. If the view can be safely cached, set
        cacheable=True.
        c                    sn     | s,| jtd jdkrtd jd}t|S ddlm} ||  td jdS | g|R i |S )Nzadmin:logoutcurrent_appadmin:indexr   )redirect_to_loginzadmin:login)rd   pathr   r-   r   django.contrib.auth.viewsrh   get_full_path)rc   argskwargs
index_pathrh   r4   viewr#   r$   inner   s   
z#AdminSite.admin_view.<locals>.innercsrf_exemptF)r   getattrr   r   )r4   rp   	cacheablerq   r#   ro   r$   
admin_view   s   
zAdminSite.admin_viewc                    sr  ddl m} ddlm}m}m} d  fdd	}|d| jdd	|d
 jdd	|d| jdd	|d| j	dddd	|d| j
dddd	|d| jdd	|d| jdddd	|d||jdd	g}g } j D ]$\}}	||d|jj|jjf ||	jg7 }|jj|vr||jj ql|rdd| d }
|||
| jdd	g7 } jr||d| j |S )!Nr   )views)includeri   re_pathFc                    s     fdd}|_ t|S )Nc                     s     | i |S r6   )ru   )rl   rm   )rt   r4   rp   r#   r$   wrapper   s   z1AdminSite.get_urls.<locals>.wrap.<locals>.wrapper)
admin_siter   )rp   rt   ry   r^   )rt   rp   r$   wrap   s   
z AdminSite.get_urls.<locals>.wraprG   index)r-   zlogin/loginzlogout/logoutzpassword_change/T)rt   password_changezpassword_change/done/password_change_donezautocomplete/autocompletezjsi18n/jsi18nz)r/<int:content_type_id>/<path:object_id>/view_on_sitez%s/%s/z^(?P<app_label>|z)/$app_listz(?P<url>.*)$F)django.contrib.contenttypesrv   django.urlsrw   ri   rx   r|   r}   r~   r   r   autocomplete_viewi18n_javascriptshortcutr,   r]   r?   	app_label
model_nameurlsappendjoin	app_indexfinal_catch_all_viewcatch_all_view)r4   contenttype_viewsrw   ri   rx   r{   urlpatternsvalid_app_labelsr>   model_adminregexr#   r^   r$   get_urls   sH   zAdminSite.get_urlsc                 C   s   |   d| jfS Nr*   )r   r-   r^   r#   r#   r$   r   )  s   zAdminSite.urlsc                 C   sH   |j d }| jdkr|r|n| j}| j| j|| || |d| jdS )z
        Return a dictionary of variables to put in the template context for
        *every* page in the admin site.

        For sites running on a subpath, use the SCRIPT_NAME value if site_url
        hasn't been customized.
        SCRIPT_NAMEr(   F)
site_titlesite_headersite_urlrd   available_appsis_popupis_nav_sidebar_enabled)METAr   r   r   rd   get_app_listenable_nav_sidebar)r4   rc   script_namer   r#   r#   r$   each_context-  s   
zAdminSite.each_contextc                 C   st   ddl m} ddlm} td| jd}||i | ||pi d}| jdur,| j|d< | j|_|j	d	i ||S )
zX
        Handle the "change password" task -- both form display and validation.
        r   )AdminPasswordChangeForm)PasswordChangeViewzadmin:password_change_donere   )
form_classsuccess_urlextra_contextNtemplate_namer#   )
django.contrib.admin.formsr   rj   r   r   r-   r   password_change_templaterf   as_view)r4   rc   r   r   r   urldefaultsr#   r#   r$   r   A  s   

zAdminSite.password_changec                 C   sV   ddl m} di | ||pi i}| jdur| j|d< | j|_|jdi ||S )zE
        Display the "success" page after a password change.
        r   )PasswordChangeDoneViewr   Nr   r#   )rj   r   r   password_change_done_templater-   rf   r   )r4   rc   r   r   r   r#   r#   r$   r   R  s   

zAdminSite.password_change_donec                 C   s   t jdgd|S )z
        Display the i18n JavaScript that the Django admin requires.

        `extra_context` is unused but present for consistency with the other
        admin views.
        zdjango.contrib.admin)packages)r   r   )r4   rc   r   r#   r#   r$   r   _  s   zAdminSite.i18n_javascriptc                 C   s^   ddl m} di | |ddi|pi i}| jdur!| j|d< | j|_|jdi ||S )	z~
        Log out the user for the given HttpRequest.

        This should *not* assume the user is already logged in.
        r   )
LogoutViewr   rd   FNr   r#   )rj   r   r   logout_templater-   rf   r   )r4   rc   r   r   r   r#   r#   r$   r~   h  s   
	
zAdminSite.logoutc                 C   s   |j dkr| |rtd| jd}t|S ddlm} ddlm} i | 	|t
d| |j d}t|jvrHt|jvrHtd| jd|t< ||pMi  || jpT|| jpXd	d
}| j|_|jdi ||S )zC
        Display the login form for the given HttpRequest.
        GETrg   re   r   )AdminAuthenticationForm)	LoginViewzLog in)titleapp_pathusernamezadmin/login.html)r   authentication_formr   Nr#   )methodrd   r   r-   r   r   r   rj   r   r   _rk   r_   get_usernamer	   r   POSTupdate
login_formlogin_templaterf   r   )r4   rc   r   rn   r   r   contextr   r#   r#   r$   r}   ~  s,   

zAdminSite.loginc                 C   s   t j| d|S )N)rz   )r   r   rb   r#   r#   r$   r     s   zAdminSite.autocomplete_viewc                 C   sf   t jr1|ds1t|dd }z
td|j |}W n
 ty"   Y t
w t|jddr1td|j	 S t
)Nr(   urlconfz%s/should_append_slashT)r   APPEND_SLASHrK   rs   r   	path_infor   funcr   ri   r   )r4   rc   r   r   matchr#   r#   r$   r     s   zAdminSite.catch_all_viewc              	      sx  i } r fdd| j  D }n| j }| D ]\}}|jj}||}|s(q||}	d|	 vr4q||jjf}
t|jj	|jj
|	ddd}|	dsR|	drq|	d |d< ztd	|
 | jd
|d< W n	 typ   Y nw |	drztd|
 | jd
|d< W n	 ty   Y nw ||v r|| d | qt|j|tdd|i| jd||gd||< q r| S |S )zt
        Build the app dictionary. The optional `label` parameter filters models
        of a specific app.
        c                    s"   i | ]\}}|j j kr||qS r#   )r?   r   )r8   mm_alabelr#   r$   
<dictcomp>  s
    z-AdminSite._build_app_dict.<locals>.<dictcomp>TN)r-   object_nameperms	admin_urladd_urlchangerp   	view_onlyzadmin:%s_%s_changelistre   r   r2   zadmin:%s_%s_addr   modelszadmin:app_listr   )rm   rf   )r-   r   app_urlhas_module_permsr   )r,   r]   r?   r   has_module_permissionget_model_permsr=   r   r   verbose_name_pluralr   getr   r-   r   r   r   get_app_configverbose_name)r4   rc   r   app_dictr   r>   r   r   r   r   info
model_dictr#   r   r$   _build_app_dict  sb   






zAdminSite._build_app_dictc                 C   s@   |  |}t| dd d}|D ]}|d jdd d q|S )zp
        Return a sorted list of all the installed apps that have been
        registered in this site.
        c                 S   s   | d   S Nr-   )lowerxr#   r#   r$   <lambda>  s    z(AdminSite.get_app_list.<locals>.<lambda>keyr   c                 S      | d S r   r#   r   r#   r#   r$   r          )r   sortedr=   sort)r4   rc   r   r   appr#   r#   r$   r     s
   
zAdminSite.get_app_listc                 C   sH   |  |}i | || jd|d|pi }| j|_t|| jp!d|S )z
        Display the main admin index page, which lists all of the installed
        apps that have been registered in this site.
        N)r   subtitler   zadmin/index.html)r   r   index_titler-   rf   r   index_template)r4   rc   r   r   r   r#   r#   r$   r|     s   
zAdminSite.indexc                 C   s   |  ||}|std|d jdd d i | |tdd|d i d |g|d	|p/i }| j|_t|| jp?d
| dg|S )Nz(The requested admin page does not exist.r   c                 S   r   r   r#   r   r#   r#   r$   r     r   z%AdminSite.app_index.<locals>.<lambda>r   z%(app)s administrationr   r-   )r   r   r   r   zadmin/%s/app_index.htmlzadmin/app_index.html)	r   r   r   r   r   r-   rf   r   app_index_template)r4   rc   r   r   r   r   r#   r#   r$   r     s*   	
zAdminSite.app_index)r*   r6   r   )/r    r!   r"   __doc__r   r   r   r   r   r   empty_value_displayr   r   r   r   r   r   r   r   r5   rB   rU   rV   rW   rY   rZ   r[   propertyr   rd   ru   r   r   r   r   r   r   r   r~   r}   r   r   r   r   r   r|   r   r#   r#   r#   r$   r'   %   s^    	

0


,8



	!

Dr'   c                   @   s   e Zd Zdd ZdS )DefaultAdminSitec                 C   s   t tdj}| | _d S r   )r   r   r   default_site_wrapped)r4   AdminSiteClassr#   r#   r$   _setup0  s   zDefaultAdminSite._setupN)r    r!   r"   r   r#   r#   r#   r$   r   /  s    r   )8rL   	functoolsr   weakrefr   django.appsr   django.confr   django.contrib.adminr   r   'django.contrib.admin.views.autocompleter   django.contrib.authr	   django.core.exceptionsr
   django.db.models.baser   django.httpr   r   r   django.template.responser   r   r   r   r   r   django.utils.functionalr   django.utils.module_loadingr   django.utils.textr   django.utils.translationr   r   r   django.views.decorators.cacher   django.views.decorators.commonr   django.views.decorators.csrfr   django.views.i18nr   r1   	Exceptionr   r&   r'   r   siter#   r#   r#   r$   <module>   s>        

