a
    JcòfõO  ã                   @   sæ  d dl Z d dlZd dl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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 d dlm Z! dd„ dD ƒZ"dd„ e" #¡ D ƒZ$edd %dd„ e$D ƒ¡ ƒZ&G dd„ de'ƒZ(dd„ Z)dHdd„Z*dd „ Z+d!d"„ Z,d#d$„ Z-d%d&„ Z.d'd(„ Z/G d)d*„ d*eƒZ0d+d,„ Z1dId-d.„Z2dJd/d0„Z3d1d2„ Z4dKd4d5„Z5d6d7„ Z6d8d9„ Z7dLd:d;„Z8G d<d=„ d=e'ƒZ9d>d?„ Z:d@dA„ Z;dBdC„ Z<dDdE„ Z=dFdG„ Z>dS )Mé    N)Údefaultdict)ÚFieldDoesNotExist)ÚmodelsÚrouter)Ú
LOOKUP_SEP)Ú	Collector)Úpretty_name)ÚNoReverseMatchÚreverse)ÚformatsÚtimezone)Úmake_hashable)Úformat_html)Ú_lazy_re_compile)Úcapfirst)Úngettext)Úoverridec                 C   s   i | ]}|d | “qS )z_%02X© )Ú.0Úir   r   úW/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/contrib/admin/utils.pyÚ
<dictcomp>   ó    r   s   ":/_#?;@&=+$,"[]<>%
\c                 C   s   i | ]\}}|t |ƒ“qS r   )Úchr)r   ÚkÚvr   r   r   r      r   z_(?:%s)ú|c                 C   s   g | ]}|d d… ‘qS )é   Nr   )r   Úxr   r   r   Ú
<listcomp>   r   r   c                   @   s   e Zd ZdZdS )ÚFieldIsAForeignKeyColumnNamez/A field is a foreign key attname, i.e. <FK>_id.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r       s   r    c              	   C   s€   |  t¡}|D ]l}|dkr"| jj}z|  |¡}W n tyF   Y qY q0 t|dƒr|j}|d j} t	dd„ |D ƒƒr dS qdS )zA
    Return True if the given lookup path spawns duplicates.
    ÚpkÚ
path_infoséÿÿÿÿc                 s   s   | ]}|j V  qd S ©N)Zm2m)r   Úpathr   r   r   Ú	<genexpr>2   r   z+lookup_spawns_duplicates.<locals>.<genexpr>TF)
Úsplitr   r%   ÚnameÚ	get_fieldr   Úhasattrr&   Úto_optsÚany)ÚoptsZlookup_pathZlookup_fieldsÚ
field_nameÚfieldZ	path_infor   r   r   Úlookup_spawns_duplicates   s    



r4   ú,c                 C   s0   |   d¡r| |¡}n|   d¡r,| ¡ dv}|S )zJ
    Return a lookup value prepared to be used in queryset filtering.
    Z__inZ__isnull)Ú ÚfalseÚ0)Úendswithr+   Úlower)ÚkeyÚvalueÚ	separatorr   r   r   Úprepare_lookup_value9   s
    

r>   c                 C   s   t | tƒr|  t¡S | S )a*  
    Ensure that primary key values do not confuse the admin URLs by escaping
    any '/', '_' and ':' and similarly problematic characters.
    Similar to urllib.parse.quote(), except that the quoting is slightly
    different so that it doesn't get automatically unquoted by the web browser.
    )Ú
