File: //home/arjun/projects/buyercall/buyercall/blueprints/widgets/__pycache__/bw_tasks.cpython-310.pyc
o
�we � @ s� d dl Zd dlZd dlZd dlZd dlmZmZ d dlm Z d dl
mZ d dlm
Z
mZmZ d dlmZ d dlmZ d dlmZ d d lmZ d
ZdZdZe e�Ze�� d
d� �Zejdd� �Zejdd� �Z dS )� N)�url_for�current_app)�create_celery_app)�db)�CallStorage�InboundCallState�select_voice)� bw_client)�BandwidthException)�Lead)�Agenti�Q z=https://s3.amazonaws.com/buyercall-static-sounds/dialtone.mp3z1We're sorry, there was an error calling the leed.c
K s� t jtjd tjd d�}ddlm}m} t�d� t j
�t j| k��
� }|s1t�d�| �� d S dd lm}m} |j
�|j|jk��
� }|jj} | sP|jjj} | jrat�d
�|jj|j�� d S |jdv rmt�d� d S t||j�}
|
�� d |
_d
d� t|jj dd� d�D �}|s�t�d� d S d|_| j!d7 _!t"j#�$� |jj%d r�t&|j� d S |jj%d r�t'�(|� t)|j*d�}||�}
z|
�+||j|� W d S t,y� } zt�-t.�/� � d|_t"j#�$� |||j� W Y d }~d S d }~ww )N�REDIS_CONFIG_URL�REDIS_CONFIG_PORT��host�port� )�BandwidthRouting�send_notify_emailzTrying to call lead again...zLead {} not available.r ��Partnership�PartnershipAccountz5Partnership account {} ({}) has exceeded their quota.)�ringing� completedz!Lead is currently in another callc S s g | ] }|j jr|j�qS � )�agent�
available_now�agent_id)�.0�asgr r �G/home/arjun/projects/buyercall/buyercall/blueprints/widgets/bw_tasks.py�
<listcomp>I s ��zcall_again.<locals>.<listcomp>c S s | j S )N)�order)�xr r r �<lambda>K s zcall_again.<locals>.<lambda>)�keyz*No agents were configured for this widget.r �routeSimultaneously�
routeRandomly�voice�missed)0�redis�StrictRedis�celery�conf�routingr r �log�infor �query�filter�id�first�warning�format�'buyercall.blueprints.partnership.modelsr r �partnership_account_id�partnership_account�subscription�partnership�usage_over_limit�name�status�debugr �inc_callback_cntr �sorted�widget�assignments�
call_countr �session�commit�options�
call_parallel�random�shuffler �partnership_id�route_sequential� Exception�error� traceback�
format_exc)�lead_id�kwargs�redis_db�Routingr �leadr r �partner_accountr: �storage� agent_ids�client�r�er r r �
call_again sf
�
�
�
��r\ c K s� t jtjd tjd d�}ddlm}m}m} t� d� t
j�t
j
��t
j| k��� }|j
}ddlm}m} | j�| j|jk��� }
||�}td t|� � |sgt�d
�|j�� d|_tj�� |||j
� t|| �}tj |_!|�"� d�| �}
d
�| �}d�| �}|�#|dt$ d� |�#|dt$ d� t%j�t%j�&|���'� }t� d�t(|��� d }t)|
j*d�}||�}|D ]�}zi|�+d�|j��}t� d�|�� d�| �}
|�,|
dd�}t� d�|�� |�-|||�}|�.� �. |j/�r|j/t|j�k�r|�0� W d � W n@|�1|
d�|j|�� W d � n 1 �sw Y W q� t2�yG } zt�t3�4� � t�d�|j�� W Y d }~q�d }~ww |d u �r^|�#|dt$ d� d|_tj�� |�5|
dt$ � d S )Nr
r r r )r r �get_available_agent_idszParallel call task started.r r zAgent ids: z&No agents are available for widget {}.r) zLIST{}zSUCC{}z CONNECT{}� �0zCalling {} agents...r( zBUSY_{}zagent busy: {}���zThe calls are: {}z{}_{}zError calling agent {}...z-1)6r* r+ r, r- r. r r r] r/ r0 r r1 �joinrB r2 r3 �oner7 r r r8 r4 �print�strr5 r6 r>