o
    Mfj                    @   sD  d dl Z d dlZd dlZd dlZd dl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 d dlmZmZ d dl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 m!Z!m"Z"m#Z#m$Z$m%Z%m	Z	m&Z& d dl'm(Z(m)Z) d dl*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@mAZA d dlBmCZCmDZD d dlEmFZF d dlGmHZH d dlImJZJmKZK d dlLmMZM d dlNmOZO d dlPmQZQ d dlRmSZS d dlTmUZU d dlVmWZWmXZXmYZYmZZZm[Z[ d dl\m]Z^m_Z_ d d l`maZa d d!lbmcZc d"Zdd#Zed$\ZfZgd%d& Zhd'd( ZiG d)d* d*ejZke4jlejmejnd+e4jod,ejpie4jqd,ejrie4jsd,ejtie4jud,ejvie4jwd,ejxie4jyd,ejzie4j{d,ej|ie4j}d,ej~ie4jd,ej~ie4jd,ejie4jd,ejiiZeOeaZG d-d. d.ejd/ZG d0d1 d1eZG d2d3 d3eZG d4d5 d5eZG d6d7 d7eZdS )8    N)partialreduceupdate_wrapper)quote)forms)settings)messages)helperswidgets)BaseModelAdminChecksInlineModelAdminChecksModelAdminChecks)display)DisallowedModelAdminToField)add_preserved_filters)	NestedObjectsconstruct_change_messageflatten_fieldsetsget_deleted_objectslookup_needs_distinctmodel_format_dictmodel_ngettextr   unquote)AutocompleteSelectAutocompleteSelectMultiple)get_permission_codename)FieldDoesNotExist
FieldErrorPermissionDeniedValidationError)	Paginator)modelsroutertransaction)
LOOKUP_SEP)DELETION_FIELD_NAME	all_valid)BaseInlineFormSetinlineformset_factorymodelform_defines_fieldsmodelform_factorymodelformset_factory)CheckboxSelectMultipleSelectMultiple)HttpResponseRedirect)HttpResponseBase)SimpleTemplateResponseTemplateResponse)reverse)method_decorator)format_html)	urlencode)	mark_safe)capfirstformat_lazyget_text_listsmart_splitunescape_string_literal)gettextngettext)csrf_protect)RedirectView_popup	_to_field)      c                 C   s   ddl m} |jj| ddS )Nr   )ContentTypeF)for_concrete_model)"django.contrib.contenttypes.modelsrD   objectsget_for_model)objrD    rJ   ^/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/contrib/admin/options.pyget_content_type_for_model=   s   rL   c                 C   s   | t krdS dS )N	radiolistzradiolist inline)VERTICAL)radio_stylerJ   rJ   rK   get_ul_classD      rP   c                   @   s   e Zd ZdS )IncorrectLookupParametersN)__name__
__module____qualname__rJ   rJ   rJ   rK   rR   H   s    rR   )
form_classwidgetrW   c                   @   sB  e Zd ZdZdZdZdZdZdZe	j
ZdZdZi Zi Zi 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d Zdd Zdd Zdd Zd;ddZ dd Z!d;ddZ"d;ddZ#d;ddZ$dd  Z%d!d" Z&d;d#d$Z'd;d%d&Z(d'd( Z)d)d* Z*d+d, Z+d-d. Z,d/d0 Z-d;d1d2Z.d;d3d4Z/d;d5d6Z0d;d7d8Z1d9d: Z2dS )<BaseModelAdminz8Functionality common to both ModelAdmin and InlineAdmin.rJ   NTc                 K   s   |   j| fi |S N)checks_classcheck)selfkwargsrJ   rJ   rK   r[   y      zBaseModelAdmin.checkc                 C   s:   t t}| j D ]\}}||i | q
|| _d S rY   )copydeepcopyFORMFIELD_FOR_DBFIELD_DEFAULTSformfield_overridesitems
setdefaultupdate)r\   	overrideskvrJ   rJ   rK   __init__|   s   

zBaseModelAdmin.__init__c                 K   sZ  |j r| j||fi |S t|tjtjfr|j| jv r&i | j|j |}t|tjr7| j||fi |}nt|tjrG| j	||fi |}|r|j
| jvr| jj|jj}i }|rq|j||||||||d tj|j|j| jfi ||_|S |j D ]}|| jv ri t| j| |}|jdi |  S q|jdi |S )z
        Hook for specifying the form Field instance for a given database Field
        instance.

        If kwargs are given, they're passed to the form Field's constructor.
        )can_add_relatedcan_change_relatedcan_delete_relatedcan_view_relatedNrJ   )choicesformfield_for_choice_field
