o
    cZh                     @   s   d dl 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 d dlmZ d dlmZ G d	d
 d
ZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)gettext_lazy)
exceptions)unicode_http_header_reverse)api_settings)replace_query_param)
_MediaTypec                   @   s8   e Zd ZejZejZejZ	dd Z
dddZdd ZdS )	BaseVersioningc                 O   s   d}t |j| jjd)Nz.{cls}.determine_version() must be implemented.)cls)NotImplementedErrorformat	__class____name__)selfrequestargskwargsmsg r   P/var/www/html/lang_env/lib/python3.10/site-packages/rest_framework/versioning.pydetermine_version   s   z BaseVersioning.determine_versionNc                 K   s   t |||||fi |S Nr   r   viewnamer   r   r   r   extrar   r   r   reverse   s   zBaseVersioning.reversec                 C   s&   | j sdS |d ur|| jkp|| j v S )NT)allowed_versionsdefault_version)r   versionr   r   r   is_allowed_version   s
   z!BaseVersioning.is_allowed_versionNNNN)r   
__module____qualname__r   DEFAULT_VERSIONr   ZALLOWED_VERSIONSr   ZVERSION_PARAMversion_paramr   r   r    r   r   r   r   r
      s    
r
   c                   @   s    e Zd ZdZedZdd ZdS )AcceptHeaderVersioningzb
    GET /something/ HTTP/1.1
    Host: example.com
    Accept: application/json; version=1.0
    z#Invalid version in "Accept" header.c                 O   s>   t |j}|j| j| j}t|}| |st	| j
|S r   )r	   Zaccepted_media_typeparamsgetr%   r   r   r    r   ZNotAcceptableinvalid_version_message)r   r   r   r   
media_typer   r   r   r   r   *   s   

z(AcceptHeaderVersioning.determine_versionN)r   r"   r#   __doc___r)   r   r   r   r   r   r&   "   s    r&   c                       2   e Zd ZdZedZdd Zd fdd	Z  ZS )	URLPathVersioninga8  
    To the client this is the same style as `NamespaceVersioning`.
    The difference is in the backend - this implementation uses
    Django's URL keyword arguments to determine the version.

    An example URL conf for two views that accept two different versions.

    urlpatterns = [
        re_path(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
        re_path(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
    ]

    GET /1.0/something/ HTTP/1.1
    Host: example.com
    Accept: application/json
    zInvalid version in URL path.c                 O   s8   | | j| j}|d u r| j}| |st| j|S r   )r(   r%   r   r    r   NotFoundr)   r   r   r   r   r   r   r   r   r   I   s   
z#URLPathVersioning.determine_versionNc                    sB   |j d ur|d u ri n|}|j || j< t j|||||fi |S r   )r   r%   superr   r   r   r   r   r   R   s   

zURLPathVersioning.reverser!   	r   r"   r#   r+   r,   r)   r   r   __classcell__r   r   r2   r   r.   6   s
    	r.   c                       s:   e Zd ZdZedZdd Zd
 fdd	Zdd	 Z  Z	S )NamespaceVersioninga  
    To the client this is the same style as `URLPathVersioning`.
    The difference is in the backend - this implementation uses
    Django's URL namespaces to determine the version.

    An example URL conf that is namespaced into two separate versions

    # users/urls.py
    urlpatterns = [
        path('/users/', users_list, name='users-list'),
        path('/users/<int:pk>/', users_detail, name='users-detail')
    ]

    # urls.py
    urlpatterns = [
        path('v1/', include('users.urls', namespace='v1')),
        path('v2/', include('users.urls', namespace='v2'))
    ]

    GET /1.0/something/ HTTP/1.1
    Host: example.com
    Accept: application/json
    zBInvalid version in URL path. Does not match any version namespace.c                 O   sT   t |dd }|d u s|js| jS |jd}|D ]}| |r#|  S qt| j)Nresolver_match:)getattr	namespacer   splitr    r   r/   r)   )r   r   r   r   r6   Zpossible_versionsr   r   r   r   r   v   s   
z%NamespaceVersioning.determine_versionNc                    s2   |j d ur| ||}t j|||||fi |S r   )r   get_versioned_viewnamer1   r   r   r2   r   r   r      s   

zNamespaceVersioning.reversec                 C   s   |j d | S )Nr7   )r   )r   r   r   r   r   r   r;      s   z*NamespaceVersioning.get_versioned_viewnamer!   )
r   r"   r#   r+   r,   r)   r   r   r;   r4   r   r   r2   r   r5   \   s    r5   c                   @   s*   e Zd ZdZedZedZdd Z	dS )HostNameVersioningzX
    GET /something/ HTTP/1.1
    Host: v1.example.com
    Accept: application/json
    z,^([a-zA-Z0-9]+)\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$zInvalid version in hostname.c           	      O   sN   |  d\}}}| j|}|s| jS |d}| |s%t| j	|S )Nr7      )
get_host	partitionhostname_regexmatchr   groupr    r   r/   r)   )	r   r   r   r   hostname	separatorportrA   r   r   r   r   r      s   

z$HostNameVersioning.determine_versionN)
r   r"   r#   r+   recompiler@   r,   r)   r   r   r   r   r   r<      s
    
r<   c                       r-   )	QueryParameterVersioningza
    GET /something/?version=0.1 HTTP/1.1
    Host: example.com
    Accept: application/json
    z#Invalid version in query parameter.c                 O   s,   |j | j| j}| |st| j|S r   )Zquery_paramsr(   r%   r   r    r   r/   r)   r0   r   r   r   r      s   
z*QueryParameterVersioning.determine_versionNc                    s:   t  j|||||fi |}|jd urt|| j|jS |S r   )r1   r   r   r   r%   )r   r   r   r   r   r   r   urlr2   r   r   r      s   

z QueryParameterVersioning.reverser!   r3   r   r   r2   r   rH      s
    rH   )rF   Zdjango.utils.translationr   r,   Zrest_frameworkr   Zrest_framework.compatr   Zrest_framework.reverser   Zrest_framework.settingsr   Z*rest_framework.templatetags.rest_frameworkr   Zrest_framework.utils.mediatypesr	   r
   r&   r.   r5   r<   rH   r   r   r   r   <module>   s    &1