HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall/buyercall/blueprints/widgets/__pycache__/models.cpython-310.pyc
o

we�C�@sddlZddlZddlmZddlmZddlmZddl	m
Z
mZmZddl
mZddlmZddlmZdd	lmZdd
lmZmZmZddlmZddlmZdd
lmZddlm Z Gdd�deej!�Z"Gdd�dej!�Z#Gdd�deej!�Z$dd�Z%dS)�N)�jsonify)�current_user)�
ResourceMixin)�backref�column_property�	load_only)�
postgresql)�association_proxy)�
flag_modified)�Index)�and_�func�select)�db)�User)�Lead)�Agentc	@s�eZdZdZedddd�fZejejdd�Z	ejej
dddd�Zeje
d	d
�Zedd�Zejejejd
ddd�ddd�Zeje�
d�ddd�Zeje�
d�dd�Zeje�
d�ddd�Ze�dejejdddd��Zejdejddd�Zejejdddd�Zeje�
d�dddd�Zejejdddd�Zeee �!e
j	�g��"e
j#ek��Z$ejejejdddd�ddd�Z%ejejejdddd�ddd�Z&d d!�Z'd"d#�Z(d$d%�Z)d&d'�Z*e+dLd)d*��Z,e+dLd+d,��Z-e+d-d.��Z.e+d/d0��Z/e0d1d2��Z1e0d3d4��Z2e2j3d5d4��Z2e0d6d7��Z4e0d8d9��Z5e0d:d;��Z6e0d<d=��Z7e+d>d?��Z8e+d@dA��Z9e+dBdC��Z:e+dDdE��Z;e0dFdG��Z<e0dHdI��Z=dJdK�Z>d(S)M�Widget�widgets�uq_widgets_guid�guidT)�unique��primary_keyF)�index�nullabler�widget�r�assignments�agent�partnership_accounts.id�CASCADE��onupdate�ondelete�rr�i,�r�2�phone_number_idzphonenumbers.idzSET NULL�optionsz{}�r�server_default�true)r�defaultr,�)rrrzusers.idcCs*g}|jD]	}|�|jj�qtt|��S�N)�channels�append�channel_type�name�list�set)�self�
channel_types�channel�r:�E/home/arjun/projects/buyercall/buyercall/blueprints/widgets/models.py�get_channel_typesVs
zWidget.get_channel_typescCs$|jD]}|jj|kr|SqdSr0)r1r3r4)r7�c_typer9r:r:r;�get_channel_by_type\s

