o
    "7h                     @   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   n/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/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   ALLOWED_VERSIONSr   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	   accepted_media_typeparamsgetr'   r   r   r    r   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                    s:   |j d ur| j|j i|pi }t j|||||fi |S r   )r   r'   superr   r   r   r   r   r   R   s   

zURLPathVersioning.reverser!   	r   r"   r#   r/   r0   r-   r   r   __classcell__r   r   r6   r   r2   6   s
    	r2   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   r3   r-   )r   r   r   r   r:   possible_versionsr   r   r   r   r   x   s   
z%NamespaceVersioning.determine_versionNc                    s2   |j d ur| ||}t j|||||fi |S r   )r   get_versioned_viewnamer5   r   r   r6   r   r   r      s   

zNamespaceVersioning.reversec                 C   s   |j d | S )Nr;   )r   )r   r   r   r   r   r   r@      s   z*NamespaceVersioning.get_versioned_viewnamer!   )
r   r"   r#   r/   r0   r-   r   r   r@   r8   r   r   r6   r   r9   ^   s    r9   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 )Nr;      )
get_host	partitionhostname_regexmatchr   groupr    r   r3   r-   )	r   r   r   r   hostname	separatorportrF   r   r   r   r   r      s   

z$HostNameVersioning.determine_versionN)
r   r"   r#   r/   recompilerE   r0   r-   r   r   r   r   r   rA      s
    
rA   c                       r1   )	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   )query_paramsr+   r'   r   r    r   r3   r-   r4   r   r   r   r      s   
z*QueryParameterVersioning.determine_versionNc                    s:   t  j|||||fi |}|jd urt|| j|jS |S r   )r5   r   r   r   r'   )r   r   r   r   r   r   r   urlr6   r   r   r      s   

z QueryParameterVersioning.reverser!   r7   r   r   r6   r   rM      s
    rM   )rK   django.utils.translationr   r0   rest_frameworkr   rest_framework.compatr   rest_framework.reverser   rest_framework.settingsr   *rest_framework.templatetags.rest_frameworkr   rest_framework.utils.mediatypesr	   r
   r(   r2   r9   rA   rM   r   r   r   r   <module>   s    (1