
    tiZ                     l    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	 Zd
 Zd Zd Zy)    N)JsonResponse)settings)render   )fetch_zoho_data)get_access_token)	Paginatorc                 `    t        d      }|j                  dg       d d }t        | dd|i      S )NCallsdata
   zfrontend/home.htmlcalls)r   getr   )request
calls_datar   s      -/var/www/html/zoho_frontend/frontend/views.pyhomer   ?   s8     )JNN62&s+E'/'51ABB    c                     t        d      }|j                  dg       }| j                  j                  dd      }t        |d      }t	        d|       |j                  |      }t        | dd|i      S )	NLeadsr   pager   r   	paginatorzfrontend/leads.htmlleads)r   r   GETr	   printget_pager   )r   
leads_datar   page_numberr   page_objs         r   
leads_pager    E   sk     )JNN62&E++//&!,K%$I	+i !!+.H'07H2EFFr   c                     t        d      }|j                  dg       }t        |d      }| j                  j                  dd      }|j	                  |      }t        | dd|i      S )Nr   r   r   r   r   zfrontend/call_logs.htmlr   )r   r   r	   r   r   r   )r   r   r   r   r   r   s         r   call_logs_pager"   O   sa     )JNN62&E%$I++//&!,K!!+.H'4w6IJJr   c                    t               }|st        ddid      S d| }dd| i}	 t        j                  ||      }|j                  d	k7  r/t        d
|j                         t        ddi|j                        S |j                         j                  dg       }|st        ddid      S |d   }|j                  d      xsa |j                  d      xsN |j                  d      xs; |j                  d      xs( |j                  d      xs |j                  d      xs d}|j                  d      xs( |j                  d      xs |j                  d      xs d}	t        |j                  dd      |j                  dd      ||	d      S # t        $ r7}
t        dt        |
             t        dt        |
      id       cY d!}
~
S d!}
~
ww xY w)"zl
    Fetch a single call's details from Zoho CRM including Description, Status, Start Time, Transcript.
    errorz$Failed to generate Zoho access tokeni  )statusz%https://www.zohoapis.in/crm/v2/Calls/AuthorizationzZoho-oauthtoken )headers   z Error fetching call insights:zFailed to fetch call datar   zNo data foundi  r   DescriptionCall_DescriptionNotesCall_ResultOutcomeCustom_DescriptionzNo description available
TranscriptAI_TranscriptCall_TranscriptzNo transcript availableCall_StatusUnknownCall_Start_Time)r2   r4   r)   r/   z" Exception fetching call insights:i  N)
r   r   requestsr   status_coder   textjson	Exceptionstr)r   call_idaccess_tokenurlr'   response	data_listcalldescription
transcriptes              r   get_call_insightsrD   Z   s    $%LW&LMVYZZ1'
;C"2<. ABG(;<<W53&2HMMB*E FxOcOcddMMO''3	/ :3GG| HH]# *xx*+*xx * xx&* xx	"	*
 xx,-* * 	 HH\" )xx()xx)*) )	 	 88M9=#xx(99E&$	
  	  ;2CF;Wc!f-c::;s+   AF >0F /C#F 	G,GGG)r5   django.httpr   django.confr   django.shortcutsr   zoho_servicer   r   django.core.paginatorr	   r   r    r"   rD    r   r   <module>rK      s4   l  $   # ) * +CGK3;r   