U
    ڲg                     @   sr   d 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 G dd	 d	ZG d
d deZdS )aL  
The metadata API is used to allow customization of how `OPTIONS` requests
are handled. We currently provide a single default implementation that returns
some fairly ad-hoc information about the view.

Future implementations might use JSON schema or other definitions in order
to return this information in a more standardized way.
    )PermissionDenied)Http404	force_str)
exceptionsserializers)clone_request)ClassLookupDictc                   @   s   e Zd Zdd ZdS )BaseMetadatac                 C   s   t ddS )zx
        Return a dictionary of metadata about the view.
        Used to return responses for OPTIONS requests.
        z).determine_metadata() must be overridden.N)NotImplementedError)selfrequestview r   ;/tmp/pip-unpacked-wheel-11h17kvo/rest_framework/metadata.pydetermine_metadata   s    zBaseMetadata.determine_metadataN)__name__
__module____qualname__r   r   r   r   r   r
      s   r
   c                -   @   s   e Zd ZdZeejdejdejdej	dej
dejdejdejdejd	ejd
ejdejdejdejdejdejdejdejdejdejdejdejdiZdd Zdd Zdd Zdd Z dS )SimpleMetadataz
    This is the default metadata implementation.
    It returns an ad-hoc set of information about the view.
    There are not any formalized standards for `OPTIONS` responses
    for us to base this on.
    fieldbooleanstringurlemailregexZslugintegerfloatdecimaldatedatetimetimedurationchoicezmultiple choicezfile uploadzimage uploadlistznested objectc                 C   sT   |  | dd |jD dd |jD d}t|drP| ||}|rP||d< |S )Nc                 S   s   g | ]
}|j qS r   
media_type).0Zrendererr   r   r   
<listcomp>?   s     z5SimpleMetadata.determine_metadata.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   r%   )r'   parserr   r   r   r(   @   s     )namedescriptionZrendersZparsesget_serializeractions)Zget_view_nameZget_view_descriptionZrenderer_classesZparser_classeshasattrdetermine_actions)r   r   r   metadatar-   r   r   r   r   ;   s    
z!SimpleMetadata.determine_metadatac                 C   s   i }ddht |j@ D ]}t|||_zlz4t|dr@||j |dkrZt|drZ|  W n tjt	t
fk
rx   Y nX | }| |||< W 5 ||_X q|S )z
        For generic class based views we return information about
        the fields that are accepted for 'PUT' and 'POST' methods.
        PUTPOSTcheck_permissions
get_object)setZallowed_methodsr   r   r.   r3   r4   r   ZAPIExceptionr   r   r,   get_serializer_info)r   r   r   r-   method
serializerr   r   r   r/   H   s    

z SimpleMetadata.determine_actionsc                    s(   t |dr|j} fdd|j D S )zn
        Given an instance of a serializer, return a dictionary of metadata
        about its fields.
        childc                    s(   i | ] \}}t |tjs| |qS r   )
isinstancer   ZHiddenFieldget_field_info)r'   
field_namer   r   r   r   
<dictcomp>l   s    z6SimpleMetadata.get_serializer_info.<locals>.<dictcomp>)r.   r9   fieldsitems)r   r8   r   r=   r   r6   c   s
    

z"SimpleMetadata.get_serializer_infoc              	   C   s   | j | t|ddd}dddddd	d
ddg	}|D ]0}t||d}|dk	r2|dkr2t|dd||< q2t|ddr| |j|d< nt|ddr| ||d< |dst|tj	tj
fst|drdd |j D |d< |S )zl
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        requiredF)typerA   Z	read_onlylabel	help_textZ
min_length
max_lengthZ	min_valueZ	max_valueZ
max_digitsZdecimal_placesN TZstrings_onlyr9   r?   childrenchoicesc                 S   s"   g | ]\}}|t |d ddqS )TrG   )valueZdisplay_namer   )r'   Zchoice_valueZchoice_namer   r   r   r(      s   
z1SimpleMetadata.get_field_info.<locals>.<listcomp>)label_lookupgetattrr   r;   r9   r6   getr:   r   ZRelatedFieldZManyRelatedFieldr.   rI   r@   )r   r   Z
field_infoattrsattrrJ   r   r   r   r;   r   s<    
     

zSimpleMetadata.get_field_infoN)!r   r   r   __doc__r	   r   ZFieldZBooleanFieldZ	CharFieldZ	UUIDFieldZURLFieldZ
EmailFieldZ
RegexFieldZ	SlugFieldZIntegerFieldZ
FloatFieldZDecimalFieldZ	DateFieldZDateTimeFieldZ	TimeFieldZDurationFieldZChoiceFieldZMultipleChoiceFieldZ	FileFieldZ
ImageFieldZ	ListFieldZ	DictField
SerializerrK   r   r/   r6   r;   r   r   r   r   r      sf                         r   N)rP   Zdjango.core.exceptionsr   Zdjango.httpr   Zdjango.utils.encodingr   Zrest_frameworkr   r   Zrest_framework.requestr   Z"rest_framework.utils.field_mappingr	   r
   r   r   r   r   r   <module>   s   	