isinstancer!   
ForeignKeyManyToManyField	__class__rb   formfield_for_foreignkeyformfield_for_manytomanynameraw_id_fields
admin_site	_registrygetremote_fieldmodelre   has_add_permissionhas_change_permissionhas_delete_permissionhas_view_permissionr
   RelatedFieldWidgetWrapperrW   mror_   r`   	formfield)r\   db_fieldrequestr]   r   related_modeladminwrapper_kwargsklassrJ   rJ   rK   formfield_for_dbfield   s>   	
z$BaseModelAdmin.formfield_for_dbfieldc                 K   sj   |j | jv r-d|vrtjdt| j|j  id|d< d|vr-|j|jdtdfgd|d< |jd	i |S )
zR
        Get a form Field for a database Field that has declared choices.
        rW   classattrsrn    None)include_blankblank_choiceNrJ   )	rv   radio_fieldsr
   AdminRadioSelectrP   get_choicesblank_r   )r\   r   r   r]   rJ   rJ   rK   ro      s   
z)BaseModelAdmin.formfield_for_choice_fieldc                 C   sN   | j j|jj}|dur%||}|dur%|dkr%|jjj|j| S dS )z
        If the ModelAdmin specifies ordering, the queryset should respect that
        ordering.  Otherwise don't specify the queryset, let the field decide
        (return None in that case).
        NrJ   )	rx   ry   rz   r{   r|   get_ordering_default_managerusingorder_by)r\   dbr   r   related_adminorderingrJ   rJ   rK   get_field_queryset   s   
z!BaseModelAdmin.get_field_querysetc                 K   s   | d}d|vrP|j| |v rt|| j|d|d< n4|j| jv r/tj|j| j|d|d< n!|j| j	v rPtj
dt| j	|j id|d< |jrLtdnd|d< d	|vrc| |||}|durc||d	< |jd
i |S )z4
        Get a form Field for a ForeignKey.
        r   rW   r   r   r   r   Nempty_labelquerysetrJ   )rz   rv   get_autocomplete_fieldsr   rx   rw   r
   ForeignKeyRawIdWidgetr{   r   r   rP   r   r   r   r   )r\   r   r   r]   r   r   rJ   rJ   rK   rt      s    
z'BaseModelAdmin.formfield_for_foreignkeyc           
      K   s  |j jjjsdS |d}d|vrQ| |}|j|v r&t|| j|d|d< n+|j| j	v r9t
j|j | j|d|d< n|jg | j| jv rQt
|j|j| jv |d< d|vrd| |||}|durd||d< |jdi |}t|jtrt|jttfstd}|j}	|	rtd|	|n||_|S )	z9
        Get a form Field for a ManyToManyField.
        Nr   rW   r   r   uL   Hold down “Control”, or “Command” on a Mac, to select more than one.z{} {}rJ   )r{   through_metaauto_createdrz   r   rv   r   rx   rw   r
   ManyToManyRawIdWidgetfilter_verticalfilter_horizontalFilteredSelectMultipleverbose_namer   r   rp   rW   r-   r,   r   	help_textr8   )
r\   r   r   r]   r   autocomplete_fieldsr   
form_fieldmsgr   rJ   rJ   rK   ru      sD   



z'BaseModelAdmin.formfield_for_manytomanyc                 C      | j S )zw
        Return a list of ForeignKey and/or ManyToMany fields which should use
        an autocomplete widget.
        )r   r\   r   rJ   rJ   rK   r        z&BaseModelAdmin.get_autocomplete_fieldsc                 C   sN   |d u s| j s	d S t| j r|  |S t|dr%tdt|j|jddS d S )Nget_absolute_urlzadmin:view_on_site)content_type_id	object_id)r]   )view_on_sitecallablehasattrr2   rL   pkr\   rI   rJ   rJ   rK   get_view_on_site_url%  s   



z#BaseModelAdmin.get_view_on_site_urlc                 C   s,   zt | jW S  ty   t | jj Y S w )zP
        Return the empty_value_display set on ModelAdmin or AdminSite.
        )r6   empty_value_displayAttributeErrorrx   r\   rJ   rJ   rK   get_empty_value_display2  s
   z&BaseModelAdmin.get_empty_value_displayc                 C   r   )z.
        Hook for specifying exclude.
        )excluder\   r   rI   rJ   rJ   rK   get_exclude;     zBaseModelAdmin.get_excludec                 C   s.   | j r| j S | ||}g |j| ||S )z-
        Hook for specifying fields.
        )fields_get_form_for_get_fieldsbase_fieldsget_readonly_fields)r\   r   rI   formrJ   rJ   rK   
get_fieldsA  s   zBaseModelAdmin.get_fieldsc                 C   s"   | j r| j S dd| ||ifgS )z0
        Hook for specifying fieldsets.
        Nr   )	fieldsetsr   r   rJ   rJ   rK   get_fieldsetsK  s   zBaseModelAdmin.get_fieldsetsc                 C   r   )z#Hook for specifying custom inlines.)inlinesr   rJ   rJ   rK   get_inlinesS     zBaseModelAdmin.get_inlinesc                 C   s
   | j pdS )z5
        Hook for specifying field ordering.
        rJ   )r   r   rJ   rJ   rK   r   W  s   
zBaseModelAdmin.get_orderingc                 C   r   )z=
        Hook for specifying custom readonly fields.
        )readonly_fieldsr   rJ   rJ   rK   r   ]  r   z"BaseModelAdmin.get_readonly_fieldsc                 C   r   )zA
        Hook for specifying custom prepopulated fields.
        )prepopulated_fieldsr   rJ   rJ   rK   get_prepopulated_fieldsc  r   z&BaseModelAdmin.get_prepopulated_fieldsc                 C   s(   | j j }| |}|r|j| }|S )z
        Return a QuerySet of all model instances that can be edited by the
        admin site. This is used by changelist_view.
        )r|   r   get_querysetr   r   )r\   r   qsr   rJ   rJ   rK   r   i  s
   

zBaseModelAdmin.get_querysetc                 C   s   | j dur| j S | |S )zAHook for specifying which fields can be sorted in the changelist.N)sortable_byget_list_displayr   rJ   rJ   rK   get_sortable_byu  s   zBaseModelAdmin.get_sortable_byc              	   C   sb  ddl m} | j}|jjD ]}t|r| }||ft| v r$ dS qg }d }|	t
D ]:}z|j|}	W n
 tyB   Y  n'w |rQ|jrV|	| d jvrV|| t|	dd s^ n|	}|	 d jj}q.t|dkrqdS | jh}
| jD ]'}t|trt||r|
|j qxt|ttfr|
|d  qx|
| qxt
|t
||g h|
 S )Nr   )SimpleListFilterTget_path_inforB   )django.contrib.admin.filtersr   r|   r   related_fkey_lookupsr   r
   url_params_from_lookup_dictrc   splitr$   	get_fieldr   is_relationr   target_fieldsappendgetattrto_optslendate_hierarchylist_filterrp   type
issubclassaddparameter_namelisttuplejoin
isdisjoint)r\   lookupvaluer   r|   	fk_lookuprelation_parts
prev_fieldpartfieldvalid_lookupsfilter_itemrJ   rJ   rK   lookup_allowedy  sJ   


zBaseModelAdmin.lookup_allowedc                    s   | j j}z||}W n
 ty   Y dS w |jrdS |jD ]}| |kr) dS qt }| jj	
 D ]\}}|| |jD ]}	||	j  q?q3dd |jddD }
|
D ]"}|j |jj}t fdd|D rxt|drx| |krx dS qVdS )z
        Return True if the model associated with this admin should be
        allowed to be referenced by the specified field.
        FTc                 s   s     | ]}|j r|js|V  qd S rY   )r   concrete.0frJ   rJ   rK   	<genexpr>  s    
z2BaseModelAdmin.to_field_allowed.<locals>.<genexpr>)include_hiddenc                 3   s    | ]}t | V  qd S rY   )r   )r   r|   related_modelrJ   rK   r         get_related_field)r|   r   r   r   primary_keymany_to_manym2m_target_field_namesetrx   ry   rc   r   r   r   r   r   r{   anyr   r  )r\   r   to_fieldoptsr   r  registered_modelsr|   admininlinerelated_objectsrelated_objectr{   rJ   r   rK   to_field_allowed  s>   



zBaseModelAdmin.to_field_allowedc                 C   s&   | j }td|}|jd|j|f S )z
        Return True if the given request has permission to add an object.
        Can be overridden by the user in subclasses.
        r   %s.%sr  r   userhas_perm	app_label)r\   r   r  codenamerJ   rJ   rK   r}     s   
z!BaseModelAdmin.has_add_permissionc                 C   &   | j }td|}|jd|j|f S )a  
        Return True if the given request has permission to change the given
        Django model instance, the default implementation doesn't examine the
        `obj` parameter.

        Can be overridden by the user in subclasses. In such case it should
        return True if the given request has permission to change the `obj`
        model instance. If `obj` is None, this should return True if the given
        request has permission to change *any* object of the given type.
        changer  r  r\   r   rI   r  r  rJ   rJ   rK   r~        
z$BaseModelAdmin.has_change_permissionc                 C   r  )a  
        Return True if the given request has permission to change the given
        Django model instance, the default implementation doesn't examine the
        `obj` parameter.

        Can be overridden by the user in subclasses. In such case it should
        return True if the given request has permission to delete the `obj`
        model instance. If `obj` is None, this should return True if the given
        request has permission to delete *any* object of the given type.
        deleter  r  r  rJ   rJ   rK   r     r  z$BaseModelAdmin.has_delete_permissionc                 C   sF   | j }td|}td|}|jd|j|f p"|jd|j|f S )a  
        Return True if the given request has permission to view the given
        Django model instance. The default implementation doesn't examine the
        `obj` parameter.

        If overridden by the user in subclasses, it should return True if the
        given request has permission to view the `obj` model instance. If `obj`
        is None, it should return True if the request has permission to view
        any object of the given type.
        viewr  r  r  )r\   r   rI   r  codename_viewcodename_changerJ   rJ   rK   r     s   

z"BaseModelAdmin.has_view_permissionc                 C   s   |  ||p| ||S rY   )r   r~   r   rJ   rJ   rK   has_view_or_change_permission  s   z,BaseModelAdmin.has_view_or_change_permissionc                 C   s   |j | jjS )a  
        Return True if the given request has any permission in the given
        app label.

        Can be overridden by the user in subclasses. In such case it should
        return True if the given request has permission to view the module on
        the admin index page and access the module's index page. Overriding it
        does not restrict access to the add, change or delete views. Use
        `ModelAdmin.has_(add|change|delete)_permission` for that.
        )r  has_module_permsr  r  r   rJ   rJ   rK   has_module_permission  s   z$BaseModelAdmin.has_module_permissionrY   )3rS   rT   rU   __doc__r   rw   r   r   r   r   	ModelFormr   r   r   r   r   rb   r   r   r   r   show_full_result_countr   rZ   r[   ri   r   ro   r   rt   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r}   r~   r   r   r  r  rJ   rJ   rJ   rK   rX   d   s\    9*

	




6-
	


rX   )	metaclassc                       s  e Z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ZdZeZdZg ZdZdZdZdZdZdZdZg ZejZdZdZ dZ!e"Z# fd	d
Z$dd Z%dddZ&dd Z'e(dd Z)e(dd Z*dd Z+dd Z,dddZ-dd Z.dd Z/ddd Z0d!d" Z1d#d$ Z2dd%d&Z3dd(d)Z4d*d+ Z5d,d- Z6d.d/ Z7e8e9d0d1d2d3 Z:e;d4d5 Z<d6d7 Z=d8d9 Z>d:d; Z?e@jAfd<d=ZBd>d? ZCd@dA ZDdBdC ZEdDdE ZFdFdG ZGdHdI ZHdJdK ZIdLdM ZJddNdOZKeLjMdPdfdQdRZNdSdT ZOdUdV ZPdWdX ZQdYdZ ZRd[d\ ZSd]d^ ZTdd_d`ZUddadbZVdcdd ZWdedf ZXdgdh ZYdidj ZZdkdl Z[dmdn Z\dodp Z]ddqdrZ^dsdt Z_dudv Z`eaddwdxZbdydz Zcdd{d|Zddd}d~Zedd Zfdd ZgeadddZhdd ZieadddZjdd ZkdddZldd Zm  ZnS )
ModelAdminzBEncapsulate all admin options and functionality for a given model.)__str__rJ   Fd      NTc                    s"   || _ |j| _|| _t   d S rY   )r|   r   r  rx   superri   )r\   r|   rx   rs   rJ   rK   ri   K  s   zModelAdmin.__init__c                 C   s   d| j jj| jjf S )Nr  )r|   r   r  rs   rS   r   rJ   rJ   rK   r%  Q  r^   zModelAdmin.__str__c                 C   sn   g }|  ||D ],}|| j| j}|r/|||s&|||s&|||s&q|||s/d|_|| q|S )Nr   )r   r|   rx   r  r}   r   max_numr   )r\   r   rI   inline_instancesinline_classr  rJ   rJ   rK   get_inline_instancesT  s   

zModelAdmin.get_inline_instancesc                    s   ddl m}  fdd} jjj jjjf}|d| jd| d|d| jd	| d|d
| jd| d|d| j	d| d|d| j
d| d|d|tjd jjf|  dgS )Nr   )pathc                    s    fdd}|_ t| S )Nc                     s    j | i |S rY   )rx   
admin_view)argsr]   )r\   r  rJ   rK   wrapperg  r^   z2ModelAdmin.get_urls.<locals>.wrap.<locals>.wrapper)model_adminr   )r  r1  r   )r  rK   wrapf  s   
z!ModelAdmin.get_urls.<locals>.wrapr   z%s_%s_changelist)rv   zadd/z	%s_%s_addz<path:object_id>/history/z%s_%s_historyz<path:object_id>/delete/z%s_%s_deletez<path:object_id>/change/z%s_%s_changez<path:object_id>/z%s:%s_%s_change)pattern_name)django.urlsr.  r|   r   r  
model_namechangelist_viewadd_viewhistory_viewdelete_viewchange_viewr?   as_viewrx   rv   )r\   r.  r3  inforJ   r   rK   get_urlsc  s   
zModelAdmin.get_urlsc                 C   s   |   S rY   )r>  r   rJ   rJ   rK   urlsz  s   zModelAdmin.urlsc              	   C   s@   t jrdnd}d| dddddd	d
| g}tjdd |D dS )Nr   .minvendor/jquery/jquery%s.jsjquery.init.jszcore.jszadmin/RelatedObjectLookups.jsz
actions.jsz	urlify.jszprepopulate.jszvendor/xregexp/xregexp%s.jsc                 S      g | ]}d | qS zadmin/js/%srJ   r   urlrJ   rJ   rK   
<listcomp>      z$ModelAdmin.media.<locals>.<listcomp>js)r   DEBUGr   Mediar\   extrarJ  rJ   rJ   rK   media~  s   
zModelAdmin.mediac                 C   s&   |  || || || |dS )z
        Return a dict of all perms for this model. This dict has the keys
        ``add``, ``change``, ``delete``, and ``view`` mapping to the True/False
        for each of those actions.
        )r   r  r  r  )r}   r~   r   r   r   rJ   rJ   rK   get_model_perms  s
   zModelAdmin.get_model_permsc                 C   s   | j ||d dS N)r   )get_formr   rJ   rJ   rK   r     rQ   z#ModelAdmin._get_form_for_get_fieldsc              
      s`  d|v r
