
    Nwg                         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
      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    N)gettext_lazy)
exceptions)unicode_http_header_reverse)api_settings)replace_query_param)
_MediaTypec                   h    e Zd Zej                  Zej                  Zej                  Z	d Z
ddZd Zy)BaseVersioningc                 d    d}t        |j                  | j                  j                              )Nz.{cls}.determine_version() must be implemented.)cls)NotImplementedErrorformat	__class____name__)selfrequestargskwargsmsgs        P/var/www/horilla/myenv/lib/python3.12/site-packages/rest_framework/versioning.pydetermine_versionz BaseVersioning.determine_version   s1    >!#**'' #- #
  	    Nc                 "    t        |||||fi |S Nr   )r   viewnamer   r   r   r   extras          r   reversezBaseVersioning.reverse   s    $I5IIr   c                 f    | j                   sy|d uxr || j                  k(  xs || j                   v S )NT)allowed_versionsdefault_version)r   versions     r   is_allowed_versionz!BaseVersioning.is_allowed_version   s>    $$$HD4H4H)H 3D111	4r   NNNN)r   
__module____qualname__r   DEFAULT_VERSIONr"   ALLOWED_VERSIONSr!   VERSION_PARAMversion_paramr   r   r$    r   r   r   r      s4    "22O#44 ..MJ4r   r   c                   &    e Zd ZdZ ed      Zd Zy)AcceptHeaderVersioningzb
    GET /something/ HTTP/1.1
    Host: example.com
    Accept: application/json; version=1.0
    z#Invalid version in "Accept" header.c                    t        |j                        }|j                  j                  | j                  | j
                        }t        |      }| j                  |      st        j                  | j                        |S r   )r
   accepted_media_typeparamsgetr+   r"   r   r$   r   NotAcceptableinvalid_version_message)r   r   r   r   
media_typer#   s         r   r   z(AcceptHeaderVersioning.determine_version*   sj     ; ;<
##''(:(:D<P<PQ%g.&&w/**4+G+GHHr   N)r   r&   r'   __doc___r4   r   r,   r   r   r.   r.   "   s    
   EFr   r.   c                   :     e Zd ZdZ ed      Zd Zd fd	Z x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                     |j                  | j                  | j                        }|| j                  }| j                  |      st	        j
                  | j                        |S r   )r2   r+   r"   r$   r   NotFoundr4   r   r   r   r   r#   s        r   r   z#URLPathVersioning.determine_versionI   sX    **T//1E1EF?**G&&w/%%d&B&BCCr   c                     |j                   | j                  |j                   i|xs i }t        |   |||||fi |S r   )r#   r+   superr   r   r   r   r   r   r   r   r   s          r   r   zURLPathVersioning.reverseR   sU    ??&""GOO<RF
 wdFGV
7<
 	
r   r%   	r   r&   r'   r6   r7   r4   r   r   __classcell__r   s   @r   r9   r9   6   s%        >?	
 	
r   r9   c                   @     e Zd ZdZ ed      Zd Zd fd	Zd Z x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                     t        |dd       }||j                  s| j                  S |j                  j                  d      }|D ]  }| j	                  |      s|c S  t        j                  | j                        )Nresolver_match:)getattr	namespacer"   splitr$   r   r;   r4   )r   r   r   r   rF   possible_versionsr#   s          r   r   z%NamespaceVersioning.determine_versionx   s     *:DA!)A)A''' +44::3?( 	G&&w/	 !!$">">??r   c                 h    |j                   | j                  ||      }t        |   |||||fi |S r   )r#   get_versioned_viewnamer>   r   r?   s          r   r   zNamespaceVersioning.reverse   sC    ??&228WEHwdFGV
7<
 	
r   c                 &    |j                   dz   |z   S )NrG   )r#   )r   r   r   s      r   rM   z*NamespaceVersioning.get_versioned_viewname   s    $x//r   r%   )
r   r&   r'   r6   r7   r4   r   r   rM   rA   rB   s   @r   rD   rD   ^   s&    .   de
@
0r   rD   c                   J    e Zd ZdZ ej
                  d      Z ed      Zd Z	y)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                     |j                         j                  d      \  }}}| j                  j                  |      }|s| j                  S |j                  d      }| j                  |      st        j                  | j                        |S )NrG      )
get_host	partitionhostname_regexmatchr"   groupr$   r   r;   r4   )	r   r   r   r   hostname	separatorportrV   r#   s	            r   r   z$HostNameVersioning.determine_version   s~    $+$4$4$6$@$@$E!)T##))(3'''++a.&&w/%%d&B&BCCr   N)
r   r&   r'   r6   recompilerU   r7   r4   r   r,   r   r   rP   rP      s+    
  RZZ OPN >?r   rP   c                   :     e Zd ZdZ ed      Zd Zd fd	Z xZS )QueryParameterVersioningza
    GET /something/?version=0.1 HTTP/1.1
    Host: example.com
    Accept: application/json
    z#Invalid version in query parameter.c                     |j                   j                  | j                  | j                        }| j	                  |      st        j                  | j                        |S r   )query_paramsr2   r+   r"   r$   r   r;   r4   r<   s        r   r   z*QueryParameterVersioning.determine_version   sP    &&**4+=+=t?S?ST&&w/%%d&B&BCCr   c                     t        |   |||||fi |}|j                  !t        || j                  |j                        S |S r   )r>   r   r#   r	   r+   )	r   r   r   r   r   r   r   urlr   s	           r   r   z QueryParameterVersioning.reverse   sN    godFGV
7<
 ??&&sD,>,>PP
r   r%   r@   rB   s   @r   r^   r^      s%    
   EF r   r^   )r[   django.utils.translationr   r7   rest_frameworkr   rest_framework.compatr   rest_framework.reverser   rest_framework.settingsr   *rest_framework.templatetags.rest_frameworkr	   rest_framework.utils.mediatypesr
   r   r.   r9   rD   rP   r^   r,   r   r   <module>rj      se    	 6 % 5 + 0 J 64 4*^ (%
 %
P.0. .0b .~ r   