o
    j5                  
   @   sx  d Z ddlmZ ddlZee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 dd	lmZ dd
lmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z# ddl$m%Z% zddl m&Z& W n e'y Z( ze(Z)dd Z&W Y dZ([(ndZ([(ww 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.G dd de/Z0dS ) zThe ``spyne.server.django`` module contains a Django-compatible Http
transport. It's a thin wrapper around
:class:`spyne.server.wsgi.WsgiApplication`.
    )absolute_importN)update_wrapper)Address)get_fault_string_from_exceptionApplication)process_contexts)Fault)Soap11)HttpRpc)HttpBaseHttpMethodContextHttpTransportContext)WsgiApplication)_bytes_join)address_parser)HttpResponseHttpResponseNotAllowedHttp404)csrf_exempt)StreamingHttpResponsec                  O   s   t N)_local_import_error)argskwargs r   S/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/server/django.pyr   2   s   r   c                   @   s$   e Zd ZdZeZdd Zdd ZdS )DjangoApplicationz$You should use this for regular RPC.c                    s<   |     fdd}|j }t| ||}|  |  S )Nc                    s4   |  dd\} }t|  _|D ]\}}| |< qd S )N    )splitintstatus_code)statusheadersreasonheadervalueretvalr   r   start_responseA   s
   

z2DjangoApplication.__call__.<locals>.start_response)HttpResponseObjectMETAcopyr   __call__set_response)selfrequestr)   environresponser   r'   r   r-   >   s   
zDjangoApplication.__call__c                 C   s   t |d|_d S )N    )r   contentr/   r(   r2   r   r   r   r.   ]   s   zDjangoApplication.set_responseN)__name__
__module____qualname____doc__r   r*   r-   r.   r   r   r   r   r   6   s
    r   c                   @   s   e Zd ZdZeZdd ZdS )StreamingDjangoApplicationz^You should use this when you're generating HUGE data as response.

    New in Django 1.5.
    c                 C   s
   ||_ d S r   )streaming_contentr5   r   r   r   r.   i      
z'StreamingDjangoApplication.set_responseN)r6   r7   r8   r9   r   r*   r.   r   r   r   r   r:   a   s    r:   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )DjangoHttpTransportContextc                 C      | j jS r   )reqpathr/   r   r   r   get_pathn      z#DjangoHttpTransportContext.get_pathc                 C   r>   r   )r?   methodrA   r   r   r   get_request_methodq   rC   z-DjangoHttpTransportContext.get_request_methodc                 C   s   | j jd S )NCONTENT_TYPE)r?   r+   rA   r   r   r   get_request_content_typet      z3DjangoHttpTransportContext.get_request_content_typec                 C   s
   | j  S r   )r?   get_full_pathrA   r   r   r   get_path_and_qsw   r<   z*DjangoHttpTransportContext.get_path_and_qsc                 C   s   | j j| S r   )r?   COOKIES)r/   keyr   r   r   
get_cookiez   rH   z%DjangoHttpTransportContext.get_cookiec                 C   sZ   t | jjt | jj}}t ||rttj||dS t ||r+ttj	||dS d S )N)typehostport)
r   get_ipr?   r+   get_portis_valid_ipv4r   TCP4is_valid_ipv6TCP6)r/   addrrP   r   r   r   get_peer}   s   z#DjangoHttpTransportContext.get_peerN)	r6   r7   r8   rB   rE   rG   rJ   rM   rX   r   r   r   r   r=   m   s    r=   c                   @   s   e Zd ZeZdS )DjangoHttpMethodContextN)r6   r7   r8   r=   r   r   r   r   r   rY      s    rY   c                       sL   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Zdd ZdddZ	  Z
S )DjangoServerz2Server talking in Django request/response objects.FTc                    s$   t t| j||d d | _|| _d S )N)chunked)superrZ   __init___wsdl_cache_wsdl)r/   appr[   
cache_wsdl	__class__r   r   r]      s   
zDjangoServer.__init__c           
   
   O   sR  |  |}|d |dd }}d|_|jr| |||jS | | |jr4t|j | |||jS | | |jrD| |||jS z| 	| W n' t
yr } zt| tdt||_| |||jW  Y d}~S d}~ww t|jto}|jdu}|r|jj|j |jr|jjrt| jrt|j}	ntd|j}	| |	||S )z}Handle rpc request.

        :params request: Django HttpRequest instance.
        :returns: HttpResponse instance.

        r   r   NTServerr3   )get_contextsactivein_errorhandle_errorget_in_objectloggererrorget_out_object	out_errorget_out_string	Exception	exceptionr   r   
isinstanceout_protocolr
   out_header_doc	transportresp_headersupdate
descriptormtomNotImplementedErrorr[   r   
out_stringr   joinr2   )
r/   r0   r   r   contextsp_ctxothersehave_protocol_headersr2   r   r   r   
handle_rpc   sB   



zDjangoServer.handle_rpcc                 O   s   t | |d}| jjdu rtd| jdu r0i | jj_| jj|  | jj }| j	r/|| _n| j}||j
_t|j
j}| ||dS )zReturn services WSDL.ztext/xml; charset=utf-8NzWSDL is not availabler   )r   docwsdl11r   r^   service_elt_dictbuild_interface_documentbuild_absolute_uriget_interface_documentr_   rt   wsdlr   r2   )r/   r0   r   r   ctxr   r2   r   r   r   handle_wsdl   s    