| d}nt ||} ||}|du rg nt|} ||}|| |r?t|dr? ||s?|| |du rVt j	drV j	j
jrV| j	j
j |pYd}t fdd|D }	t j	j j	f|	}
|
||t j|dd|}|d du rt|d	 stj|d< z
t jfi |W S  ty } z
td
| jjf d}~ww )zv
        Return a Form class for use in the admin add view. This is used by
        add_view and change_view.
        r   Nr  r   c                 3   s     | ]}| j jv r|V  qd S rY   )r   declared_fieldsr   r   rJ   rK   r     s    z&ModelAdmin.get_form.<locals>.<genexpr>r   )r   r   r   formfield_callbackr   z:%s. Check fields/fieldsets/exclude attributes of class %s.)popr   r   r   r   r   extendr   r~   r   r   r   dictfromkeysr   rS   r   r   r)   r   
ALL_FIELDSr*   r|   r   rs   )r\   r   rI   r  r]   r   excludedr   r   	new_attrsr   defaultserJ   r   rK   rR    sB   



zModelAdmin.get_formc                 K   s   ddl m} |S )zM
        Return the ChangeList class for use on the changelist page.
        r   )
ChangeList)django.contrib.admin.views.mainr_  )r\   r   r]   r_  rJ   rJ   rK   get_changelist  s   zModelAdmin.get_changelistc                 C   sz   |  |}| ||}| |rdg|}| |}| |}||| j||| || j| || 	|| j
| j| j| |S )zs
        Return a `ChangeList` instance based on `request`. May raise
        `IncorrectLookupParameters`.
        action_checkbox)r   get_list_display_linksget_actionsr   ra  r|   get_list_filterr   get_search_fieldsget_list_select_relatedlist_per_pagelist_max_show_alllist_editable)r\   r   list_displaylist_display_linksr   r_  rJ   rJ   rK   get_changelist_instance  s*   




z"ModelAdmin.get_changelist_instancec              
   C   sl   |  |}|j}|du r|jjn|j|}z||}|jdi |j|iW S  |jt	t
fy5   Y dS w )z
        Return an instance matching the field and value provided, the primary
        key is used if no field is provided. Return ``None`` if no match is
        found or the object_id fails validation.
        NrJ   )r   r|   r   r   r   	to_pythonrz   rv   DoesNotExistr   
ValueError)r\   r   r   
from_fieldr   r|   r   rJ   rJ   rK   
get_object  s   

zModelAdmin.get_objectc                 K   sN   dt | j|di|}|ddu rt|dstj|d< t| jfi |S )zT
        Return a Form class for use in the Formset on the changelist page.
        rU  rT  r   Nr   )r   r   rz   r)   r   rZ  r*   r|   r\   r   r]   r]  rJ   rJ   rK   get_changelist_form  s   
