a
    Ig                     @   sT   d dl mZ d dlmZ d dlmZ d dlmZ G dd deZ	G dd deZ
d	S )
    )
HTMLParser)gettext_lazy)Panel)is_processable_html_responsec                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )
FormParserzh
    HTML form parser, used to check for invalid configurations of forms that
    take file inputs.
    c                    s,   t    d| _i | _g | _g | _g | _d S )NF)super__init__in_formcurrent_formformsform_idsreferenced_file_inputs)self	__class__ W/var/www/html/django/DPS/env/lib/python3.9/site-packages/debug_toolbar/panels/alerts.pyr      s    
zFormParser.__init__c                 C   s   t |}|dkr@d| _|d}|r0| j| d|g d| _n| jr|dkr|ddkr|drt|dd	krd| jd
< nl| jr|dkr|ddv s|dkr|dr|dd	kr| jd | n|dkr|dr| j| d S )NformTidF)	file_form
form_attrssubmit_element_attrsinputtypefile r   >   imagesubmitZbuttonr   )dictr	   getr   appendr
   r   )r   tagattrsform_idr   r   r   handle_starttag   sH    

zFormParser.handle_starttagc                 C   s&   |dkr"| j r"| j| j d| _ d S )Nr   F)r	   r   r    r
   )r   r!   r   r   r   handle_endtag6   s    zFormParser.handle_endtag)__name__
__module____qualname____doc__r   r$   r%   __classcell__r   r   r   r   r   	   s   r   c                       sh   e Zd ZdZedededdZedZdZ fdd	Ze	d
d Z
dd Zdd Zdd Z  ZS )AlertsPanelz+
    A panel to alert users to issues.
    zlForm with id "{form_id}" contains file input, but does not have the attribute enctype="multipart/form-data".zXForm contains file input, but does not have the attribute enctype="multipart/form-data".zzInput element references form with id "{form_id}", but the form does not have the attribute enctype="multipart/form-data".)form_id_missing_enctypeform_missing_enctypeinput_refs_form_missing_enctypeZAlertsz debug_toolbar/panels/alerts.htmlc                    s   t  j|i | g | _d S N)r   r   alerts)r   argskwargsr   r   r   r   Q   s    zAlertsPanel.__init__c                 C   s@   |   d }r8t|dkr"dnd}t| d| S dS d S )Nr0      alert r   )Z	get_statsr   len)r   r0   Z
alert_textr   r   r   nav_subtitleU   s    zAlertsPanel.nav_subtitlec                 C   s   | j | d S r/   )r0   r    )r   r4   r   r   r   	add_alert]   s    zAlertsPanel.add_alertc           	      C   s  t  }|| |jD ]r}|d r|d ddkrtdd |d D s|d d }rp| jd	 j|d
}n
| jd }| d|i qdd |jD }|jD ]\}|d}|r|ddkr||}|r|ddkr| jd j|d
}| d|i q| j	S )z
        Inspects HTML content for a form that includes a file input but does
        not have the encoding type set to multipart/form-data, and warns the
        user if so.
        r   r   Zenctypemultipart/form-datac                 s   s   | ]}| d dkV  qdS )Zformenctyper9   Nr   ).0elemr   r   r   	<genexpr>o   s   zDAlertsPanel.check_invalid_file_form_configuration.<locals>.<genexpr>r   r   r,   )r#   r-   r4   c                 S   s    i | ]}|d   d|d  qS )r   r   r:   )r;   r   r   r   r   
<dictcomp>}   s   zEAlertsPanel.check_invalid_file_form_configuration.<locals>.<dictcomp>r   r   r   r.   )
r   feedr   r   anymessagesformatr8   r   r0   )	r   html_contentparserr   r#   r4   Zform_attrs_by_idr"   r   r   r   r   %check_invalid_file_form_configuration`   s<    







z1AlertsPanel.check_invalid_file_form_configurationc                 C   s8   t |sd S |j|j}| | | d| ji d S )Nr0   )r   contentdecodecharsetrE   Zrecord_statsr0   )r   requestresponserC   r   r   r   generate_stats   s
    
zAlertsPanel.generate_stats)r&   r'   r(   r)   _rA   titletemplater   propertyr7   r8   rE   rK   r*   r   r   r   r   r+   <   s&   
-r+   N)html.parserr   Zdjango.utils.translationr   rL   Zdebug_toolbar.panelsr   Zdebug_toolbar.utilsr   r   r+   r   r   r   r   <module>   s
   3