isinstanceÚstrÚ	translateÚ	QUOTE_MAP©Úsr   r   r   ÚquoteF   s    rE   c                 C   s   t  dd„ | ¡S )zUndo the effects of quote().c                 S   s   t | d  S )Nr   )ÚUNQUOTE_MAP)Úmr   r   r   Ú<lambda>R   r   zunquote.<locals>.<lambda>)Ú
UNQUOTE_REÚsubrC   r   r   r   ÚunquoteP   s    rK   c                 C   s6   g }| D ](}t |ttfƒr&| |¡ q| |¡ q|S )zS
    Return a list which is a single level of flattening of the original list.
    )r?   ÚlistÚtupleÚextendÚappend)ÚfieldsZflatr3   r   r   r   ÚflattenU   s    rQ   c                 C   s(   g }| D ]\}}|  t|d ƒ¡ q|S )z?Return a list of field names from an admin fieldsets structure.rP   )rN   rQ   )Z	fieldsetsÚfield_namesr,   r1   r   r   r   Úflatten_fieldsetsb   s    rS   c           	         s¦   z| d }W n  t y,   g i tƒ g f Y S 0 t |jj¡}t|| d�}| | ¡ tƒ ‰‡ ‡‡fdd„‰| ˆ¡}‡fdd„|j	D ƒ}dd„ |j
 ¡ D ƒ}||ˆ|fS )	a  
    Find all objects related to ``objs`` that should also be deleted. ``objs``
    must be a homogeneous iterable of objects (e.g. a QuerySet).

    Return a nested list of strings suitable for display in the
    template with the ``unordered_list`` filter.
    r   )ÚusingÚoriginc                    s¦   | j }|ˆ jv }| j}dt|jƒ| f }|ržˆ j|  ˆ| ¡sJˆ |j¡ z(tdˆ j|j	|j
f d t| jƒfƒ}W n tyˆ   | Y S 0 tdt|jƒ|| ƒS |S d S )Nz%s: %sz%s:%s_%s_changez{}: <a href="{}">{}</a>)Ú	__class__Ú	_registryÚ_metar   Úverbose_nameZhas_delete_permissionÚaddr
   r,   Ú	app_labelÚ
model_namerE   r%   r	   r   )ÚobjÚmodelZ	has_adminr1   Zno_edit_linkZ	admin_url)Ú
admin_siteÚperms_neededÚrequestr   r   Úformat_callback|   s*    
ÿ
ü
ÿz,get_deleted_objects.<locals>.format_callbackc                    s   g | ]}ˆ |ƒ‘qS r   r   )r   r]   )rb   r   r   r   œ   r   z'get_deleted_objects.<locals>.<listcomp>c                 S   s   i | ]\}}|j jt|ƒ“qS r   )rX   Úverbose_name_pluralÚlen)r   r^   Úobjsr   r   r   r   �   s   ÿz'get_deleted_objects.<locals>.<dictcomp>)Ú
IndexErrorÚsetr   Zdb_for_writerX   r^   ÚNestedObjectsÚcollectÚnestedÚ	protectedÚ
model_objsÚitems)	re   ra   r_   r]   rT   Ú	collectorZ	to_deleterk   Zmodel_countr   )r_   rb   r`   ra   r   Úget_deleted_objectsj   s    

þro   c                       sX   e Zd Z‡ fdd„Zdd„ Zd‡ fdd„	Z‡ fdd	„Zd
d„ Zddd„Zdd„ Z	‡  Z
S )rh   c                    s.   t ƒ j|i |¤Ž i | _tƒ | _ttƒ| _d S r(   )ÚsuperÚ__init__Úedgesrg   rk   r   rl   ©ÚselfÚargsÚkwargs©rV   r   r   rq   ¦   s    zNestedObjects.__init__c                 C   s   | j  |g ¡ |¡ d S r(   )rr   Ú
setdefaultrO   )rt   ÚsourceÚtargetr   r   r   Úadd_edge¬   s    zNestedObjects.add_edgeNc              
      sè   |D ]\}|r@|  d¡s@||jj|jjdœ }|  t||ƒ|¡ n|  d |¡ | j|jj  |¡ qzt	ƒ j
|fd|i|¤ŽW S  tjy° } z| j |j¡ W Y d }~n<d }~0  tjyâ } z| j |j¡ W Y d }~n
d }~0 0 d S )Nú+)Úclassr[   Úsource_attr)r9   rX   r\   r[   r{   Úgetattrrl   r^   rZ   rp   ri   r   ZProtectedErrorrk   ÚupdateZprotected_objectsZRestrictedErrorZrestricted_objects)rt   re   ry   r~   rv   r]   Úrelated_nameÚerw   r   r   ri   ¯   s    þ"zNestedObjects.collectc                    s$   t ƒ  |||¡}|jdd„ |D ƒŽ S )Nc                 S   s   g | ]
}|j ‘qS r   )r,   )r   Zrelated_fieldr   r   r   r   Ä   r   z1NestedObjects.related_objects.<locals>.<listcomp>)rp   Úrelated_objectsZselect_related)rt   Úrelated_modelZrelated_fieldsre   Úqsrw   r   r   rƒ   Á   s    ÿzNestedObjects.related_objectsc                 C   sj   ||v rg S |  |¡ g }| j |d¡D ]}| |  |||¡¡ q(|rR||ƒg}n|g}|rf| |¡ |S )Nr   )rZ   rr   ÚgetrN   Ú_nestedrO   )rt   r]   Úseenrb   ÚchildrenÚchildÚretr   r   r   r‡   Ç   s    

zNestedObjects._nestedc                 C   s6   t ƒ }g }| j dd¡D ]}| |  |||¡¡ q|S )z4
        Return the graph as a nested list.
        Nr   )rg   rr   r†   rN   r‡   )rt   rb   rˆ   ÚrootsÚrootr   r   r   rj   Ö   s
    zNestedObjects.nestedc                 O   s   dS )z�
        We always want to load the objects into memory so that we can display
        them to the user in confirm page.
        Fr   rs   r   r   r   Úcan_fast_deleteà   s    zNestedObjects.can_fast_delete)NN)N)r!   r"   r#   rq   r{   ri   rƒ   r‡   rj   rŽ   Ú__classcell__r   r   rw   r   rh   ¥   s   

rh   c                 C   sF   t | tjtjjfƒr| j}nt | tjjƒr4| jj}n| }|j	|j
dœS )zË
    Return a `dict` with keys 'verbose_name' and 'verbose_name_plural',
    typically for use with string formatting.

    `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
    )rY   rc   )r?   r   ZModelÚbaseZ	ModelBaserX   ÚqueryÚQuerySetr^   rY   rc   )r]   r1   r   r   r   Úmodel_format_dictè   s    