zModelAdmin.get_changelist_formc                 K   s8   dt | j|di|}t| j| |fd| jd|S )zi
        Return a FormSet class for use on the changelist page if list_editable
        is used.
        rU  rT  r   )rN  r   )r   r   r+   r|   rt  rj  rs  rJ   rJ   rK   get_changelist_formset  s   z!ModelAdmin.get_changelist_formsetc                 c   s*    |  ||D ]}||||fV  qdS )z?
        Yield formsets and the corresponding inlines.
        N)r-  get_formset)r\   r   rI   r  rJ   rJ   rK   get_formsets_with_inlines  s   z$ModelAdmin.get_formsets_with_inlinesr   c                 C   s   |  ||||S rY   )	paginator)r\   r   r   per_pageorphansallow_empty_first_pagerJ   rJ   rK   get_paginator!  rQ   zModelAdmin.get_paginatorc                 C   8   ddl m}m} |jj|jjt|j|jt|||dS )z
        Log that an object has been successfully added.

        The default implementation creates an admin LogEntry object.
        r   )ADDITIONLogEntryuser_idr   r   object_repraction_flagchange_message)	django.contrib.admin.modelsr~  r  rG   
log_actionr  r   rL   str)r\   r   objectmessager~  r  rJ   rJ   rK   log_addition$     zModelAdmin.log_additionc                 C   r}  )z
        Log that an object has been successfully changed.

        The default implementation creates an admin LogEntry object.
        r   )CHANGEr  r  )	r  r  r  rG   r  r  r   rL   r  )r\   r   r  r  r  r  rJ   rJ   rK   
log_change4  r  zModelAdmin.log_changec                 C   s2   ddl m}m} |jj|jjt|j|j||dS )z
        Log that an object will be deleted. Note that this method must be
        called before the deletion.

        The default implementation creates an admin LogEntry object.
        r   )DELETIONr  )r  r   r   r  r  )r  r  r  rG   r  r  r   rL   )r\   r   r  r  r  r  rJ   rJ   rK   log_deletionD  s   zModelAdmin.log_deletionz*<input type="checkbox" id="action-toggle">)descriptionc                 C   s   t jt jt|jS )zE
        A list_display column containing a checkbox widget.
        )r	   checkboxrenderACTION_CHECKBOX_NAMEr  r   r   rJ   rJ   rK   rb  T  s   zModelAdmin.action_checkboxc                 C   s   t | dt|ddS )Nshort_descriptionr    )r   r7   replace)funcrv   rJ   rJ   rK   _get_action_description[  s   z"ModelAdmin._get_action_descriptionc                    s~   g } fdd j pg D }dd |D }dd |D } jj D ]\}}||v r)q  ||}||||f q || |S )zAReturn the list of actions, prior to any request-based filtering.c                 3   s    | ]}  |V  qd S rY   )
get_actionr   actionr   rJ   rK   r   b  r   z/ModelAdmin._get_base_actions.<locals>.<genexpr>c                 S   s   g | ]}|r|qS rJ   rJ   r  rJ   rJ   rK   rG  d  rH  z0ModelAdmin._get_base_actions.<locals>.<listcomp>c                 S   s   h | ]\}}}|qS rJ   rJ   )r   r   rv   rJ   rJ   rK   	<setcomp>e  s    z/ModelAdmin._get_base_actions.<locals>.<setcomp>)actionsrx   r  r   rW  )r\   r  base_actionsbase_action_namesrv   r  r  rJ   r   rK   _get_base_actions_  s   
zModelAdmin._get_base_actionsc                    sd   g }|D ]+}|d }t |ds|| qfdd|jD }t fdd|D r/|| q|S )z<Filter out any actions that the user doesn't have access to.r   allowed_permissionsc                 3   s    | ]
}t  d | V  qdS )zhas_%s_permissionN)r   )r   
permissionr   rJ   rK   r   y  s
    
z<ModelAdmin._filter_actions_by_permissions.<locals>.<genexpr>c                 3   s    | ]}| V  qd S rY   rJ   )r   has_permissionrT  rJ   rK   r   }      )r   r   r  r  )r\   r   r  filtered_actionsr  r   permission_checksrJ   )r   r\   rK   _filter_actions_by_permissionsq  s   



z)ModelAdmin._filter_actions_by_permissionsc                 C   s6   | j du s
t|jv ri S | ||  }dd |D S )z
        Return a dictionary mapping the names of all actions for this
        ModelAdmin to a tuple of (callable, name, description) for each action.
        Nc                 S   s   i | ]\}}}||||fqS rJ   rJ   )r   r  rv   descrJ   rJ   rK   
<dictcomp>      z*ModelAdmin.get_actions.<locals>.<dictcomp>)r  IS_POPUP_VARGETr  r  )r\   r   r  rJ   rJ   rK   rd    s   zModelAdmin.get_actionsc                 C   sB   g | }|  | D ]\}}}||t| j f}|| q|S )zy
        Return a list of choices for use in a form object.  Each choice is a
        tuple (name, description).
        )rd  valuesr   r  r   )r\   r   default_choicesrn   r  rv   r  choicerJ   rJ   rK   get_action_choices  s
   zModelAdmin.get_action_choicesc                 C   sj   t |r
|}|j}n t| j|rt| j|}nz| j|}W n
 ty)   Y dS w | ||}|||fS )z
        Return a given action from a parameter, which can either be a callable,
        or the name of a method on the ModelAdmin.  Return is a tuple of
        (callable, name, description).
        N)	r   rS   r   rs   r   rx   r  KeyErrorr  )r\   r  r  r  rJ   rJ   rK   r    s   
zModelAdmin.get_actionc                 C   r   )zd
        Return a sequence containing the fields to be displayed on the
        changelist.
        )rk  r   rJ   rJ   rK   r     r   zModelAdmin.get_list_displayc                 C   s*   | j s
| j du s
|s| j S t|dd S )z
        Return a sequence containing the fields to be displayed as links
        on the changelist. The list_display parameter is the list of fields
        returned by get_list_display().
        NrB   )rl  r   )r\   r   rk  rJ   rJ   rK   rc    s   z!ModelAdmin.get_list_display_linksc                 C   r   )z
        Return a sequence containing the fields to be displayed as filters in
        the right sidebar of the changelist page.
        )r   r   rJ   rJ   rK   re    r   zModelAdmin.get_list_filterc                 C   r   )zt
        Return a list of fields to add to the select_related() part of the
        changelist items query.
        )list_select_relatedr   rJ   rJ   rK   rg    r   z"ModelAdmin.get_list_select_relatedc                 C   r   )zz
        Return a sequence containing the fields to be searched whenever
        somebody submits a search query.
        )search_fieldsr   rJ   rJ   rK   rf    r   zModelAdmin.get_search_fieldsc                    s   fddd} |}|rQ|rQfdd|D }t|D ]%  dr1 d  d kr1t   fd	d|D }ttj|q|tfd
d|D O }|fS )z
        Return a tuple containing a queryset to implement the search
        and a boolean indicating if the results may contain duplicates.
        c              	      s   |  drd| dd   S |  drd| dd   S |  dr'd| dd   S  jj}| t}d }|D ]6}|dkr>|jj}z||}W n ty[   |rY|	|rY|  Y   S Y q4w |}t
|d	rj| d
 j}q4d|  S )N^z%s__istartswithrB   =z
%s__iexact@z
%s__searchr   r   r   z%s__icontains)
startswithr|   r   r   r$   r   rv   r   r   
get_lookupr   r   r   )
field_namer  lookup_fieldsr   	path_partr   r   rJ   rK   construct_search  s0   




z7ModelAdmin.get_search_results.<locals>.construct_searchFc                    s   g | ]} t |qS rJ   )r  )r   search_field)r  rJ   rK   rG    s    z1ModelAdmin.get_search_results.<locals>.<listcomp>)"'r   r   c                    s    g | ]}t jd i | iqS )rJ   )r!   Q)r   
orm_lookup)bitrJ   rK   rG    s    c                 3   s    | ]	}t  j|V  qd S rY   )r   r  )r   search_specr   rJ   rK   r   	  s
    

