U
    ÑßŽiæ  ã                   @   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   úU/var/www/html/IITS/school/venv/lib/python3.8/site-packages/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ÚregexÚ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)Ú.0Ú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)Úget_view_nameÚget_view_descriptionÚrenderer_classesÚ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)ÚsetÚallowed_methodsr   r   r4   r9   r:   r   ÚAPIExceptionr   r   r.   Úget_serializer_info)r   r   r   r/   ÚmethodÚ
serializerr   r   r   r5   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   ÚHiddenFieldÚget_field_info)r(   Ú
field_namer   ©r   r   r   Ú
<dictcomp>l   s   þ z6SimpleMetadata.get_serializer_info.<locals>.<dictcomp>)r4   rA   ÚfieldsÚitems)r   r@   r   rF   r   r>   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)ÚtyperJ   Ú	read_onlyÚlabelÚ	help_textÚ
min_lengthÚ
max_lengthÚ	min_valueÚ	max_valueÚ
max_digitsÚdecimal_placesNÚ T©Ústrings_onlyrA   rH   ÚchildrenÚchoicesc                 S   s"   g | ]\}}|t |d d�dœ‘qS )TrV   )ÚvalueÚdisplay_namer   )r(   Zchoice_valueZchoice_namer   r   r   r*   �   s   ý
þz1SimpleMetadata.get_field_info.<locals>.<listcomp>)Úlabel_lookupÚgetattrr   rD   rA   r>   ÚgetrB   r   ÚRelatedFieldÚManyRelatedFieldr4   rY   rI   )r   r   Ú
field_infoÚattrsÚattrrZ   r   r   r   rD   r   s<    
þ     ü
ÿþû
zSimpleMetadata.get_field_infoN)!r   r   r   Ú__doc__r	   r   ÚFieldÚBooleanFieldÚ	CharFieldÚ	UUIDFieldÚURLFieldÚ
EmailFieldÚ
RegexFieldÚ	SlugFieldÚIntegerFieldÚ
FloatFieldÚDecimalFieldÚ	DateFieldÚDateTimeFieldÚ	TimeFieldÚDurationFieldÚChoiceFieldÚMultipleChoiceFieldÚ	FileFieldÚ
ImageFieldÚ	ListFieldÚ	DictFieldÚ
Serializerr\   r   r5   r>   rD   r   r   r   r   r      sf                         êr   N)rd   Údjango.core.exceptionsr   Údjango.httpr   Údjango.utils.encodingr   Úrest_frameworkr   r   Úrest_framework.requestr   Ú"rest_framework.utils.field_mappingr	   r
   r   r   r   r   r   Ú<module>   s   	