zDjangoServer.handle_wsdlc                 C   sF   |j jdu r|j||j _| | td|j}| ||||S )zSerialize errors to an iterable of strings and return them.

        :param p_ctx: Primary (non-aux) context.
        :param others: List if auxiliary contexts (can be empty).
        :param error: One of ctx.{in,out}_error.
        Nr3   )	rt   	resp_coderr   fault_to_http_response_codern   r   r{   rz   r2   )r/   r}   r~   rk   respr   r   r   rh      s   

zDjangoServer.handle_errorc                 C   s&   t | || jjj}|jg|_| |S )zGenerate contexts for rpc request.

        :param request: Django HttpRequest instance.
        :returns: generated contexts
        )rY   r`   rr   	mime_typebody	in_stringgenerate_contexts)r/   r0   initial_ctxr   r   r   re      s
   

zDjangoServer.get_contextsNc              
   C   s   |j j D ]\}}|dur|||< q|j jr"t|j jdd |_z
t| |||d W n tyC } zt	| W Y d}~nd}~ww |
  |S )a<  Populate response with transport headers and finalize it.

        :param response: Django HttpResponse.
        :param p_ctx: Primary (non-aux) context.
        :param others: List if auxiliary contexts (can be empty).
        :param error: One of ctx.{in,out}_error.
        :returns: Django HttpResponse

        N   )rk   )rt   ru   itemsr   r    r!   r   ro   rj   rp   close)r/   r2   r}   r~   rk   hvr   r   r   r   r2      s   
zDjangoServer.response)FTr   )r6   r7   r8   r9   r]   r   r   rh   re   r2   __classcell__r   r   rb   r   rZ      s    2rZ   c                   @   s   e Zd ZdZdZdZdZdZdZe	ddZ
e	 ZdZdZd	Zg d
Zdd Zedd Zedd Zdd Zdd Zdd Zdd Zdd ZdS )
DjangoViewz3Represent spyne service as Django class based view.Nr   zspyne.applicationr   lxml)	validatorFT)getpostputpatchdeleteheadoptionstracec                 K   s(   || _ | D ]
\}}t| || qd S r   )serverr   setattr)r/   r   r   rL   r&   r   r   r   r]   +  s   zDjangoView.__init__c                    s   D ]}| j v rtd| jf t |s td j|f q fdd} fdd}|dpEt|d|d	|d
|d|dd}|dpTt||d|dd fdd}t| dd t| jdd |S )zdRegister application, server and create new view.

        :returns: callable view function
        zUYou tried to pass in the %s method name as a keyword argument to %s(). Don't do that.zm%s() received an invalid keyword %r. as_view only accepts arguments that are already attributes of the class.c                    s     | }|d ur|S t | S r   )r   getattrrL   r&   cls
initkwargsr   r   r   D  s   
zDjangoView.as_view.<locals>.getc                    s"    | d }|d ur|S t | S r   )popr   r   r   r   r   r   H  s   zDjangoView.as_view.<locals>.popapplicationservicestnsnamein_protocolrr   )r   r   r   r   rr   r   r[   ra   )r[   ra   c                    sX    ddi}t |drt |ds|j|_| |_||_||_|j| g|R i |S )Nr   r   r   r   )hasattrr   r   r0   r   r   dispatch)r0   r   r   r/   r   r   r   r   r   viewW  s   z DjangoView.as_view.<locals>.viewr   )updated)assigned)http_method_names	TypeErrorr6   r   r   rZ   r   r   )r   r   rL   r   r   r   r   r   r   r   as_view1  s:   



zDjangoView.as_viewc                 O   sB   |j  | jv rt| |j  | j}n| j}||g|R i |S r   )rD   lowerr   r   http_method_not_allowed)r/   r0   r   r   handlerr   r   r   r   h  s   zDjangoView.dispatchc                 O      | j j|g|R i |S r   )r   r   r/   r0   r   r   r   r   r   r   t     zDjangoView.getc                 O   r   r   )r   r   r   r   r   r   r   w  r   zDjangoView.postc                 O   s*   t jd|j|jd| jdd t|  S )NzMethod Not Allowed (%s): %si  )r!   r0   )extra)rj   warningrD   r@   r0   r   _allowed_methodsr   r   r   r   r   z  s   
z"DjangoView.http_method_not_allowedc                 O   s$   t  }d|  |d< d|d< |S )z8Handle responding to requests for the OPTIONS HTTP verb.z, Allow0zContent-Length)r   r{   r   )r/   r0   r   r   r2   r   r   r   r     s   zDjangoView.optionsc                    s    fdd j D S )Nc                    s   g | ]}t  |r| qS r   )r   upper).0mrA   r   r   
<listcomp>  s    z/DjangoView._allowed_methods.<locals>.<listcomp>)r   rA   r   rA   r   r     s   zDjangoView._allowed_methods)r6   r7   r8   r9   r   r   r   r   r   r	   r   rr   	interfacer[   ra   r   r]   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r     s.    

6
r   )1r9   
__future__r   logging	getLoggerr6   rj   	functoolsr   spyner   spyne.applicationr   r   spyne.auxprocr   spyne.model.faultr   spyne.protocol.soapr	   spyne.protocol.httpr
   spyne.server.httpr   r   r   spyne.server.wsgir   
spyne.utilr   spyne.util.addressr   django.httpr   r   r   django.views.decorators.csrfr   r   ImportError_import_errorr   r   r:   r=   rY   rZ   objectr   r   r   r   r   <module>   s>   
+ 