þr“   c                 C   sN   t | tjjƒr$|du r|  ¡ }| j} t| ƒ}|d |d  }}t|||pJdƒS )a-  
    Return the appropriate `verbose_name` or `verbose_name_plural` value for
    `obj` depending on the count `n`.

    `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
    If `obj` is a `QuerySet` instance, `n` is optional and the length of the
    `QuerySet` is used.
    NrY   rc   r   )r?   r   r‘   r’   Úcountr^   r“   r   )r]   ÚnÚdZsingularÚpluralr   r   r   Úmodel_ngettextû   s    	r˜   c              	   C   s¢   |j }zt|| ƒ}W nt ttfyˆ   t| ƒr<| }||ƒ}nDt|| ƒrb| dkrbt|| ƒ}||ƒ}nt|| ƒ}t|ƒr||ƒ }n|}d }Y n0 d }t|| ƒ}|||fS )NÚ__str__)rX   Ú_get_non_gfk_fieldr   r    Úcallabler.   r   )r,   r]   Úmodel_adminr1   ÚfÚattrr<   r   r   r   Úlookup_field  s$    





rŸ   c                 C   sR   |   |¡}|jr(|jr|jr"|jr(tƒ ‚|jrN|jsNt|dƒrN|j|krNt	ƒ ‚|S )a  
    For historical reasons, the admin app relies on GenericForeignKeys as being
    "not found" by get_field(). This could likely be cleaned up.

    Reverse relations should also be excluded as these aren't attributes of the
    model (rather something like `foo_set`).
    Úattname)
r-   Úis_relationZmany_to_oner„   Zone_to_manyr   Zmany_to_manyr.   r    r    )r1   r,   r3   r   r   r   rš   '  s(    
ÿüüüÿþýürš   Fc           	      C   s¬  d}z:t |j| ƒ}z
|j}W n ty8   |jjj}Y n0 W �nV t�yv   | dkrjt|jjƒ}t}�nt| ƒrx| }n†t|| ƒrŽt	|| ƒ}npt|| ƒr¤t	|| ƒ}nZ|r¾| |j
v r¾|j
|  }n@d| |jjf }|râ|d|jj 7 }|rö|d|jj 7 }t|ƒ‚t|dƒ�r|j}n`t|tƒ�rBt|dƒ�rBt|jdƒ�rB|jj}n0t|ƒ�rj|jdk�r^d}n
t|jƒ}nt| ƒ}Y n  t�y”   t| ƒ}| }Y n0 |�r¤||fS |S dS )	ak  
    Return a sensible label for a field name. The name can be a callable,
    property (but not created with @property decorator), or the name of an
    object's attribute, as well as a model field. If return_attr is True, also
    return the resolved attribute (which could be a callable). This will be
    None if (and only if) the name refers to a field.
    Nr™   zUnable to lookup '%s' on %sz or %sÚshort_descriptionÚfgetz<lambda>z--)rš   rX   rY   ÚAttributeErrorr„   r   r@   r›   r.   r   rP   Zobject_namerV   r!   r¢   r?   Úpropertyr£   r   r    )	r,   r^   rœ   Zreturn_attrÚformrž   r3   ÚlabelÚmessager   r   r   Úlabel_for_fieldD  s`    


þÿþ
ý


r©   c              	   C   s@   d}zt |j| ƒ}W n ttfy*   Y n0 t|dƒr<|j}|S )Nr6   Ú	help_text)rš   rX   r   r    r.   rª   )r,   r^   rª   r3   r   r   r   Úhelp_text_for_field…  s    
r«   c                 C   sp  ddl m} t|dd ƒrfzt|jƒ | |¡W S  ty`   t|jƒ}t| ƒ} t|ƒ | |¡ Y S 0 �nt|t	j
ƒrz|| ƒS | d u r†|S t|t	jƒr¢t t | ¡¡S t|t	jt	jfƒr¾t | ¡S t|t	jƒrØt | |j¡S t|t	jt	jfƒ� röt | ¡S t|t	jƒ�r| �rtd| j| ƒS t|t	jƒ�rb| �rbztj| d|jd�W S  t�y^   t| |ƒ Y S 0 n
t| |ƒS d S )Nr   ©Ú_boolean_iconÚflatchoicesz<a href="{}">{}</a>F)Úensure_asciiÚcls)Ú,django.contrib.admin.templatetags.admin_listr­   r   Údictr®   r†   Ú	TypeErrorr   r?   r   ZBooleanFieldZDateTimeFieldr   Úlocalizer   Útemplate_localtimeZ	DateFieldZ	TimeFieldZDecimalFieldÚnumber_formatZdecimal_placesZIntegerFieldZ
FloatFieldZ	FileFieldr   ÚurlZ	JSONFieldÚjsonÚdumpsÚencoderÚdisplay_for_value)r<   r3   Úempty_value_displayr­   r®   r   r   r   Údisplay_for_field‘  s8    


