o
    !i                      @   s   d dl Z d dlZ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 d d	lmZ dd
lmZ ddlmZ edejejB Zdd ZG dd deZG dd deZG dd deeeZG dd deeeZG dd deZ dS )    N)OrderedDict)MutableMapping)cached_property   )Mask)abort)Draft4Validator)ValidationError)not_none)
HTTPStatusz)u?\'(?P<name>.*)\' is a required propertyc                 C   s   t | tr|  S | S N)
isinstancetype)cls r   V/var/www/html/crm_dreinet/venv_linux/lib/python3.10/site-packages/flask_restx/model.pyinstance   s   
r   c                       sj   e Zd ZdZ fddZedd Zdd Zedd	 Ze	d
d Z
dddZdd Zdd ZeZ  ZS )	ModelBasez
    Handles validation and swagger style inheritance for both subclasses.
    Subclass must define `schema` attribute.

    :param str name: The model public name
    c                    sB   t t j|i | d|i _| _g  _ fdd}| _d S )Nnamec                        j j|  g|R  S r   )	__class__inheritr   parentsselfr   r   instance_inherit+      z,ModelBase.__init__.<locals>.instance_inherit)superr   __init__
__apidoc__r   __parents__r   )r   r   argskwargsr   r   r   r   r   %   s   

zModelBase.__init__c                 C   s*   dd | j D }tjt| jgg|R  S )z+
        Return the ancestors tree
        c                 S   s   g | ]}|j qS r   )	ancestors.0pr   r   r   
<listcomp>5   s    z'ModelBase.ancestors.<locals>.<listcomp>)r!   setunionr   )r   r%   r   r   r   r%   0   s   zModelBase.ancestorsc                 C   s@   | j |kr| S | jD ]}||}|r|  S q
td| d )NzParent z
 not found)r   r!   
get_parent
ValueError)r   r   parentfoundr   r   r   r,   8   s   


zModelBase.get_parentc                 C   s.   | j }| jrdd | jD }d||g iS |S )Nc                 S   s   g | ]
}d d |jiqS )z$refz#/definitions/{0})formatr   )r'   r.   r   r   r   r)   G   s    z(ModelBase.__schema__.<locals>.<listcomp>allOf)_schemar!   )r   schemarefsr   r   r   
__schema__B   s   zModelBase.__schema__c                 G   s    | ||d }|dd |_ |S )z
        Inherit this model (use the Swagger composition pattern aka. allOf)
        :param str name: The new model name
        :param dict fields: The new model extra fields
        N)r!   )r   r   r   modelr   r   r   r   P   s   zModelBase.inheritNc                    s^   t  j||d}z|| W d S  ty.   ttjdt fdd||D d Y d S w )N)resolverformat_checkerzInput payload validation failedc                 3   s    | ]}  |V  qd S r   )format_error)r'   er   r   r   	<genexpr>e   s    z%ModelBase.validate.<locals>.<genexpr>)messageerrors)	r   r5   validater	   r   r   BAD_REQUESTdictiter_errors)r   datar8   r9   	validatorr   r   r   r?   [   s   zModelBase.validatec                 C   sN   t |j}|jdkrt|jd}|| ddd |D }||jfS )Nrequiredr   .c                 s   s    | ]}t |V  qd S r   )strr&   r   r   r   r<   m   s    z)ModelBase.format_error.<locals>.<genexpr>)	listpathrD   RE_REQUIREDmatchr=   groupappendjoin)r   errorrI   r   keyr   r   r   r:   h   s   



zModelBase.format_errorc                 C   s   dj | jd|  dS )NzModel({name},{{{fields}}}),)r   fields)r0   r   rN   keysr   r   r   r   __unicode__p   s   zModelBase.__unicode__)NN)__name__
__module____qualname____doc__r   propertyr%   r,   r5   classmethodr   r?   r:   rT   __str____classcell__r   r   r$   r   r      s    





r   c                       sX   e Zd ZdZeZ fddZedd Ze	dd Z
dd	 Zed
d Zdd Z  ZS )RawModela:  
    A thin wrapper on ordered fields dict to store API doc metadata.
    Can also be used for response marshalling.

    :param str name: The model public name
    :param str mask: an optional default model mask
    :param bool strict: validation should raise error when there is param not provided in schema
    c                    sn   | dd  _| dd _ jrt jtst j _tt j|g|R i |  fdd}| _d S )NmaskstrictFc                    r   r   )r   cloner   r   r   r   instance_clone   r   z)RawModel.__init__.<locals>.instance_clone)	pop__mask__