�zWidget.get_channel_by_typecCsddlm}|�|j�}|jS)Nr)�PartnershipAccount)�'buyercall.blueprints.partnership.modelsr?�	get_by_id�partnership_account_id�partnership_id)r7r?�partnership_accountr:r:r;�get_partnership_idbszWidget.get_partnership_idcCs(ddlm}m}|�|j�}|�|j�S)Nr)r?�Partnership)r@r?rFrArB�get_sid_from_idrC)r7r?rFrDr:r:r;�get_partnership_sidgszWidget.get_partnership_sidNcCsP|r&g}|j�|jdktjtjk���}|D]
}||��vr#|�|�q|SgS)NT)	�query�filter�
is_options_v3rrBr�allr<r2)�cls�type_r�_widgets�_widgetr:r:r;�get_unallocated_typels��
�zWidget.get_unallocated_typec
Cs�ddlm}d}d}z!tdi|��}|j�|j|k���}|r#|j|_|�	�W||fSt
yL}zt�d�
|��t|�}WYd}~||fSd}~ww)zb
        Return widget id if widget was created successfully.

        :return: widget id
        r��PhoneN� Error creating widget. Error: {}r:)�(buyercall.blueprints.phonenumbers.modelsrSrrIrJ�sid�first�id�
inbound_id�save�	Exception�log�error�format�str)rM�params�phonenumber_idrSrr]�phone�er:r:r;�
api_createxs 
���zWidget.api_createc
Cs�ddlm}d}z2tdi|��}|j�|j|k|j|jk���}|r%||_t	j
�|�t	j
��|j
}t	j
��W|StyU}zt�d�|��WYd}~|Sd}~ww)zf
        Return widget guid if widget was created successfully.

        :return: widget guid
        rrR���rTNr:)rUrSrrIrJrXrBrW�inboundr�session�add�flushr�commitr[r\r]r^)rMr`rarS�widget_guidrrbrcr:r:r;�api_create_new�s,
��
���zWidget.api_create_newc
Cs�ddlm}d}z�tj�ttj|ktj|k����}|r�|r!||_	|r&||_
|dvr-||_|jr�|j}t
�|�|r�|d}|rV|d}|
rJ|
|d<|rP||d<|rV||d<|r\||d	<|	rb|	|d
<|rh||d<|
rn|
|d<|rt||d
<|rz||d<|r�||d<|r�||d<|r�||d<|r�||d<t|d�|j�|j|k|j|jk���}|r�||_tj��d}W|SW|Sty�}zt
�d�|��WYd}~|Sd}~ww)z[
        Return True if widget was updated successfully.

        :return: Boolean
        rrRF)TF�agentsrX�contactUsing�fullName�addHiddenInformation�hiddenInformation�language�recordCalls�	voicemail�voicemailMessage�retryRouting�
routeRandomly�routeInSequence�routeSimultaneously�_optionsTz Error updating widget. Error: {}N)rUrSrrIrJrrBrrWr4�type�enabledr*r\r]r
rXrYrrgrjr[r^)rMrBrr4r{rar|�add_hidden_informationrr�hidden_information�record_callsrt�voicemail_message�agent_id�agent_contact_using�agent_full_name�route_randomly�route_in_sequence�route_simultaneously�
retry_routingrS�resultr�result_options�result_option_agents�first_agentrbrcr:r:r;�
api_update�s�
�


��
�J���zWidget.api_updatecCstdd�|jD��S)zC Checks if the agent schedule allows calling at this time.
        css�|]}|jVqdSr0)�
available_now)�.0rr:r:r;�	<genexpr>s�z*Widget.agents_available.<locals>.<genexpr>)�anyrm�r7r:r:r;�agents_availableszWidget.agents_availablecCsndd�t|jdd�d�D�}||jd<|j|jd<z
|jd|jd	<W|jSty6d
|jd	<Y|jSw)NcSs"g|]
}t|j|jj|jd��qS))rXrorn)�dictr�r�	full_name�
contact_using)r��asgr:r:r;�
<listcomp>s����"Widget.options.<locals>.<listcomp>cS�|jSr0)�order)�xr:r:r;�<lambda>sz Widget.options.<locals>.<lambda>)�keyrm�
widgetType�
notifyNone�enableNotificationsF)�sortedrrzr{�KeyError)r7rmr:r:r;r*s�
��zWidget.optionsc	Cs�|sdSdd�|jD�D]}tj�|�qt|�dg��D]\}}|d|d}}|j�t||||d��q|�dd�|_|d	|d
<||_	dS)NcSsg|]}|�qSr:r:�r�r�r:r:r;r�-sr�rmrXrn)rr�r�r�r�rr�r�)
rrrg�delete�	enumerate�getr2�AgentAssignmentr{rz)r7�optr��ir�id_r�r:r:r;r*'s
�
cCs�t�}|jdr
|Stj�tjdktj�d�tj|jk��	�}|r$|j
nd}dd�|jD�}|jdrS|jdr?|r?|�|�|jd	rI|�
|�|�
t|jd
��|jdrx|jdrd|rd|�|�|jd
rn|�
|�|�
t|jd��|S)z6 Return a list of notification emails for the widget. r��adminFNcSsg|]}|j�qSr:)�email)r��ar:r:r;r�Lsz!Widget.emails.<locals>.<listcomp>�notifyAllLeads�notifyAllMe�notifyAllAgents�notifyAllCustom�notifyMissedLeads�notifyMissedMe�notifyMissedAgents�notifyMissedCustom)r5r*rrIrJ�role�is_deactivated�is_rBrWr�rmr2�extend�split_emails)r7r�r��my_email�
agents_emailsr:r:r;�emails?s2


��







z
Widget.emailscCs<d}d|jvr|jdr|jddus|jddurd}|S)�? Returns whether or not the lead is an ADF widget lead
        F�	notifyAdfTr-)r*�r7r�r:r:r;�is_adf]s

&z
Widget.is_adfcCr�)r�)rYr�r:r:r;r)iszWidget.phone_number_idcCs(t�}|jdr|�t|jd��|S)zB Return a list of adf notification emails for the widget.
        r��notifyAdfCustom)r5r*r�r�r�r:r:r;�adf_notification_emailsos
zWidget.adf_notification_emailscCs�d}|rtj�td���tj|ktj|k���}n|r/tj�td���tj|ktj|k���}|r>t	j
�|�t	j
��dSdS)z< Deletes widget. Provide None where not applicable.
        NrXrTF)
rrIr*rrJrXrBrWrrrgr�rj�rMrXrrBrr:r:r;r�zs0�
���
��
z
Widget.deletecCs~d}|rtj�tdd���tj|ktj|k���}n|r1tj�tdd���tj|ktj|k���}|r=d|_	t
j��dSdS)z< Disable widget. Provide None where not applicable.
        NrXr|rFT�
rrIr*rrJrXrBrWrr|rrgrjr�r:r:r;�disable��$����
zWidget.disablecCs~d}|rtj�tdd���tj|ktj|k���}n|r1tj�tdd���tj|ktj|k���}|r=d|_	t
j��dSdS)z; Enable widget. Provide None where not applicable.
        NrXr|rTFr�r�r:r:r;�enable�r�z
Widget.enablecCs.tj�tjj|d�tj|k���}|rdSdS)N)rTF)rrIrJr1r�r{rW)rM�type_id�	widget_idrr:r:r;�is_widget_channel_type_exist�s"z#Widget.is_widget_channel_type_existcC�|j�d�S)z6 Return the date/time this widget was created
        �%Y-%m-%d %H:%M:%S)�
created_on�strftimer�r:r:r;�created_datetime��zWidget.created_datetimecCr�)z6 Return the date/time this widget was updated
        r�)�
updated_onr�r�r:r:r;�updated_datetime�r�zWidget.updated_datetimecCs t|j|j|j|j|j|jd�S)N)rXr4�user_idr�r�r*)rrXr4r�r�r�r*r�r:r:r;r�s�zWidget.jsonifyr0)?�__name__�
__module__�__qualname__�
__tablename__r�__table_args__r�Column�IntegerrX�Stringr�relationshipr�leadsr	rm�
ForeignKeyrBr4�descriptionr{rYr�JSONrz�BooleanrKr�r|rrr
�count�whererk�
lead_count�
created_by�
updated_byr<r>rErH�classmethodrQrdrlr��propertyr�r*�setterr�r�r)r�r�r�r�r�r�r�rr:r:r:r;rs���
���	
�
�
�"����

]
	












rc@s�eZdZdZejeeddd�d�Zeje	eddd�d�Z
ejejej
dddd�d	d
�Zejejej
dddd�d	d
�Z	eje�d�d
dd�Ze�e���ZdS)r��
widget_agentsrrL)�cascaderz
widgets.idr!r"Trz	agents.id�
Frbr+N)r�r�r�r�rr�rrrrrr�r�r�r�r�r�r�r�r:r:r:r;r��s2�����r�c@sTeZdZdZeje�d�dd�Zejejej	dddd�ddd	�Z
ejejdd
�ZdS)�
WidgetFile�widget_files�$Trr r!r"Fr%r'N)r�r�r�r�rr�r�rr�r�rB�urlr:r:r:r;r��s�r�cCsdd�t�d|�D�S)z� Split a string of email addresses into individual emails.

    :param email_str: A comma- or semicolon-separated list of emails.
    :type email_str: str
    :return: A list of emails
    :rtype: list
    cSsg|]}|r|���qSr:)�stripr�r:r:r;r�sz split_emails.<locals>.<listcomp>z,|;)�re�split)�	email_strr:r:r;r�sr�)&r��loggingr\�flaskr�flask_loginr�buyercall.lib.util_sqlalchemyr�sqlalchemy.ormrrr�sqlalchemy.dialectsr�sqlalchemy.ext.associationproxyr	�sqlalchemy.orm.attributesr
�sqlalchemy.schemar�
sqlalchemyrr
r�buyercall.extensionsr� buyercall.blueprints.user.modelsr�!buyercall.blueprints.leads.modelsr�"buyercall.blueprints.agents.modelsr�Modelrr�r�r�r:r:r:r;�<module>s,C