o
    !i?                  
   @   s"  d dl Z zd dlmZ W n ey   d dlmZ Y nw 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mZ dd	lmZ dd
lmZ ddlmZ G dd deZddddddddZdddddddZededededddiZ d Z!G d!d" d"e"Z#G d#d$ d$e"Z$d%d& Z%dS )'    N)Hashable)deepcopy)current_apprequest)	MultiDictFileStorage)
exceptions   )abort
SpecsError)marshal)Model)
HTTPStatusc                   @   s    e Zd ZdZdd Zdd ZdS )ParseResultz9
    The default result container as an Object dict.
    c                 C   s"   z| | W S  t y   t|w N)KeyErrorAttributeError)selfname r   Y/var/www/html/crm_dreinet/venv_linux/lib/python3.10/site-packages/flask_restx/reqparse.py__getattr__   s
   
zParseResult.__getattr__c                 C   s   || |< d S r   r   )r   r   valuer   r   r   __setattr__   s   zParseResult.__setattr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   zthe JSON bodyzthe post bodyzthe query stringz!the post body or the query stringzthe HTTP headerszthe request's cookieszan uploaded file)jsonformargsvaluesheaderscookiesfilesqueryformDataheaderbody)r    r   r"   r   r!   r$   integerstringbooleannumbervoid,c                   @   sd   e Zd ZdZddddedddddddddfd	d
Zdd Zdd Zdd ZdddZ	e
dd ZdS )Argumenta:  
    :param name: Either a name or a list of option strings, e.g. foo or -f, --foo.
    :param default: The value produced if the argument is absent from the request.
    :param dest: The name of the attribute to be added to the object
        returned by :meth:`~reqparse.RequestParser.parse_args()`.
    :param bool required: Whether or not the argument may be omitted (optionals only).
    :param string action: The basic type of action to be taken when this argument
        is encountered in the request. Valid options are "store" and "append".
    :param bool ignore: Whether to ignore cases where the argument fails type conversion
    :param type: The type to which the request argument should be converted.
        If a type raises an exception, the message in the error will be returned in the response.
        Defaults to  :class:`str`.
    :param location: The attributes of the :class:`flask.Request` object
        to source the arguments from (ex: headers, args, etc.), can be an
        iterator. The last item listed takes precedence in the result set.
    :param choices: A container of the allowable values for the argument.
    :param help: A brief description of the argument, returned in the
        response when the argument is invalid. May optionally contain
        an "{error_msg}" interpolation token, which will be replaced with
        the text of the error raised by the type converter.
    :param bool case_sensitive: Whether argument values in the request are
        case sensitive or not (this will convert all values to lowercase)
    :param bool store_missing: Whether the arguments default value should
        be stored if the argument is missing from the request.
    :param bool trim: If enabled, trims whitespace around the argument.
    :param bool nullable: If enabled, allows null value in argument.
    NF)r   r!   r   store)=Tc                 C   s^   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _|| _|| _|| _d S r   )r   defaultdestrequiredignorelocationtypechoicesactionhelpcase_sensitive	operatorsstore_missingtrimnullable)r   r   r2   r3   r4   r5   r7   r6   r8   r9   r:   r<   r;   r=   r>   r?   r   r   r   __init___   s   
zArgument.__init__c                 C   s   t | jtr*| jdv r|jdd}nt|| jt }t|r!| }|dur'|S t S t }| jD ]#}|dv r=|jdd}nt||d}t|rJ| }|durS|| q0|S )z
        Pulls values off the request in the provided location
        :param request: The flask request object to parse arguments from
        >   r   get_jsonT)silentN)
isinstancer6   strrA   getattrr   callableupdate)r   r   r   r!   lr   r   r   source   s(   


zArgument.sourcec                 C   s   |d u r| j stdd S t| jtrt|trt|| jS t|tr*| jtkr*|S z	| || j|W S  t	yg   z| jt
ju rM| t|| jW  Y S | || jW  Y S  t	yf   | | Y  Y S w w )NzMust not be null!)r?   
ValueErrorrC   r7   r   dictr   r   r   	TypeErrordecimalDecimalrD   )r   r   opr   r   r   convert   s&   zArgument.convertc                 C   sT   t |}| jrdt | j|gn|}| j|i}|r t||fS ttjd|d dS )ac  
        Called when an error is raised while parsing. Aborts the request
        with a 400 status and an error message

        :param error: the error that was raised
        :param bool bundle_errors: do not abort when first error occurs, return a
            dict with the name of the argument and the error message to be
            bundled
         Input payload validation failederrorsN)rD   r:   joinr   rJ   r
   r   BAD_REQUEST)r   errorbundle_errors	error_str	error_msgrT   r   r   r   handle_validation_error   s   

z Argument.handle_validation_errorc                    s^  t jddp|}|}g }d}d}jD ] j ddd }||v rt|dr2||}n||g}|D ]}	t|	drHj	rH|	
 }	t|	d	rcjsc|	 }	tjd
rcdd jD _zjdkrw fdd|	tD }	n|	 }	W n% ty }
 zjrW Y d}
~
q:|
|W  Y d}
~
    S d}