__strict__r   r   r   r]   r   r`   )r   r   r"   r#   ra   r$   r   r   r      s   
zRawModel.__init__c                 C   s   |   }t }d }|  D ]\}}t|}|j||< |jr"|| t|ddr*|}qtt	|p2d ||| j
r<t| j
nd dd}| jrHd|d< t|S )NdiscriminatorFobject)rE   
propertiesre   zx-maskr   additionalProperties)wrapperr*   itemsr   r5   rE   addgetattrsortedrH   rc   rG   rd   r
   )r   rg   rE   re   r   field
definitionr   r   r   r2      s(   

zRawModel._schemac                 C   sd   t | }| jD ]}||j qdd | D }t|dkr$tdt|dkr0| j|d _	|S )zG
        Resolve real fields before submitting them to marshal
        c                 S   s   g | ]
}t |d dr|qS )re   N)rl   )r'   fr   r   r   r)      s    z%RawModel.resolved.<locals>.<listcomp>r   z-There can only be one discriminator by schemar   )
copydeepcopyr!   updateresolvedvalueslenr-   r   default)r   rt   r.   
candidatesr   r   r   rt      s   

zRawModel.resolvedc                 C   s<   t jdtdd t|ttfr| j|g|R  S | ||S )z
        Extend this model (Duplicate all fields)

        :param str name: The new model name
        :param dict fields: The new model extra fields

        :deprecated: since 0.9. Use :meth:`clone` instead.
        z*extend is is deprecated, use clone instead   )
stacklevel)warningswarnDeprecationWarningr   rH   tupler`   )r   r   rR   r   r   r   extend   s   	zRawModel.extendc                 G   s,   |   }|D ]
}|t| q| ||S )a\  
        Clone these models (Duplicate all fields)

        It can be used from the class

        >>> model = Model.clone(fields_1, fields_2)

        or from an Instanciated model

        >>> new_model = model.clone(fields_1, fields_2)

        :param str name: The new model name
        :param dict parents: The new model extra fields
        )ri   rs   rq   rr   )r   r   r   rR   r.   r   r   r   r`      s   
zRawModel.clonec                    s6   | j | j fdd|  D | j| jd}| j|_|S )Nc                    s    g | ]\}}|t | fqS r   )rq   rr   )r'   rP   valuememor   r   r)      s     z)RawModel.__deepcopy__.<locals>.<listcomp>)r^   r_   )r   r   rj   rc   rd   r!   )r   r   objr   r   r   __deepcopy__   s   zRawModel.__deepcopy__)rU   rV   rW   rX   rA   ri   r   rY   r2   r   rt   r   rZ   r`   r   r\   r   r   r$   r   r]   x   s    	


r]   c                   @   s   e Zd ZdZdS )Modelz
    A thin wrapper on fields dict to store API doc metadata.
    Can also be used for response marshalling.

    :param str name: The model public name
    :param str mask: an optional default model mask
    N)rU   rV   rW   rX   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZeZdS )OrderedModelz
    A thin wrapper on ordered fields dict to store API doc metadata.
    Can also be used for response marshalling.

    :param str name: The model public name
    :param str mask: an optional default model mask
    N)rU   rV   rW   rX   r   ri   r   r   r   r   r      s    r   c                       s.   e Zd ZdZd fdd	Zdd ZeZ  ZS )SchemaModelz
    Stores API doc metadata based on a json schema.

    :param str name: The model public name
    :param dict schema: The json schema we are documenting
    Nc                    s   t t| | |pi | _d S r   )r   r   r   r2   )r   r   r3   r$   r   r   r     s   zSchemaModel.__init__c                 C   s   dj | j| jdS )NzSchemaModel({name},{schema}))r   r3   )r0   r   r2   r   r   r   r   rT     s   zSchemaModel.__unicode__r   )rU   rV   rW   rX   r   rT   r[   r\   r   r   r$   r   r     s
    r   )!rq   rer{   collectionsr   collections.abcr   werkzeug.utilsr   r^   r   r>   r   
jsonschemar   jsonschema.exceptionsr	   utilsr
   _httpr   compileIUrJ   r   rf   r   r]   rA   r   r   r   r   r   r   r   <module>   s&    [|