z0ModelAdmin.get_search_results.<locals>.<genexpr>)	rf  r:   r  r;   filterr   operatoror_r  )r\   r   r   search_termmay_have_duplicatesr  orm_lookups
or_queriesrJ   )r  r  r   r\   rK   get_search_results  s$   



zModelAdmin.get_search_resultsc                 C   sl   |j }| jr4|r4| jj}d|j|jf }d|j|jf }||kr&|j	 }n|j
d}|r4t	d|iS dS )z;
        Return the preserved filters querystring.
        z%s:%sadmin:%s_%s_changelist_changelist_filtersr   )resolver_matchpreserve_filtersr|   r   app_nameurl_namer  r6  r  r5   rz   )r\   r   matchr  current_urlchangelist_urlpreserved_filtersrJ   rJ   rK   get_preserved_filters  s   
z ModelAdmin.get_preserved_filtersc                 C   s   t |||S )zV
        Construct a JSON structure describing changes from a changed object.
        )r   )r\   r   r   formsetsr   rJ   rJ   rK   r   !     z#ModelAdmin.construct_change_messager   c                 C   sv   t |ts/z
ttj| }W n ty.   tjj }d	dd |D }t
d||f w tj|||||d dS )a  
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.

        Exposes almost the same API as messages.add_message(), but accepts the
        positional arguments in a different order to maintain backwards
        compatibility. For convenience, it accepts the `level` argument as
        a string rather than the usual level number.
        z, c                 s   s    | ]}d | V  qdS )z`%s`NrJ   )r   levelrJ   rJ   rK   r   8  r  z*ModelAdmin.message_user.<locals>.<genexpr>z7Bad message level string: `%s`. Possible values are: %s)
extra_tagsfail_silentlyN)rp   intr   r   	constantsupperr   DEFAULT_TAGSr  r   rp  add_message)r\   r   r  r  r  r  levelslevels_reprrJ   rJ   rK   message_user'  s   
zModelAdmin.message_userc                 C   s   |j ddS )z
        Given a ModelForm return an unsaved instance. ``change`` is True if
        the object is being changed, and False if it's being added.
        F)commitsave)r\   r   r   r  rJ   rJ   rK   	save_form@     zModelAdmin.save_formc                 C      |   dS )zA
        Given a model instance save it to the database.
        Nr  )r\   r   rI   r   r  rJ   rJ   rK   
save_modelG  r  zModelAdmin.save_modelc                 C   r  )zE
        Given a model instance delete it from the database.
        Nr  r   rJ   rJ   rK   delete_modelM  r  zModelAdmin.delete_modelc                 C   r  )z.Given a queryset, delete it from the database.Nr  r\   r   r   rJ   rJ   rK   delete_querysetS  s   zModelAdmin.delete_querysetc                 C   s   |   dS )zB
        Given an inline formset save it to the database.
        Nr  )r\   r   r   formsetr  rJ   rJ   rK   save_formsetW  r  zModelAdmin.save_formsetc                 C   s(   |   |D ]}| j||||d qdS )aO  
        Given the ``HttpRequest``, the parent ``ModelForm`` instance, the
        list of inline formsets and a boolean value based on whether the
        parent is being added or changed, save the related objects to the
        database. Note that at this point save_form() and save_model() have
        already been called.
        r  N)save_m2mr  )r\   r   r   r  r  r  rJ   rJ   rK   save_related]  s   zModelAdmin.save_relatedc                 C   sn  | j j}|j}| |}	t|	|d|}| |}
d}|d D ]}|js*|js*|jr.d} nq|	i d|d|d| 
||d| |d	| ||d
| ||d|d|d j phtdd |d D d|
d ud|
d|d|dt| j jd| jd| jdtdtd|i |r| jd ur| j}n| j}| jj|_t||pd||jf d| dg|S )Nr  r  Finline_admin_formsetsTr   r  r   r}   r~   r   "has_editable_inline_admin_formsetshas_file_field	adminformc                 s   s    | ]}|j  V  qd S rY   )r  is_multipart)r   admin_formsetrJ   rJ   rK   r   |  s
    
z0ModelAdmin.render_change_form.<locals>.<genexpr>has_absolute_urlabsolute_urlform_urlr  r   save_assave_on_topto_field_varis_popup_varr  zadmin/%s/%s/change_form.htmlzadmin/%s/change_form.htmlzadmin/change_form.html)r|   r   r  r  r   r   r}   r~   r   re   r   r   r  r  rL   r   r  r   TO_FIELD_VARr  add_form_templatechange_form_templaterx   rv   current_appr1   r6  )r\   r   contextr   r  r  rI   r  r  r  view_on_site_urlr  r  form_templaterJ   rJ   rK   render_change_formi  sz   



zModelAdmin.render_change_formc                 C   s  |j }| |}td|j|jf t|jf| jjd}| 	||r*t
dt||}nt|}|j|d}t|jv rs|jt}	|	rFt|	}
n|j jj}
||
}tt|t|d}t|| jpnd|j|jf d|j dgd	|iS d
|jv sd|jv r| jr| 	||rtd}| 	||r|dtd 7 }| |t
|fi |tj |du r|}t||d|}t|S d|jv rt
tdfi |}| ||tj |j}t||d|}t|S t
tdfi |}| ||tj |  ||S )zD
        Determine the HttpResponse for the add_view stage.
        admin:%s_%s_changer0  r  <a href="{}">{}</a>rv   rI   )r   rI   admin/%s/%s/popup_response.htmladmin/%s/popup_response.htmladmin/popup_response.htmlpopup_response_data	_continue
_saveasnewu.   The {name} “{obj}” was added successfully.r  zYou may edit it again below.Nr  _addanotheruP   The {name} “{obj}” was added successfully. You may add another {name} below.)!r   r  r2   r  r6  r   r   rx   rv   r~   r4   urlquoter  r   r  POSTrz   r  attnameserializable_valuejsondumpsr1   popup_response_templatesave_as_continuer   r  r   SUCCESSr   r.   r.  response_post_save_add)r\   r   rI   post_url_continuer  r  obj_urlobj_reprmsg_dictr  attrr   r  r   redirect_urlrJ   rJ   rK   response_add  s~   









zModelAdmin.response_addc                 C   s  t |jv rJ|j}|jt}|rt|n|jj}|jj	d }|
|}tdt|t|t|d}t|| jpEd|j|jf d|j dgd|iS | jj}| |}	|jtdt|j|d	}
d
|jv rttdfi |
}| ||tj |j}t|	|d|}t|S d|jv rttdfi |
}| ||tj td|j|jf |jf| jjd}t|	|d|}t|S d|jv rttdfi |
}| ||tj td|j|jf | jjd}t|	|d|}t|S ttdfi |
}| ||tj |  ||S )zG
        Determine the HttpResponse for the change_view stage.
        r   r  )r  r   rI   	new_valuer  r  r  r  r  r  r  uM   The {name} “{obj}” was changed successfully. You may edit it again below.r  r  uK   The {name} “{obj}” was added successfully. You may edit it again below.r  r  r  uR   The {name} “{obj}” was changed successfully. You may add another {name} below.zadmin:%s_%s_addr  u0   The {name} “{obj}” was changed successfully.)!r  r  r   rz   r  r  r   r  r  r]   r  r  r  r1   r  r  r6  r|   r  r   r4   r  r.  r   r  r   r  r   r.   r2   rx   rv   response_post_save_change)r\   r   rI   r  r  r$  r   r'  r  r  r#  r   r%  rJ   rJ   rK   response_change  s   








zModelAdmin.response_changec                 C   sh   | j j}| |r(td|j|jf | jjd}| |}t	||d|}t
|S td| jjd}t
|S )Nr  r(  r  admin:index)r|   r   r  r2   r  r6  rx   rv   r  r   r.   )r\   r   rI   r  post_urlr  rJ   rJ   rK   _response_post_save0  s   


