o
    {,h&                     @   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
 d dlmZmZ d dlmZ d d	lmZ d
d Z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ZG dd deeZG dd deeeZdd Zdd ZdS )    N)UserList)settings)ValidationError)get_default_renderer)timezone)escapeformat_html_join)	mark_safe)gettext_lazyc                 C   s   | sdS |  dd S )z%Convert 'first_name' to 'First name'. _ )replace
capitalize)name r   H/var/www/html/rh/venv/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)attrskey_value_attrsboolean_attrsattrvaluer   r   r   flatatt   s   

r   c                   @   s&   e Zd Zdd ZdddZeZeZdS )RenderableMixinc                 C      t d)NzBSubclasses of RenderableMixin must provide a get_context() method.NotImplementedErrorselfr   r   r   get_context.      zRenderableMixin.get_contextNc                 C   s0   |p| j }|p	| j}|p|  }t|||S N)renderertemplate_namer%   r	   render)r$   r)   contextr(   templater   r   r   r*   3   s   

zRenderableMixin.render)NNN)__name__
__module____qualname__r%   r*   __str____html__r   r   r   r   r   -   s
    
r   c                   @   s0   e Zd Zdd Zdd Zdd Zdd ZeZd	S )
RenderableFieldMixinc                 C   s   |   S r'   )r*   r#   r   r   r   as_field_group>   s   z#RenderableFieldMixin.as_field_groupc                 C   r    )NzFSubclasses of RenderableFieldMixin must provide an as_hidden() method.r!   r#   r   r   r   	as_hiddenA   r&   zRenderableFieldMixin.as_hiddenc                 C   r    )NzFSubclasses of RenderableFieldMixin must provide an as_widget() method.r!   r#   r   r   r   	as_widgetF   r&   zRenderableFieldMixin.as_widgetc                 C   s$   | j jr|  | jdd S |  S )z$Render this field as an HTML widget.T)only_initial)fieldshow_hidden_initialr5   r4   r#   r   r   r   r0   K   s   zRenderableFieldMixin.__str__N)r-   r.   r/   r3   r4   r5   r0   r1   r   r   r   r   r2   =   s    r2   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
RenderableFormMixinc                 C      |  | jS )zRender as <p> elements.)r*   template_name_pr#   r   r   r   as_pU      zRenderableFormMixin.as_pc                 C   r:   )z>Render as <tr> elements excluding the surrounding <table> tag.)r*   template_name_tabler#   r   r   r   as_tableY   r=   zRenderableFormMixin.as_tablec                 C   r:   )z;Render as <li> elements excluding the surrounding <ul> tag.r*   template_name_ulr#   r   r   r   as_ul]   r=   zRenderableFormMixin.as_ulc                 C   r:   )zRender as <div> elements.)r*   template_name_divr#   r   r   r   as_diva   r=   zRenderableFormMixin.as_divN)r-   r.   r/   r<   r?   rB   rD   r   r   r   r   r9   T   s
    r9   c                   @   s&   e Zd Zd	ddZdd Zdd ZdS )
RenderableErrorMixinFc                 C   s   t | |S r'   )jsondumpsget_json_datar$   escape_htmlr   r   r   as_jsong   s   zRenderableErrorMixin.as_jsonc                 C   r:   r'   )r*   template_name_textr#   r   r   r   as_textj      zRenderableErrorMixin.as_textc                 C   r:   r'   r@   r#   r   r   r   rB   m   rN   zRenderableErrorMixin.as_ulNF)r-   r.   r/   rK   rM   rB   r   r   r   r   rE   f   s    
rE   c                       sL   e Zd ZdZdZdZdZdd fdd
Zd	d
 ZdddZ	dd Z
  Z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.
    z%django/forms/errors/dict/default.htmlz!django/forms/errors/dict/text.txtz django/forms/errors/dict/ul.htmlN)r(   c                   s"   t  j|i | |pt | _d S r'   )super__init__r   r(   )r$   r(   argskwargs	__class__r   r   rR   |   s   zErrorDict.__init__c                 C   s   dd |   D S )Nc                 S   s   i | ]	\}}||  qS r   )as_data.0fer   r   r   
<dictcomp>   s    z%ErrorDict.as_data.<locals>.<dictcomp>r   r#   r   r   r   rW      s   zErrorDict.as_dataFc                    s    fdd|   D S )Nc                    s   i | ]
\}}||  qS r   )rH   rX   rJ   r   r   r\      s    z+ErrorDict.get_json_data.<locals>.<dictcomp>r]   rI   r   r^   r   rH      s   zErrorDict.get_json_datac                 C   s   |   ddS )N	errorlisterrorserror_classr]   r#   r   r   r   r%      s   zErrorDict.get_contextrO   )r-   r.   r/   __doc__r)   rL   rA   rR   rW   rH   r%   __classcell__r   r   rU   r   rP   q   s    
rP   c                       s   e Zd ZdZdZdZdZd fdd	Zdd	 Z fd
dZ	d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.
    z%django/forms/errors/list/default.htmlz!django/forms/errors/list/text.txtz django/forms/errors/list/ul.htmlNc                    s>   t  | |d u rd| _nd|| _|pt | _|| _d S )Nr_   zerrorlist {})rQ   rR   rb   formatr   r(   field_id)r$   initlistrb   r(   rg   rU   r   r   rR      s   
zErrorList.__init__c                 C   s   t | jjS r'   )r   data
error_listr#   r   r   r   rW      rN   zErrorList.as_datac                    s   t   }| j|_| j|_|S r'   )rQ   copyrb   r(   )r$   rk   rU   r   r   rk      s   
zErrorList.copyFc                 C   sD   g }|   D ]}tt|}||rt|n||jpdd q|S )Nr   )messagecode)rW   nextiterr   r   rm   )r$   rJ   ra   errorrl   r   r   r   rH      s   zErrorList.get_json_datac                 C   s   | | j dS )Nr`   )rb   r#   r   r   r   r%      s   zErrorList.get_contextc                 C   s   t t| S r'   )reprlistr#   r   r   r   __repr__   rN   zErrorList.__repr__c                 C   s   |t | v S r'   rr   )r$   itemr   r   r   __contains__   rN   zErrorList.__contains__c                 C   s   t | |kS r'   rt   )r$   otherr   r   r   __eq__   rN   zErrorList.__eq__c                 C   s$   | j | }t|trtt|S |S r'   )ri   r   r   rn   ro   )r$   irp   r   r   r   __getitem__   s   

zErrorList.__getitem__c                    s&   t t| j|i |}|d d d S )N   )NN)rQ   r   __reduce_ex__)r$   rS   rT   inforU   r   r   r|      s   zErrorList.__reduce_ex__)NNNNrO   )r-   r.   r/   rc   r)   rL   rA   rR   rW   rk   rH   r%   rs   rv   rx   rz   r|   rd   r   r   rU   r   re      s    

re   c              
   C   sz   t jr;| dur;t| r;t }z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.ambiguous_timezone)datetimecurrent_timezone)rm   params)r   USE_TZr   is_naiveget_current_timezone _datetime_ambiguous_or_imaginary
ValueError
make_aware	Exceptionr   r   )r   r   excr   r   r   from_current_timezone   s&   
r   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   r   r   is_aware
make_naive)r   r   r   r   to_current_timezone   s   
r   )rF   collectionsr   django.confr   django.core.exceptionsr   django.forms.renderersr   django.utilsr   django.utils.htmlr   r   django.utils.safestringr	   django.utils.translationr
   r   r   r   r   r2   r9   rE   dictrP   rr   re   r   r   r   r   r   r   <module>   s&    J