o
    hl                     @   s   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mZmZ d dlmZ dd	 Zd
d ZeG dd deZeG dd deeZdd Zdd ZdS )    N)UserList)settings)ValidationError)timezone)escapeformat_htmlformat_html_join	html_safe)gettext_lazyc                 C   s   | sdS |  dd S )z%Convert 'first_name' to 'First name'. _ )replace
capitalize)name r   Z/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/django/forms/utils.pypretty_name   s   r   c                 C   sl   g }g }|   D ]\}}t|tr|r||f q|dur%|||f qtddt|tddt| S )a  
    Convert a dictionary of attributes to a single string.
    The returned string will contain a leading space followed by key="value",
    XML-style pairs. In the case of a boolean value, the key will appear
    without a value. It is assumed that the keys do not need to be
    XML-escaped. If the passed dictionary is empty, then return an empty
    string.

    The result is passed through 'mark_safe' (by way of 'format_html_join').
    Nr   z {}="{}"z {})items
isinstanceboolappendr   sorted)attrsZkey_value_attrsboolean_attrsattrvaluer   r   r   flatatt   s   
r   c                   @   sD   e Zd ZdZdd ZdddZdddZd	d
 Zdd Zdd Z	dS )	ErrorDictz
    A collection of errors that knows how to display itself in various formats.

    The dictionary keys are the field names, and the values are the errors.
    c                 C   s   dd |   D S )Nc                 S   s   i | ]	\}}||  qS r   )as_data.0fer   r   r   
<dictcomp>4   s    z%ErrorDict.as_data.<locals>.<dictcomp>r   selfr   r   r   r   3   s   zErrorDict.as_dataFc                    s    fdd|   D S )Nc                    s   i | ]
\}}||  qS r   )get_json_datar    escape_htmlr   r   r$   7   s    z+ErrorDict.get_json_data.<locals>.<dictcomp>r%   r'   r*   r   r)   r   r(   6   s   zErrorDict.get_json_datac                 C      t | |S Njsondumpsr(   r+   r   r   r   as_json9      zErrorDict.as_jsonc                 C   s   | sdS t dtdd|  S )Nr   z<ul class="errorlist">{}</ul>z<li>{}{}</li>)r   r   r   r&   r   r   r   as_ul<   s   zErrorDict.as_ulc                 C   sH   g }|   D ]\}}|d|  |ddd |D  qd|S )N* %s
c                 s       | ]}d | V  qdS )z  * %sNr   r!   r#   r   r   r   	<genexpr>H       z$ErrorDict.as_text.<locals>.<genexpr>)r   r   join)r'   outputfielderrorsr   r   r   as_textD   s
   
zErrorDict.as_textc                 C      |   S r-   r3   r&   r   r   r   __str__K      zErrorDict.__str__NF)
__name__
__module____qualname____doc__r   r(   r1   r3   r>   rA   r   r   r   r   r   ,   s    

r   c                       s   e Zd ZdZd fdd	Zdd Z fddZdd
dZdddZdd Z	dd Z
dd Zdd Zdd Zdd Zdd Z fddZ  ZS ) 	ErrorListzU
    A collection of errors that knows how to display itself in various formats.
    Nc                    s.   t  | |d u rd| _d S d|| _d S )NZ	errorlistzerrorlist {})super__init__error_classformat)r'   initlistrK   	__class__r   r   rJ   T   s   
zErrorList.__init__c                 C   s   t | jjS r-   )r   dataZ
error_listr&   r   r   r   r   \      zErrorList.as_datac                    s   t   }| j|_|S r-   )rI   copyrK   )r'   rR   rN   r   r   rR   _   s   
zErrorList.copyFc                 C   sD   g }|   D ]}tt|}||rt|n||jpdd q|S )Nr   )messagecode)r   nextiterr   r   rT   )r'   r*   r=   errorrS   r   r   r   r(   d   s   
zErrorList.get_json_datac                 C   r,   r-   r.   r+   r   r   r   r1   n   r2   zErrorList.as_jsonc                 C   s*   | j sdS td| jtdddd | D S )Nr   z<ul class="{}">{}</ul>z<li>{}</li>c                 s   s    | ]}|fV  qd S r-   r   r7   r   r   r   r8   x   s    z"ErrorList.as_ul.<locals>.<genexpr>)rP   r   rK   r   r&   r   r   r   r3   q   s   zErrorList.as_ulc                 C   s   d dd | D S )Nr5   c                 s   r6   )r4   Nr   r7   r   r   r   r8   |   r9   z$ErrorList.as_text.<locals>.<genexpr>)r:   r&   r   r   r   r>   {   s   zErrorList.as_textc                 C   r?   r-   r@   r&   r   r   r   rA   ~   rB   zErrorList.__str__c                 C   s   t t| S r-   )reprlistr&   r   r   r   __repr__   rQ   zErrorList.__repr__c                 C   s   |t | v S r-   rY   )r'   itemr   r   r   __contains__   rQ   zErrorList.__contains__c                 C   s   t | |kS r-   r[   )r'   otherr   r   r   __eq__   rQ   zErrorList.__eq__c                 C   s$   | j | }t|trtt|S |S r-   )rP   r   r   rU   rV   )r'   irW   r   r   r   __getitem__   s   

zErrorList.__getitem__c                    s&   t t| j|i |}|d d d S )N   NN)rI   r   __reduce_ex__)r'   argskwargsinforN   r   r   rd      s   zErrorList.__reduce_ex__rc   rC   )rD   rE   rF   rG   rJ   r   rR   r(   r1   r3   r>   rA   rZ   r]   r_   ra   rd   __classcell__r   r   rN   r   rH   O   s    



rH   c              
   C   s   t jr@| dur@t| r@t }zt|s t| |r tdt| |W S  t	y? } zt
tdd| |dd|d}~ww | S )z}
    When time zone support is enabled, convert naive datetimes
    entered in the current time zone to aware datetimes.
    NzAmbiguous or non-existent time.ur   %(datetime)s couldn’t be interpreted in time zone %(current_timezone)s; it may be ambiguous or it may not exist.Zambiguous_timezone)datetimecurrent_timezone)rT   params)r   USE_TZr   Zis_naiveZget_current_timezoneZ_is_pytz_zoneZ _datetime_ambiguous_or_imaginary
ValueErrorZ
make_aware	Exceptionr   r   )r   rj   excr   r   r   from_current_timezone   s(   
rp   c                 C   s&   t jr| durt| rt| S | S )z
    When time zone support is enabled, convert aware datetimes
    to naive datetimes in the current time zone for display.
    N)r   rl   r   Zis_awareZ
make_naive)r   r   r   r   to_current_timezone   s   
rq   )r/   collectionsr   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.utilsr   Zdjango.utils.htmlr   r   r   r	   Zdjango.utils.translationr
   r   r   r   dictr   rY   rH   rp   rq   r   r   r   r   <module>   s    "L