U
    PN^                     @   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	m
Z
 ddlmZ G dd	 d	eZG d
d de	ZeZG dd deZeZdS )    N)Iterable)FileStorage)	FileField)DataRequiredStopValidation   )FlaskWTFDeprecationWarningc                   @   s    e Zd ZdZdd Zdd ZdS )r   z=Werkzeug-aware subclass of :class:`wtforms.fields.FileField`.c                 C   s2   dd |D }t |d }|d k	r(|| _nd| _d S )Nc                 s   s    | ]}t |tr|r|V  qd S N)
isinstancer   .0x r   5/tmp/pip-install-o1yuzyg2/flask-wtf/flask_wtf/file.py	<genexpr>   s     
  z-FileField.process_formdata.<locals>.<genexpr>r   )nextdataraw_data)selfZ	valuelistr   r   r   r   process_formdata   s
    
zFileField.process_formdatac                 C   s   t td t| jS )a  Return ``True`` if ``self.data`` is a
        :class:`~werkzeug.datastructures.FileStorage` object.

        .. deprecated:: 0.14.1
            ``data`` is no longer set if the input is not a non-empty
            ``FileStorage``. Check ``form.data is not None`` instead.
        zc"has_file" is deprecated and will be removed in 1.0. The data is checked during processing instead.)warningswarnr   boolr   )r   r   r   r   has_file   s    	zFileField.has_fileN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   	r   c                   @   s   e Zd ZdZdd ZdS )FileRequiredzValidates that the data is a Werkzeug
    :class:`~werkzeug.datastructures.FileStorage` object.

    :param message: error message

    You can also use the synonym ``file_required``.
    c                 C   s:   t |jtr|js6| jd kr(|d}n| j}t|d S )NzThis field is required.)r
   r   r   messagegettextr   )r   formfieldr   r   r   r   __call__0   s
    
zFileRequired.__call__N)r   r   r   r   r#   r   r   r   r   r   '   s   r   c                   @   s"   e Zd ZdZdddZdd ZdS )FileAllowedaA  Validates that the uploaded file is allowed by a given list of
    extensions or a Flask-Uploads :class:`~flaskext.uploads.UploadSet`.

    :param upload_set: A list of extensions or an
        :class:`~flaskext.uploads.UploadSet`
    :param message: error message

    You can also use the synonym ``file_allowed``.
    Nc                 C   s   || _ || _d S r	   )
upload_setr   )r   r%   r   r   r   r   __init__G   s    zFileAllowed.__init__c                    s   t |jtr|jsd S |jj  t | jtrnt fdd| jD rJd S t| j	pj|
djd| jd| j|j st| j	p|
dd S )Nc                 3   s   | ]}  d | V  qdS ).N)endswithr   filenamer   r   r   R   s     z'FileAllowed.__call__.<locals>.<genexpr>z6File does not have an approved extension: {extensions}z, )
extensionsz)File does not have an approved extension.)r
   r   r   r*   lowerr%   r   anyr   r   r    formatjoinfile_allowed)r   r!   r"   r   r)   r   r#   K   s    
zFileAllowed.__call__)N)r   r   r   r   r&   r#   r   r   r   r   r$   <   s   

r$   )r   collectionsr   Zwerkzeug.datastructuresr   Zwtformsr   Z
_FileFieldZwtforms.validatorsr   r   _compatr   r   Zfile_requiredobjectr$   r0   r   r   r   r   <module>   s   "