o
    I<Vî  ã                   @   s¼   d dl mZ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 d d	lmZ d d
lZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd
S )é    )Úabsolute_importÚunicode_literals©Úwraps)ÚHttpResponseÚHttp404)Ú	serialize)Ústates)Útasks)ÚAsyncResult)Úget_full_cls_nameÚkwdict)Ú	safe_reprNc                 C   s   t t| ƒdd�S )Nzapplication/json)Úcontent_type)r   r   )Úresponse© r   ú@/var/www/html/env/lib/python3.10/site-packages/djcelery/views.pyÚJsonResponse   s   r   c                    s   ‡ fdd„}|S )a  Decorator turning any task into a view that applies the task
    asynchronously. Keyword arguments (via URLconf, etc.) will
    supercede GET or POST parameters when there are conflicts.

    Returns a JSON dictionary containing the keys ``ok``, and
        ``task_id``.

    c                    sT   t | jdkr	| jp| jƒ}tdd„ | ¡ D ƒfi |¤Ž}ˆ j|d�}td|jdœƒS )NÚPOSTc                 s   s   � | ]	\}}||fV  qd S )Nr   )Ú.0ÚkÚvr   r   r   Ú	<genexpr>%   s   € z.task_view.<locals>._applier.<locals>.<genexpr>)ÚkwargsÚtrue)ÚokÚtask_id)	r   Úmethodr   ÚGETÚdictÚ	iteritemsÚapply_asyncr   r   )ÚrequestÚoptionsr   Úresult©Útaskr   r   Ú_applier!   s   ÿÿztask_view.<locals>._applierr   )r&   r'   r   r%   r   Ú	task_view   s   
r(   c                 C   s0   zt | }W n ty   tdƒ‚w t|ƒ| ƒS )z«View applying a task.

    **Note:** Please use this with caution. Preferably you shouldn't make this
        publicly accessible without ensuring your code is safe!

    zapply: no such task)r
   ÚKeyErrorr   r(   )r"   Ú	task_namer&   r   r   r   Úapply,   s   ÿr+   c                 C   s   t d|t|ƒ ¡ dœiƒS )z+Returns task execute status in JSON format.r&   )ÚidÚexecuted)r   r   Ú
successful)r"   r   r   r   r   Úis_task_successful:   s   

þr/   c                 C   s\   t |ƒ}|j|j}}t|||d�}|tjv r(|j}| t|ƒt	|j
ƒ|dœ¡ td|iƒS )z.Returns task status and result in JSON format.)r,   Ústatusr$   )r$   ÚexcÚ	tracebackr&   )r   Ústater$   r   r	   ÚEXCEPTION_STATESr2   Úupdater   r   Ú	__class__r   )r"   r   r$   r3   ÚretvalÚresponse_datar2   r   r   r   Útask_statusB   s   

þr9   c                 C   s   t t ¡  ¡ t ¡  ¡ dœƒS )z2View returning all defined tasks as a JSON object.)ÚregularÚperiodic)r   r
   r:   Úkeysr;   )r"   r   r   r   Úregistered_tasksO   s   
ÿr=   c                    s   t ˆ ƒ‡ fdd„ƒ}|S )aQ  Decorator turning a function into a task webhook.

    If an exception is raised within the function, the decorated
    function catches this and returns an error JSON response, otherwise
    it returns the result as a JSON response.


    Example:

    .. code-block:: python

        @task_webhook
        def add(request):
            x = int(request.GET['x'])
            y = int(request.GET['y'])
            return x + y

        def view(request):
            response = add(request)
            print(response.content)

    Gives::

        "{'status': 'success', 'retval': 100}"

    c               
      s^   z	ˆ | i |¤Ž}W n t y% } zdt|ƒdœ}W Y d }~t|ƒS d }~ww d|dœ}t|ƒS )NÚfailure)r0   ÚreasonÚsuccess)r0   r7   )Ú	Exceptionr   r   )Úargsr   r7   r1   r   ©Úfunr   r   Ú_innerq   s   €û
ztask_webhook.<locals>._innerr   )rD   rE   r   rC   r   Útask_webhookU   s   
rF   )Ú
__future__r   r   Ú	functoolsr   Údjango.httpr   r   Úanyjsonr   Úceleryr	   Úcelery.registryr
   Úcelery.resultr   Úcelery.utilsr   r   Úcelery.utils.encodingr   Úcelery.taskr   r(   r+   r/   r9   r=   rF   r   r   r   r   Ú<module>   s"    