r½   c                 C   s¸   ddl m} |r|| ƒS | d u r$|S t| tƒr6t| ƒS t| tjƒrRt t 	| ¡¡S t| tj
tjfƒrnt | ¡S t| ttjtfƒrŠt | ¡S t| ttfƒr¬d dd„ | D ƒ¡S t| ƒS d S )Nr   r¬   z, c                 s   s   | ]}t |ƒV  qd S r(   )r@   )r   r   r   r   r   r*   Æ  r   z$display_for_value.<locals>.<genexpr>)r±   r­   r?   Úboolr@   Údatetimer   r´   r   rµ   ÚdateÚtimeÚintÚdecimalÚDecimalÚfloatr¶   rL   rM   Újoin)r<   r¼   Úbooleanr­   r   r   r   r»   ¶  s     


r»   c                   @   s   e Zd ZdS )ÚNotRelationFieldN)r!   r"   r#   r   r   r   r   rÈ   Ë  s   rÈ   c                 C   s    t | dƒr| jd jjS t‚d S )Nr&   r'   )r.   r&   r/   r^   rÈ   )r3   r   r   r   Úget_model_from_relationÏ  s    
rÉ   c              	   C   s®   g }| }|  t¡}|D ]ˆ}|j |¡}t|ƒt|ƒd kr`zt|ƒ W n ty^   Y  q Y n0 |jr„|jrr|j	r„| 
¡ }|jj}n|jj}|j}| d|¡ q|t |¡fS )zªCreate a reversed field path.

    E.g. Given (Order, "user__groups"),
    return (Group, "user__order").

    Final field must be a related model, not a data field.
    r   r   )r+   r   rX   r-   rd   rÉ   rÈ   r¡   Zauto_createdZconcreteZrelated_query_nameZremote_fieldr^   r3   r,   r„   ÚinsertrÆ   )r^   r)   Zreversed_pathÚparentÚpiecesÚpiecer3   r�   r   r   r   Úreverse_field_pathÖ  s"    

rÎ   c                 C   sD   |  t¡}g }|D ],}|r(t|d ƒ}n| }| |j |¡¡ q|S )a:  Return list of Fields given path relative to model.

    e.g. (ModelX, "user__groups__name") -> [
        <django.db.models.fields.related.ForeignKey object at 0x...>,
        <django.db.models.fields.related.ManyToManyField object at 0x...>,
        <django.db.models.fields.CharField object at 0x...>,
    ]
    r'   )r+   r   rÉ   rO   rX   r-   )r^   r)   rÌ   rP   rÍ   rË   r   r   r   Úget_fields_from_pathõ  s    	
rÏ   c                 C   s<  | j }tdƒ� t| |ƒ}W d  ƒ n1 s.0    Y  g }|rP| di i¡ n| j rh| dd|ii¡ |�r8tdƒ�° |D ]š}|jD ]$}| dt|jjƒt|ƒdœi¡ q†|jD ]6\}}	| dt|jjƒt|ƒt|j	d |	ƒdœi¡ q²|j
D ]$}
| dt|
jjƒt|
ƒdœi¡ qðq|W d  ƒ n1 �s.0    Y  |S )	zÊ
    Construct a JSON structure describing changes from a changed object.
    Translations are deactivated so that strings are stored untranslated.
    Translation happens later on LogEntry access.
    NÚaddedÚchangedrP   )r,   Úobjectr   )r,   rÒ   rP   Zdeleted)Úchanged_dataÚtranslation_overrideÚ#_get_changed_field_labels_from_formrO   Znew_objectsr@   rX   rY   Zchanged_objectsZformsZdeleted_objects)r¦   ZformsetsrZ   rÓ   Úchanged_field_labelsZchange_messageZformsetZadded_objectZchanged_objectZchanged_fieldsZdeleted_objectr   r   r   Úconstruct_change_message	  sN    	
(


þÿÿ

ÿýÿÿ

þÿÿ(r×   c              	   C   sL   g }|D ]>}z| j | jp|}W n ty6   |}Y n0 | t|ƒ¡ q|S r(   )rP   r§   ÚKeyErrorrO   r@   )r¦   rÓ   rÖ   r2   Zverbose_field_namer   r   r   rÕ   B  s    
rÕ   )r5   )N)N)NFN)F)?r¿   rÃ   r¸   Úcollectionsr   Zdjango.core.exceptionsr   Z	django.dbr   r   Zdjango.db.models.constantsr   Zdjango.db.models.deletionr   Zdjango.forms.utilsr   Zdjango.urlsr	   r
   Zdjango.utilsr   r   Zdjango.utils.hashabler   Zdjango.utils.htmlr   Zdjango.utils.regex_helperr   Zdjango.utils.textr   Zdjango.utils.translationr   r   rÔ   rB   rm   rF   rÆ   rI   Ú	Exceptionr    r4   r>   rE   rK   rQ   rS   ro   rh   r“   r˜   rŸ   rš   r©   r«   r½   r»   rÈ   rÉ   rÎ   rÏ   r×   rÕ   r   r   r   r   Ú<module>   sT   

;C


A%
9