zModelAdmin._response_post_savec                 C      |  ||S )zy
        Figure out where to redirect after the 'Save' button has been pressed
        when adding a new object.
        r-  r   rJ   rJ   rK   r  =  r  z!ModelAdmin.response_post_save_addc                 C   r.  )z
        Figure out where to redirect after the 'Save' button has been pressed
        when editing an existing object.
        r/  r   rJ   rJ   rK   r)  D  r  z$ModelAdmin.response_post_save_changec                 C   sT  zt |jdd}W n ty   d}Y nw |j }|tjd |dd z|d|	d| i W n	 t
y@   Y nw | j|dd}| ||jd _| r|jd }|jd }| || d }|j	tj}	|	s|std}
| ||
tj dS |s|j|	d}|| ||}t|tr|S t| S td	}
| ||
tj dS )
z
        Handle an admin action. This is called if a request is POSTed to the
        changelist; it returns an HttpResponse if the action was handled, and
        None otherwise.
        indexr   Nr  auto_idselect_acrossWItems must be selected in order to perform actions on them. No items have been changed.pk__inzNo action selected.)r  r  rz   rp  r_   rV  r	   r  re   getlist
IndexErroraction_formr  r   rn   is_validcleaned_datard  r   r  r   WARNINGr  rp   r/   r.   get_full_path)r\   r   r   action_indexdatar9  r  r3  r  selectedr   responserJ   rJ   rK   response_actionK  sB   




zModelAdmin.response_actionc                 C   s   | j j}t|jv r*tdt|d}t|| jp%d|j	|j
f d|j	 dgd|iS | |td|j|d tj | |d	r_td
|j	|j
f | jjd}| |}t||d|}t|S td| jjd}t|S )zG
        Determine the HttpResponse for the delete_view stage.
        r  )r  r   r  r  r  r  u4   The %(name)s “%(obj)s” was deleted successfully.r  Nr  r(  r  r+  )r|   r   r  r  r  r  r  r1   r  r  r6  r  r   r   r   r  r~   r2   rx   rv   r  r   r.   )r\   r   obj_displayobj_idr  r  r,  r  rJ   rJ   rK   response_delete  sD   

	

zModelAdmin.response_deletec                 C   sR   | j j}|j}| jj|_|jtt| j	d t
|| jp&d||jd|dg|S )N)r  r  rO  z$admin/{}/{}/delete_confirmation.htmlz!admin/{}/delete_confirmation.htmlzadmin/delete_confirmation.html)r|   r   r  rx   rv   r  re   r  r  rO  r1   delete_confirmation_templateformatr6  )r\   r   r  r  r  rJ   rJ   rK   render_delete_form  s"   
zModelAdmin.render_delete_formc                 C   s   |r|  ||n| |}g }t||D ]W\}}t|||}	t|||}
|r=|||}| ||}|||}nd } }}d |_|_|	||}t
|||}tj|||	||
| ||||d
}|| q|S )NFr   )r2  r}   r~   r   r   )r~   r}   zipr   r   r   r   rN  r*  r   rX  r   r	   InlineAdminFormSetr   )r\   r   r  r+  rI   can_edit_parentr  r  r  r   readonlyr}   r~   r   r   prepopulatedinline_admin_formsetrJ   rJ   rK   get_inline_formsets  s(   zModelAdmin.get_inline_formsetsc              	   C   s`   t |j }|D ]$}z	| jj|}W n	 ty   Y q	w t|tj	r-|| 
d||< q	|S )zJ
        Get the initial form data from the request's GET params.
        ,)rX  r  rc   r|   r   r   r   rp   r!   rr   r   )r\   r   initialrg   r   rJ   rJ   rK   get_changeform_initial_data  s   z&ModelAdmin.get_changeform_initial_datac                 C   s@   t d|jt|d }| ||tj td| jjd}t	|S )z
        Create a message informing the user that the object doesn't exist
        and return a redirect to the admin index page.
        uG   %(name)s with ID “%(key)s” doesn’t exist. Perhaps it was deleted?)rv   keyr+  r(  )
r   r   r   r  r   r<  r2   rx   rv   r.   )r\   r   r  r   r   rF  rJ   rJ   rK    _get_obj_does_not_exist_redirect  s   z+ModelAdmin._get_obj_does_not_exist_redirectc                 C   sH   t jt| jd | ||||W  d    S 1 sw   Y  d S Nr   )r#   atomicr"   db_for_writer|   _changeform_viewr\   r   r   r  extra_contextrJ   rJ   rK   changeform_view  s   $zModelAdmin.changeform_viewc                 C   s  |j t|jt}|r| ||std| | j}|j}|jdkr+d|j v r+d }|d u }|r;| 	|s8t
d }	n*| |t||}	|jdkrR| ||	sQt
n| ||	sZt
|	d u re| |||S | ||	}
| j||	| t|
d}|jdkr||j |j|	d}| }|r| j||| d}n|j}| j||| d\}}t|r|r| ||||  | ||||  | ||||}|r| ||| | ||S | ||| | ||S d}n'|r|  |}||d}| j||jdd\}}n||	d}| j||	d	d\}}|s| ||	st|
}n| !||	}t"j#|t$|
|s.| ||	r4| %||	ni || d
}| j&|j& }| '||||	}|D ]}||j& }qJ|r[t(d}n| ||	rgt(d}nt(d}i | j)*|||j+ |	r}t,|	nd |||	t-|j v pt-|jv |||t".||| /|d}|jdkr|sd|j v rd|d< d|d< d}|0|pi  | j1|||| |	|dS )N"The field %s cannot be referenced.r  r  )r  r   )instancer  F)rQ  T)r2  zAdd %sz	Change %szView %s)titlesubtitler  r   originalis_popupr  rO  r  errorsr  	show_saveshow_save_and_continue)r   r  rI   r  )2r  rz   r  r  r  r   r|   r   methodr}   r   rr  r   r~   r  rT  r   rR  r   FILESr:  r  r]  _create_formsetsr&   r  r  r   r  r&  r  r*  rR  r   r	   	AdminFormr   r   rO  rO  r   rx   each_contextr   r  r  AdminErrorListr  re   r
  )r\   r   r   r  rZ  r  r|   r  r   rI   r   r!  r   form_validated
new_objectr  r+  r  rQ  r   	adminFormrO  inline_formsetsinline_formsetr^  r  rJ   rJ   rK   rX    s   






"



zModelAdmin._changeform_viewc                 C   s   |  |d ||S rY   r[  )r\   r   r  rZ  rJ   rJ   rK   r8  x  rQ   zModelAdmin.add_viewc                 C   s   |  ||||S rY   rp  rY  rJ   rJ   rK   r;  {  rQ   zModelAdmin.change_viewc                    s8   t dt || jjjj  fdd|j	 D S )z6Return POST data values of list_editable primary keys.z
{}-\d+-{}$c                    s   g | ]\}}  |r|qS rJ   )r  )r   rS  r   
pk_patternrJ   rK   rG    r  z5ModelAdmin._get_edited_object_pks.<locals>.<listcomp>)
recompilerG  escaper|   r   r   rv   r  rc   )r\   r   prefixrJ   rq  rK   _get_edited_object_pks~  s   z!ModelAdmin._get_edited_object_pksc                 C   s\   |  ||}| |}|jjjj}z|D ]}|| qW n ty'   | Y S w |j|dS )zr
        Based on POST data, return a queryset of the objects that were edited
        via list_editable.
        r5  )rw  r   r|   r   r   rn  r   r  )r\   r   rv  
object_pksr   validater   rJ   rJ   rK   _get_list_editable_queryset  s   

z&ModelAdmin._get_list_editable_querysetc                 C   s   ddl m} | jj}|j}| |stz| |}W n$ ty?   ||j	v r2t
ddtdi Y S t|jd | d  Y S w d}|jtj}| |}	|	r|jd	krd
|jv rd|jvr|rr| j|||d}
|
ro|
S d}ntd}| ||tj d}|	r|jd	krtj|jv rd
|jvrd|jvr|r| j|||d}
|
r|
S d}|rt| S d }|_|jd	krD|jrDd|jv rD| |st| |}| || }||j|j |d }|_|! rCd}|j"D ]3}|# r#| j$||dd}| j%|||dd | j&||g dd | '||d}| (||| |d7 }q|r=t)dd||t*||d }| ||tj+ t| S n|jr\| |r\| |}||j,d }|_|rf| j-|j- }n| j-}|	r| j.dd}| /||j0d _1||j-7 }nd}t)dd|j2}i | j34|i dt5|j6dtddt7|j,i d|d|j2i d|j8d dd!|j9d"|j:d#|d$|d%| ;|d&|j<d'|d(| j=d)| j>d*| j?d+| @||pi }| j3jA|_BtC|| jDpd,||jEf d-| d.g|S )/z>
        The 'change list' admin view for this model.
        r   )
ERROR_FLAGzadmin/invalid_setup.htmlr^  zDatabase error?z=1Fr  r0  _saver  Tr4  Nr  )r  r  rB   z,%(count)s %(name)s was changed successfully.z-%(count)s %(name)s were changed successfully.)countrv   r1  r  z%(total_count)s selectedzAll %(total_count)s selectedmodule_nameselection_notez0 of %(cnt)s selectedcntselection_note_alltotal_countr_  ra  r  clrO  r}   r  r9  actions_on_topactions_on_bottomactions_selection_counterr  zadmin/%s/%s/change_list.htmlzadmin/%s/change_list.htmlzadmin/change_list.html)Fr`  r{  r|   r   r  r  r   rm  rR   r  r0   r   r.   r.  r  r7  r	   r  rd  re  rB  r   r  r   r<  r=  r  rj  r~   ru  rz  get_default_prefixrf  r:  r   has_changedr  r  r  r   r  r=   r   r  result_listrO  r9  r  r   rn   result_countrx   ri  r  verbose_name_pluralr   r^  ra  r  r}   r  r  r  r  r  rv   r  r1   change_list_templater6  )r\   r   rZ  r{  r  r  r  action_failedr@  r  rA  r   r  FormSetmodified_objectschangecountr   rI   
change_msgrO  r9  r  r  rJ   rJ   rK   r7    s  




 







	



zModelAdmin.changelist_viewc                 C   s   t ||| jS )zw
        Hook for customizing the delete process for the delete view and the
        "delete selected" action.
        )r   rx   )r\   objsr   rJ   rJ   rK   r   1  s   zModelAdmin.get_deleted_objectsc                 C   sF   t jt| jd | |||W  d    S 1 sw   Y  d S rU  )r#   rV  r"   rW  r|   _delete_view)r\   r   r   rZ  rJ   rJ   rK   r:  8  s   $zModelAdmin.delete_viewc                 C   s|  | j j}|j}|jt|jt}|r | ||s td| | 	|t