~
ww jr|	jvrd|	|}||    S ||jv r|j| ||	 q:q|sjrtjtrtjj}ndd jD }d|}d|}||S |stj r	  |fS j |fS jdkr||fS jdks%t!|dkr+|d |fS ||fS )al  
        Parses argument value(s) from the request, converting according to
        the argument's type.

        :param request: The flask request object to parse arguments from
        :param bool bundle_errors: do not abort when first error occurs, return a
            dict with the name of the argument and the error message to be
            bundled
        BUNDLE_ERRORSFTr1    r	   getliststriplower__iter__c                 S   s   g | ]}|  qS r   )r`   ).0choicer   r   r   
<listcomp>   s    z"Argument.parse.<locals>.<listcomp>splitc                    s   g | ]} | qS r   )rP   )rb   voperatorr   r   r   rd      s    
Nz0The value '{0}' is not a valid choice for '{1}'.c                 S   s   g | ]}t ||qS r   )_friendly_locationget)rb   locr   r   r   rd     s    z or z!Missing required parameter in {0}appendr0   r   )"r   configrj   rI   r<   r   replacehasattrr^   r>   r_   r;   r`   r8   r9   re   
SPLIT_CHARrP   	Exceptionr5   r[   formatunparsed_argumentspoprl   r4   rC   r6   rD   ri   rU   rF   r2   len)r   r   rX   rI   results
_not_found_foundr   r!   r   rW   msgr6   	locationsrZ   r   rg   r   parse   sp   




 




zArgument.parsec                 C   s   | j dkrd S | jt| j dd}t| | | jrd|d< | jr&| j|d< | jd ur9t| jr4|  n| j|d< | j	dkr[d	|d	 i|d
< d|v rS|
d|d
 d< d|d	< d|d< | j	dkrpd	|d	 i|d
< d|d	< d|d< | jrx| j|d< |S )Ncookier%   )r   inTr4   descriptionr2   rl   r7   itemspatternarraymulticollectionFormatre   csvenum)r6   r   	LOCATIONSrj   _handle_arg_typer4   r:   r2   rF   r9   rt   r8   )r   paramr   r   r   
__schema__  s0   






zArgument.__schema__)F)r   r   r   r   rD   r@   rI   rP   r[   r{   propertyr   r   r   r   r   r/   B   s.    
%
Rr/   c                   @   sX   e Zd ZdZeeddfddZdd Zddd	Zd
d Z	dd Z
dd Zedd ZdS )RequestParseraU  
    Enables adding and parsing of multiple arguments in the context of a single request.
    Ex::

        from flask_restx import RequestParser

        parser = RequestParser()
        parser.add_argument('foo')
        parser.add_argument('int_bar', type=int)
        args = parser.parse_args()

    :param bool trim: If enabled, trims whitespace on all arguments in this parser
    :param bool bundle_errors: If enabled, do not abort when first error occurs,
        return a dict with the name of the argument and the error message to be
        bundled and return all validation errors
    Fc                 C   s"   g | _ || _|| _|| _|| _d S r   )r    argument_classresult_classr>   rX   )r   r   r   r>   rX   r   r   r   r@   N  s
   
zRequestParser.__init__c                 O   sp   t |dkrt|d | jr| j|d  n| j| j|i | | jr6| jtu r6|d| j| jd _| S )a  
        Adds an argument to be parsed.

        Accepts either a single instance of Argument or arguments to be passed
        into :class:`Argument`'s constructor.

        See :class:`Argument`'s constructor for documentation on the available options.
        r	   r   r>   )ru   rC   r   r    rl   r>   r/   rj   )r   r    kwargsr   r   r   add_argument[  s   
zRequestParser.add_argumentNc           
      C   s   |du rt }|  }|rt| d|ni |_i }| jD ]$}||| j\}}t	|t
r5|| d}|s:|jrB|||jpA|j< q|rMttjd|d |rd|jrdd|j }d|}	t|	|S )aN  
        Parse all arguments from the provided request and return the results as a ParseResult

        :param bool strict: if req includes args not in parser, throw 400 BadRequest exception
        :return: the parsed results as :class:`ParseResult` (or any class defined as :attr:`result_class`)
        :rtype: ParseResult
        Nr]   rR   rS   z, zUnknown arguments: {0})r   r   rK   r   rI   rs   r    r{   rX   rC   rJ   rG   r=   r3   r   r
   r   rV   rU   keysrr   r   
BadRequest)
r   reqstrictresultrT   argr   found	argumentsry   r   r   r   
parse_argsq  s.   






zRequestParser.parse_argsc                 C   s0   |  | j| j}t| j|_| j|_| j|_|S )zCCreates a copy of this RequestParser with the same set of arguments)	__class__r   r   r   r    r>   rX   )r   parser_copyr   r   r   copy  s
   zRequestParser.copyc                 O   s^   | j |g|R i |}t| jdd D ]\}}|j|jkr,| j|= | j|  | S q| S )z@Replace the argument matching the given name with a new version.N)r   	enumerater    r   rl   )r   r   r    r   new_argindexr   r   r   r   replace_argument  s   zRequestParser.replace_argumentc                 C   s8   t | jdd D ]\}}||jkr| j|=  | S q	| S )z,Remove the argument matching the given name.N)r   r    r   )r   r   r   r   r   r   r   remove_argument  s   
zRequestParser.remove_argumentc                 C   sT   g }t  }| jD ]}|j}|r|| ||d  qd|v r(d|v r(td|S )Nr}   r(   r&   z,Can't use formData and body at the same time)setr    r   rl   addr   )r   paramsrz   r   r   r   r   r   r     s   

zRequestParser.__schema__)NF)r   r   r   r   r/   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   <  s    

&
r   c                 C   s   t | jtr| jtv rt| j |d< d S t| jdr(| jjd |d< d|d< d S t| jdr7|| jj d S | jdkrBd|d< d S d	|d< d S )
Nr7   
__apidoc__r   r(   r}   r   r$   filer*   )	rC   r7   r   PY_TYPESro   r   rG   r   r6   )r   r   r   r   r   r     s   
r   )&rM   collections.abcr   ImportErrorcollectionsr   r   flaskr   r   werkzeug.datastructuresr   r   werkzeugr   rT   r
   r   marshallingr   modelr   _httpr   rK   r   ri   r   intrD   boolfloatr   rp   objectr/   r   r   r   r   r   r   <module>   sT     { 