3
Ah`                 @   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   F/tmp/pip-build-9m32_hle/djangorestframework/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  }|j |d< |j |d< dd |jD |d< dd |jD |d< t|drh| j||}|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 }xddht |j@ D ]}t|||_zly4t|drB|j|j |dkr\t|dr\|j  W n tjt	t
fk
rz   Y nX |j }| j|||< W d||_X qW |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_objectN)setZallowed_methodsr   r   r,   r1   r2   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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| j|fqS r   )
isinstancer   ZHiddenFieldget_field_info)r'   
field_namer   )r   r   r   r(   n   s   z6SimpleMetadata.get_serializer_info.<locals>.<listcomp>)r,   r7   r   fieldsitems)r   r6   r   )r   r   r4   d   s
    

z"SimpleMetadata.get_serializer_infoc             C   s   t  }| j| |d< t|dd|d< dddddd	d
g}x8|D ]0}t||d}|dk	r<|dkr<t|dd||< q<W t|ddr| j|j|d< nt|ddr| j||d< |jd rt|t	j
t	jf rt|drdd |j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)strings_onlyr7   r;   childrenchoicesc             S   s"   g | ]\}}|t |d ddqS )T)rC   )valueZdisplay_name)r   )r'   Zchoice_valueZchoice_namer   r   r   r(      s   z1SimpleMetadata.get_field_info.<locals>.<listcomp>)r   label_lookupgetattrr   r9   r7   r4   getr8   r   ZRelatedFieldZManyRelatedFieldr,   rE   r<   )r   r   Z
field_infoattrsattrrF   r   r   r   r9   s   s(    

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
SerializerrG   r   r-   r4   r9   r   r   r   r   r      s8   r   N)rL   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   	