o
    `ZhF                     @   sV  d dl Z d dlmZ d dlmZmZmZmZmZ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 d dlmZ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Z!G dd dej"Z#G dd dZ$G dd dZ%G dd dZ&G dd dZ'G dd dZ(G dd dZ)G dd de$Z*G dd de%Z+G d d! d!ej,j-Z.dS )"    N)forms)display_for_fieldflatten_fieldsetshelp_text_for_fieldlabel_for_fieldlookup_fieldquote)ObjectDoesNotExist)ForeignObjectRelManyToManyRelOneToOneField)flatatt)capfirstlinebreaksbr)NoReverseMatchreverse)conditional_escapeformat_html)	mark_safe)gettext)gettext_lazyZ_selected_actionc                   @   s8   e Zd ZejeddZejdddeddidZ	d	S )

ActionFormzAction:)label Fr   classzselect-across)r   requiredinitialwidgetN)
__name__
__module____qualname__r   ZChoiceField_actionZBooleanFieldZHiddenInputZselect_across r#   r#   S/var/www/html/lang_env/lib/python3.10/site-packages/django/contrib/admin/helpers.pyr      s    
r   c                   @   sf   e Zd Z		dddZdd Zdd Zedd	 Zed
d Zedd Z	edd Z
edd ZdS )	AdminFormNc                    sB    || _ | _ fdd| D | _|| _|d u rd}|| _d S )Nc                    s,   g | ]\}} |  fd d|D dqS )c                    s   g | ]} | qS r#   r#   .0fformr#   r$   
<listcomp>2   s    z1AdminForm.__init__.<locals>.<listcomp>.<listcomp>)fielddependenciesr#   )r'   
field_namer-   r)   r#   r$   r+   1   s    z&AdminForm.__init__.<locals>.<listcomp>r#   )r*   	fieldsetsitemsprepopulated_fieldsmodel_adminreadonly_fields)selfr*   r/   r1   r3   r2   r#   r)   r$   __init__(   s   

zAdminForm.__init__c                 C   s$   d| j j d| jj j d| jdS )N<z: form=z fieldsets=>)	__class__r    r*   r/   r4   r#   r#   r$   __repr__:   s   zAdminForm.__repr__c                 c   s6    | j D ]\}}t| j|f| j| jd|V  qd S )N)r3   r2   )r/   Fieldsetr*   r3   r2   r4   nameoptionsr#   r#   r$   __iter__A   s   
zAdminForm.__iter__c                 C      | j jS N)r*   errorsr9   r#   r#   r$   rB   K      zAdminForm.errorsc                 C   r@   rA   )r*   non_field_errorsr9   r#   r#   r$   rD   O   rC   zAdminForm.non_field_errorsc                 C   r@   rA   )r*   fieldsr9   r#   r#   r$   rE   S   rC   zAdminForm.fieldsc                 C   r@   rA   )r*   is_boundr9   r#   r#   r$   rF   W   rC   zAdminForm.is_boundc                 C   s    | j j}| D ]}||j7 }q|S rA   )r*   mediar4   rG   fsr#   r#   r$   rG   [   s   zAdminForm.mediaNN)r   r   r    r5   r:   r?   propertyrB   rD   rE   rF   rG   r#   r#   r#   r$   r%   '   s     





r%   c                   @   s6   e Zd Z						d	ddZedd Zdd ZdS )
r;   Nr#   c                 C   s6   || _ ||| _| _d|| _|| _|| _|| _d S )N )r*   r=   rE   joinclassesdescriptionr2   r3   )r4   r*   r=   r3   rE   rN   rO   r2   r#   r#   r$   r5   d   s   

zFieldset.__init__c                 C   s    d| j v rtjdgdS t S )NZcollapsezadmin/js/collapse.js)js)rN   r   ZMediar9   r#   r#   r$   rG   u   s   
zFieldset.mediac                 c   s*    | j D ]}t| j|| j| jdV  qd S )Nr2   )rE   	Fieldliner*   r3   r2   )r4   r,   r#   r#   r$   r?   {   s   

zFieldset.__iter__)Nr#   r#   r#   NN)r   r   r    r5   rK   rG   r?   r#   r#   r#   r$   r;   c   s    

r;   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	rR   Nc                    sb   | _ t|drt|tr|g _n| _t fdd jD   _| _|d u r,d}| _d S )Nr?   c                 3   s,    | ]}| j jv o j j| jjV  qd S rA   )r*   rE   r   	is_hidden)r'   r,   r9   r#   r$   	<genexpr>   s
    
z%Fieldline.__init__.<locals>.<genexpr>r#   )	r*   hasattr
isinstancestrrE   allZhas_visible_fieldr2   r3   )r4   r*   r,   r3   r2   r#   r9   r$   r5      s   

zFieldline.__init__c                 c   sV    t | jD ]"\}}|| jv rt| j||dk| jdV  qt| j||dkdV  qd S )Nr   )is_firstr2   )rY   )	enumeraterE   r3   AdminReadonlyFieldr*   r2   
AdminField)r4   ir,   r#   r#   r$   r?      s   

zFieldline.__iter__c                    s$   t d fdd jD dS )N
c                 3   s*    | ]}| j vr j| j V  qd S rA   )r3   r*   rB   as_ulr&   r9   r#   r$   rT      s    
z#Fieldline.errors.<locals>.<genexpr>)r   rM   rE   stripr9   r#   r9   r$   rB      s   zFieldline.errorsrJ   )r   r   r    r5   r?   rB   r#   r#   r#   r$   rR      s    
	rR   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r\   c                 C   s.   || | _ || _t| j j jtj| _d| _d S NF)r,   rY   rV   r   r   ZCheckboxInputis_checkboxis_readonly)r4   r*   r,   rY   r#   r#   r$   r5      s   

zAdminField.__init__c                 C   s~   g }t | jj}| jr|d | jjjr|d | js"|d |r+dd|ini }| jjt	||| jr;ddS d dS )NZvCheckboxLabelr   inliner   rL   r   )contentsattrslabel_suffix)
r   r,   r   rb   appendr   rY   rM   	label_tagr   )r4   rN   re   rf   r#   r#   r$   ri      s    



zAdminField.label_tagc                 C   s   t | jj S rA   )r   r,   rB   r_   r9   r#   r#   r$   rB      s   zAdminField.errorsN)r   r   r    r5   ri   rB   r#   r#   r#   r$   r\      s    r\   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )r[   Nc           	      C   s   t |r|jdkr|jnd}n|}|jjr"||jjv r"|jj| }n
t||jj||d}|jjr=||jjv r=|jj| }nt||jj}||jv rQ|j| j	j
}nd}|||||d| _|| _|| _|| _d| _d| _| | _d S )Nz<lambda>r   r)   F)r=   r   	help_textr,   rS   T)callabler   _metalabelsr   model
help_textsr   rE   r   rS   r,   r*   r2   rY   rb   rc   Zget_empty_value_displayempty_value_display)	r4   r*   r,   rY   r2   
class_namer   rj   rS   r#   r#   r$   r5      s0   
zAdminReadonlyField.__init__c                 C   s6   i }| j s	d|d< | jd }tdt|t|| jjS )Nrd   r   r   z<label{}>{}{}</label>)rY   r,   r   r   r   r*   rg   )r4   rf   r   r#   r#   r$   ri      s   
zAdminReadonlyField.label_tagc                 C   s^   d|j jj|j jjf }zt|t|jg| jjj	d}t
d||W S  ty.   t| Y S w )Nzadmin:%s_%s_change)argsZcurrent_appz<a href="{}">{}</a>)rn   rl   Z	app_labelZ
model_namer   r   pkr2   Z
admin_siter=   r   r   rW   )r4   remote_fieldZ
remote_objZurl_nameurlr#   r#   r$   get_admin_url   s   
z AdminReadonlyField.get_admin_urlc           
   
   C   sP  ddl m} | jd | jj| j}}}zt|||\}}}W n ttt	fy1   | j
}Y t|S w || jjv rK| j| jj}	t|	ddrK|	||S |d u rpt|ddr]||}t|S t|drh|}t|S t|}t|S t|jtr|d urdtt| }nt|jttfr|d ur| |j|}nt||| j
}t|}t|S )	Nr   )_boolean_iconr,   Z	read_onlyFbooleanZ__html__z, )Z,django.contrib.admin.templatetags.admin_listrw   r,   r*   instancer2   r   AttributeError
ValueErrorr	   rp   rE   r   getattrrenderrU   r   rV   rt   r   rM   maprW   rX   r
   r   rv   r   r   )
r4   rw   r,   objr2   r(   attrvalueZresult_reprr   r#   r#   r$   re     sB   

zAdminReadonlyField.contentsrA   )r   r   r    r5   ri   rv   re   r#   r#   r#   r$   r[      s
    
&r[   c                   @   sx   e Zd ZdZ							dddZdd Zdd	 Zd
d Zedd Z	dd Z
edd Zedd Zedd ZdS )InlineAdminFormSetzI
    A wrapper around an inline formset for use in the admin system.
    NTc                 C   sp   || _ || _|| _|| _|d u rd}|| _|d u ri }|| _|jr'd|jnd| _|| _|| _	|	| _
|
| _d S )Nr#   rL   r   )optsformsetr/   r2   r3   r1   rN   rM   has_add_permissionhas_change_permissionhas_delete_permissionhas_view_permission)r4   rd   r   r/   r1   r3   r2   r   r   r   r   r#   r#   r$   r5   3  s   
zInlineAdminFormSet.__init__c                 c   s    | j r| j}n| jt| j }t| jj| j D ]\}}| j	|}t
| j|| j| j||| j|dV  q| jjD ]}t
| j|| j| jd | j| jdV  q:| jrft
| j| jj| j| jd | j| jdV  d S d S )N)r2   view_on_site_urlrQ   )r   r3   r   r/   zipr   Zinitial_formsZget_querysetr   Zget_view_on_site_urlInlineAdminFormr1   Zextra_formsr   
empty_form)r4   Zreadonly_fields_for_editingr*   originalr   r#   r#   r$   r?   P  sR   


	zInlineAdminFormSet.__iter__c           
      c   s   t | jdd }| jj}|jjpi }|jjpi }tt| jD ]k\}}|r+|j	|kr+q| j
r3|| jv rg|j|}d}|d urC|jj}|||pSt|| jj| j|dd|id||pbt|| jjdV  q|j| }|j}	|	d u r~t|| jj| j|d}	||	|j|j|jdV  qd S )NfkFr)   rS   )r=   r   r   r   rj   )r|   r   r   rl   rm   ro   rZ   r   r/   r=   r   r3   rE   getr   rS   r   r   rn   r   r   r   rj   )
r4   r   r   Zmeta_labelsZmeta_help_textsr]   r.   Z
form_fieldZwidget_is_hiddenr   r#   r#   r$   rE   {  sN   




zInlineAdminFormSet.fieldsc                 C   s@   | j j}td| jj | jjtddt|i tdddS )Nz#%szAdd another %(verbose_name)sverbose_nameZRemove)prefixZaddTextZ
deleteText)r=   r>   )r   r   jsondumpsr   r   r   r   )r4   r   r#   r#   r$   inline_formset_data  s   
z&InlineAdminFormSet.inline_formset_datac                 C   r@   rA   )r   r   r9   r#   r#   r$   r     rC   zInlineAdminFormSet.formsc                 C   s
   | j  S rA   )r   non_form_errorsr9   r#   r#   r$   r     s   
z"InlineAdminFormSet.non_form_errorsc                 C   r@   rA   )r   rF   r9   r#   r#   r$   rF     rC   zInlineAdminFormSet.is_boundc                 C   r@   rA   )r   total_form_countr9   r#   r#   r$   r     rC   z#InlineAdminFormSet.total_form_countc                 C   s(   | j j| jj }| D ]}||j7 }q
|S rA   )r   rG   r   rH   r#   r#   r$   rG     s   zInlineAdminFormSet.media)NNNTTTT)r   r   r    __doc__r5   r?   rE   r   rK   r   r   rF   r   rG   r#   r#   r#   r$   r   .  s,    	
+*


r   c                       sP   e Zd ZdZ			d fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )r   zF
    A wrapper around an inline form for use in the admin system.
    Nc	           	         s>   || _ || _|| _|o|d u| _|| _t ||||| d S rA   )r   r2   r   show_urlZabsolute_urlsuperr5   )	r4   r   r*   r/   r1   r   r3   r2   r   r8   r#   r$   r5     s   
zInlineAdminForm.__init__c                 c   s:    | j D ]\}}t| j| j|| jfd| ji|V  qd S )Nr2   )r/   InlineFieldsetr   r*   r3   r2   r<   r#   r#   r$   r?     s   
zInlineAdminForm.__iter__c                 C   s>   | j jjjjp| j jjjjj ptdd | j jjj D S )Nc                 s   s(    | ]}|j jp|j jj jj V  qd S rA   )rl   
auto_fieldrn   rs   editable)r'   parentr#   r#   r$   rT     s
    
z:InlineAdminForm.needs_explicit_pk_field.<locals>.<genexpr>)r*   rl   rn   r   rs   r   anyZget_parent_listr9   r#   r#   r$   needs_explicit_pk_field  s   z'InlineAdminForm.needs_explicit_pk_fieldc                 C   s   t | j| jjjdS ra   )r\   r*   r   Z	_pk_fieldr=   r9   r#   r#   r$   pk_field  s   zInlineAdminForm.pk_fieldc                 C   s&   t | jdd }|rt| j|jdS dS )Nr   Fr   )r|   r   r\   r*   r=   )r4   r   r#   r#   r$   fk_field  s   zInlineAdminForm.fk_fieldc                 C   s   ddl m} t| j|dS )Nr   )DELETION_FIELD_NAMEF)Zdjango.forms.formsetsr   r\   r*   )r4   r   r#   r#   r$   deletion_field	  s   zInlineAdminForm.deletion_field)NNN)r   r   r    r   r5   r?   r   r   r   r   __classcell__r#   r#   r   r$   r     s    r   c                       s$   e Zd Z fddZdd Z  ZS )r   c                    s   || _ t j|i | d S rA   )r   r   r5   )r4   r   rr   kwargsr   r#   r$   r5     s   zInlineFieldset.__init__c                 c   sF    t | jdd }| jD ]}|r|j|kr t| j|| j| jdV  qd S )Nr   rQ   )r|   r   rE   r=   rR   r*   r3   r2   )r4   r   r,   r#   r#   r$   r?     s   
zInlineFieldset.__iter__)r   r   r    r5   r?   r   r#   r#   r   r$   r     s    r   c                       s    e Zd ZdZ fddZ  ZS )AdminErrorListz9Store errors for the form/formsets in an add/change view.c                    sZ   t    |jr)| |j  |D ]}| |  |jD ]	}| |  qqd S d S rA   )r   r5   rF   extendrB   valuesr   )r4   r*   Zinline_formsetsZinline_formsetZerrors_in_inline_formr   r#   r$   r5      s   

zAdminErrorList.__init__)r   r   r    r   r5   r   r#   r#   r   r$   r     s    r   )/r   djangor   Zdjango.contrib.admin.utilsr   r   r   r   r   r   Zdjango.core.exceptionsr	   Zdjango.db.models.fields.relatedr
   r   r   Zdjango.forms.utilsr   Zdjango.template.defaultfiltersr   r   Zdjango.urlsr   r   Zdjango.utils.htmlr   r   Zdjango.utils.safestringr   Zdjango.utils.translationr   r   r!   ZACTION_CHECKBOX_NAMEZFormr   r%   r;   rR   r\   r[   r   r   r   utilsZ	ErrorListr   r#   r#   r#   r$   <module>   s0     
<#k C