||}| ||s1t|du r<| |||S | |g|\}}	}
}|jrw|sw|
rPtt|}|rZt|n|jj}||}| ||| | || | |||S t|j}|
s|rtdd|i }ntd}i | j||d|||t|	 |
|||| |t|jv pt|jv |d|pi }| ||S )z'The 'delete' admin view for this model.r\  NzCannot delete %(name)srv   zAre you sure?)r^  r_  object_namer  deleted_objectsmodel_countperms_lacking	protectedr  r  r  ra  r  )r|   r   r  r  rz   r  r  r  r   rr  r   r   r   rT  r   r  r   r  r  r  r  rE  r   r   rx   ri  rX  rc   r  r  rH  )r\   r   r   rZ  r  r  r  rI   r  r  perms_neededr  rC  r$  rD  r  r^  r  rJ   rJ   rK   r  =  sX   




zModelAdmin._delete_viewc              
   C   s   ddl m} | j}| |t|}|du r| ||j|S | ||s%t|j}|j	}|j
jt|t|d d}	i | j|td| d|	tt|j||| |d|p\i }
| jj|_t|| jpsd||jf d	| d
g|
S )z(The 'history' admin view for this model.r   )r  N)r   content_typeaction_timezChange history: %s)r^  r_  action_listr  r  r  r  zadmin/%s/%s/object_history.htmlzadmin/%s/object_history.htmlzadmin/object_history.html)r  r  r|   rr  r   rT  r   r  r   r  rG   r  rL   select_relatedr   rx   ri  r   r  r7   r  r  rv   r  r1   object_history_templater6  )r\   r   r   rZ  r  r|   rI   r  r  r  r  rJ   rJ   rK   r9  x  sH   


	

zModelAdmin.history_viewc                    s(  g }g }i }|g}|r| | | j| D ]z\} | }	||	dd ||	< ||	 dks/|	s7d|	||	 f }	||	 |d}
|jdkrU|
|j |j	d|jv d |di |
} fdd	} 
||ri|nd
st|jD ]\}}|||||r}qqi |_|j|_qq| | |   q||fS )z9Helper function to generate formsets for add/change_view.r   rB   z%s-%s)r]  rv  r   r  r  )r?  filessave_as_newc                    s      | |od|j|| jv S )z0Return whether or not the user deleted the form.z{}-{}-DELETE)r   rG  rv  r  )r   rI   r  r0  r  rJ   rK   user_deleted_form  s   z6ModelAdmin._create_formsets.<locals>.user_deleted_formNrJ   )r   rw  r  rz   r   re  re   r  r_   rf  r~   	enumerateinitial_forms_errorsrQ  r;  )r\   r   rI   r  r  r+  prefixesget_formsets_argsr  rv  formset_paramsr  r  r0  r   rJ   r  rK   rg    s@   

	

zModelAdmin._create_formsetsrY   NF)r   T)F)FFr   N)Nr   N)r   N)orS   rT   rU   r   rk  rl  r   r  rh  ri  rj  r  r   r  r  r   r    rx  r  r   r  r  r  rF  %delete_selected_confirmation_templater  r  r  r	   
ActionFormr9  r  r  r  r   rZ   ri   r%  r-  r>  propertyr?  rO  rP  r   rR  ra  rm  rr  rt  ru  rw  r|  r  r  r  r   r6   rb  staticmethodr  r  r  rd  r!   BLANK_CHOICE_DASHr  r  r   rc  re  rg  rf  r  r  r   r   INFOr  r  r  r  r  r  r  r
  r&  r*  r-  r  r)  rB  rE  rH  rO  rR  rT  csrf_protect_mr[  rX  r8  r;  rw  rz  r7  r   r:  r  r9  rg  __classcell__rJ   rJ   r)  rK   r$  '  s    



0




4



/MKC)

r
 
;)r$  c                       s   e Zd ZdZdZdZeZdZdZ	dZ
dZdZdZdZdZeZdZ fddZedd	 Zd d
dZd ddZd ddZd ddZd ddZ fddZdd Z fddZd  fdd	Zd  fdd	Zd  fdd	Z   Z!S )!InlineModelAdminz
    Options for inline editing of ``model`` instances.

    Provide ``fk_name`` to specify the attribute name of the ``ForeignKey``
    from ``model`` to its parent. This is required if ``model`` has more than
    one ``ForeignKey`` to its parent.
    N   TFc                    sb   || _ || _| jj| _|| j| _t   | j	d u r"| jjj	| _	| j
d u r/| jjj
| _
d S d S rY   )rx   parent_modelr|   r   r  is_registeredhas_registered_modelr(  ri   r   r  )r\   r  rx   r)  rJ   rK   ri     s   



