U
    Û@cjæ  ã                   @   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-iijbbyd9/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¡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.
        Ú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   	