U
    Æ½„b  ã                   @   s~   d 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
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.
é    )ÚOrderedDict)Ú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-iohwuy7z/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Úchoicezmultiple choicezfile uploadzimage uploadÚlistznested objectc                 C   sl   t ƒ }| ¡ |d< | ¡ |d< dd„ |jD ƒ|d< dd„ |jD ƒ|d< t|dƒrh|  ||¡}|rh||d	< |S )
NÚnameÚdescriptionc                 S   s   g | ]
}|j ‘qS r   ©Ú
media_type)Ú.0Zrendererr   r   r   Ú
<listcomp>A   s     z5SimpleMetadata.determine_metadata.<locals>.<listcomp>Zrendersc                 S   s   g | ]
}|j ‘qS r   r'   )r)   Úparserr   r   r   r*   B   s     ZparsesÚget_serializerÚactions)r   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/   I   s    

z SimpleMetadata.determine_actionsc                    s,   t |dƒr|j}t‡ fdd„|j ¡ D ƒƒS )zn
        Given an instance of a serializer, return a dictionary of metadata
        about its fields.
        Úchildc                    s*   g | ]"\}}t |tjƒs|ˆ  |¡f‘qS r   )Ú
isinstancer   ZHiddenFieldÚget_field_info)r)   Ú
field_namer   ©r   r   r   r*   m   s   þz6SimpleMetadata.get_serializer_info.<locals>.<listcomp>)r.   r9   r   ÚfieldsÚitems)r   r8   r   r=   r   r6   d   s
    
þz"SimpleMetadata.get_serializer_infoc                 C   sæ   t ƒ }| j| |d< t|ddƒ|d< dddddd	d
g}|D ]0}t||dƒ}|dk	r:|dkr:t|dd�||< q:t|ddƒrŠ|  |j¡|d< nt|ddƒr¤|  |¡|d< | d¡sât|t	j
t	jfƒsât|dƒrâdd„ |j ¡ D ƒ|d< |S )zl
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        ÚtypeÚrequiredFZ	read_onlyÚlabelÚ	help_textZ
min_lengthÚ
max_lengthZ	min_valueZ	max_valueNÚ T©Zstrings_onlyr9   r>   ÚchildrenÚchoicesc                 S   s"   g | ]\}}|t |d d�dœ‘qS )TrF   )ÚvalueZdisplay_namer   )r)   Zchoice_valueZchoice_namer   r   r   r*   �   s   ý
þz1SimpleMetadata.get_field_info.<locals>.<listcomp>)r   Úlabel_lookupÚgetattrr   r;   r9   r6   Úgetr:   r   ZRelatedFieldZManyRelatedFieldr.   rH   r?   )r   r   Z
field_infoÚattrsÚattrrI   r   r   r   r;   s   s8        ý
ÿþû
zSimpleMetadata.get_field_infoN)!r   r   r   Ú__doc__r
   r   ZFieldZBooleanFieldZNullBooleanFieldZ	CharFieldZ	UUIDFieldZURLFieldZ
EmailFieldZ
RegexFieldZ	SlugFieldZIntegerFieldZ
FloatFieldZDecimalFieldZ	DateFieldZDateTimeFieldZ	TimeFieldZChoiceFieldZMultipleChoiceFieldZ	FileFieldZ
ImageFieldZ	ListFieldZ	DictFieldÚ
SerializerrJ   r   r/   r6   r;   r   r   r   r   r      sf                         êr   N)rO   Úcollectionsr   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   	