zInlineModelAdmin.__init__c                 C   sf   t jrdnd}d| ddg}| js| jr|ddg | jr(d| jv r(|d	 tjd
d |D dS )Nr   r@  rA  rB  z
inlines.jszSelectBox.jszSelectFilter2.jscollapsezcollapse.jsc                 S   rC  rD  rJ   rE  rJ   rJ   rK   rG    rH  z*InlineModelAdmin.media.<locals>.<listcomp>rI  )	r   rK  r   r   rW  classesr   r   rL  rM  rJ   rJ   rK   rO    s   
zInlineModelAdmin.mediac                 K   r   )z6Hook for customizing the number of extra inline forms.)rN  r\   r   rI   r]   rJ   rJ   rK   	get_extra  r   zInlineModelAdmin.get_extrac                 K   r   )z4Hook for customizing the min number of inline forms.)min_numr  rJ   rJ   rK   get_min_num  r   zInlineModelAdmin.get_min_numc                 K   r   )z:Hook for customizing the max number of extra inline forms.)r*  r  rJ   rJ   rK   get_max_num  r   zInlineModelAdmin.get_max_numc                    s  d|v r
| d}nt| ||}| ||}|du rg nt|}|| || |du rBt| jdrB| jj	j
rB|| jj	j
 |pEd}| joN| ||}| j| j| j||t| j|d| j||fi || j||fi || j||fi ||d
|}|d }	|r| ||nd|r| ||nd G  fdd	d	|	}
|
|d< |d du rt|d stj|d< t| j| jfi |S )
zCReturn a BaseInlineFormSet class for use in admin add/change views.r   Nr   rT  )
r   r  fk_namer   r   rU  rN  r  r*  
can_deleter   Tc                       s4   e Zd Zdd Z fddZ fddZ  ZS )z>InlineModelAdmin.get_formset.<locals>.DeleteProtectedModelFormc                 S   s   | j tdrUt| jj}t|d}| jj	j
rdS || jg |jrWg }|jD ]}|td|jj|d  q)| jjjj| jt|tdd}td}t|d	|d
dS dS )z
                We don't validate the 'DELETE' field itself because on
                templates it's not rendered using the field information, but
                just using a generic "deletion_field" of the InlineModelAdmin.
                Fr   Nz%(class_name)s %(instance)s)
class_namer]  and)r  r]  r  zxDeleting %(class_name)s %(instance)s would require deleting the following protected related objects: %(related_objects)sdeleting_protected)codeparams)r;  rz   r%   r"   rW  r   r|   r   r]  _stateaddingcollectr  r   r   r   r9   r   )r\   r   	collectorr  pr  r   rJ   rJ   rK   hand_clean_DELETE+  s.   



zPInlineModelAdmin.get_formset.<locals>.DeleteProtectedModelForm.hand_clean_DELETEc                    s   t   }|   |S rY   )r(  r:  r  )r\   resultr)  rJ   rK   r:  K  s   
zGInlineModelAdmin.get_formset.<locals>.DeleteProtectedModelForm.is_validc                    s.   s	| j jjs	dS s| j jjrdS t  S r  )r]  r  r  r(  r  r   )rs   can_add
can_changerJ   rK   r  P  s
   
zJInlineModelAdmin.get_formset.<locals>.DeleteProtectedModelForm.has_changed)rS   rT   rU   r  r:  r  r  rJ   r  r  r)  rK   DeleteProtectedModelForm)  s     r  )rV  r   r   r   r   rW  r   r   r   r   r   r  r   r  r  r   r   r  r  r  r~   r}   r)   r   rZ  r(   r  r|   )r\   r   rI   r]   r   r[  r   r  r]  base_model_formr  rJ   r  rK   rv    s>   /
zInlineModelAdmin.get_formsetc                 C   s   | j ||d djS rQ  )rv  r   r   rJ   rJ   rK   r   _  s   z)InlineModelAdmin._get_form_for_get_fieldsc                    s"   t  |}| |s| }|S rY   )r(  r   r  noner  r)  rJ   rK   r   b  s   
zInlineModelAdmin.get_querysetc                    sL   | j   jD ]}|jr|jj| jkr|jjj  nqt fdd|D S )a1  
        This method is called only when the ModelAdmin's model is for an
        ManyToManyField's implicit through model (if self.opts.auto_created).
        Return True if the user has any of the given permissions ('add',
        'change', etc.) for the model that points to the through model.
        c                 3   s,    | ]}j d  jt| f V  qdS )r  N)r  r  r  r   )r   permr  r   rJ   rK   r   u  s
    
zCInlineModelAdmin._has_any_perms_for_target_model.<locals>.<genexpr>)r  r   r{   r|   r  r   r  )r\   r   permsr   rJ   r  rK   _has_any_perms_for_target_modelh  s   

z0InlineModelAdmin._has_any_perms_for_target_modelc                    "   | j jr| |dgS t |S Nr  )r  r   r  r(  r}   r   r)  rJ   rK   r}   z  s   z#InlineModelAdmin.has_add_permissionc                    r  r  )r  r   r  r(  r~   r   r)  rJ   rK   r~     s   z&InlineModelAdmin.has_change_permissionc                    s$   | j jr| |dgS t ||S r  )r  r   r  r(  r   r   r)  rJ   rK   r     s   z&InlineModelAdmin.has_delete_permissionc                    s$   | j jr| |ddgS t |S )Nr  r  )r  r   r  r(  r   r   r)  rJ   rK   r     s   z$InlineModelAdmin.has_view_permissionrY   )"rS   rT   rU   r   r|   r  r'   r  rN  r  r*  templater   r  r  show_change_linkr   rZ   r  ri   r  rO  r  r  r  rv  r   r   r  r}   r~   r   r   r  rJ   rJ   r)  rK   r    s:    

	



Y	r  c                   @      e Zd ZdZdS )StackedInlinezadmin/edit_inline/stacked.htmlNrS   rT   rU   r  rJ   rJ   rJ   rK   r        r  c                   @   r  )TabularInlinezadmin/edit_inline/tabular.htmlNr  rJ   rJ   rJ   rK   r    r  r  )r_   r  r  rs  	functoolsr   r   r   urllib.parser   r  djangor   django.confr   django.contribr   django.contrib.adminr	   r
   django.contrib.admin.checksr   r   r   django.contrib.admin.decoratorsr   django.contrib.admin.exceptionsr   ,django.contrib.admin.templatetags.admin_urlsr   django.contrib.admin.utilsr   r   r   r   r   r   r   r   django.contrib.admin.widgetsr   r   django.contrib.authr   django.core.exceptionsr   r   r   r   django.core.paginatorr    	django.dbr!   r"   r#   django.db.models.constantsr$   django.forms.formsetsr%   r&   django.forms.modelsr'   r(   r)   r*   r+   django.forms.widgetsr,   r-   django.httpr.   django.http.responser/   django.template.responser0   r1   r5  r2   django.utils.decoratorsr3   django.utils.htmlr4   django.utils.httpr5   django.utils.safestringr6   django.utils.textr7   r8   r9   r:   r;   django.utils.translationr<   r   r=   django.views.decorators.csrfr>   django.views.genericr?   r  r  
HORIZONTALrN   rL   rP   	ExceptionrR   DateTimeFieldSplitDateTimeFieldAdminSplitDateTime	DateFieldAdminDateWidget	TimeFieldAdminTimeWidget	TextFieldAdminTextareaWidgetURLFieldAdminURLFieldWidgetIntegerFieldAdminIntegerFieldWidgetBigIntegerFieldAdminBigIntegerFieldWidget	CharFieldAdminTextInputWidget
ImageFieldAdminFileWidget	FileField
EmailFieldAdminEmailInputWidget	UUIDFieldAdminUUIDInputWidgetra   r  MediaDefiningClassrX   r$  r  r  r  rJ   rJ   rJ   rK   <module>   s    ,   F           3 I