
    aܪi,                       d dl mZmZ d dlmZ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 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mZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE d dlFZFd dlGZGd dlHmIZI d dlJmKZKmLZL d dlMmMZMmNZN d dlOZOd dlPZPd dlQmRZR d dlSZSd dlTmUZV d dlWZWd dlOZOd dlXZXd dlYZYd dlZZZd dl[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZk d dllmmZm d dlnmoZo d dlpmqZqmrZrmsZsmtZt ddlumvZv d dl mwZw d dlxmyZymzZzm{Z{ d dl|Z|d dl}m~Z~ d dlmZ d dlmZ d d lmZ d dlSZSd dlZd d!lmZ d d"lmZ d d#lmZ d d	lMmZ d d$lmZmZ d d%lmZ d dlZd dlZd d&lmZ d d'lmZ d d(lmZ d dlZd dlZd dlZd d)lmZ d d*lmZ d d+lmZ d d,lmZmZmZ d d-lmZ d dlZd dlZd d.llmZ d d/lmZ d d0lmZ dd1lmZ  G d2 d3e      Z G d4 d5e      Z G d6 d7e      Z G d8 d9e      Z G d: d;e      Z G d< d=e      Z G d> d?e      Z G d@ dAe      Z G dB dCe      Z G dD dEe      Z G dF dGe      Z G dH dI      Z G dJ dKe      Z G dL dMe      Z G dN dOe      Z G dP dQe      Z G dR dSe      Z G dT dUe      Z G dV dWe      Z G dX dYe      Z G dZ d[e      Z G d\ d]e      Z G d^ d_e      Z G d` dae      Z G db dce      Z G dd deew      Z edfg       eeg      dg               Z edhg       eeg      di               Z G dj dke      Z G dl dme      Z edfg      dn        ZddoZ edhg      dp        Z edhg      dq        Z edhg      dr        Z edfg       eeg      ds               Z edhg       eeg      dt               Z edfg       eeg      du               Z G dv dwe      Z edhg       eeg      dx               Z edhg       eeg      dy               Z edhg       eeg      dz               Z edhg       eeg      d{               Z edfg       eeg      d|               Z edhg       eeg      d}               Z edhg       eeg      d~               Z edfg      d        Z edhg       eeg      d               Z edfg       eeg      d               Z G d de      Z G d de      Zd ZddZd Zd Z edfg       eeg      d               Z G d de      Zd Z G d de      Z edfg       eeg      d               Z G d de      Zd Z edhg      d        Zd Z edhg       eeg      d               Z edhg       eeg      d               Z edfg       eeg      d               Z G d de      Z edfg       eeg      d               Z edfg       eeg      d               Z edfg       eeg      d               Z G d de      Z G d de      Zy)    )ModelViewSetReadOnlyModelViewSet)IsAuthenticatedAllowAny)MultiPartParser
FormParser
JSONParserResponsestatus)settings)GroupUser)APIViewtimezone)transaction)FileSystemStorage)ImplementerDetails   )	TblClientTblDepartment	TblVendorTblJobDescriptionTblImpDepartmentTblClientDocumentTblVendorDocumentAssignJdEmailOTPUserLinkedInJobPostImplementerPortalConfigTblCandidateProfileTblCandidateResumecareerJobApplicationTblMatchedProfilesCareerJobApplicationL0InterviewListL0InterviewSessions
PublicLinkTblMatchedProfilesCandidateJobEmailShare)ClientSerializerTblDepartmentSerializerVendorSerializerTblJobDescriptionSerializerTblImpDepartmentSerializerClientDocumentSerializerVendorDocumentSerializer!ImplementerPortalConfigSerializerTblCandidateProfileSerializerTblCandidateResumeSerializercareerSerializerResumeBasicSerializerL0InterviewListSerializerL0InterviewSessionsSerializerTblMatchedProfilesSerializer%TblMatchedProfilesCandidateSerializerAuditLogSerializerN)decode_header)	parseaddrparsedate_to_datetime)datetime	timedelta)unescape)AIEditLLMGCS)generate_passwordgenerate_apply_linkdelete_linkedin_post
LlmPromptsnormalize_resume_titlehighlight_inline_diff$check_ai_title_similarity_and_notifysend_resume_ack_emailsend_shortlist_emailDocumentTextExtractionsend_email_safenotify_recruiter_multiple_jdsprocess_shortlist_replysend_interview_calendar_inviteDBAuthFiltersget_email_template)EmailMessage)ObjectDoesNotExist)CountQFValue)post_to_linkedin)ViewSet)redirectget_object_or_404render)	urlencode)TfidfVectorizer)cosine_similarity)action)ValidationError)models)parse_datetime)api_viewpermission_classes)CosineDistance)TrigramSimilarity)APIRequestFactory)now)gTTS)File)signing)FileResponseHttpResponseForbiddenHttpResponse)reverse)	send_mail)ContentType)LogEntry)run_scraperc                        e Zd Zej                  j                         j                  d      ZeZ	e
gZeefZ fdZd Zd Z xZS )ClientViewSet-created_datec                     | j                   j                  }t        |          }t	        j
                  |      }|r|j                  |      S g S )Nclient_id__in)requestusersuperget_querysetrT   get_client_by_auth_userfilter)selfr   	query_set
client_ids	__class__s       (/var/www/auto_recruiter/masters/views.pyr   zClientViewSet.get_querysetM   sK    ||  G(*	"::4@
##*#==I    c                 &   |j                   j                         }|j                  d      sd|d<   | j                  |j                         }|j	                         r|j                  |j                  j                        }|j                  j                  d      }|D ]#  }t        j                  j                  ||       % t        d|j                   dt        j                  	      S t        |j                   t        j"                  	      S )
Nclient_typeEnddata
created_by	documentsclientdocumentzClient created successfullymessager   r   )r   copygetget_serializeris_validsaver   usernameFILESgetlistr   objectscreater   r   HTTP_201_CREATEDerrorsHTTP_400_BAD_REQUEST)	r   r   argskwargsr   
serializerr   r   docs	            r   r   zClientViewSet.createV   s    ||  "xx&"'D((gll(;
 __"<<00 % F  --k:I  !))00!  1  <&OO ..  ..
 	
r   c                    | j                         }| j                  ||j                  d      }|j                         r|j	                  |j
                  j                        }|j                  j                  d      }|D ]#  }t        j                  j                  ||       % t        d|j                  dt        j                        S t        |j                  t        j                         S )	NTr   partial
updated_byr   r   zClient updated successfullyr   r   )
get_objectr   r   r   r   r   r   r   r   r   r   r   r   r   HTTP_200_OKr   r   )	r   r   r   r   instancer   r   r   r   s	            r   updatezClientViewSet.updatev   s    ??$(( ) 

  __"<<00 % F  --k:I  !))00!  1  <&OO ))  ..
 	
r   )__name__
__module____qualname__r   r   allorder_byquerysetr.   serializer_classr   ri   r   r   parser_classesr   r   r   __classcell__r   s   @r   rz   rz   G   sK      $$&//@H')*%z2N
@
r   rz   c                   \     e Zd ZegZeZej                  j                         Z
 fdZ xZS )ClientDocumentViewSetc                     t         |          }| j                  j                  j	                  d      }|r|j                  t        |            }|S )Nr   	client_idr   r   r   query_paramsr   r   int)r   r   r   r   s      r   r   z"ClientDocumentViewSet.get_queryset   F    7')LL--11(;	Y@Hr   )r   r   r   r   ri   r3   r   r   r   r   r   r   r   r   s   @r   r   r      0    )*/ ((,,.H r   r   c                   ~    e Zd Zej                  j                         j                  d      ZeZ	e
gZeeefZd Zd Zy)DepartmentViewSetr{   c                 @   | j                  |j                        }|j                         rN|j                  |j                  j
                         t        d|j                  dt        j                        S t        |j                  t        j                        S Nr   r   zDepartment created successfullyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s        r   r   zDepartmentViewSet.create       ((gll(;
 OO"<<00   @&OO ..  ..
 	
r   c                 d   | j                         }| j                  ||j                  d      }|j                         rN|j	                  |j
                  j                         t        d|j                  dt        j                        S t        |j                  t        j                        S NTr   r   zDepartment updated successfullyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s         r   r   zDepartmentViewSet.update       ??$(( ) 

  OO"<<00   @&OO ))  ..
 	
r   N)r   r   r   r   r   r   r   r   r/   r   r   ri   r	   r   r   r   r   r    r   r   r   r      sF    $$((*33ODH.)* /:>N
(
r   r   c                   |    e Zd Zej                  j                         j                  d      ZeZ	e
gZeefZd Zd Zy)VendorsViewSetr{   c                    | j                  |j                        }|j                         r|j                  |j                  j
                        }|j                  j                  d      }|D ]#  }t        j                  j                  ||       % t        d|j                  dt        j                        S t        |j                  t        j                        S )Nr   r   r   vendorr   zVendor created successfullyr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   s           r   r   zVendorsViewSet.create   s    ((gll(;
 __"<<00 % F  --k:I  !))00!  1 
 <&OO ..  ..
 	
r   c                    | j                         }| j                  ||j                  d      }|j                         r|j	                  |j
                  j                        }|j                  j                  d      }|D ]#  }t        j                  j                  ||       % t        d|j                  dt        j                        S t        |j                  t        j                         S )	NTr   r   r   r   zVendor updated successfullyr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   s	            r   r   zVendorsViewSet.update   s    ??$(( ) 

  __"<<00 % F  --k:I  !))00!  1 
 <&OO ))  ..
 	
r   N)r   r   r   r   r   r   r   r   r0   r   r   ri   r   r   r   r   r   r   r   r   r   r      sD      $$&//@H')*%z2N
6
r   r   c                   \     e Zd ZegZeZej                  j                         Z
 fdZ xZS )VendorDocumentViewSetc                     t         |          }| j                  j                  j	                  d      }|r|j                  t        |            }|S )Nr   )	vendor_idr   )r   r   r   r   s      r   r   z"VendorDocumentViewSet.get_queryset  r   r   )r   r   r   r   ri   r4   r   r   r   r   r   r   r   r   s   @r   r   r     r   r   r   c                       e Zd ZegZd Zy)CountryCodeListViewc                 4    t        t        j                        S N)r   r   COUNTRY_CODES)r   r   s     r   r   zCountryCodeListView.get'  s    ..//r   Nr   r   r   r   ri   r   r   r   r   r   r   $  s    )*0r   r   c                   6    e Zd Zej                  j                   ed            j                  d      Ze	Z
egZeeefZ fdZd Zdededefd	Zd
 ZdedefdZd Zd Zd ZddZddZ eddgd      d        ZddZ  eddgd      d        Z! xZ"S )TblJobDescriptionViewlinkedin_posts)linkedin_posts_countz-jd_idc                    t         |          j                  d      }| j                  j                  }| j                  j
                  }t        j                  |      }|r|j                  |      }ng }|j                  d      r|j                  |d         }|j                  d      r|j                  |d         }|j                  d      r|j                  |d   	      }|j                  d
      r|j                  |d
         }|j                  d      r|j                  |d         }|j                  d      r|j                  |d         }|j                  d      r|j                  |d         }|j                  d      r+|j                  d      r|j                  |d   |d   g      }|S )NActive)client_id__status	jd_id__injob_type)r   r   r   r   r   department_id)r   min_exp)years_of_experience__gtemax_exp)years_of_experience__ltelocation)job_location__icontains
start_dateend_datejd_date__range)	r   r   r   r   r   r   rT   get_jd_by_auth_userr   )r   qsparamsr   jd_idsr   s        r   r   z"TblJobDescriptionView.get_queryset3  s   W!#**X*F**||  2248V,BB::j!F:$67B::h&"23B::k"VK%89B::o&)@AB::i F94EFB::i F94EFB::j!6*3EFB::l#

:(> &| 4fZ6HI  B 	r   c                 f   g }| j                  d      s|j                  d       | j                  d      s|j                  d       | j                  d      s|j                  d       | j                  d      s|j                  d       |s|j                  d	       |s|j                  d
       |S N	job_titlezMissing Job Titlejob_summaryzMissing Job SummaryresponsibilitieszMissing Responsibilitiesyears_of_experiencezMissing Years of Experiencez'Invalid or missing Job Description Datez'Client Data not found for sender domainr   append)jd_datajd_dateclient_datar   s       r   validate_jd_dataz&TblJobDescriptionView.validate_jd_dataZ      {{;'MM-.{{=)MM/0{{-.MM45{{01MM78 MMCD MMCDr   textlengthreturnc                 <   | sd|z  S t        j                  dd|       j                         j                         }|j	                         }dj                  d |D              }t        |      |k  r"|r |d   }|t        |      z
  }|dd|z    }||z  }|d|z  z   d | S )NX
[^A-Za-z ] c              3   &   K   | ]	  }|d      ywr   Nr   .0words     r   	<genexpr>z4TblJobDescriptionView.format_code.<locals>.<genexpr>|       3Ta3   r   resubstripuppersplitjoinlen)r	  r
  cleanwordsresult	last_wordneededextras           r   format_codez!TblJobDescriptionView.format_codes      < }b$/557==? 3U33 v;Eb	Ic&k)F aF
+EeOF #,'&11r   c                    |syt        j                  d|      }|j                  dd      j                  dd      }t        j                  dd|      }dj                  d |j                  d      D              }|j                         S )	Nr  NFKCz

z[ \t]+ c              3   <   K   | ]  }|j                           y wr   )r  )r  lines     r   r  zHTblJobDescriptionView.normalize_responsibilities_text.<locals>.<genexpr>  s     C$Cs   )unicodedata	normalizereplacer  r  r  r  r  )r   r	  s     r   normalize_responsibilities_textz5TblJobDescriptionView.normalize_responsibilities_text  sr    $$VT2||FD)11$=vvid+yyC$**T2BCCzz|r   c                     t        j                  dd|t         j                        j                         }t	        j
                  |      S Nz```json|```r  flagsr  r  
IGNORECASEr  jsonloadsr   r	  cleaneds      r   extract_json_from_llmz+TblJobDescriptionView.extract_json_from_llm  2    &&TGMMOzz'""r   c                     |sy|j                         j                         }|j                  dd      j                  dd      }t        j                  dd|      }|S Nr  ,.\s+r-  r  lowerr2  r  r  r   values     r   normalize_db_textz'TblJobDescriptionView.normalize_db_text  P    ##%c2&..sB7vsE*r   c                 	   | j                  |j                        }|j                         rj|j                  }t	        j
                         j                         }|j                  d      }|j                  d      }t        j                  |||      }	t        |	      }
|	rdj                  |	      nd }|
rdnd}t        j                  j                         }t        j                          5  d }t	        j
                         j#                  d      }|rt$        j                  j'                         j                  |j(                        }t+        |j,                        j/                  d	      }t        j1                  |j                  d
      d      }t3        |j4                  xs d      }t+        |      j/                  d	      }| d| d| d| }|dz   |_        |j7                  dg       nt        j                  j                  |j(                        }t        j1                  |j8                  d	      }t        j1                  |j                  d
      d      }t3        |j:                  xs d      }t+        |      j/                  d	      }| d| d| d| }|dz   |_        |j7                  dg       d d d        t=               }|j?                  |j                  d            }|j7                  |||||d||nd |
||j@                  jB                        }|jD                  |jF                  |jH                  |jJ                  |jL                  |jN                  |jP                  |jR                  |jT                  |jV                  |jX                  |jZ                  |j\                  |j^                  |j`                  d}tc        jd                  |g      }ddd}|jg                  |d   |d          |ji                  |      }| jk                  |jl                        }to        |jl                         |r,|d   j                  d      |_8        |j7                  dg       ts        d|j                  dtt        jv                        S ts        |jx                  tt        jz                        S # 1 sw Y   xY w)Nr   r   r   ; ErrorOpen%y%m%dpk   r      r   _r   client_jd_serial_numberupdate_fieldsimplementor_jd_serial_numberr   Created)r   jd_display_idr   implementor_idr  r   jd_stagejd_responsibilities_embedding	has_error
error_descr   jd_idrZ  r   r   r  about_companyr   r   domain_requirementscertification_requirementssecurity_clearance_requirements
onsite_jobjob_locationrequired_qualificationspreferred_qualifications皙?   temperaturethinking_budgetrm  rn  search_patternz$Job Description created successfullyr   r   )>r   r   r   validated_datar   rm   dater   r   r  boolr  r   r   firstr   atomicstrftimer   select_for_updaterQ  strr   zfillr'  r   rU  r   display_namerX  	AIEditLLMgenerate_embeddingsr   r   ra  rZ  r   r   r  rb  r   r   rc  rd  re  rf  rg  rh  ri  rI   jd_search_pattern_promptcreate_chatsend_messager>  r	  printro  r   r   r   r   r   )r   r   r   r   r   r  r  r  department_datavalidation_errorsr^  r_  status_valueimplemntor_datarZ  date_strclient_codejob_codecurrent_serial
serial_strimplementor_codeairecruit_llmresponsibilities_embeddingjd_instancellm_sending_datar|  generation_configjd_llm_responsellm_jd_datas                                r   r   zTblJobDescriptionView.create  s~   ((gll(;
  //Glln))+G!++k2K%kk/:O 5 F F+! ./I9J#45PTJ&/7VL"**002  ##% Y $#<<>228<"+"3"3"E"E"G"K"K{~~"K"^K"%k&;&;"<"B"B1"EK4@@[AY[\]H%()L)L)QPQ%RN!$^!4!:!:1!=J'2m1XJaz:,$WM:H1:LK7$$4M3N$O +2266/:L:L6M $ (='H'HIeIegh'i$4@@[AY[\]H%()U)U)ZYZ%[N!$^!4!:!:1!=J'7&8(1XJaPZ|$\MCQTUCUO@#((8V7W(X9Y< &KM)6)J)J7;;WiKj)k&$//%+-.#"LfLr.Hx|#%"<<00 * K %**!,!:!:(22'00'2'F'F!,!:!:*66$/$@$@'2'F'F.9.T.T3>3^3^)44 + 8 8+6+N+N,7,P,P $ (2'J'JL\K]'^$"#(! %%!-02CDU2V ,889QRO44_5I5IJK/&&'-8^-?-?@P-Q*  0@/A BE&OO ..  ..
 	
uY Ys   -GR==Sc                 p   | j                         }| j                  ||j                  d      }|j                         rS|j                  }|j                  d|j                        }|d   }|j                  d|j                        |j                  d|j                        |j                  d|j                        |j                  d|j                        d	}	t        j                  |	||      }
|j                  }| j                  |j                  d            }| j                  |j                        }t               }||k7  r |j!                  |j                  d            }t#        |
      }|
rd
j%                  |
      nd }|j&                  }g }|rt(        j*                  j-                  |ddg      j/                  |j0                        }| j3                  |j                  d            }| j3                  |j                  d            }| j3                  |j                  d            }| j3                  |j                  d            }|D ]  }| j3                  |j                        }| j3                  |j4                        }| j3                  |j6                        }| j3                  |j                        }||k(  r!||k(  r||k(  r||k(  r|j9                  |       |j:                  xs g }|j<                  |v s|j?                  |j<                         ||_        |sd|_        n|j&                  |_        |jA                  ddg        |rk|D ]f  }|j&                  dk(  s|j:                  xs g }|j<                  |vs2|j9                  |j<                         ||_        |jA                  dg       h |j                  d|j&                        }|rd}n|}|j<                  } ||jB                  k7  rtE        jF                         jI                  d      }!tK        |jB                        jM                  d      }"t        jO                  |j                  d      d      }#tQ        |jR                  xs d      }$tK        |$      jM                  d      }%|" d|# d|! d|% } |$dz   |_)        |jA                  dg       |jA                  |jT                  jV                  |||| ||nd       }&|j                  j                  d      rJ|&j0                  |&j<                  |&j                  |&j6                  |&j                  |&jX                  |&j                  |&j                  |&jZ                  |&j\                  |&j^                  |&j`                  |&j4                  |&jb                  |&jd                  d}'tg        jh                  |'g      }(dd d!})|jk                  |)d"   |)d#          |jm                  |(      }*| jo                  |*jp                        }+ts        |*jp                         |+r,|+d   j                  d$      |&_:        |&jA                  d$g       tw        d%|j                  d&t&        jx                  '      S tw        |jz                  t&        j|                  '      S )(NTr   r  r   r   r   r   r  )r   r   r   r  rL  rN  	Duplicater   
status__inra  rg  r   duplicated_jd_idr   rV  rM  rO  rR  rS  r   rT  r   rU  )r   r^  r_  r   rZ  r]  search_pattern_requiredr`  rj  rk  rl  rm  rn  ro  z$Job Description updated successfullyr   r   )?r   r   r   r   rp  r   r  r   r   r   r  r   r  r]  r3  rz  r{  rr  r  r   r   r   r   excludera  rI  rg  r   r  r  rZ  remover   r   r   rm   ru  rw  rx  r'  r   rU  r   r   rb  rc  rd  re  rf  rh  ri  rI   r|  r}  r~  r>  r	  r  ro  r   r   r   r   ),r   r   r   r   r   r   r  r  r  validation_inputr  r  new_respold_respr  r^  r_  current_statusduplicate_jdsexisting_jdsincoming_job_titleincoming_locationincoming_job_typeincoming_expjddb_job_titledb_locationdb_job_typedb_expcurrect_duplicate_idsincoming_statusr  rZ  r  r  r  r  r  r  r  r|  r  r  r  s,                                               r   r   zTblJobDescriptionView.update  sO   ??$(( ) 

   //Gkk)X-=-=>G!+.K %[[h6H6HI&{{=(:N:NO$+KK0BHD]D]$^'.{{3H(JfJf'g	  !6 F F ';! *2)O)O&;;GKKHZ<[\H;;H<U<UVH%KM8#-:-N-NKK 23.* ./I9J#45PTJ%__NM088??) &4  @   ''/ 
 &*%;%;GKK<T%U"$($:$:7;;~;V$W!$($:$:7;;z;R$S!#55gkkBW6XY& RB#'#9#9",,#GL"&"8"8"IK"&"8"8"EK!33B4J4JKF %(::'+<<'+<<"l2%,,R0020C0C0Ir-#115JJ1889O9OP2GB/#8,2	,.II	GG3Ex2PGQ-R2 ' HByyK/020C0C0Ir-#119NN1889O9OP2GB/GG3E2FGGH &kk(HOODO&.$22Mh000#<<>228<!+"7"78>>qA0<<W[[=UWXY!$[%H%H%MA!N 066q9
#.-q
!H:Qzl S6Dq6H3  0I/J K$//"<<00#%#+LfLr.Hx| * K || 9:(..%0%>%>!,!6!6 + 4 4+6+J+J%0%>%>#.#:#:(3(D(D+6+J+J2=2X2X7B7b7b"-"8"8$/$<$</:/R/R0;0T0T$ $ ,6+N+NP`Oa+b(#&',%! ))%m46GHY6Z #0"<"<=U"V"889M9MNo**+1<Q1C1CDT1UK.$$4D3E$FE&OO ))  ..
 	
r   c                    	 t         j                  j                  |      }|j                  }t         j                  j	                  |j
                  |g      }|D ]F  }|j                  xs g }||v s|D cg c]
  }||k7  s	| }}||_        |j                  dg       H |j                          t        dddd	      S c c}w # t        $ r#}	t        d
t        |	      dd	      cY d }	~	S d }	~	ww xY w)NrP  )r   duplicated_jd_id__containsr  rV  successz$Job Description deleted successfullyr   r      r   failedr   error  )r   r   r   rZ  r   r   r  r   deleter   	Exceptionrw  )
r   r   rQ  jd_objdeleted_display_idr  r  duplicated_idsxes
             r   destroyzTblJobDescriptionView.destroy  s   	O&..22b29F!'!5!5,44;; **,>+? < L
 # 	@!#!4!4!:%7#1&Q:L5L&N & +9B'GG+=*>G?	@ MMO y=cdmpqq&  	Ox#a&A#NN	Os<   A1C
 4C
 8
CC=C
 C
 
	C6C1+C61C6Fc                 (   | j                   }t        j                  j                  |ddd      j	                         }|sddiS |j
                  }|j                  }t        j                  j                  | d      j                  d      j	                         }i }g d}	d	 }
|r|j                  xs i }|s|sd}| j                  | j                  | j                  | j                  | j                  | j                  d}|	D ]>  } |
|j!                  |            } |
|j!                  |            }||k7  s7||d
||<   @ |s|sddiS |rA|s|r=|j"                  rt%        ||j"                         d|_        |j)                  dg       | j*                  dk(  r|sd}dg}|j-                  d| j                          |j-                  d| j                          |j-                  d| j                          | j                  r,|j-                  d       |j-                  | j                         | j                  rf|j-                  d       | j                  j/                         D ]8  }|j1                  d      j1                         }|s%|j-                  d|        : | j                  r|j-                  d| j                          t3        |       }|j-                  d|        |j-                  d| j4                          |j-                  dt7        j8                         j;                  d              dj=                  |      }t?        |||      }|j@                  dv rxd| _        d| _        | j)                  d dg       t        j                  jC                  | ||jE                         j!                  d!      |di d"tG        |      i#       d$dd%S t        j                  jC                  | |d |d&d'di#       dd$d%S )(NlinkedinTimplementerportalis_connected	is_activeskippedPostedr  r   
-posted_at)r   rg  r  r   salary_ranger   c                 :    | yt        |       j                         S )Nr  rw  r  )vals    r   r1  zKTblJobDescriptionView.post_jd_via_existing_linkedin_flow.<locals>.normalize  s    {s8>>##r   )oldnew)access_tokenpost_urnDeletedr   rV  rY     🚀 Hiring Now!   📌 Role:    📍 Location:    🧠 Experience: u   📝 Job Summary:u   🎯 Responsibilities:u   -• u   •    💰 Salary:    👉 Apply here:    🆔 JD Ref:    ⏰ %d-%m-%Y %H:%M

r     rN  r\  idrepostedr  	posted_bylinkedin_post_urn	apply_urlr   posted_payloadF)r  postedFailedbulk)$r[  r"   r   r   rs  r  portal_user_urnr!   r   r  r   rg  r  r   r  r   r   r  rH   r   r   r\  r  
splitlinesr  rG   rZ  r   rm   ru  r  r\   status_coder   r:  rr  )r  r   force_repostr  configr  linkedin_urn	last_postchanged_fieldsIMPORTANT_JD_FIELDSr1  previous_payloadcurrent_payloadkeyr  r  linesrr  r	  ress                        r   "post_jd_via_existing_linkedin_flowz8TblJobDescriptionView.post_jd_via_existing_linkedin_flow  s   ''(0077#	 8 

 %' 	 t$$**--#++22( 3 

(<
  	 
	$
 (77=2#L#  \\ "')'='=!~~ "$&$7$7O + C 0 4 4S 9: 3 3C 89#:25c*BN3'C ",!4((.L**$!-&88
  )INN(N4;;)#IL#${2<<.12r&789()?)?(@AB>>LL,-LL(LL12((335 -GGG$**,LL4s,-
 ??LL=(9:;'+	(45}R%5%5$678tHLLN334DEFGH{{5!|\4@??j("BKBIGG:x"8G9##**"%((*.."6# % Y  + 
  %55&&""D> 	' 	
  511r   postzpost-by-time-range)detailmethodsurl_pathc                 T   |j                   j                  d      }|j                   j                  d      }|r|st        ddit        j                        S t        |      }t        |      }|r|st        ddit        j                        S t        j                         }t        j                  |      rt        j                  ||      }t        j                  |      rt        j                  ||      }|j                  t        j                        }|j                  t        j                        }t        d|       t        d|       t        d	|       t        d
|       t        j                  j!                  dg d      j!                  t#        ||f      t#        ||f      z        j%                         }	d}
d}|	D ]@  }t&        j)                  ||j*                  d      }|j                  d      r|
dz  }
<|dz  }B t        |||	j-                         |
|d      S )Nfrom_datetimeto_datetimer  z*from_datetime and to_datetime are requiredr   zInvalid datetime formatzFROM LOCAL:z	TO LOCAL:z	FROM UTC:zTO UTC:rN  rY  Assignedr  r   jd_stage__in)created_dt__range)updated_dt__ranger   F)r  r   r  r  r   )r  r  	total_jdsr  r  )r   r   r   r   r   rg   r   get_current_timezoneis_naive
make_aware
astimezonedt_timezoneutcr  r   r   r   rY   distinctr   r  r   count)r   r   from_dt_raw	to_dt_rawfrom_dtto_dtlocal_tzfrom_dt_utc	to_dt_utcjdsr  r  r  r#  s                 r   post_by_time_rangez(TblJobDescriptionView.post_by_time_range_  s   ll&&7LL$$]3	)GH22 
 !-y)e4522 
 002W%))'8<GU#''x8E((9$$[__5	mW%k5!k;'i#''..: / 
 &i 89i 89:
 (* 	  
	B*MM\\" N F zz(#!1
	 $ 
  	r   c                    g }t        |      D ]|  }|j                  d|j                   d|dz    dd|dz    d|j                  xs d|j                  xs d|j
                  xs d|j                  xs d|j                  xs dd	       ~ |S )	Nln_rT  r   LinkedIn	Candidater  India)	linkedin_id
first_name	last_nameemailtitler   aboutskills_textexperience_text)ranger  rZ  r   rg  r   ro  r   )r   r  limitprofilesis        r   linkedin_search_candidatesz0TblJobDescriptionView.linkedin_search_candidates  s    u 	AOO!$V%9%9$:!AaC5A((1.))8["//:7++1r%44:#)#:#:#@b
 
	 r   zfetch-linkedin-candidatesc                 	   |j                   j                  d      }|j                   j                  d      }t        |j                   j                  dd            }|s|st        ddid      S |rt	        t
        |	      }nt	        t
        |
      }t        j                  j                  |j                  ddd      j                         }|st        ddid      S | j                  ||      }|st        ddid      S d}d}	|D ]d  }
|
j                  d      xs dj                         }|
j                  d      xs dj                         }|
j                  d      xs d }t        j                  j                  ||||
j                  d      xs d |
j                  d      xs d dd|j                  j                   d      \  }}dj#                  |
j                  dd      |
j                  dd      |
j                  dd      |
j                  d d      |
j                  d!d      g      j                         }t$        j                  j                  ||"      j'                         }|r|	d#z  }	qt$        j                  j)                  |||
j                  dd      |
j                  dd      |
j                  d d      ||j                  j                   $      }|d#z  }	 t+        |j,                        }|j                  d%i       }|st1        t2        j                  j                  d&d'      j5                  d(      j5                  d)      j7                  dd*            }t1        t2        j                  j                  d+d'      j5                  d(      j5                  d)      j7                  dd*            }t9        d,d-.      \  }}|j:                   d|j<                   }|j?                  |/      }|j?                  |j,                  |tA        |d0      r|jB                  nd1tE        |      2      }|D ]!  }tG        |||tH        jJ                  |d-3       # g t        |jL                  |jN                  |jP                  d||	tE        |      d4      S # t.        $ r}i }Y d }~d }~ww xY w)5Nra  rZ  r#     r  z"jd_id or jd_display_id is required  r   r  rZ  r  Tr  z?LinkedIn portal not connected. Please connect in Portal Config.)r#  zRLinkedIn did not return any candidate profiles. Please check LinkedIn integration.r  r   r  r  r  r  mobile_numbercountry_coder   )r  r  r+  coutry_codesourcecandidate_statusr   r  defaultsr-  r  r   r  r   r!  )candidate_idparsed_textr   )r2  r  r  r   primary_skillsr3  r   matchesDelivery Manager (DM)groups__namer  email__isnullemail__exactflatImplementer Super Admincandidate_jd_match_notificationFis_htmlcandidate_name
created_dtN/A	resume_idrD  upload_datematch_count	cc_emailsrB  )ra  rZ  jd_titler  saved
duplicatestotal_fetched))r   r   r   r   r_   r   r"   r   r   r[  rs  r&  r  r#   get_or_creater   r   r  r$   existsr   run_matching_engine_candidaterH  r  listr   r  values_listrU   r  r  formathasattrrE  r   rP   r   EMAIL_HOST_USERra  rZ  r   )r   r   ra  rZ  r#  r  r  r$  rN  rO  pr  r  email_valuecandcreatedresume_textalready
resume_objmatch_responser5  r  manager_sending_emailssuper_admin_cc_emailssubjectbodyrD  to_mails                               r   fetch_linkedin_candidatesz/TblJobDescriptionView.fetch_linkedin_candidates  s     )((9GLL$$Wb12]?@ 
 &'8FF&'8VF(0077--	 8 

 %' 	 \] 
 2262Gop 
 
 \	A%%-3::<J{+1r88:I55>1TK/77EE!",!*%&UU?%;%Ct#$55#8#@D((0"),,"7"7 F MD' ((gr"j"%gr"mR(',$  uw  )0077!' 8  fh 
 a
+33::!!eeGR(z2. uu]B7'"<<00 ; J QJE!>z?S?S!T(,,Y; )-LLV)@DVQW4W0W"W- [t[4*& )-LLV)BdVSW4W0W"W- [t[4)% !33T^c d$(OO#4Adnn5E!F!...G{{(22#19@\9Z
 5 5`e #G	 #   6 G# 00"7 %i\	| \\#11(( $ ]
  	W  s   +'R,,	S5R==Sr   )F)r(  )#r   r   r   r   r   annotaterX   r   r   r1   r   r   ri   r	   r   r   r   r   r  rw  r   r'  r3  dictr>  rI  r   r   r  r  rd   r  r&  rf  r   r   s   @r   r   r   )  s    !!	u-='>	?	(	 
 3)* /:>N%N22# 2s 2s 2.## #$ #q
fd
LO8B2H 56(5IJ@ K@D  56(5PQN RNr   r   c                       e Zd ZefZd Zd ZdedefdZ	d Z
g fdZdedefd	Zd
edefdZdedefdZd
ededefdZd ZdededefdZdedefdZd Zd Zy)MailReaderGooglec                     |syt        |      }d}|D ]5  \  }}t        |t              r||j                  |xs dd      z  }1||z  }7 |S )Nr  utf-8ignorer   )r?   
isinstancebytesdecode)r   rH  decodedr#  r	  encodings         r   decode_mime_wordsz"MailReaderGoogle.decode_mime_wordsE  s`    &% 	ND($&$++h&9'(+KK$		
 r   c                     t        j                  dd|t         j                        }t        j                  dd|      }t        |      }t        j                  dd|      j	                         }|S )Nz<(script|style).*?>.*?</\1>r  r6  z<[^>]+>rD  r-  )r  r  SrD   r  )r   htmlr	  s      r   html_to_textzMailReaderGoogle.html_to_textQ  s\    vv4b$bddK vvj"d+ ~ vvfc4(..0r   r	  r  c                     t        j                  dd|t         j                        j                         }t	        j
                  |      S r5  r8  r<  s      r   r>  z&MailReaderGoogle.extract_json_from_llm`  r?  r   c                 f   g }|j                  d      s|j                  d       |j                  d      s|j                  d       |j                  d      s|j                  d       |j                  d      s|j                  d       |s|j                  d	       |s|j                  d
       |S r   r  r   r  r  r  r   s        r   r  z!MailReaderGoogle.validate_jd_dataf  s     {{;'MM-.{{=)MM/0{{-.MM45{{01MM78 MMCD MMCDr   c                     	 t        ||t        j                  ||      } |j                          y# t        $ r}t        j                          Y d }~yd }~ww xY wNrc  rd  
from_emailtoccTFrV   r   rX  sendr  	traceback	print_excr   rc  r   sending_mailscc_mailsr  r  s          r   send_notify_mailz!MailReaderGoogle.send_notify_mail  U    	 #33 E EJJL 	!	   /2 	AAAtxtc                     t        j                  dd|j                  dd      j                         j	                               S NrD  r-  rT  r  r  r2  r  rF  )r   r  s     r   normalize_textzMailReaderGoogle.normalize_text  s9    vvKKS!'')//1
 	
r   rc  c                 N    g d}| j                  |      }|sy|D ]  }||v s y y)N)zjob descriptionr  zjob requirementzjob openingzjob opportunityhiringrequirementzposition openvacancyzcareer opportunityzopening forzlooking forFT)r  )r   rc  JD_KEYWORDSkeywords       r   
is_jd_mailzMailReaderGoogle.is_jd_mail  s@    
 %%g." 	G'!	 r   filenamec                 f    |syg d}|j                         }|D ]  }|j                  |      s y y)NF).pdf.doc.docxT)rF  endswith)r   r  RESUME_EXTENSIONSexts       r   is_resume_attachmentz%MailReaderGoogle.is_resume_attachment  s?    5>>#$ 	C  %	 r   attachmentsc                     g d}sy| j                        t        fd|D              }|sy|sy|D ]A  }t        |t              r|j	                  d      n
t        |      }| j                  |      sA y y)N)resumecvzcurriculum vitaezcandidate profilezprofile forzapplication forzjob applicationzapplying forz
my profilezmy cvz	my resumezsubmission forzprofile attachedzresume attachedzcandidate detailszcandidate cvzcandidate resumeFc              3   &   K   | ]  }|v  
 y wr   r   )r  r  rc  s     r   r  z2MailReaderGoogle.is_resume_mail.<locals>.<genexpr>  s     N7Gw.N   r  T)r  anyro  rh  r   rw  r  )r   rc  r  RESUME_KEYWORDSsubject_match
attachmentr  s    `     r   is_resume_mailzMailReaderGoogle.is_resume_mail  s    
( %%g.NoNN% 	J5?
D5Qz~~j1WZ[eWfH((2		 r   c                     |sy|j                         j                         }|j                  dd      j                  dd      }t        j                  dd|      }|S rA  rE  rG  s     r   rI  z"MailReaderGoogle.normalize_db_text  rJ  r   r
  c                 <   |sd|z  S t        j                  dd|      j                         j                         }|j	                         }dj                  d |D              }t        |      |k  r"|r |d   }|t        |      z
  }|dd|z    }||z  }|d|z  z   d | S )Nr  r  r  c              3   &   K   | ]	  }|d      ywr  r   r  s     r   r  z/MailReaderGoogle.format_code.<locals>.<genexpr>  r  r  r  r   r  	r   r	  r
  r!  r"  r#  r$  r%  r&  s	            r   r'  zMailReaderGoogle.format_code  r(  r   headerc                     t        j                  dd|j                  dd      j                         j	                               S r  r  )r   r  s     r   normalize_headerz!MailReaderGoogle.normalize_header	  s9    vvNN3$**,224
 	
r   c           
      6   |j                  di       }|j                  di       }|j                  dg       }|j                  dg       }|j                  dg       }|j                  dg       }g }|rt        |      dkD  r|}g }	|rt        |      dkD  r|}	g }
|rt        |      dkD  r|}
g }|rt        |      dkD  r|}i d|j                  dd	      d
|j                  d
d	      d|j                  dd	      d|j                  dd	      ddd|j                  dd	      d|j                  dd	      d|j                  dd	      d|d|j                  dd	      d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d|	d|
||j                  dd	      |j                  dd	      |j                  dd	      |j                  dd	      |j                  dd	      dS zm
        Maps the Complex Nested JSON from the Prompt to the Flat DB Structure in TblCandidateResume
        personal_informationskillswork_experience	educationcertificationsprojectsr   r  r  r  r  mobiler,  Not Specifiedr   	total_expsalarycompanyr  r4  technical_skillstools_and_frameworkssoft_skillsdomain_skillsresponsibility_summaryagegendernotice_periodemployment_type)r  r  r  r  r  r  r   r   r   r   personalr  work_expr  r  r  company_detailseducation_detailscertification_detailsprojects_detailss               r   flatten_data_for_dbz$MailReaderGoogle.flatten_data_for_db      882B7(B'88-r2HH["-	"2B788J+H)&OY!+ ) "c.1A5$2!H)'
(,,|R8
k26
 X\\'2.
 hll8R0	

 O
 Z4
 +r2
 dhhx,
 
 TXXgr*
 dhh'7<
 

+=r B
 #FJJ/Er$J
 6::mR8
 TXXor:
  *!
" 3#
$ )&*hh/G&L<<r*ll8R0%\\/2>'||,=rB/
 	
r   c                 `9   	 t               }ddd}t        j                  t        j                  d       t        j
                  j                  t        j                  d      }t        j                  |d       t        j
                  j                  |d      }t        j                  |d       t        j
                  j                  t        j                  d      }t        j                  |d       t        j                  j                         }|j                  D ]s  }|d	   st        |d
    d       d}	|d
   }
|d	   }t        j                  |	      }|j                  |
|       |j                  d       |j!                  d      }|rt#        j$                  |      }n/t'        j(                         t+        t        j,                        z
  }|j/                  d      }|j1                  d d| d      \  }}|dk7  rt        d        y |d   j3                         }|}|}|D ]  }|j5                  |d      \  }}|dk7  r|d   d   }t7        j8                  |      }|j!                  d      }| j;                  |j!                  d            }| j;                  |j!                  d            }| j;                  |j!                  d            }|j!                  d      }t=        |      }|r*t'        j>                  |      rt'        j@                  |      }||k  r||kD  r|}t        d       t        d|       t        d|       t        d |       t        d!|       t        d"|       g }|jC                         D ]f  } tE        | j!                  d#      xs d$      }!d%|!jG                         v s4| j;                  | jI                               }"|"sV|jK                  |"       h t        d|       tM        |      \  }#}$d }%d }&|jC                         D ]  } | jO                         }'tE        | j!                  d#            }!|'d&k(  r'd%|!vr#| jQ                  d'      jS                  d()      }%Y|'d*k(  s_d%|!vsd| jQ                  d'      jS                  d()      }& |%s|&r| jU                  |&      }%t        d+|%       d }(|rtW        j0                  d,|      }(|(s|%rtW        j0                  d-|%      }(|(rT|(jY                  d      })t        d.|)       d/}*g d0}+|%r|%jG                         },|+D ]
  }-|-|,v sd}* n t[        |)|%|*1       %| j]                  |      }.| j_                  ||      }/|.s|/st        d2       YtM        |      \  }#}$d }%d }&g }0g }1|jC                         D ]  } | jO                         }'tE        | j!                  d#            }!|'d&k(  r'd%|!vr#| jQ                  d'      jS                  d()      }%Z|'d*k(  r'd%|!vr#| jQ                  d'      jS                  d()      }&d%|!v s| j;                  | jI                               }"|"st        j
                  j                  ||"      }2ta        |2d3      5 }3|3jc                  | jQ                  d'             d d d        |0jK                  |2       |"jG                         je                  d4      rCt        d5|" d6       	 tg        jh                  |2      }4|4rd7|4z   }5|1jK                  d8|5gd9       v|"jG                         je                  d;      rCt        d<|" d6       	 tg        jl                  |2      }7|7rd7|7z   }5|1jK                  d=|5gd9       |"jG                         je                  d@      st        j
                  jo                  |"      d   jG                         }8tg        jp                  |2|8      }9dAdBg}:g };|9D ](  ts        fdC|:D              s|;jK                         * |1jK                  dD|;d9        |1D ]  }<|%r|%ju                         }=n|&r| jU                  |&      }=nd$}=|<dE   r"|<dF   dDk(  s|=dGdHj                  |<dE         z   z  }=t        dI|=       t        dJ|0       |jw                  |dK   |dL          |.	rty        jz                  |=      }>	 g }?|<dF   dDk(  sS|j}                  |>      }@t        |@j~                         | j                  |@j~                        }A|?jK                  |A       n|<dE   D ]  }|?jK                  |        |?D 	]  }A|Aj!                  dA      rAj!                  dA      dMk(  rdN}BdO|$ dP| dQ}Ct        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }E| j                  |B|C|D|E       	 t=        |      j                         }FFs"t'        j(                         j                         }Fh dX}Gt        j                  j                  |$Y      j                         }H|HsS|$j3                  dZ      d[   jG                         }It        j                  j                  dZ|Iz   \      j                         }Hg }JHr\t        j                  j                  Hd]d^g_      }K| j                  Aj!                  dA            }L| j                  |Aj!                  d`            }M| j                  |Aj!                  da            }N| j                  |Aj!                  db            }O|KD ]  }P| j                  |Pj                        }Q| j                  |Pj                        }R| j                  |Pj                        }S| j                  |Pj                        }T|QLk(  suRMk(  s{SNk(  sTOk(  sJjK                  P        Jrt        dct        J       dd       ns|$j3                  dZ      d[   jG                         }I|IGv rt        de       df}Bdg|$ dP| dh}Ut        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }E| j                  |B|U|D|E       |j                          t        didjidkl      c c c c S Ij3                  dm      d   j                         }Vt        j                  j                  |V|$dndo      }H| j                  AFH      }Wt        |W      }X|Wrdpj                  W      nd }YJrd^}ZnXrdq}Znd]}Zd }[|0r:t        j
                  j                  |0d         }\dr|\ }]t        j                  |]z   }[|j                  Aj!                  dB            }^t        j                  j                  di dsHdt|duFdA|Aj!                  dA      dv|Aj!                  dv      da|Aj!                  da      dw|Aj!                  dx      db|Aj!                  db      dy|Aj!                  dy      dz|Aj!                  dz      d{|Aj!                  d{      dB|Aj!                  dB      d||Aj!                  d|      d}|Aj!                  d}      d~|Aj!                  d~      d`|Aj!                  d`      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      d|Aj!                  d      diZdXdYd[d|^^nd dAj!                  d      }_t        |_       |_j                   d}`t        j
                  j                  ||`      }ata        |add      5 }b|bjc                  |=       d d d        dr` }ct        j                  |cz   }d|d__[        |_j                  dg       Jsg }eg }fJD ]H  }PejK                  d|Pj                   d|Pj                          fjK                  |Pj                         J f__]        |_j                  dg       dHj                  e      }gd}hd|_j                   d|_j                   d|g d}it        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }E| j                  |h|i|D|E       	 
(|/s
,ty        j                  |=      }j	 |<dF   dDk(  s|j}                  j      }kt        |kj~                         | j                  |kj~                        }l| j                  |l      }mg }nd }o |o|mj!                  d            rnjK                  d        omj!                  d            rnjK                  d        omj!                  d
            rnjK                  d        omj!                  d            rnjK                  d        omj!                  d            rnjK                  d       nrdj                  n      }pd}qd|p d|$ dP| d}rt        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }E| j                  |q|r|D|E       Wd }s|0r:t        j
                  j                  |0d         }td|t }ut        j                  |uz   }st        j                         5  md
   ju                         jG                         }vt        j                  j                  |v      j                         }w|wsat        j                  j                  md   |md   |md
   |md   |md   rmd   nd$md   |md   |md   |md   dn|j                  j                        }wnmd   w_e        |md   |w_f        |md   |w_g        |md   xs d$w_h        md   |w_i        |md   |w_j        |md   |w_k        |md   |w_l        |j                  j                  |w_m        |wj                          dj                  d |<dE   D              }xt        j                  j                  w      }yd }zmd   rCyj                         r3t        md         }{yD ]   }|t        ||j                        }}|}{k(  s|}z n |j                  mj!                  d            }~zrJd}d|$ dP| d}t        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }E| j                  |||D|E       mj!                  d      z_r        |mj!                  d      |z_s        |mj!                  d      |z_t        |mj!                  d      |z_u        |mj!                  d      |z_v        |mj!                  d      |z_w        |mj!                  d      |z_x        |mj!                  d      |z_y        |mj!                  d«      |z_z        |mj!                  dë      |z_{        |mj!                  dī      |z_|        |mj!                  dū      |z_}        |mj!                  di      |z_~        |mj!                  d      |z_        ~r~nd z_        |j                  j                  |z_m        d|z_        |zj                          |z}nt        j                  j                  wmd   |md   |md   |md   |md   |md   |md   |md   |md   |md   |md   |md   |md   sx|md   ~r~nd d|j                  j                  dʬ˫      }	 t        j                        }|j!                  di       }rat        t        j                  j                  dRdS      j                  dT      j                  d$U      j                  d
dV            }Dt        t        j                  j                  dWdS      j                  dT      j                  d$U      j                  d
dV            }Et	        dd/Ϋ      \  }}wj                   d|wj                   }|j                  |ϫ      }|j                  j                  |t        |dЫ      rj                  ndt              ҫ      }DD ]#  }t        ||t        j                  Ed/ӫ       % d d d          ||kD  rjt        |j                  xs g       }d/}|D ].  }|j!                  d
      |
k(  s|j                         d<   d} n r|_
        |j                  dg       |j                          v t        dididkl      S # 1 sw Y   xY w# tj        $ r}6t        d:|6        Y d }6~6%d }6~6ww xY w# tj        $ r}6t        d>|" d?|6        Y d }6~6Md }6~6ww xY w# tj        $ r d }FY w xY w# 1 sw Y   axY w# tj        $ r}6tk        d|6      d }6~6ww xY w# tj        $ r}6i }Y d }6~6d }6~6ww xY w# 1 sw Y   PxY w# tj        $ r}6tk        d|6      d }6~6ww xY w# tj        $ r#}6t        dtE        |6      dלdجl      cY d }6~6S d }6~6ww xY w)Nrj  rk  rl  Texist_okJD_filesmail_attachmentsResumesemail_app_passwordr  z. doesn't have app password to read the mailboxzimap.gmail.comINBOX	last_readdaysz%d-%b-%Yz(SINCE "z")OKzFailed to search emailsr   z(RFC822)r   z
Message-IDSubjectFromToDatez 
-------------------------------zMessage-ID:zSubject:zFrom:zTo:zDate:zContent-Dispositionr  r  z
text/plain)rq  rm  rn  z	text/htmlzBody:zRef:(\d+_\d+)zReference ID:\s*(\d+_\d+)u   🎯 Shortlist reply detected:F)yeszyes i am interestedzyes, i am interested
interestedzi am interestedzi'm interested)r  zBSkipping this mail not belongs to JD mail or Candidate Resume Mailwbr  z
--- Extracting text from PDF: z ---
--- Attachment Text ---
pdf)	file_typer	  zFailed to extract PDF text: r  r  z)
--- Extracting text from Word Document: docxzWord extraction failed for : z.xlsxz.xlsz.csvr   r   c              3   @   K   | ]  }j                  |        y wr   r   )r  frows     r   r  z'MailReaderGoogle.get.<locals>.<genexpr>2  s     2W!3771:2W   xlsr	  r  r  r+  z
Text Body:zAttachments:rm  rn  r  zCAuto Recruiter: Job Description Received - Job Title Not IdentifiedzHello Admin,

We received a Job Description requirement email, but the Job Title was not specified in the content.

Received mail from: z
Received on: z

Please review this and either identify the job title or manually add the JD through the application.

Best regards,
The Auto Recruiter Support Teamr6  r7  r9  r;  r=  r?  >
   aol.comlive.com	gmail.com	yahoo.com
icloud.comhotmail.comoutlook.comyahoo.co.inprotonmail.comrediffmail.com)client_contact_email@r  )client_contact_email__iendswithrN  r  r  rg  r   r  Found  duplicate JD(s)z.Sender Domain belongs to Public E-mail Domainsz@Auto Recruiter: Job Description Received - Client Not IdentifiedzHello Admin,

We received a Job Description requirement from a public domain email, but could not determine the associated client.

Received mail from: z

Please review this and either identify the client or manually add the JD through the application.

Best regards,
The Auto Recruiter Support Teamr   z)Got Public Mail Domain, Notified to Adminr  r   rC  r   )client_namer  r   rU  rL  rM  zJD_files/mail_attachments/r   r[  r  ai_titleclient_job_idjob_iddurationrb  r   rc  rd  re  rh  ri  rf  working_hoursbenefitsrequirement_priorityr  ro  no_of_open_positionsr^  r_  	jd_sourcer]  changes	_file.txtwrl  rs  jd_source_txtrV  	- JD ID: , Job Title: r  z4Auto Recruiter: Duplicate Job Description IdentifiedzHello Admin,

A new Job Description has been created, and one or more identical Job Description entries were identified in the system.

Newly Created Job Description ID: z
Job Title: z%

Identical Job Description Details:
zi

Please review these entries and take appropriate action.

Best regards,
The Auto Recruiter Support Team(An Error Occurred during Gemini Responsec                 R    |  xs# t        |       j                         dk(  xs | dk(  S )Nr  r  r  )rH  s    r   
is_missingz(MailReaderGoogle.get.<locals>.is_missingf  s.    38y/pCJ<L<L<NRT<T/pX]apXp(pr   r  z
First Namer  zMobile NumberzE-Mailr  z	Job Titler4  Primary Skills, z9Auto Recruiter: Missing Candidate Details in Resume EmailzrHello Admin,

We received a candidate resume via email, but some mandatory details are missing. 

Missing fields: z

Received mail from: z

Please review the email content or attachment and update the missing details manually in the application before proceeding.

Best regards,
The Auto Recruiter Support TeamResumes/email__iexactr  r,  r  r  r  r  r  r  r  r+  r-  r  r  r  r  r/  r   r-  c              3   \   K   | ]$  }|j                  d d      j                          & yw)r  r  N)r2  r  )r  r	  s     r   r  z'MailReaderGoogle.get.<locals>.<genexpr>  s0      F*04 -1LL9VXZ,[,a,a,cF*s   *,r2  r  z2Auto Recruiter: Existing Resume Updated from Emaila2  Hello Admin,

A candidate resume received via email matches an existing resume record in the system based on the resume title. 

The system has updated the existing candidate data using the information from the received email and attachment. 
No new candidate record has been created. 
Received mail from: z

Please review the updated candidate information in the application if required. 
Best regards,
The Auto Recruiter Support Teamr  r  r  r  r  r  r  r  r  resume_file_urlattachment_content)zOur Internal DBr   r  Internal DBParsed)r2  r  totalexpr  r  r   r4  r  r  r  r  r  r  r  resume_filer3  r  )resume_responsibilities_summary_embeddingresume_sourcer   r   r5  r@  rA  rC  rE  rF  rG  rK  email_detailsr  r  r  r  r   )rz  osmakedirsr   
MEDIA_ROOTpathr  r   r   rs  r&  r  imaplib	IMAP4_SSLloginselectr   rB   fromisoformatr   rm   rC   MAIL_LAST_READ_DAYS_INITIALru  searchr  fetchr  message_from_bytesrt  rA   r  r  walkrw  rF  get_filenamer  r@   get_content_typeget_payloadrq  rx  r  grouprR   r  r  openwriter  rO   process_pdfr  process_wordsplitextprocess_excel_or_csvr   r  r}  rI   jd_extraction_promptr~  r	  r>  rT  r   r   r  rU  r  rq  r   r   rI  r   rg  r   r  r   logoutr   r  r   r  rr  basename	MEDIA_URLr{  rL   rZ  r  r   r  resume_extraction_promptr  r   rt  r#   r   r   r  r  r+  r-  r  r  r  r  r   r$   rR  rJ   r  r"  r  r  r4  r  r  r  r  r  r  r#  r3  r   r  r$  r%  rS  rH  rU   rV  rW  rE  rP   rX  	isoformat)r   r   r  r  jd_attachments_dirjd_mail_attachmentsresume_base_dirr  mail_detail	imap_host
email_useremail_passwordmaillast_read_strlast_read_dtsince_date_strr   r   	all_mails	email_idslatest_processed_dteidmsg_data	raw_emailmsg
message_idrc  senderreceiver	mail_datemail_date_timeattachments_metapartcontent_dispositionr  sender_namesender_email	body_text	body_htmlcontent_typetracking_matchtracking_tokenr  interest_keywords
body_lowerr  r  r  r  attachment_textsfilepathr  pdf_texttext_attachmentr  doc_text	extensioncontentrequired_fieldsnormalized_rowsattachment_textfinal_body_textjd_generate_promptmultiple_jd_datar  r  public_domain_subjectjob_title_missing_messagera  rb  r  PUBLIC_EMAIL_DOMAINSr  sender_domainr  r  r  r  r  r  r  r  r  r  r  public_domain_messagebase_domainr  r^  r_  r  jd_file_urljd_file_namejd_relative_pathr  r  generated_jd_file_namegenerated_file_pathfilegenerated_jd_relative_pathgenerated_jd_file_urljd_details_linesduplicate_jd_idjd_details_strduplicate_mail_subjectduplicate_mail_messageresume_exract_promptresume_llm_responsresume_dataresume_db_datamissing_fieldsr  missing_list_strcandidate_mandatory_subjectcandidate_mandatory_messager  resume_file_nameresume_relative_pathemail_idprofiler  existing_resumesmatched_resumenormalize_title_textr  existing_title!resume_responsibilities_embeddingcandidate_edit_subjectcandidate_edit_messager_  r`  r5  rd  rD  re  r&  updateditemr  s                                                                                                                                             @r   r   zMailReaderGoogle.getE  s~   k	%KM"#(!
 KK++d;!#h.A.A:!NKK*T:"$'',,/ACU"VKK+d; ggll8+>+>	JOKK$7 #**002   /<< G"#78[122`ab,	 )1
!,-A!B((3

:~6G$ + < #+#9#9-#HL#+<<>I8CgCg4h#hL!-!6!6z!B  ${{48N;K21NOT>34 GMMO	%	 '3#$ L_C'+zz#z'B$FH~  (AI229=C!$!6J"44SWWY5GHG!33CGGFODF#55cggdmDH #I%:9%EN%(*;*;N*K)1)<)<^)L%5 %(;;.<+=>-4*g.'6*%*'9-')$ #
 B.1$((;P2Q2WUW.X+'+>+D+D+FF'+'='=d>O>O>Q'RH' 0 7 7 AB *g.09&0A-K $I $I #
 ^'+'<'<'>.1$((;P2Q.R+'<7LPc<c(,(8(8(8(E(L(LT\(L(]I)[8\Qd=d(,(8(8(8(E(L(LT\(L(]I^ %$($5$5i$@	'9-%)N)+3CW)M)i)+3OQZ)[%)7)=)=a)@>O%*
-) %)2):J+< *#*j#815J$)*
 0*%'1 !!%!9J%)%8%8BR%SN%nbc 09&0A-K $I $I"$K&($ #
 E&'+'<'<'>.1$((;P2Q.R+ (<7LPc<c(,(8(8(8(E(L(LT\(L(]I *[8\Qd=d(,(8(8(8(E(L(LT\(L(]I *-@@'+'='=d>O>O>Q'RH'+-77<<8KX+V%)(D%9 !KQ$%GGD,<,<D,<,I$J!K !, 2 28 <#+>>#3#<#<V#D$),LXJVZ*[$\%R3I3U3UV^3_+3A\`h?hO,<,C,CAF=L<M12-. &.^^%5%>%>?P%Q$),VW_V``d*e$f%]3I3V3VW_3`+3A\`h?hO,<,C,CAG=L<M12-. &.^^%5%>%>?X%Y020@0@0J10M0S0S0UI.D.Y.YZbdm.nG7BDV6WO68O/6 %D/22W2W/W,4(7(>(>s(C	%D %5$;$;9>4C)*%&AE&N ,< T_$.7oo.?O&.2.?.?	.JO.0O*62#2;#?5#H /6DIIoV\F]<^3^ ^ no>nk:%11-m<>OPa>b &1;1P1PQ`1a.F_35 0'6{'Cu'L6C6P6PQc6dO$)/*>*>$?.2.H.HI]I].^G$4$;$;G$D0?0G %F(8(?(?(E%F 0@ x!]G+2;;{+Cw{{S^G_crGr AF(=-CCO. Q<<E; GN-N )B BF,0LL-3VAXdhV-i-4W4W-H-4W"W-E-8[t[-LB*(> AE,0LL-3VAZfjV-k-4W4W-H-4W"W-E-8[t[-LA*(= )-(=(=>SUn  qG  I^  )_(0%72G	2R2W2W2Y ,32:,,.2E2E2G<&$8 3<2C2C2J2J`l2J2m2s2s2uK+68D8J8J38OPR8S8Y8Y8[6?6G6G6N6NLOR_L_ 7O 7**/%' )4 57M'27H7P7P7W7W6A8>7L 8X 8*
 >B=S=ST[T_T_`kTl=m(:<@<R<RSZS^S^_mSn<o(9<@<R<RSZS^S^_iSj<k(97;7M7MgkkZoNp7q2> )IB;?;Q;QRTR^R^;_L:>:P:PQSQ`Q`:aK:>:P:PQSQ\Q\:]K595K5KBLbLb5cF 1=@R0R4?CT4T4?CT4T4:l4J0=0D0DR0H)I ,9,1F3};M:NN^2_,`8D8J8J38OPR8S8Y8Y8[+8<P+P,12b,c EG,A1G HTn U@@I{ KR	1R -B FJ0417E\hl1m18t1L18b1I1<W41PF.,B EI0417E^jn1o18t1L18b1I1<W41PE.,A -1,A,ABWYn  qG  I^  -_,0KKM3;4<>i12 8;	4. -. -. 7D6I6I#6Nq6Q6W6W6Y6?6G6G6N6N9DAM3;DE	 7O 7* 9=8M8MgW^`k8l$5045F0GIQb;L1MhlJ'47B)27>7=26K'279ww7G7GTU7W=WXdWe;f(86>6H6HK[6[ANAbAbcjcncn  pB  dC  BD$>->-F-F-M-M  .&2= .&7F .& 18 .& 3:++k2J	 .&
 29Z1H .& 29Z1H .& 7>kk(6K .& =DKKH]<^ .& 29Z1H .& 7>kk/6R .& 5<KK4N .& :AEW9X .& =DKKH]<^ .& DK;;OkCl .& IPTuHv .&  6=[[5P! .&" AHLe@f# .&$ BIMgAh% .&& 4;;;|3L' .&( 7>kk/6R) .&* 29Z1H+ .&, >E[[I_=`- .&. 6=[[5P/ .&0 8?{{CS7T1 .&2 >E[[I_=`3 .&4 0<5 .&6 3<7 .&8 4>9 .&: 5@; .&< gA  gMHb  SW= .&> 3:++i2H? .&FD %I$P@F@T@T?UU^=_$::<'',,GZ\r:s$7)-.A3QX)Y %D]a(,

?(C%D D^^t]uAv$><D<N<NQk<k$9;PF$8$*KK>OK$P'4;=(8:<2? )UB,<,C,C2;B<L<L;M][][g[gZh0i-. -<,B,B2CSCS,T	)U CR(?(.CUBV(W9=CS9TAw(>-Q RXQeQePf g::@:J:J9K LS/=.> ?N	-N )? BF,0LL-3VAXdhV-i-4W4W-H-4W"W-E-8[t[-LB*(> AE,0LL-3VAZfjV-k-4W4W-H-4W"W-E-8[t[-LA*(= )-(=(=>TVl  oE  G\  )]qx!]v ,3=3V3VWf3g0v_'6{'Cu'L9F9S9STh9i$6$)*<*A*A$B262L2LM_MdMd2eK595M5Mk5ZN57N%q (2.2D2D\2R'S(6(=(=l(K'1.2D2DX2N'O(6(=(=o(N'1.2D2DW2M'N(6(=(=h(G'1.2D2DW2M'N(6(=(=k(J'1.2D2DEU2V'W(6(=(=>N(O'5;?99^;T(8 GB(C-??O>P QCCO. Q<<E; GN	-N )D BF,0LL-3VAXdhV-i-4W4W-H-4W"W-E-8[t[-LB*(> AE,0LL-3VAZfjV-k-4W4W-H-4W"W-E-8[t[-LA*(= )-(=(=>Y[v  yO  Qf  )g(06:O'2;=77;K;KKXYN;[(8AIJZI[?\(<:B:L:LOc:c)4););)= n%63A'3J3P3P3R3X3X3Z2E2M2M2T2Tck2T2l2r2r2t/66I6Q6Q6X6X;I,;W:H:U6DW6M>LX>V^lm{^|N><Z  CE4B54I7Eh7O>L_>]@NO`@aAI;B<<;P;P 7Y 7.G BPP\A]G,>@N{@[G,=DRS[D\G,ABPQ_B`BfdfG,?:H:OGK=KH=UGNDRSbDcG,AFTUfFgG,CAHAVAVG,>,3LLN=@XX F*8G8OF* >*(:
 <N;U;U;\;\jq;\;r(89=+9'+BGWG^G^G`CYZhipZqCr,@:J -:AWX^XdXdAe3AEY3YEKN49	-: MZLmLmn|  oA  oA  BZ  o[  M\(I+9Ey,B1G HTn U@@I{ KR
1R -C FJ0417E\hl1m18t1L18b1I1<W41PF.,B EI0417E^jn1o18t1L18b1I1<W41PE.,A -1,A,ABXZp  sI  K`  -aFTFXFXYdFeN,CESEWEWXaEbN,BDRDVDVW_D`N,ALZL^L^_oLpN,IN\N`N`asNtN,KR`RdRde{R|N,OIWI[I[\iIjN,FKYK]K]^mKnN,HGUGYGYZeGfN,DLZL^L^_oLpN,IIWI[I[\mInN,FIWI[I[\pIqN,FDRDVDVW_D`N,ATbTfTfg  UAN,Q Mn  hI  txN,dHOH]H]N,EI[N,H,:,?,?,A9GJ9K9S9S9Z9Z=D6DW6M9G9T8Fy8Q7Eh7O9G
9S?MN^?_CQRdCeGUVlGm>L]>[@N@_:H:U9G
9S?MN^?_<K<NIWXpIq Bc\}  im>K;B<<;P;P7?+ :[ :.J0-=A^_i_s_sAt:H:L:LYXZ:[ 07IM48LL5;VI`lpV5q5<W4W5P5<W"W5M5@[t[5TJ20F IM48LL5;VIbnrV5s5<W4W5P5<W"W5M5@[t[5TI20E ASSt  D  AEDKDVDVCWWXY`YjYjXkAl:A..Xf.:g7;{{>H>R>RCQY`akmyYz
@U@U  AF@CG	 8C 82 @V 16G4C8?8<8?8@8P8PBW@E5616Mn%6GT_qL_\ '5$()F)F)L"$MM#G - "88G,
:0C0M0M0OD-&*G!	" 8E5',,O;L,MOGP i 	 w!K !K$ ,5 %R(-0LQC.P(Q(Q%R ,5 %](-0KH:UWXYWZ.[(\(\%]n ,5 %726%7F%D %Dj $- _&/0Z\]&^ ^_\ 4= -=:<-=Kn%6 n%6` $- _&/0Z\]&^ ^_2  	& V  	s`  HAr F'Ar =!Ar BAr ?Ar B9Ar >C9Ar 8!Ar ,Ar "An(	(AAr 01An5!0Ar 1Ao!Ar &D.Ar E9Ap!Ap(G	Ap!2Ap!8Ap!>Ap!EAp!Ar "LAp!B#ApB5<Ap!C3E+Ap!IAr I$Ar I:HAq$RAr RAAq$S!F1Aq	ZK(Aq	e<)Ap>f%E$Aq	l	Aq$l?Ar mAAr n(An2n-Ar n5	Ao	n>Ao	oAr oAo	oAr o	Ao?	o#Ao:	o4Ar o:Ao?	o?Ar pAppAp!pAppAp!pAppAp!p!	Ap;	p*Ap6	p6Ap;	p;Ar p>	AqqAqq	Aq	qAqqAq	qAq!qAq$q$	Aq>	q-Aq9	q9Aq>	q>Ar r	Ar-r
Ar(r"Ar-r(Ar-N)r   r   r   r   ri   rt  rx  rw  rh  r>  r  r  r  rr  r  r  rT  r  rI  r   r'  r  r  r   r   r   r   rj  rj  C  s    )+
## #$ #4 JL "
# 
# 
# $ 6S T (c ( ( (T2 2S 2S 2.
s 
s 
3
jlr   rj  c                   ^    e Zd ZefZdedefdZd Zd Z	g fdZ
dededefdZd	 Zd
 Zd Zy)UploadJobDescriptionFiler	  r  c                     t        j                  dd|t         j                        j                         }t	        j
                  |      S r5  r8  r<  s      r   r>  z.UploadJobDescriptionFile.extract_json_from_llmv  r?  r   c                 f   g }|j                  d      s|j                  d       |j                  d      s|j                  d       |j                  d      s|j                  d       |j                  d      s|j                  d       |s|j                  d	       |s|j                  d
       |S r   r  r{  s        r   r  z)UploadJobDescriptionFile.validate_jd_data|  r  r   c                     |sy|j                         j                         }|j                  dd      j                  dd      }t        j                  dd|      }|S rA  rE  rG  s     r   rI  z*UploadJobDescriptionFile.normalize_db_text  rJ  r   c                     	 t        ||t        j                  ||      } |j                          y# t        $ r}t        j                          Y d }~yd }~ww xY wr}  r  r  s          r   r  z)UploadJobDescriptionFile.send_notify_mail  r  r  r
  c                 <   |sd|z  S t        j                  dd|      j                         j                         }|j	                         }dj                  d |D              }t        |      |k  r"|r |d   }|t        |      z
  }|dd|z    }||z  }|d|z  z   d | S )Nr  r  r  c              3   &   K   | ]	  }|d      ywr  r   r  s     r   r  z7UploadJobDescriptionFile.format_code.<locals>.<genexpr>  r  r  r  r   r  r  s	            r   r'  z$UploadJobDescriptionFile.format_code  r(  r   c                    ddd}|j                  |d   |d          	 |j                  |      }t        |j                         | j	                  |j                        }	|	j                  d      r|	j                  d      dk(  rt        d      t        j                         j                         }
t        j                  j                  |	      }g }|rYt        j                  j                  |d
dg      }| j                  |	j                  d            }| j                  |	j                  d            }| j                  |	j                  d            }| j                  |	j                  d            }|D ]  }| j                  |j                        }| j                  |j                         }| j                  |j"                        }| j                  |j$                        }||k(  su||k(  s{||k(  s||k(  s|j'                  |        |rt        dt)        |       d       | j+                  |	|
|      }t-        |      }|rdj/                  |      nd }|rd}n|rd}nd
}t0        j                  j3                         }|j5                  |	j                  d            }t        j                  j6                  dIi d|d|d|
d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d|	j                  d      d |	j                  d       d|	j                  d      d!|	j                  d!      d"|	j                  d"      d#|	j                  d#      d$|	j                  d$      d%|	j                  d%      d&|	j                  d&      d'|	j                  d'      d(|	j                  d(      d)|	j                  d)      d*|d+|d,|d-|d.||nd d/|	j                  d/      }t9        |       |j:                   d0}t<        j>                  j/                  ||      }tA        |d1d23      5 } | jC                  |       d d d        d4| }!tD        jF                  |!z   }"|"|_$        |jK                  d5g6       |rg }#g }$|D ]H  }|#j'                  d7|j:                   d8|j                          |$j'                  |j:                         J |$|_&        |jK                  d9g6       d:j/                  |#      }%tO        d;d<=      \  }&}'|'jQ                  |j:                  |j                  |%>      }'tS        tT        j                  j                  d?d@A      jW                  d@B      jW                  dCD      jY                  dEd@F            }(tS        tT        j                  j                  dGd@A      jW                  d@B      jW                  dCD      jY                  dEd@F            })| j[                  |&|'|(|)       y y # 1 sw Y   xY w# t        $ r}*t        dH|*      d }*~*ww xY w)JNrj  rk  rl  rm  rn  r   r  z#Job Title Missing from LLM Responser   rN  r  r  rg  r   r  r   r  rL  rM  r   r   r[  r  r  r  r  r  rb  r   rc  rd  re  rh  ri  rf  r  r  r	  r  ro  r
  r   r^  r_  r  r]  r  r  r  rl  r  JD_files/uploaded_files/r  rV  r  r  r  r+  duplicate_jd_notificationFrA  	new_jd_idr   duplicate_jd_listr6  Tr7  r9  r  r;  r  r=  r?  r  r   ).r}  r~  r  r	  r>  r   r  r   rm   rq  r   r   r   r   rI  r   rg  r   r  r  r   r  rr  r  r   rs  r{  r   rL   rZ  r'  r*  r9  r:  r   rB  r  r   r  rU   rV  rT  r   r  rU  r  )+r   rs  r   r{  file_contentbase_dirr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r^  r_  r  r  r  r  r~  r  r  r  r  r  r  r  r  r  ra  rb  r  s+                                              r   jd_response_handlez+UploadJobDescriptionFile.jd_response_handle  s   $
 	!!m,.?@Q.R	
Z	K+889KLO/&&'001E1EFG;;{+w{{;/G?/Z EFFlln))+G#++//)/DKM088??) &4  @  
 &*%;%;GKK<T%U"$($:$:7;;~;V$W!$($:$:7;;z;R$S!#55gkkBW6XY& 1B#'#9#9",,#GL"&"8"8"IK"&"8"8"EK!33B4J4JKF %(::'+<<'+<<"l2%,,R01 !F3}#5"66FGH $ 5 5gw T./I9J#45PTJ*&% #**002  *7)J)J7;;WiKj)k&&..55  % .     "++k2	 
 !Z0  !Z0  &kk(3  %,KK0E$F  !Z0  &kk/:  $KK6  ")-?!@  %,KK0E$F  ,3;;7S+T  18<]0^   %[[8! " )04M(N# $ *15O)P% & #;;|4' ( &kk/:) * !Z0+ , &-[[1G%H- . %[[8/ 0  '{{+;<1 2 &-[[1G%H3 4 $5 6 $7 8 &9 : (; < OiNt0Jz~= > "++i0? FD 18(.(<(<'=Y%G""$'',,x9O"P)3A )T

<() ,DDZC[)\&$,$6$69S$S!#8F KK&7K8#% "$' =B$++#B$4$4#5]2<<.Q $**2+;+;<	= +:'+=*>?!%+;!<AS/B>&(> *@)F)F$22$..&4 *G *& *.LLV)@DVQW4W0W"W- [t[4*& )-LLV)BdVSW4W0W"W- [t[4)% %%***)	Q ) )n  	KFJJ	KsJ   GZ( 1Z( 7Z( =Z( K	Z( ZF;Z( Z% Z( (	[1Z==[c                     g }|j                         D ]>  \  }}|s	|j                  dd      j                         }|j                  | d|        @ dj	                  |      S )NrT  r-  r  r+  )itemsr2  r  r  r  )r   r   r  r  rH  labels         r   build_jd_text_from_rowz/UploadJobDescriptionFile.build_jd_text_from_rowi	  sd    **, 	2JCC-335wb01	2 yyr   c                 &  89 	 |j                   j                  d      }|j                  j                  d      }|st        dddd      S t        j
                  j                  t        j                  dd	      }t	        j                  |d
       t        |      }|j                  |j                  |      }|j                  |      }t        j
                  j                  |      d   j                         }d| }	t        j                  |	z   }
t!               }|dv r~t#        j$                  ||      }|r t'        j(                         5  ddg}g }|D ]  8t+        8fd|D              st-        j.                         j1                         }t2        j4                  j                  |      }g }|rYt6        j4                  j9                  |ddg      }| j;                  8j                  d            }| j;                  8j                  d            }| j;                  8j                  d            }| j;                  8j                  d            }|D ]  9| j;                  9j<                        }| j;                  9j>                        }| j;                  9j@                        }| j;                  9jB                        }||k(  su||k(  s{||k(  s||k(  s|jE                  9        |rtG        dtI        |       d       | jK                  8||      }tM        |      }|rdj                  |      nd }|rd}n|rd}nd}tN        j4                  jQ                         }|jS                  8j                  d            } t6        j4                  jU                  |||8j                  d      8j                  d      8j                  d      8j                  d      8j                  dd       8j                  d!      8j                  d      8j                  d"      8j                  d#      8j                  d$      8j                  d      8j                  d%      8j                  d&      8j                  d'      8j                  d(      8j                  d)      8j                  d*      8j                  d+      8j                  d,      |||| | nd |
-      }!|jE                  |!       tW        |!       |!jX                   d.}"t        j
                  j                  ||"      }#| j[                  8      }$t]        |#d/d01      5 }%|%j_                  |$       d d d        d|" }&t        j                  |&z   }'|'|!_0        |!j                  d2g3       |sLg }(g })|D ]H  9|(jE                  d49jX                   d59j<                          |)jE                  9jX                         J |)|!_1        |!j                  d6g3       d7j                  |(      }*te        d8d9:      \  }+},|,jg                  |!jX                  |!j<                  |*;      },ti        tj        j4                  j9                  d<d
=      jm                  d
>      jm                  d ?      jo                  d@d
A            }-ti        tj        j4                  j9                  dBd
=      jm                  d
>      jm                  d ?      jo                  d@d
A            }.| jq                  |+|,|-|.        g }/|D ]  99jr                  9jX                  9j<                  9j@                  9jB                  9jt                  9jv                  9jx                  9jz                  9j|                  9j~                  9j                  9j>                  9j                  9j                  dC}0|/jE                  |0        t        j                  |/      }1dDdEdF}2|j                  |2dG   |2dH          |j                  |1      }3| j                  |3j                        }4tG        |3j                         |4D ]7  9t        9fdI|D        d       }5|5s9dJ   |5_J        |5j                  dJg3       9 	 d d d        n|dKk(  rAt#        j                  |      }t        j                  |      }6| j                  |6||
|||       nU|dLv rAt#        j                  |      }t        j                  |      }6| j                  |6||
|||       nt        ddMdd      S t        dN|dOdP      S # 1 sw Y   xY w# 1 sw Y   &xY w# t        $ r#}7t        dt        |7      ddQ      cY d }7~7S d }7~7ww xY w)RNr  r   r  zNo file providedr  r)  r   r  uploaded_filesTr  r   r   r  r  r   r   c              3   @   K   | ]  }j                  |        y wr   r  )r  r  r   s     r   r  z0UploadJobDescriptionFile.post.<locals>.<genexpr>	  s     &Lqtxx{&Lr  r   rN  r  r  rg  r   r  r   r  rL  rM  rb  r  r  r   rc  rd  re  rh  ri  rf  r  r  r	  r  r
  )r   r[  r  r   r   r  rb  r  r   r   rc  rd  re  rg  rh  ri  rf  r  r  r	  r  r
  r   r^  r_  r]  r  r  r  rl  r  r  rV  r  r  r  r+  r  FrA  r  r6  r7  r9  r;  r  r=  r?  r`  rj  rk  rl  rm  rn  c              3   H   K   | ]  }|j                   d    k(  s|  yw)ra  Nr  )r  r  r  s     r   r  z0UploadJobDescriptionFile.post.<locals>.<genexpr>F
  s!      Z$

bQXk@Y Zs   ""ro  r  r  zUnsupported file typer  )r   r  r  r  )Qr   r   r   r   r'  r*  r  r   r)  r(  r   r   namer=  rF  rB  rz  rO   r>  r   rt  r   r   rm   rq  r   r   r   r   rI  r   rg  r   r  r  r  r   r  rr  r   rs  r{  r   rL   rZ  r  r9  r:  r  r  rU   rV  rT  r   r  rU  r  ra  rb  r   r   rc  rd  re  rf  rh  ri  rI   r|  r}  r~  r>  r	  nextro  r;  r?  r  r<  r  rw  ):r   r   uploaded_filer   r  fsr  	file_pathrm  r}  r{  r  rn  ro  
created_jdr  r  r  r  r  r  r  r  r  r  r  r  r  r^  r_  r  r  r  r  r~  r  r  r  r  r  r  r  r  r  r  ra  rb  llm_input_datar  r|  r  r  r  found_jdrs  r  r   r  s:                                                           @@r   r  zUploadJobDescriptionFile.postq	  sY	   {	#MM--f5M((5I '2DE 
 ww||H$7$7EUVHKK40"H5Bww}11=AH)I((215;;=I!9(D",,/??K%KM550EEiQZ[$++- @P+68J*K%'
$+ Q"D#&&LO&L#L (&.lln&9&9&;G*3*;*;*?*?)*?*TK,.M*/@/H/H/O/O.906/D 0P 0"
 6:5K5KDHHU`La5b 2484J4J488TbKc4d 1484J4J488T^K_4` 1/3/E/EdhhOdFe/f*6 !AB373I3I",,3WL262H2H2YK262H2H2UK-1-C-CBDZDZ-[F )58J(J,7;L,L,7;L,L,2l,B(5(<(<R(@!A $1$)F3}3E2FFV*W$X040E0EdGU`0a-(,->(?IIZ3D)E`dJ,/:!*/6/5 !3 : : @ @ B , :G9Z9Z[_[c[cdv[w9x6%6%>%>%E%E*5/>(/*.((;*?)-*)=48HH=R4S.2hh.G.2hhx.D,0HH],C15:L1M48HH=R4S;?88D`;a@DIj@k-1XXn-E8<AZ8[9=B\9]+/88L+A.2hh.G)-*)=59XX>T5U-1XXn-E59XX>T5U'3*3+5^x  _E@Z  KO,77 &F &F: '--f5@H8>8L8L7MY5W224'',,xI_2`/+/+F+Ft+LL!%&93!Q 9UY $

< 89 <TTjSk9l64<4F4FIc4c13HF0"KK6GKH,35 024*7 !MB$4$;$;*3B4D4D3E]SUS_S_R`(a%& %4$:$:2;K;K$L	!M ;J 7 &;M:N O15;K1LQc$?R" N 68N :P9V9V.4.B.B.4.>.>6D :W :" 6 :>$(LL%+V9P\`V%a%,W4W%@%,W"W%=%0[t[%D:" 6 9=$(LL%+V9R^bV%c%,W4W%@%,W"W%=%0[t[%D9" 5 !% 5 5$:$:$:$9	!"YQ"f *,", DB)+131A1A-/\\,.KK797M7M131A1A/1~~464G4G797M7M>@>[>[CECeCe.0mm02;=;U;U<><W<W0," +112BC%D( 4>3V3VWe3f0+./4-) &11-m<>OPa>b +8*D*DE]*^&*&@&@AUAU&Vo223"- PB'+ Z* Z $(H
  (:<=M:N 7 (=M<N OPq@P @PF f$0<<YG%/%D%DW%M"''(:I{T[]egtu//0==iH%/%D%DW%M"''(:I{T[]egtu  '2IJ 
 ' (  k9 9}@P @Pt  	& V  	s   Ae$ De$ Feeee$I0ee&<e$Ke7eB#e$ ;e$ eee!e$ $	f-fffN)r   r   r   r   ri   rw  rh  r>  r  rI  r  r   r'  r  r  r  r   r   r   r  r  s  sa    )+## #$ #2 JL "2 2S 2S 2.bKH |r   r  c                   ~    e Zd Zej                  j                         j                  d      ZeZ	e
gZeeefZd Zd Zy)ImpDepartmentViewSetr{   c                 @   | j                  |j                        }|j                         rN|j                  |j                  j
                         t        d|j                  dt        j                        S t        |j                  t        j                        S r   r   r   s        r   r   zImpDepartmentViewSet.createu
  r   r   c                 d   | j                         }| j                  ||j                  d      }|j                         rN|j	                  |j
                  j                         t        d|j                  dt        j                        S t        |j                  t        j                        S r   r   r   s         r   r   zImpDepartmentViewSet.update
  r   r   N)r   r   r   r   r   r   r   r   r2   r   r   ri   r	   r   r   r   r   r   r   r   r   r  r  o
  sF    ''++-66GH1)* /:>N
(
r   r  c                   $    e Zd ZegZd Zd Zd Zy)AssignJDViewc           	      X   |j                   j                  dg       }|j                   j                  dg       }|r|st        ddit        j                        S t        |t              st        ddit        j                        S g }d}d}t        j                         5  |D ]  }t        j                  j                         j                  |      }|D ]|  }	t        j                  j                  ||		      j                         }
|
r|d
z  }:t        j                  j                  |	||j                   d      }|j#                  |       |d
z  }~ d|_        d|_        |j'                  ddg        	 d d d        |rt)        |       t        ||dd      S # 1 sw Y   &xY w)Nr  
recruitersr  zJDs and recruiters requiredr   zjds must be a listr   r  )r  user_idr   r  )r  r  r   r   rN  r\  r   rV  )total_createdtotal_skippedr  )r   r   r   r   r   ro  rT  r   rt  r   r   rv  r   r   rR  r   r   r  r\  r   rQ   )r   r   r   r  new_assignmentsr  r  ra  r  recruiter_idrR  
assignments               r   r  zAssignJDView.post
  s   !!%,\\%%lB7
Z8922 
 &$'/022 
 ! 	B B*22DDFJJQVJW$. 'L%--44! , 5  fh 
 %* !)!1!1!8!8 ,!%//)	 "9 "J $**:6!Q&M''* #- &:x*@A5B	B< )/:!.!. 
 	
C	B 	Bs   !CF  F)c                    |j                   j                  d      }|st        ddid      S t        t        |      }t
        j                  j                  |      j                  d      j                  d	      }g }|D ]M  }|j                  |j                  |j                  |j                  r|j                  j                  nd
d       O t        |j                  |j                   |dd      S )Nra  r  jd_id is requiredr)  r   r  r  r   -idr  )r  r  recruiter_name)ra  rZ  assigned_recruitersr  )r   r   r   r_   r   r   r   r   select_relatedr   r  r  r  r   r   ra  rZ  )r   r   ra  r  assignmentsr#  as          r   r   zAssignJDView.get
  s    $$((1X':;CHH"#4EB VvV^F#Xe_	 	  	AMMdd !		56VV!&&// 	 \\#11#)
 	 	r   c           	         |j                   j                  d      }|j                   j                  dg       }|st        ddid      S t        |t              st        ddid      S t        t        |      }t        t        j                  j                  |	      j                  d
d            }t        |      }g }||z
  }|D ]  }		 t        j                  j                  |	      }
|
j                  s1t        dd      \  }}|j                  |j                         }|j                  |
j#                         xs |
j$                  |j                         }t'        ||j)                         |
j                  g t*        j,                  d        t1        j2                         5  t        j                  j                  |	      j5                  |      j7                          ||z
  D ]Q  }t        t        |      }t        j                  j9                  |||j:                  d      }|j=                  |       S |rd|_        d|_         nd|_        d|_         |jC                  ddg       d d d        |rtE        |       t        d|j>                  |j@                  dd      S # t        j.                  $ r Y w xY w# 1 sw Y   UxY w)Nra  recruiter_idsr  r  r)  r   zrecruiter_ids must be listr  r  r  Tr=  r  recruiter_removed_from_jdFrA  )r  ra  )rc  rd  to_emailrL  r  rB  )user_id__inr  )r  r   r   r   rN  rY  r\  r   rV  zRecruiters updated successfully)r   r\  r   r  )#r   r   r   ro  rT  r_   r   setr   r   r   rU  r   r  rU   rV  rZ  get_full_namer   rP   r  r   rX  DoesNotExistr   rt  r  r  r   r   r  r\  r   r   rQ   )r   r   ra  r  r  existing_idsnew_idsr  removed_idsr  	recruiterrc  rd  riduser_objr  s                   r   putzAssignJDView.put
  s     )(("=X':;CHH-.X'CDSQQ"#4EB##v#.::94:P
 m$"W," 	G LL,,,8	  23NX] ^!.. .. )  {{#,#:#:#<#R	@R@R .. # 
  #&__ '77!%	< ! 	>##v#.6676KRRT- 
3,Tc:%--44!!++%	 5 
  &&z2
3 ", &"+ &KKz8&<K=-	>0 )/:<"OO --
 
 	
= $$ 	> 	>s&   ,J/8BJ/*CK	/KK	KN)r   r   r   r   ri   r  r   r  r   r   r   r  r  
  s    )*;
z:U
r   r  c                       e Zd Zed        Zy)generateKeyc                     |  dS )Nz-Some-Strong-Static-Secretr   )
identifiers    r   returnValuezgenerateKey.returnValueW  s    788r   N)r   r   r   staticmethodr  r   r   r   r  r  U  s    9 9r   r  c                   *    e Zd ZegZd Zd Zd Zd Zy)ForgotPasswordMailOTPc                    	 |j                   j                  dd      }|st        dddd      S t        j                  j                  |      }|j
                  st        dd	dd      S | j                  |      }|r(t        d
d|j
                   |j                  dd      S t        dddd      S # t        j                  $ r t        dddd      cY S t        $ r' t        j                          t        dddd      cY S w xY w)Nr   r  FzUsername is requiredr  r)  r   )r   User has no email registeredTzEmail OTP has been sent to )
otp_statusr   r  r  zFailed to send OTPr  zPlease enter valid username  Unexpected error)GETr   r   r   r   r  sendOTPr  r  r  r  r  )r   r   	user_namer   r  s        r   r   zForgotPasswordMailOTP.get^  s   	Z
B7I5=S T]`aa<<##Y#7D::5=[ \ehiid+J"&!<TZZLI''! 	   5=Q R[^__   	eu9VW`cdd 	Z!u9KLUXYY	Zs(   -B7 ;B7 ,:B7 'B7 7#D,D
Dc                    ddl m} 	 t        j                  j	                  |      \  }}|xj
                  dz  c_         |j                         |_        |j                          t               }t        j                  |j                  |j                        j                               }t        j                   |t"        j$                        }|j                         }t'        dd      \  }	}
|	}	|
j)                  |j*                  |t"        j,                  	      }
t/        |	|
t"        j0                  |j2                  gg 
      } |j4                          y# t6        $ r t9        j:                          Y yw xY w)Nr   r   )r   r   intervalsend_otp_reset_passwordFrA  )r   otp_codeotp_minutesr~  T)django.utilsr   r    r   rQ  counterrm   
created_atr   r  base64	b32encoder  r  encodepyotpTOTPr   OTP_EXPIRY_SECONDSrU   rV  r   OTP_EXPIRY_MINUTESrV   rX  r  r  r  r  r  )r   r   r   objr\  keygenr  OTPr  rc  r   r  s               r   r  zForgotPasswordMailOTP.sendOTPx  s/   )!	'//==4=HLCKK1K)X\\^CNHHJ ]F""6#5#5dgg#>#E#E#GHC**S8+F+FGCwwyH12KUZ[GWGnn!$77 % G !#33JJ<B	E EJJL 	!	s   EE E:9E:c                 r   ddl m} ddlm} |j                  j                  d      }|j                  j                  d      }|r|st        dd      S 	 t        j                  j                  |	      } |j                         |j                   |t        j                        z   kD  rt        dd      S t               }t        j                   |j#                  |      j%                               }t'        j(                  |t        j*                        }	|	j-                  |d      rd|_        |j1                          t3        d      }
t4        j                  j                  |      }|j7                  |
       |j1                          t9        dd      \  }}|j;                  |j<                  |
      }t?        ||t        j@                  |jB                  gg       } |jD                          t        d|jB                   d      S t        dd      S # t        $ r t        d
d      cY S w xY w)Nr   )rC   r   r  fp_codezUser ID and OTP are requiredr)  r   )r  zUser ID does not existr  )minuteszYour OTP has expiredr  r   valid_windowT   r  password_reset_successFrA  )r   new_passwordr~  zNew Password has been sent to r  zOTP is wrong or expired)#rB   rC   r  r   r   r   r   r    r   rW   rm   r  r   r  r  r  r   r  r  r  r  r  verifyis_verifiedr   rF   r   set_passwordrU   rV  r   rV   rX  r  r  )r   r   rC   r   r  r  r  r  r  r  r  r   rc  r   r  s                  r   r  zForgotPasswordMailOTP.post  s   &),,""4(<<##I.h:3GG	B&&**7*;C 8<<>CNNYx?Z?Z-[[[23??v11':AACDjjx'B'BC::hQ:/"COHHJ,Q/L<<##w#/Dl+IIK12JTYZGWnn) % G !#33JJ<B	E EJJL<TZZLIRUVV1#>>G " 	B4SAA	Bs    H H65H6c                    |j                   j                  d      }|st        dddd      S 	 t        j                  j                  |      }|j
                  st        dddd      S | j                  |      }|rt        d	d
|j
                   dd      S t        dddd      S # t        j                  $ r t        dddd      cY S t        $ r' t        j                          t        dddd      cY S w xY w)zS
        Resend OTP to user's email.
        Requires: { "id": <user_id> }
        r  FzUser ID is requiredr  r)  r   r  r  TzOTP resent to r  zFailed to resend OTPr  zInvalid User IDr  r  )r   r   r   r   r   r  r  r  r  r  r  )r   r   r  r   r  s        r   patchzForgotPasswordMailOTP.patch  s
   
 ,,""4(u9NOX[\\	Z<<##w#/D::5=[ \ehiid+J"!/

|<!  
  5=S T]`aa   	Yu9JKTWXX 	Z!u9KLUXYY	Zs#   ;B+ +/B+ B+ +#C?,C?>C?N)	r   r   r   r   ri   r   r  r  r  r   r   r   r  r  [  s"    "Z4#J.?`Zr   r  c                       e Zd Zd Zy)RecruiterJDCountViewc                     t         j                  j                  d      j                  d      j	                  t        d            }t        |      S )Nr  r   user__idr  )jd_count)r   r   r   valuesrg  rX   r   )r   r   r   s      r   r   zRecruiterJDCountView.get  sB    V:V&VJXuT{X+	 	 ~r   Nr   r   r   r   r   r   r   r  r    s    r   r  c                       e Zd ZegZd Zy)LinkedInCallbackc           	         |j                   j                  d      }|st        ddid      S t        j                  j                         }t        j                  j                  |d      }t        j                  dd	||j                  |j                  |j                  d
ddi      }|j                         }d|vrt        d|id      S |d   }t        j                  dd| dd      j                         }|j                  d      }	|	st        d|dd      S ||_        |	|_        t        j                          t#        |j                  dd            z   |_        d|_        |j)                          t        ddi      S )Ncoder  zMissing authorization coder)  r   r  r  r  z-https://www.linkedin.com/oauth/v2/accessTokenauthorization_code)
grant_typer   r   client_secretredirect_urizContent-Typez!application/x-www-form-urlencoded)r   headersr  linkedin_errorz$https://api.linkedin.com/v2/userinfozBearer z2.0.0)AuthorizationzX-Restli-Protocol-Version)r&  r  z&LinkedIn OpenID did not return user id)r  linkedin_response
expires_inr   )secondsTr   zLinkedIn connected)r  r   r   r   r   rs  r"   requestsr  r   r$  r%  r:  r  r  r   rm   rC   
expires_atr  r   )
r   r   r   r  r  
token_resp
token_datar  r  r  s
             r   r   zLinkedInCallback.get  s   {{v&W&BCCPP(00668(0044# 5 

 ]];2#--!'!5!5 & 3 3 $%HI


  __&
+-z:3GG!.1 ,,2#*<.!9-4
 $& 	 {{5)A%,   +!-$LLNYz~~l\]?^-__"#7899r   Nr   r   r   r   r  r    s    )*8:r   r  c                       e Zd ZegZegZd Zy)PostJDToLinkedInc                    |j                   j                  d      }|j                   j                  di       }|j                   j                  dd      }t        j                  j	                         }t        d|       |st        ddid	      S t        t        |
      t        j                  j                  |dd      }|j                  }|j                  }|r|st        ddid	      S t        j                  j                  d      j                  d      j	                         }	|	r|	j                  ni }
fd}i }|j!                         D ].  \  }}|s	 ||      |
j                  |      k7  s$ ||      ||<   0 |	r|s|st        dddd	      S |	r6|r4t#        ||	j$                        }|rdnd|	_        |	j)                  dg       dg}|j                  d      r|j+                  dj,                          |j                  d      r|j+                  dj.                          |j                  d      r|j+                  d j0                          |j                  d!      r|j+                  j2                         |j                  d"      r*j4                  r|j+                  d#j4                          t7              }|j                  d$      r|j+                  d%|        |j                  d&      r|j+                  |d&          |j+                  d'j8                          |j+                  d(t;        j<                         j?                  d)              d*jA                  |      }tC        |||      }i |
|}|jD                  d+v }t        j                  jG                  |jH                  |r|jK                         j                  d,      nd ||rdnd-|.       |r"d_&        d/_        j)                  d0dg       t        d1|tO        |jQ                               d2d3	      S )4Nra  fieldsr  Fr  r  zJD ID requiredr)  r   r*  r  Tr  r  r  zLinkedIn token not configuredr  r  r  r  c                     j                   j                  j                  j                  j                  ddj                  |       S )NT)r   rg  
experiencer   r  
apply_link)r   rg  r  r   r  r   )r  r  s    r   current_valuez,PostJDToLinkedIn.post.<locals>.current_valueR  s@    \\ " 44!~~//" c#hr   z#No changes detected since last post)r  requires_confirmationr  DeleteFailedr   rV  r  r   r  rg  r  r6  r  r   r  r  r7  r  custom_textr  r  r  r  r  r  r  r  rN  r\  zPosted to LinkedIn)r   r  r  r  ))r   r   r   r   rs  r  r   r_   r   r"   r  r  r!   r   r   r  r  rH   r  r   r   r  r   rg  r  r   r  rG   rZ  r   rm   ru  r  r\   r  r   r   r:  r\  rT  keys)r   r   rZ  r3  r  r  r  r  r  r  r  r8  r  r  enableddeletedr  r  r	  r  merged_payloadpost_successr  s                         @r   r  zPostJDToLinkedIn.post4  s   ((1!!(B/||''>(00668m[)W&67DD0N(0044# 5 
 **--<W&EFsSS#++22( 3 

(<
  	 8A933b	 "LLN 	9LC=-1A1E1Ec1JJ&3C&8s#	9 ^LB-1   *++G -4yINN(N4#$::k"LL;r||n56::n%LL?2??*;<=::l#LL,R-C-C,DEF::m$LL(::hBOOLL=(9:;'+	::l#LL,YK89::m$LL./}R%5%5$678tHLLN334DEFGH{{5!|\4@7,77*4&&ll6BchhjnnT2+8) 	' 	
 "BKBIGG:x"8G9. ,"&~':':'<"=
 
 	
r   N)r   r   r   r   ri   r	   r   r  r   r   r   r1  r1  0  s    )* \Nv
r   r1  c                       e Zd ZegZd Zy)LinkedInStatusc                     t         j                  j                         }t        j                  j	                  |ddd      j                         }t        dt        |      i      S )Nr  T)r  r  r  r  	connected)r   r   rs  r"   r   r   rr  )r   r   r  r  s       r   r   zLinkedInStatus.get  sf    (00668(0077#	 8 

 %' 	 f
  	r   Nr   r   r   r   rB  rB    s    )*r   rB  c                       e Zd ZegZd Zy)JobFilterOptionsAPIViewc           
         t        t        j                  j                         D cg c]  }|j                  |j
                  d c}t        j                  j                         D cg c]  }|j                  |j                  d c}t        j                  j                  dd      j                         j                  d      D cg c]  }||d	 c}ddddddgd      S c c}w c c}w c c}w )	N)r  rH  r   Tr=  )job_type__isnullrN  rM  )clientsdepartmentsr   r   )r   r   r   r   r  r   r   department_namer   r   rU  r
  r  )r   r   cdjts        r   r   zJobFilterOptionsAPIView.get  s     #**..0
  ]][[ '..224
  ..__ ,33 [$[7XZWdW3	
   	 "F4"W63
  		s    C)( C.
C3Nr   r   r   r   rF  rF    s    )*r   rF  c                   r     e Zd ZegZeZej                  j                         Z
 fdZd Zd Z fdZ xZS )ImplementerPortalConfigViewSetc                     t         |          }| j                  j                  j	                  d      }|r|j                  |      }|S Nr  implementer_idr   r   r   r   r   r   )r   r   r  r   s      r   r   z+ImplementerPortalConfigViewSet.get_queryset  sB    W!#ll//33MB+6B	r   c                     |j                         }|j                  dk(  r0|j                  r#d|_        d|_        |j                  ddg       y y y )Nr  Tr  r  rV  )r   r  	has_tokenr  r  )r   r   r   s      r   perform_createz-ImplementerPortalConfigViewSet.perform_create  sN    ??$??j(X-?-?$(H!!%HMM(EMF .@(r   c                 2   | j                         }|j                  }|j                         }|r(|j                  du rd|_        |j                  dg       |j                  r5|j                  r(|j
                  rd|_        |j                  dg       y y y y )NFr  rV  T)r   r  r   r  rW  token)r   r   r   previous_actives       r   perform_updatez-ImplementerPortalConfigViewSet.perform_update  s    ??$",,??$x11U:$)H!MM(8M9 ""$(H!MM(8M9  # r   c                     | j                         }|j                  rd|_        |j                          t        |   |g|i |S )NF)r   r  r   r   r  )r   r   r   r   r   r   s        r   r  z&ImplementerPortalConfigViewSet.destroy	  sB    ??$  $)H!MMOww8888r   )r   r   r   r   ri   r5   r   r"   r   r   r   r   rX  r\  r  r   r   s   @r   rP  rP    s@    )*8&..224HG:&9 9r   rP  c                       e Zd ZegZd Zy)LinkedInAuthStartc                    t         j                  j                         }t        j                  j	                  |d      }d|j
                  |j                  |j                  dd}dt        |      z   }t        d|i      S )Nr  r!  r   auto_recruiter)response_typer   r%  scopestatez0https://www.linkedin.com/oauth/v2/authorization?auth_url)
r   r   rs  r"   r   r   r%  rc  ra   r   )r   r   r  r  r   urls         r   r   zLinkedInAuthStart.get  s    (00668(0044# 5 
 $))"//\\%
 A9VCTTS)**r   Nr   r   r   r   r_  r_    s    )*+r   r_  c                   n    e Zd Zej                  j                         j                  d      ZeZ	d Z
d Zy)careerViewSet-created_dtc                     | j                   j                  j                  d      }t        j                  j                         }|r|j                  |      }|S rR  )r   r   r   r%   r   r   r   )r   r  r   s      r   r   zcareerViewSet.get_queryset.  sF    ll//33MB^^!+6B	r   c                 P    |j                  | j                  j                         y )Nr   )r   r   r   )r   r   s     r   rX  zcareerViewSet.perform_create7  s    4<<#4#45r   N)r   r   r   r%   r   r   r   r   r8   r   r   rX  r   r   r   rh  rh  *  s/    ~~!!#,,];H'6r   rh  c                       e Zd ZegZd Zy)ConnectFounditPortalc                    |j                   j                  d      }|st        ddid      S t        j                  j                  t        d      t        d      z  |	      j                         }|st        dd
it        j                        S |j                  r|j                  st        ddid      S d|_        t        j                         |_        |j!                          t        ddd      S )NrT  r  zimplementer_id is requiredr)  r   foundit)r  monsterrS  zWNo configuration found. Please add your Foundit/Monster credentials in the table first.zSFoundit credentials (Username/Password) are missing. Please edit the configuration.TzFoundit connected successfully)r   r  )r   r   r   r"   r   r   rY   rs  r   HTTP_404_NOT_FOUNDportal_usernameportal_passwordr  r   rm   connected_dater   )r   r   rT  portal_configs       r   r  zConnectFounditPortal.post@  s     ))*:;W&BCCPP 077>>Y!9"55) ? 
 %' 	 r//1 1
 ,,M4Q4Qop  &*"'/||~$7 
  	r   Nr   r   r   r   ri   r  r   r   r   rm  rm  =  s    )*!r   rm  c                       e Zd ZegZd Zy)PostToFounditViewc                 :   |j                   j                  d      }t        t        |      }t        j
                  j                  |j                  dd      j                         }|st        ddid	      S d
|j                   d|j                   }	 ddlm}  ||||      }ddlm} |j
                  j!                  ||j                  d      |j"                         t        ddi      S # t$        $ r"}	t        dt'        |	      id	      cY d }	~	S d }	~	ww xY w)Nra  rP  ro  Tr4  r  zFoundit not connectedr)  r   zWe are hiring: z
Location: r   )post_to_foundit)FounditJobPostfoundit_job_id)r  r  r  r   zSuccessfully posted to Foundit)r   r   r_   r   r"   r   r   r  rs  r   r   rg  utilsrz  rf   r{  r   r   r  rw  )
r   r   ra  r  r  r	  rz  r#  r{  r  s
             r   r  zPostToFounditView.postf  s     )0U;(0077 8 
 %'	 	 W&=>sKK l2??:KL	;.$VR6F /""))zz"23!,, * 
 Y(HIJJ 	;Wc!f-c::	;s   AC/ /	D8DDDNrv  r   r   r   rx  rx  c  s    )*;r   rx  c                   |    e Zd Zej                  j                         j                  d      ZeZ	e
gZeefZd Zd Zy)CandidateProfileViewSetr  c                 @   | j                  |j                        }|j                         rN|j                  |j                  j
                        }t        d|j                  dt        j                        S t        |j                  t        j                        S )Nr   r   z&Candidate Profile created successfullyr   r   r   r   r   r   r   r   	candidates         r   r   zCandidateProfileViewSet.create  s    ((gll(;
 ""<<00 ( I G&OO ..  ..
 	
r   c                 d   | j                         }| j                  ||j                  d      }|j                         rN|j	                  |j
                  j                        }t        d|j                  dt        j                        S t        |j                  t        j                        S )NTr   r   z&Candidate Profile Updated successfullyr   r   r   r   r   r   r   r   r   r  s          r   r   zCandidateProfileViewSet.update  s    ??$(( ) 

  ""<<00 ( I G&OO ))  ..
 	
r   N)r   r   r   r#   r   r   r   r   r6   r   r   ri   r   r   r   r   r   r   r   r   r  r    sD    "**..099%@H4)*%z2N
*
r   r  c                   t    e Zd Zej                  j                         j                  d      ZeZ	e
gZd Zd Zy)CandidateResumeViewSetz
-resume_idc                 @   | j                  |j                        }|j                         rN|j                  |j                  j
                        }t        d|j                  dt        j                        S t        |j                  t        j                        S )Nr   r   z%Candidate Resume created successfullyr   r   r   r  s         r   r   zCandidateResumeViewSet.create  s    ((gll(;
 ""<<00 ( I F&OO ..  ..
 	
r   c                 d   | j                         }| j                  ||j                  d      }|j                         rN|j	                  |j
                  j                        }t        d|j                  dt        j                        S t        |j                  t        j                        S )NTr   r   z%Candidate Resume updated successfullyr   r   r   r  s          r   r   zCandidateResumeViewSet.update  s    ??$(( ) 

  ""<<00 ( I F&OO ))  ..
 	
r   N)r   r   r   r$   r   r   r   r   r7   r   r   ri   r   r   r   r   r   r  r    s:    !))--/88FH3)*
*
r   r  c                       e Zd ZefZeefZd Zd Z	d Z
 eddgeeg      d        Z eddgeeeg      d        Z eddgeg      d	        ZeeegZ ed
dg      dd       ZddZy)ResumeUploadViewSetc                    |dk(  r	 t        j                  |      }|S |dv r	 t        j                  |      }|S |dk(  r)t        |ddd	      5 }|j                         cd d d        S y
# t        $ r}t        dt        |             d }~ww xY w# t        $ r}t        dt        |             d }~ww xY w# 1 sw Y   y
xY w)Nr  zPDF Error: r  zWord Doc Error: z.txtr  rl  rm  )rs  r   r  )rO   r;  r  rw  r<  r9  read)r   r  rm  rj  r  rl  r  s          r   process_file_contentz(ResumeUploadViewSet.process_file_content  s    81==iH ++=1>>yI & iwxH  Avvx     8+c!fX 6778  ="23q6( ;<<= s:   A) B B9)	B2B		B	B6B11B69Cc                     t        j                  dd|t         j                        j                         }t	        j
                  |      S r5  r8  r<  s      r   extract_jsonz ResumeUploadViewSet.extract_json  s2    &&TGMMOzz'""r   c           
      6   |j                  di       }|j                  di       }|j                  dg       }|j                  dg       }|j                  dg       }|j                  dg       }g }|rt        |      dkD  r|}g }	|rt        |      dkD  r|}	g }
|rt        |      dkD  r|}
g }|rt        |      dkD  r|}i d|j                  dd	      d
|j                  d
d	      d|j                  dd	      d|j                  dd	      ddd|j                  dd	      d|j                  dd	      d|j                  dd	      d|d|j                  dd	      d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d|j                  dg       d|	d|
||j                  dd	      |j                  dd	      |j                  dd	      |j                  dd	      |j                  dd	      dS r  r  r  s               r   r  z'ResumeUploadViewSet.flatten_data_for_db  r  r   Fr  )r  r  r   c                    |j                   j                  d      }|st        ddid      S t        j                  j                  t        j                  d      }t        j                  |d       t        |	      }|j                  |j                  |      }|j	                  |      }t        j                  j                  |      d
   j                         }| j                  ||      }|st        ddid      S t               }	ddd}
|	j!                  |
d   |
d          t#        j$                  |      }|	j'                  |      }| j)                  |j*                        }t-        d|j*                         | j/                  |      }|j                  d      r|j                  d      dk(  rt        ddid      S |j                  d      r|j                  d      dk(  rt        ddid      S |j                  d      r|j                  d      dk(  rt        ddid      S |j                  d      st        ddid      S |j                  d      xs dj1                         j                         }|st        ddid      S t2        j4                  j7                  |      j9                         }|st        ddd |||d!d"      S t:        j4                  j7                  |#      j=                  d$      }|j?                         s t        dddd%||||j@                  d&d"      S |j                  d'      xs d}tC        |      tE        fd(|D        d)      }|s#t        dd|j@                  d|||d*d+|d,d"      S |jF                  xs d}|xs d}tI        d-.      }|jK                  ||g      }tM        |dd
 |d
d/       d   d   }tO        |d0z  d/      }d1}||k  r,t        dd|j@                  |j@                  ||||d2d3d4
d"      S t        dd|j@                  |j@                  |||||d5d6d7d"      S )8z
        Upload file -> extract text -> detect candidate(email) -> compare with latest resume
        New Logic:
        1) If title different => force NEW resume
        2) If similarity very low => ask confirmation (update or new)
        r  r  zfile is requiredr)  r   r  Tr  r  r   Could not extract textrj  rk  rl  rm  rn  zLLM Responser  r  zFirst Name missingr  zMobile missingr  zEmail missingr4  r  r  zEmail not found in resumer  Fr   z(New candidate. No existing resume found.)candidate_existssimilarity_percentr   db_datatemp_file_namer  r  r  ri  z&Candidate exists but no resumes found.)r  has_existing_resumer  r   r  r  r  r2  r  c              3   Z   K   | ]"  }t        |j                  xs d       k(  r| $ yw)r  N)rJ   r  )r  r  	new_titles     r   r  z>ResumeUploadViewSet.check_resume_similarity.<locals>.<genexpr>  s/      !)&,,*<"=J s   (+Nz-Resume title is different. Create NEW resume.r  )r  r  r2  r  r  r  r  r   action_requirednew_title_rawnew_title_normalizedenglish)
stop_words   d      zNSimilarity score is very low. Ask confirmation: Update existing OR Create new.confirm)
r  r  r2  existing_resume_idr  r  r  r  r   r  z Existing resume found (similar).	duplicate)r  r  r2  r  r  r  r  r  r3  r   r  )(r   r   r   r'  r*  r  r   r)  r(  r   r   r  r=  rF  r  rz  r}  rI   rC  r~  r  r	  r  r  r  r#   r   r   rs  r$   r   rR  rQ  rJ   r  r3  rb   fit_transformrc   round)r   r   r  rG  r  r  r  rm  rn  r  r  resume_extract_promptresume_llm_responseraw_jsonr  r  r  r  r  r  existing_textnew_text
vectorizertfidfsimr  LOW_SIMILARITY_THRESHOLDr  s                              @r   check_resume_similarityz+ResumeUploadViewSet.check_resume_similarity  st     ))&1X'9:3GG'',,x':':IF
Od3877=--}=GGH%	GG$$X.q1779	++IyAX'?@MM!,/EJ!!m,/0	

 !+ C CG L+889NO$$%8%=%=>n1667**84{{<(GKK,E,XX';<SII{{8$H(=(PX'78EE{{7#w{{7';'NX7DD{{+,X'78EEW%+224::<X'BCCPP%--4454IOOQ$)&'E""*   .55<<'<R[[\ij&&($(',&'C""* '

	 	 	  G,2*=9	%5 
 $('+ '

&'"*"J#(!.(1   '228b=b$	:
((-)BCa
E!AJ7:1="39a0#%  88$('+ '

&4&7&7&8"*"k#,    $#'#JJ"0"3"3"4&"9*
  	r   c                 0   |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|r|r|st        ddid      S t        j                  j                  t        j                  d|      }t        j                  j                  |      st        dd	id      S t        j                  j                  |      d
   j                         }| j                  ||      }|st        ddid      S |j                  d      xs dj                         j                         }t        j                  j                  |      j!                         }	d| }
t        j"                  |
z   }|	sat        j                  j%                  |d   |d   |d   |d   |d   r|d   nd|d   |d   |d   |d   d|j&                  j(                        }	n|d   |	_        |d   |	_        |d   |	_        |d   xs d|	_        |d   |	_        |d   |	_        |d   |	_        |d   |	_        |j&                  j(                  |	_        |	j=                          t?               }|jA                  |j                  d            }|dk(  r6tB        j                  j                  |	      jE                  d      j!                         }|r|d   |_#        |d   |_$        |d    |_%        |d!   |_&        |d"   |_'        |d#   |_(        |d$   |_)        |d%   |_*        |d&   |_+        |d'   |_,        |d(   |_-        |d)   |_.        |d*   |_/        |d   |_0        |r|nd |_1        ||_2        ||_3        |j&                  j(                  |_        |j=                          t        d+d,|jh                  |	jh                  dd-d.      S tB        j                  j%                  |	|d   |d   |d    |d!   |d"   |d   |d   |d#   |d$   |d%   |d&   |d'   |d(   |d)   |d*   |d   |r|nd |d/||j&                  j(                  d01      }	 tk        |jl                        }|j                  d2i       }|rYtq        tr        j                  j                  d3d45      ju                  d46      ju                  d7      jw                  dd48            }tq        tr        j                  j                  d9d45      ju                  d46      ju                  d7      jw                  dd48            }ty        d:d;<      \  }}|	j*                   d=|	j,                   }|j{                  |>      }|j{                  |jl                  |t}        |d?      r|j~                  nd@t        |      A      }|D ]!  }t        |||t        j                  |d;B       # t        d+dC|jh                  |	jh                  dDd-dE      S # tn        $ r}i }Y d }~d }~ww xY w)FNr  choicer  r  z+temp_file_name, choice and db_data requiredr)  r   r  Temp file not foundr   r  r  r  r  r  r  r  r  r,  r  r  r  r  r   r  r  r   r  ri  r  r  r  r  r   r4  r  r  r  r  r  r  r  r  z$Existing resume updated successfully)r   r   rH  r2  r  r  r   r!  )r2  r  r"  r  r  r   r  r+  r4  r  r  r  r  r  r  r  r  r$  r#  r%  r3  r   r   r5  r6  Tr7  r9  r;  r=  r?  r@  FrA  r-  rC  rE  rF  rG  rK  z New resume uploaded successfullyr  r  )Cr   r   r   r'  r*  r  r   r)  rR  r=  rF  r  r  r#   r   r   rs  rB  r   r   r   r  r  r+  r-  r  r  r  r  r   r   rz  r{  r$   r   r  r"  r  r  r   r4  r  r  r  r  r  r  r  r  r$  r3  r#  rQ  rS  rH  r  rT  r   r  rU  rU   rV  rW  rE  r   rP   rX  )r   r   r  r  r  r  rm  rn  r  r  r  r  r  r  existing_resume
new_resumer`  r5  r  ra  rb  rc  rd  rD  re  s                            r   upload_resume_confirmz)ResumeUploadViewSet.upload_resume_confirm,  sX    ))*:;!!(+,,""9-V7X'TU^abbGGLL!4!4iP	ww~~i(X'<=cJJGG$$^4Q7==?	++IyAX'?@MMW%+224::<%--4454IOOQ!).)9:",,/CC)1188"<0!+.g&%h/7>~7NGN3TVENx(%o6 '(9 :!)"<<00 9 G ")!6G ' 4G$+H$5G!")."9"?RG!%.GK$X.GN$+O$<G!&-.?&@G#!(!6!6GLLN!,9,M,MgkkZrNs,t)X088??W?U^^_lmssuO(/(8%+2;+?(*1)*<')0):&+2:+>(189I1J.3:;M3N07>?U7V4.5m.D+070H-,3K,@)+2:+>(189I1J.9@AY9Z6 rSLm  Y]I.5+.=+-4\\-B-B*$$&'E!0!3!3$+JJ&!   (//66 '"[)I&8$Z('"!(+"#34$%78!()?!@.!/2k*Z("#34#*+C#D[|6W  CG''||,,/ 7 

4	::;O;OPN$((B7G %)%<Mt,b)W40&" %)%>$Ot,b)W40%! //PZ_`MGT ' 2 231W5F5F4GHNnnNnCG;;$..-5<Z5VJ11\aL	  D 2 ,,3! 9##JJ
  	W  	G	s   'W? ?	XXXc                    |j                   j                  d      }|j                   j                  d      }|r|st        ddid      S t        t        |      }t
        j                  j                  t        j                  d|      }t
        j                  j                  |      st        dd	id      S t
        j                  j                  |      d
   j                         }| j                  ||      xs d}|j                  xs d}|j                         }	|j                         }
|	D cg c]   }|j!                         j                         " }}|
D cg c]   }|j!                         j                         " }}t#        j$                  d ||      }g }g }d }|j'                         D ]  \  }}}}}|dk(  rKt)        |	|| |
||       D ]5  \  }}|j+                   ||d             |j+                   ||d             7 Z|dk(  r|	|| }|
|| }t-        t/        |      t/        |            }t1        |      D ]k  }|t/        |      k  r||   nd}|t/        |      k  r||   nd}t3        ||      \  }} |j+                   ||d             |j+                   || d             m |dk(  rF|	|| D ]  }!|j+                   ||!d              |	|| D ]  }"|j+                   |dd              M|dk(  sT|
|| D ]  }"|j+                   |dd              |
|| D ]  }!|j+                   ||!d               t        dj                  |      dj                  |      d      S c c}w c c}w )Nr  r  r  z.existing_resume_id and temp_file_name requiredr)  r   rP  r  r  r   r  c                     d| d|  dS Nz5
            <div style="
                background:z;
                padding:4px 8px;
                border-radius:6px;
                margin:2px 0;
                font-size:13px;
                line-height:1.5;
                color:#111;
                white-space:pre-wrap;
            ">z</div>
            r   r	  bgs     r   wrapz<ResumeUploadViewSet.compare_temp_with_existing.<locals>.wrap  '    4   f  r   equal#ffffffr2  r  #dbeafe&nbsp;insert#dcfce7v1_htmlv2_html)r   r   r   r_   r$   r'  r*  r  r   r)  rR  r=  rF  r  r3  r  r  difflibSequenceMatcherget_opcodeszipr  maxr   r"  rK   )#r   r   r  r  resume1r  rm  text2text1lines1lines2llower1lower2diffr  r  r  tagi1i2j1j2r  b	old_block	new_blockmax_lenkold_linenew_lineold_htmlnew_htmlr/  rT  s#                                      r   compare_temp_with_existingz.ResumeUploadViewSet.compare_temp_with_existing  se   $\\--.BC ))*:;!X'WXadee#$6;MNGGLL!4!4iP	ww~~i(X'<=cJJGG$$^4Q7==?	)))Y?E2##)r!!#!!#-34!'')//#44-34!'')//#44&&tVV<	 $(#3#3#5 	:CRRg~r"vb}= 7DAqNN49#56NN49#567 	!"2bM	"2bM	c)nc)n=w >A/03y>/Ay|rH/03y>/Ay|rH)>x)R&HhNN4)#<=NN4)#<=> "2bM :DNN4i#89:2 >ANN4)#<=> 2 >ANN4)#<=>"2bM :DNN4i#89:=	:B www'www'
  	o 54s   2%M9%M>T)r  r  Nc                    |j                   j                  d      }|j                   j                  d      }|r|st        ddid      S t        t        |      }t        t        |      }|j
                  xs d}|j
                  xs d}|j                         }	|j                         }
|	D cg c]   }|j                         j                         " }}|
D cg c]   }|j                         j                         " }}t        j                  d ||      }g }g }d	 }|j                         D ]  \  }}}}}|d
k(  rKt        |	|| |
||       D ]5  \  }}|j                   ||d             |j                   ||d             7 Z|dk(  r|	|| }|
|| }t        t        |      t        |            }t!        |      D ]k  }|t        |      k  r||   nd}|t        |      k  r||   nd}t#        ||      \  }} |j                   ||d             |j                   || d             m |dk(  rF|	|| D ]  }!|j                   ||!d              |	|| D ]  }"|j                   |dd              M|dk(  sT|
|| D ]  }"|j                   |dd              |
|| D ]  }!|j                   ||!d               t        dj%                  |      dj%                  |      d      S c c}w c c}w )Nv1_idv2_idr  zv1_id and v2_id are requiredr)  r   rP  r  c                     d| d|  dS r  r   r  s     r   r  z7ResumeUploadViewSet.get_side_by_side_diff.<locals>.wrap>  r  r   r  r  r2  r  r  r  r  r  r  )r   r   r   r_   r$   r3  r  r  rF  r  r  r  r  r  r  r   r"  rK   r  )#r   r   rQ  r  r  r  resume2r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r/  rT  s#                                      r   get_side_by_side_diffz)ResumeUploadViewSet.get_side_by_side_diff%  s     )  )EX'EFsSS#$65A#$65A##)r##)r!!#!!#-34!'')//#44-34!'')//#44&&tVV<	 $(#3#3#5 	:CRRg~r"vb}= 7DAqNN49#56NN49#567 	!"2bM	"2bM	c)nc)n=w >A/03y>/Ay|rH/03y>/Ay|rH)>x)R&HhNN4)#<=NN4)#<=> "2bM :DNN4i#89:2 >ANN4)#<=> 2 >ANN4)#<=>"2bM :DNN4i#89:=	:B www'www'
  	o 54s   0%K7%K<c                 4   	 t        t        |      }t        j                  j	                  |      }|D ]  }|j
                  rut        j                  j                  t        j                  t        |j
                              }t        j                  j                  |      rt        j                  |       |j                           |j                          t        dddd      S # t         $ r#}t        dt        |      d	d
      cY d }~S d }~ww xY w)NrP  r  r  zCandidate deleted successfullyr  r  r   r  r  r  )r_   r#   r$   r   r   r#  r'  r*  r  r   r)  rw  rR  r  r  r   r  )r   r   rQ  r  resumesr  r  r  s           r   r  zResumeUploadViewSet.destroyr  s    	O'(;CG(0077W7MG!  %% "X-@-@#fFXFXBY ZIww~~i0		),  NNy=]^gjkk 	Ox#a&A#NN	Os   C(C+ +	D4DDDr   )r   r   r   r   ri   r   r   r   r  r  r  rd   r  r	   r  r  r  r  r   r   r   r  r    s    )+%z2N$#3
j 56(OZ;XYM ZM^ 56(J
Tc;dec fcJ 56(J<HN IN` !*o>N4&*J +JXOr   r  r  c                 ~    t        d|       t        t        |d      }t        |      }t	        |j
                        S )Napply_token_urlrN  )r  r   )r  r_   r   r1   r   r   )r   rZ  r  r   s       r   public_job_detailr    s=     

U#	
B -R0JJOO$$r   POSTc                 2   	 | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                  j                  d      }t        |||||g      st	        ddid	      S t        t        |g d
      }t        j                  j                  |||||d      }|j                  j                  }|j                  j                  }	d}
|j                         j                  d      rt        j                   |      }
n4|j                         j                  d      rt        j"                  |      }
|
j%                         st	        ddid	      S t'               }ddd}|j)                  |d   |d          t+        j,                  |
      }|j/                  |      }t1               j3                  |j4                        }t1               j7                  |      }t8        j                  j;                  ||j                  d      xs |j=                  d      d   |j                  dd      |ddd      \  }}t>        j                  jA                  |      }d }|j                  d       rC|jC                         r3tE        |d          }|D ]   }tE        |jF                        }||k(  s|} n |jI                  |j                  d!            }|r|j                  d"d      |_%        |j                  d#g       |_&        |j                  d$g       |_'        |j                  d%g       |_(        |j                  d&g       |_)        |j                  d'g       |_*        |j                  d(g       |_+        |j                  d)g       |_,        |j                  d*g       |_-        |
|_.        |	|_/        |j                  d!      |_0        |r|nd |_1        d|_2        d+|_3        |ji                          nit>        j                  j                  |||j                  d d      |j                  d"d      |j                  d#g       |j                  d$g       |j                  d%g       |j                  d&g       |j                  d'g       |
|j                  d(g       |j                  d)g       |j                  d*g       |	|j                  d!      |r|nd dd,d+-      }	 tk        |jl                        }|j                  d.i       }|rYtq        tr        j                  jA                  d/d01      ju                  d02      ju                  d3      jw                  dd04            }tq        tr        j                  jA                  d5d01      ju                  d02      ju                  d3      jw                  dd04            }ty        d6d78      \  }} |jz                   d|j|                   }!|j                  |!9      }| j                  |jl                  |!t        |d:      r|j                  nd;t        |      <      } |D ]!  }"t        || |"t        j                  |d7=       # t        |||j                  >       t	        d?d0id@	      S # tn        $ r}i }Y d }~d }~ww xY w# tn        $ r.}dd lH}# |#j                          t	        ddAidB	      cY d }~S d }~ww xY w)CNra  r  r  r  r  r  All fields are requiredr)  r   )rN  rY  r  )rZ  r  Publicr  	full_namer  r  r  r.  r  r  r  Could not extract resume textrj  rk  rl  rm  rn  r  r-  r   r  publicr   r  r  r+  r.  r/  r0  r  r  r  r   r4  r  r  r  r  r  r  r  public_applyr!  r2  r  r  r   r4  r  r  r  r  r3  r  r  r  r#  r  r$  r%  r   r   r5  r6  Tr7  r9  r;  r=  r?  r@  FrA  rC  rE  rF  rG  rK  r  rD  r   r  r  Resume processing failedr  )Jr   r   r   r   r   r_   r   r&   r   r   r  r*  rf  rF  r  rO   r;  r<  r  rz  r}  rI   rC  r~  rj  r>  r	  r  r#   rQ  r  r$   r   rR  rJ   r  r{  r   r4  r  r  r  r  r  r  r  r3  r#  r  r$  r%  r   r   rS  rH  r  rT  r   r  rU  rU   r  r  rV  rW  rE  r   rP   r   rX  rM   r   r  r  $r   rZ  r  r  r  r#  r  applicationresume_path
resume_urlextracted_textr  r  promptai_responseparsed_json	flat_datar  rT  r  r  r  r  r  r  r_  r`  r5  r  ra  rb  rc  rd  rD  re  r  s$                                       r   public_apply_jobr    s   
((1LL$$V,	  )!!(+mm''1M9eV[IJ34 
 '4
 %,,33 4 
 "((-- ''++
''/3??LN ))*;<3@@MN##%9: 
 ",/EJ!!m,/0	

 44^D#008&(>>{?O?OP$&::;G	*22@@'mmL9TY__S=QRS=T&]];;!'"$, A 	
	1 .55<<)<T==!&6&=&=&?#9)G:L#M * !7!E!%99%+N	 -:,M,MMM23-
) &/mmJ&CN#,5MM:JB,ON).7mm<NPR.SN+2;--@VXZ2[N/)2}b)IN&+4=="+MN('0}}["'EN$,5MM:JB,ON)&/mmJ&CN#)7N&)3N&4=MMBZ4[N15V1\` D ,4N((6N%! ,33::&mmGR0"z26(}}-=rB!*/A2!F%.]]3I2%N%MM-<'mmOR@*#--R8(}}-=rB"z26&'0}}5M'N9Z5`d&)+ ; J0!>z?S?S!T(,,Y; )-LLV)@DVQW4W0W"W- [t[4*& )-LLV)BdVSW4W0W"W- [t[4)% !33T^c d$-$8$8#99;N;N:O!P!...G{{(22#19@\9Z
 5 5`e #G	 #   6 G# 00"7 % 	$ll	
 D)#66c  f  
	01
 	

s^   B%[ (C.[ E[ 0H+[ '[ F[ 	[[[ [[ 	\(#\\\c                   d    e Zd Zd Zd Zd Zd ZddZd Zd Z	d Z
d	 Zd
 ZddZd Zd Zd Zy)IdentifyMatchingProfilesc                    |sy|j                         j                         }t        j                  dd|      j                         }t        j                  d|      }|r4t        |j                  d            t        |j                  d            fS t        j                  d|      }|r*t        |j                  d            t        j                  fS t        j                  d|      }|r*t        |j                  d	            t        j                  fS t        j                  d
|      }|rdt        |j                  d	            fS t        j                  d|      }|r*t        t        |j                  d                  }||dz   fS yzE
        Normalize JD experience into (min_years, max_years)
        NN(years|year|yrs|yr)r  z!(\d+(\.\d+)?)\s*-\s*(\d+(\.\d+)?)r   rR  z(\d+(\.\d+)?)\s*\+z (over|more than)\s*(\d+(\.\d+)?)r  z#(within|upto|up to)\s*(\d+(\.\d+)?)        (\d+(\.\d+)?)
rF  r  r  r  r1  floatr8  mathinfr   r   r	  matchmin_vals       r   normalize_jd_experiencez0IdentifyMatchingProfiles.normalize_jd_experienceV  F    zz|!!#vv,b$7==? 		>EQ(%A*??? 		/6Q($((22 		=tDQ($((22 		@$Gekk!n--- 		*D1%A/0GGaK''r   c                    syj                         j                         t        fddD              ryt        fddD              ryt        j                  dd      t        j                  d	d      j                         t        j
                  d
      }|syt        |j                  d            }t        |      S )B
        Normalize candidate experience to a SINGLE value
        Nc              3   &   K   | ]  }|v  
 y wr   r   r  r  r	  s     r   r  zJIdentifyMatchingProfiles.normalize_candidate_experience.<locals>.<genexpr>       b7w$br  fresherzentry levelzentry-leveljuniorr   c              3   &   K   | ]  }|v  
 y wr   r   r  s     r   r  zJIdentifyMatchingProfiles.normalize_candidate_experience.<locals>.<genexpr>       @7w$@r  not specifiedr  r  [^\d\.\+\-\s]r  r   	rF  r  r  r  r  r1  r  r8  r   r   r	  r  rH  s    `  r   normalize_candidate_experiencez7IdentifyMatchingProfiles.normalize_candidate_experience|       zz|!!#b.abb@.?@@vv,b$7vv&D1zz| 		*D1ekk!n%5zr   c                     | j                  |      \  }}| j                  |      }||y||k  ry||t        j                  k7  r||kD  ryyNFTr  r&  r  r  r   jd_exp_textcandidate_exp_textjd_minjd_maxcandidate_yearss         r   is_years_matchz'IdentifyMatchingProfiles.is_years_match  ]    55kB==>PQ>_4V#&DHH"469Qr   c                 >    |j                         j                         S r   rF  r  r   skills     r   normalize_skillz(IdentifyMatchingProfiles.normalize_skill      {{}""$$r   c           	         | j                  |      }t        d| d       t        d|        |D ]  }| j                  |      }|j                         D ]$  \  }}|D ]  }|| j                  |      k(  s|} $ & t        d| d       ||k(  rt        d        yt        j                  j                  t        t        |      t        |                  j                  d      d d	 }	|	r|	d
   d   nd }
t        d| d| d|
        |
|
|k\  rt        d| d        yt        d| d        t        d       y)Nu#   
[JD SKILL] Checking JD skill → ''z[CANDIDATE SKILLS] u*     ↳ Comparing with candidate skill → '       ✅ EXACT MATCH FOUNDTr  r  r   r   u        🔍 TRIGRAM SCORE between 'z' and 'z' = #       ✅ TRIGRAM MATCH ACCEPTED (>= )"       ❌ TRIGRAM MATCH REJECTED (< &     ❌ NO MATCH FOUND FOR THIS JD SKILLF)	r7  r  r  r$   r   rg  rk   r[   r  )r   jd_skillcandidate_skillsskill_matrix_dataTRIGRAM_THRESHOLD
cand_skill
skill_name
alias_dataaliassimilarity_qssimilarity_values              r   skill_matchz$IdentifyMatchingProfiles.skill_match  s   ''14XJa@A#$4#567* ,	J--j9J*;*A*A*C &
J' E!T%9%9%%@@%/
 >zl!LM X%12 #**)j)h   r#  ;H}Q/6T:WZL5E4FH
  +0@DU0U,-Q0 +,A/S,	\ 	67r   c                    t        d|j                          t        d       | j                  ||j                  |      rt        d       yt        d       | j                  ||j                  |      rt        d       yt        d       | j                  ||j
                  |      rt        d       yt        d       y	)
Nz2
[CHECKING JD SKILL AGAINST CANDIDATE] Resume ID: u   ▶ Trying PRIMARY skills!   ✅ MATCH FOUND IN PRIMARY SKILLSTu&   ▶ Trying TECHNICAL skills (fallback)u#   ✅ MATCH FOUND IN TECHNICAL SKILLSu!   ▶ Trying SOFT skills (fallback)+   ❌ NO MATCH IN PRIMARY OR TECHNICAL SKILLSF)r  rH  rK  r4  r  r  )r   rA  r  rC  s       r   jd_skill_matches_candidatez3IdentifyMatchingProfiles.jd_skill_matches_candidate  s    #--.0	

 	)*Hi&>&>@QR5667Hi&@&@BST7812Hi&;&;=NO78;<r   c                    t        d|j                   d| d       |sdddddS d}|D ]  }t        d| d       | j                  |      }|j                         D ]$  \  }}|D ]  }	|| j                  |	      k(  s|} $ & | j	                  |||      r|d	z  }t        d
| d       t        d| d        t        |      }
||
z  dz  }||
z  }||
t        |d      t        |d      dS )Nz+
==============================
Resume ID: z
JD Skills: z
==============================r   r  matchedtotal
percentagescoreu   
➡️ Evaluating JD Skill: 'r:  r   u   ✅ JD Skill 'z	' MATCHEDu   ❌ JD Skill 'z' NOT MATCHEDr  r  rS  )r  rH  r7  r  rO  r   r  )r   	jd_skillsr  rC  rR  rA  normalized_jd_skillrF  rG  rH  rS  rT  rU  s                r   calculate_skill_scorez.IdentifyMatchingProfiles.calculate_skill_score  sN   %//0I;.0	
 !	  ! 	@H3H:Q?@"&"6"6x"@*;*A*A*C &
J' E*d.B.B5.II#- ..xDUV1xj	:;xj>?	@  Io,
% 
A.5!_	
 	
r   c                    |j                         D ci c]  \  }}||j                  |      || }}}|syt        |j                               }|j                         D ci c]  \  }}|||z   }}}d}|j                         D ]4  \  }}||   }	t	        |	t
              r|	rdnd}
nt        |	      }
||
|z  z  }6 t        |d      S c c}}w c c}}w Nr  g      ?r   rS  r  r   sumr  ro  rr  r  r  r   
match_database_weightsfieldweightvalid_fieldstotal_weightnormalized_weightsoverall_scorerH  score_values              r   calculate_overall_scorez0IdentifyMatchingProfiles.calculate_overall_score+  	    ".!3!3!5
v!jnnU&;&G 6M
 
 <..01 ".!3!3!5
v 6L((
 

 /557 	2ME6u%E%&%*c#El[611M	2 ]A&&5

    C&Cc                    |sy|j                         j                         }|dv ryt        j                  dd|      }d}d|v sd|v sd|v rd	}nd
|v sd|v sd|v rd}nd|v rd}t        j                  d|      }|sy|D cg c]  }t        |      |z   }}d|v sd|v sd|v r|d   t        d      fS d|v sd|v sd|v rd|d   fS t        |      dk\  rt        |      t        |      fS |d   |d   fS c c}w Nr
  )r"  
negotiablezas per company standardsu   [₹,$,]r  r   lpalakhlakhsi z	per monthmonthlypm   r  i  z	\d+\.?\d*aboveminimum+r   r  uptozup tomaximumr  	rF  r  r  r  findallr  r   minr  r   r	  
multipliernumbersnr  s         r   parse_salaryz%IdentifyMatchingProfiles.parse_salaryH  C   zz|!!# NN vvk2t,
 D=FdNgoJ D I$5J D[J **\40189A%(Z'99 d?i4/3$;1IuU|,, T>W_	T0Avay>! v;!KV-- q	6!9%% :   C5c                 v    | j                  |      \  }}| j                  |      \  }}||y ||k  xs ||kD   S r   r  r   	jd_salarycandidate_salaryr.  r/  c_minc_maxs          r   salary_matchz%IdentifyMatchingProfiles.salary_matchz  M    **95(()9:u>U]FN4efn55r   c                    | j                  |      }| j                  |      }||k(  rt        d       yt        j                  j	                  t        t        |      t        |                  j                  d      d d }|r|d   d   nd }|||k\  rt        d| d       yt        d	| d       t        d
       y)Nr;  Tr<  r  r   r   r=  r>  r?  r@  F)r7  r  r$   r   rg  rk   r[   r  )r   r  candidate_datarD  rI  rJ  s         r   trigram_matchz&IdentifyMatchingProfiles.trigram_match  s    &&w/--n= W$-. &&X%.)'N   VE]2A 	 7D=+E2',<@Q,Q(), '(+
 	67r   c                    |sy t        |      j                         j                         }d|v sd|v ry|dv ry d|v ryt        j                  d|      }|sy t        |j                               }d|v r|dz  S d	|v r|d
z  S |S )Nserving	on noticer   )r"  rl  zn/a	immediatez\d+monthr  week   )rw  rF  r  r  r1  r   r8  )r   rH  r	  r  nums        r   parse_notice_period_to_daysz4IdentifyMatchingProfiles.parse_notice_period_to_days  s    5z!'') t 399$		&$'%++- d?8OT>7N
r   c                     |r|sy|j                         }t        j                  dd|      }d}|D ]-  }|j                         j                         }|s$||v s)|dz  }/ t	        |      }t        ||z  d      S )Nr  z[^a-z0-9\s]r-  r   r   rR  )rF  r  r  r  r   r  )r   jd_keywordsresponsibilities_textr	  rR  kwkw_cleanrS  s           r   keyword_match_scorez,IdentifyMatchingProfiles.keyword_match_score  s    "7$**,vvnc40 	Bxxz'')HH,1		 K Wu_a((r   c           
      D   |j                   j                  d      }|j                   j                  dg       }|st        ddid      S i }t        j                  j                  |      }|j
                  }|j                  d   }|j                  d	   }|j                  d
   }	|j                  d   }
|j                  d   }|j                  r*|j                  dk7  r|j                  j                  d      ng }|D cg c]#  }|j                         s|j                         % }}|j                  j                  dd       }|j                  j                  dd       }|j                  j                  dd       }|j                  j                  dd       }|j                  j                  dd       }|j                  j                  dd       }|j                  j                  dd       }t        j                  j                         }i }t        j                  j                  t        j                   dd      }t#        |dd      5 }t%        j&                  |      }d d d        g }|D ]X  }t)        |j*                        }| j-                  |j.                  |j0                        }|sA|j3                  |       ddi||<   Z |r|r	|dk(  sd	|vr$|D ]  }t)        |      }||vri ||<   d ||   d	<     nd|D ]_  }t)        |      }||vri ||<   t        j                  j                  |      }| j5                  |||      }t7        |       |d   ||   d	<   a |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     na|D ]\  }t)        |      }||vri ||<   t        j                  j                  |      }|j8                  }| j;                  ||      } | ||   d<   ^ |D ]  }t)        |      }||vri ||<   d ||   d<     |rd|v rt        j                  j=                  |       j?                  d !      jA                  tC        tE        d"      |      #      jA                  d$tE        d%      z
  &      j=                  d'(      }!|!D ]B  }t)        |j*                        }|jF                  xs d)}"tI        tK        |"      d*      ||   d<   D |r|	r	|	dk(  sd
|vr$|D ]  }t)        |      }||vri ||<   d ||   d
<     n|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }d+}#|	D ]%  }$| jM                  |$|jN                  |      s!|#d$z  }#' tQ        |	      }%|#|%z  d,z  }&|#|%z  }'|#|%tI        |&d-      tI        |'d*      d.}(t7        |(       |(d   ||   d
<    |r|
r	|
dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     nd|D ]_  }t)        |      }||vri ||<   t        j                  j                  |      }| j5                  |
||      })t7        |)       |)d   ||   d<   a |r|r	|dk(  sd/|vr$|D ]  }t)        |      }||vri ||<   d ||   d/<     n|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }d+}*|D ]%  }$| jM                  |$|jR                  |      s!|*d$z  }*' tQ        |      }+|*|+z  d,z  },|*|+z  }-|*|+tI        |,d-      tI        |-d*      d.}.t7        |.       |.d   ||   d/<    |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     n|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }|jT                  xs g D /cg c]&  }/tW        |/d0      r|/jX                  r|/jX                  ( }0}/d+}1|D ]  }$| jM                  |$|0|      s|1d$z  }1 tQ        |      }2|1|2z  d,z  }3|1|2z  }4|1|2tI        |3d-      tI        |4d*      d.}5t7        |5       |5d   ||   d<    |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     ne|D ]`  }t)        |      }||vri ||<   t        j                  j                  |      }d'}6| jM                  ||jZ                  g|      rd}6|6||   d<   b |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     na|D ]\  }t)        |      }||vri ||<   t        j                  j                  |      }|j\                  }7| j_                  ||7      }8|8||   d<   ^ |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     nk|D ]f  }t)        |      }||vri ||<   t        j                  j                  |      }|j`                  }9| j-                  ||9jb                        }:|:||   d<   h |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     n|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }|j`                  }9|je                         j                         };d }<|9jf                  r(|9jf                  je                         j                         }<|;|<k(  r	d||   d<   d'||   d<    |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     nk|D ]f  }t)        |      }||vri ||<   t        j                  j                  |      }|j`                  }9| ji                  ||9jj                        }=|=||   d<   h |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     n|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }|j`                  }9|9jl                  xs d1}>d2|>je                         v sd3|>je                         v r	d||   d<   | jo                  |      }?| jo                  |>      }@|?@	d ||   d<   @|?k  ||   d<    |r|r	|dk(  sd|vr$|D ]  }t)        |      }||vri ||<   d ||   d<     ntq        |      }?ts               tu        |?4      z
  }A|D ]  }t)        |      }||vri ||<   t        j                  j                  |      }|j`                  }9|jv                  |jx                  |9jv                  |9jx                  g}B|BD Ccg c]  }C|CC	 }B}C|Brt{        B      nd }D|Ds	d'||   d<   DAk\  ||   d<    d|v rG|j}                         D EFci c],  \  }E}F|Fj                  d      d'urFj                  d      EF. }}E}Fd|v rG|j}                         D EFci c],  \  }E}F|Fj                  d      d'urFj                  d      EF. }}E}Fd|v rG|j}                         D EFci c],  \  }E}F|Fj                  d      d'urFj                  d      EF. }}E}Fd|v rG|j}                         D EFci c],  \  }E}F|Fj                  d      d'urFj                  d      EF. }}E}Fd|v rG|j}                         D EFci c],  \  }E}F|Fj                  d      d'urFj                  d      EF. }}E}Fd5d6d7d8d8d8d8d8d8d9	}G|j}                         D ]  \  }H}I| j                  |IG      |Id:<    t        t        |j}                         d; d<            }t        |tQ        |      |d=      S c c}w # 1 sw Y   xY wc c}/w c c}Cw c c}F}Ew c c}F}Ew c c}F}Ew c c}F}Ew c c}F}Ew )>Nra  eligible_search_paramsr  r  r)  r   r  keywordsr4  r  secondary_skillsr  r  rB  r   r  	age_ranger  r  r  last_modifiedmastersskill_matrix.jsonr  rl  r  r6  TrH  rU  r   )resume_id__in)r$  r$  distancer   r  responsibilities_similarityF#responsibilities_similarity__isnullr  rS  r   r  r  rQ  rc  degreer  r  r  r  g      ?g      ?g?皙?)	r4  r   r  r  r  rc  r  r   r  re  c                 ,    | d   j                  dd      S Nr   re  r   r  r  s    r   <lambda>z/IdentifyMatchingProfiles.post.<locals>.<lambda>      a_a!@ r   r  rt   )ra  total_candidatesr5  )Br   r   r   r   r   r]  ro  rc  r  r  r$   r   r'  r*  r  r   BASE_DIRr9  r:  loadrw  rH  r1  r  r"  r  rX  r  r  r  r   r  rg  rj   rZ   r  r  r  rK  r  r   r  r  rW  r  r   r  r  r2  r  rF  r  r  r  r  r  r   rm   rC   rE  
updated_dtr  r  rg  rh  sorted)Jr   r   ra  r  resultsr  r]  r  jd_primary_skillsjd_tools_and_frameworksjd_secondary_skillsjd_educationjd_domain_requirementsrM  jd_locationjd_salary_rangejd_age_rangejd_gender_filterjd_employment_typejd_notice_periodjd_last_modified
candidatesrC  	json_pathr  eligible_resume_idsrL  rH  experience_matchcandidate_objprimary_skill_scorecandidate_responsibilitieskeywords_scoresimilarity_candidates
similaritymatched_tools_skillr6  tools_totaltools_percentagetools_scoretools_skills_resultsecondary_skills_scorematched_domain_skilldomain_totaldomain_percentagedomain_scoredomain_requirements_scorer  candidate_educationmatched_education_skilleducation_totaleducation_percentageeducation_scoreeducation_requirements_scorelocation_matchr  eligibler  	age_match	jd_gendercandidate_genderjd_employment_matchcandidate_notice_rawjd_dayscandidate_dayscutoff_date
timestampstlatest_modifiedr  vBASE_WEIGHTSrT  r^  sJ                                                                             r   r  zIdentifyMatchingProfiles.post  s]     )!(!1!12JB!OW&9:3GG#++//e/<(/(M(M%,,Z8#223CD")"8"89O"P%445GH--k: **w/J/Jo/] ''--c2 	 6L!Yqwwy!'')!Y!Y,,00TB!0044^TJ--11+tD"1155hE$33778I4P"1155otL"1155otL'//335
GGLL!2!2I?RS	)S73 	-q $		!	- ! 	AAKK(I#22++

 
  #**95 !$&	"	 $(9_(LP`h~P~, @A #AI /-/	*;?GI&'78	@ - XA #AI /-/	*$6$>$>$B$BQ$B$OM*.*D*D)%)+' -.;Nw;WGI&'78X  +"@JVlDl, :A #AI /-/	*59GI&z2	: - DA #AI /-/	*$6$>$>$B$BQ$B$OM1>1U1U.%)%=%=kKe%fN5CGI&z2D % 	:AAI'%'	"59GI12		: #59O#O"**&9:4H+EF5   01AjM0A   EB "  + ,	::Ac
9>*%q:	"#56	 *.E.X\r  {Q  ]Q, FA #AI /-/	*AEGI&'=>	F - ^A #AI /-/	*$6$>$>$B$BQ$B$OM*+'!8 5++E=3U3UWhi/14/5 #&&=">K(;k(IS'P$"5"CK $7!,&+,<a&@!&{A!6	+' -.ATU\A]GI&'=>3^6 &*=*PTf  oE  UE, BA #AI /-/	*=AGI&'9:	B - ]A #AI /-/	*$6$>$>$B$BQ$B$OM-1-G-G+%).* 01=ST[=\GI&'9:]" )-C-VZo  xN  [N, EA #AI /-/	*@DGI&'<=	E - cA #AI /-/	*$6$>$>$B$BQ$B$OM+,(!7 6++E=3N3NPab0A506 $''=#>L)=)LPS(S%#7,#FL $8!-&+,=q&A!&|Q!7	1- 34@YZa@bGI&'<=3c6 <?#BkYoFo, ;A #AI /-/	*6:GI&{3	; - \A #AI /-/	*$6$>$>$B$BQ$B$OM &3%<%<%B+ "42t{{ +' + /0+!- 9++E3FHYZ3q839 '*,&7O,Co,UY\+\(&=&OO $;!0&+,@!&D!&!:	40 676RSZ6[GI&{3?\B +"@JVlDl, :A #AI /-/	*59GI&z2	: - DA #AI /-/	*$6$>$>$B$BQ$B$OM%*N''m6L6L5MO`a)-5CGI&z2D "o&HNbxLx, >A #AI /-/	*9=GI&~6	> - BA #AI /-/	*$6$>$>$B$BQ$B$OM'4';';$#00BRSH9AGI&~6B <?#BkYoFo, ;A #AI /-/	*6:GI&{3	; - @A #AI /-/	*$6$>$>$B$BQ$B$OM%2%?%?N $ 3 3$&**!I
 7@GI&{3@  #'7?'Jh^tNt, 8A #AI /-/	*37GI&x0	8 - =A #AI /-/	*$6$>$>$B$BQ$B$OM%2%?%?N 0 6 6 8 > > @I'+$%,,+9+@+@+F+F+H+N+N+P( $447;	*847<	*84%=( %);)NRc  lB  SB, AA #AI /-/	*<@GI&'89	A - PA #AI /-/	*$6$>$>$B$BQ$B$OM%2%?%?N*.*<*<=OQ_QoQo*p'<OGI&'89P #'7?'Joe{N{, ?A #AI /-/	*:>GI&7	? - \A #AI /-/	*$6$>$>$B$BQ$B$OM%2%?%?N+9+G+G+M2( $8$>$>$@@KSgSmSmSoDo>B	*?;"&"B"BCS"T)-)I)IJ^)_"?n.DBFGI.?BPT[B[GI.?)\, #'7?'Joe{N{, ?A #AI /-/	*:>GI&7	? ./!eiW&==, A #AI /-/	*$6$>$>$B$BQ$B$OM%2%?%?N &00%00&11&11	"J .8!I1=!!IJ!I9Cc*oO*>C	*?; ,{:  	*?;-4 --!(A55?%/AEE(O4O 1G 
 44!(A55)6155;Q;] 1G 
 00!(A55%U2quu[7I7U 1G 
 33!(A55(5!%%:O:[ 1G 
 44!(A55)6155;Q;] 1G  " $$) %#($

 %]]_ 	aMAz*.*F*FzS_*`J'	a @
  #G
  	E "Z	- 	-^+N "JsH   >}(}(	}-0+}:}?}?1~1~
'1~21~=1~-}7Ng?)r   r   r   r  r&  r1  r7  rK  rO  rX  rg  r  r  r  r  r  r  r   r   r   r  r  U  sN    $L6%4l02
h':0&d6$L<)&|r   r  c                       e Zd ZegZd Zy)ApplyJobRedirectc                 X    t        t        |      }d|j                   }t        |      S )N)r  z#https://career.itconnectus.com/job/)r_   r   rZ  r^   )r   r   rZ  r  frontend_urls        r   r   zApplyJobRedirect.get  s-    0%H<R=M=M<NO%%r   N)r   r   r   r   ri   r   r   r   r   r  r    s    "&r   r  c                 L   | j                   j                  d      }t        j                  j	                  d      j                  |      }d}d }d }|rt
        j                  j                  |dg      j                         }|rzt        j                  j                  d|d      j                  d	      j                         }|r9|j                  r-|j                  j                  d
      }	|	rd}|j                  }|	}t        || |||d      }
t        |
j                        S )N
matched_idr2  r  FrR  )r  r  	Completedr   matched_profile__resume_idinterview_score__isnull-created_atFinal_Hiring_RecommendationT)r   
l0_presentl0_completed_time	l0_result)context)r   r   r$   r   r  r'   r   rs  r)   r   interview_scorer  r9   r   r   )r   rH  r  r  r  r  r   matched_objl0_objfinal_recommendationr   s              r   resume_basic_detailsr    s-   %%)),7J''66~FJJU^J_FJI(0077!{ 8 
 %' 	
 $,,33"+4(- 4  h}%eeg	  &00**S67 %
 (!%J(.(9(9% 4I&$!2"	
J JOO$$r   c                     t               }| |xs g d}t        dt        fd|i      }|j                  |      }|j                  S )N)ra  r  r  r   )r  typeobjectr  r   )ra  r  viewpayloadfake_requestresponses         r   run_matching_enginer    sR    #%D"8">BG y+ L yy&H==r   c                 X   | j                   j                  d      }| j                   j                  dg       }| j                   j                  dg       }d}|st        j                  j	                  |      }|D ]K  }t
        j                  j	                  |j                        j                         r<|j                          M t        j                  j	                  |      }|j                         rTt        |d      }t        d	|j                   d
      S t        j                  j	                  |d      j                          t        j                  j                  |      }t        ||      }	|	j                  di       }
|
sEt        j                  j	                  |      }t        |d      }t        d|j                   d
      S t        |
j                         d d      d | }|D ]  \  }}t
        j                  j	                  |      j                         }|s7t        j                  j	                  ||      j                         rgt        j                  j!                  ||d|d   |d   |d   |d   |d   |d   	        |j"                  dv rd|_        |j%                  dg       t        j                  j	                  |      }t'        j(                         |_        |j%                          t        |d      }t        d|j                   d
      S )Nra  r  regen_filter   )r  r  Tmanydbr.  r   rR  )r  status__iexactr  r5  enginec                     | d   d   S Nr   re  r   r  s    r   r  z&get_matched_profiles.<locals>.<lambda>H      ad?+ r   r  rH  r  re  r4  r  r   r  r6  	rH  r  r   match_scoreprimaryskill_scoresecondaryskill_scoreresponsibilities_scoretech_framework_scoreexperience_scorer  Matchedr\  rV  )r   r   r'   r   r   r$   rH  rR  r  r<   r   r   r  r  r  rs  r   r\  r   r   rm   last_profile_search_time)r   ra  r  r  
slots_leftexistingmr   r  r`  r5  sorted_candidatesrH  rM  r  final_querysets                   r   get_matched_profilesr+    s   LLW%E$\\--.FK<<##NB7LJ%--44E4B 	A%--44q{{4KRRT
	 &--44E4B??5hTJJ"   	""))$ 	* 	
 &(		"	"	&	&U	&	3B(0FGN  B/G%--44E4B1(F
OO
  	
 + z	 * 
	1#++22Y2GMMO%%,, - 
 &( ""))/* !12!"#5!6#$%7#8!"#9!:|_ 	* 
	

0 
{{77
zl+'//66e6DN"*,,.BGGI-n4HJ  r   c                    | j                   j                  dg       }| j                   j                  d      }| j                   j                  d      }t        j                  j	                  ||      }|D ]J  }|rdnd|_        |j                          t        d       t        |j                  ||       t        d       L t        d	d
i      S )N
resume_idsra  consent)r  r  L0 completedshort_listedz-----mail sendingz-----mail sent successfullyr   Shortlisted)r   r   r'   r   r   r   r   r  rN   rH  r   )r   r-  ra  consent_requiredr$  rY  s         r   shortlist_profilesr3  u  s    !!,3JLLW%E||''	2!))00  1 H
  -%5>>	!"Q[[%1AB+,- Y.//r   c                    | j                   j                  d      }| j                   j                  dg       }t        j                  j                  |      }|j                  }t        ||      }|j                  di       }|st        ddi      S t        |j                         d d	      }d}d}	|D ]  \  }
}t        j                  j                  |

      }t        j                  j                  |

      j                         }|s0t        j                  j                  |
|      j                          t        j                  j                  |
|      j                         }|rs|ro|j                  |kD  s|j                  |kD  rQ|d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |j-                          |	dz  }	+|r |j                  |k  r|j                  |k  rMt        j                  j/                  |
|d|d   |d   |d   |d   |d   |d   	       |dz  }|dk\  s n t1        j2                         |_        |j-                          t        ||	d      S )Nra  r  r  r5  addedr   c                     | d   d   S r  r   r  s    r   r  z$find_more_profiles.<locals>.<lambda>  r  r   Tr  r  r  re  r4  r  r   r  r6  r   rR  r  r  )r5  r  )r   r   r   r   r%  r  r   r  r  r$   r   rs  r'   r  rE  r  r  r  r   r!  r"  r#  r   r   r   rm   )r   ra  r  r  	last_timer`  r5  r)  r5  r  rH  rM  r  r'  s                 r   find_more_profilesr8    s    LLW%E$\\--.FK		"	"	&	&U	&	3B++I(0FGN  B/G!%%+ EG) 6	1#++//)/D#++22Y2GMMO&&--# .  fh%--44 5 
 %' 	
 !!I-!!I-'('9$./0@.A+012D0E-234F2G/012H0I-,-lO)1  I-&2C2Cy2P""))/* !12!"#5!6#$%7#8!"#9!:|_ 	* 
	
 	
B;m6p #+,,.BGGI  r   c           
          t         j                  j                  dddg      j                  ddddd	d
dd      j	                  d      }t        |      S )NrN  r  r  r  ra  rZ  r   rg  r  r   public_tokenrE  ri  )r   r   r   r  r   r   )r   jobss     r   career_open_jobsr<    si     $$++ (+ ,  f	 h} 	 D>r   c                 4   	 | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                  j                  d      }t        |||||g      st	        ddid	      S t        t        |d
ddg      }t        j                  j                  |||||d      }|j                  j                  }|j                  j                  }	d}
|j                         j                  d      rt        j                   |      }
n4|j                         j                  d      rt        j"                  |      }
|
j%                         st	        ddid	      S t'               }ddd}|j)                  |d   |d          t+        j,                  |
      }|j/                  |      }t1               j3                  |j4                        }t1               j7                  |      }t8        j                  j;                  ||j                  d      xs |j=                  d      d   |j                  dd      |ddd       \  }}t>        j                  jA                  |!      }d }|j                  d"      rC|jC                         r3tE        |d"         }|D ]   }tE        |jF                        }||k(  s|} n |jI                  |j                  d#            }|r|j                  d$d      |_%        |j                  d%g       |_&        |j                  d&g       |_'        |j                  d'g       |_(        |j                  d(g       |_)        |j                  d)g       |_*        |j                  d*g       |_+        |j                  d+g       |_,        |j                  d,g       |_-        |
|_.        |	|_/        |j                  d#      |_0        |r|nd |_1        d-|_2        d.|_3        |ji                          nit>        j                  j                  |||j                  d"d      |j                  d$d      |j                  d%g       |j                  d&g       |j                  d'g       |j                  d(g       |j                  d)g       |
|j                  d*g       |j                  d+g       |j                  d,g       |	|j                  d#      |r|nd d-d/d.0      }	 tk        |jl                        }|j                  d1i       }|rYtq        tr        j                  jA                  d2d34      ju                  d35      ju                  d6      jw                  dd37            }tq        tr        j                  jA                  d8d34      ju                  d35      ju                  d6      jw                  dd37            }ty        d9d:;      \  }} |jz                   d|j|                   }!|j                  |!<      }| j                  |jl                  |!t        |d=      r|j                  nd>t        |      ?      } |D ]!  }"t        || |"t        j                  |d:@       # t        |||j                  A       t	        dBd3idC	      S # tn        $ r}i }Y d }~d }~ww xY w# tn        $ r.}dd lH}# |#j                          t	        ddDidE	      cY d }~S d }~ww xY w)FNra  r  r  r  r  r  r  r)  r   rN  r  r  )rZ  r   r   InternalCareerr  r  r  r  r  rj  rk  rl  rm  rn  r  r-  r   r  r  r   r  r0  r  r  r  r   r4  r  r  r  r  r  r  r  CareerCareer_applyr!  r  r5  r6  Tr7  r9  r;  r=  r?  r@  FrA  rC  rE  rF  rG  rK  r  r  r  r  r  )Jr   r   r   r   r   r_   r   r(   r   r   r  r*  rf  rF  r  rO   r;  r<  r  rz  r}  rI   rC  r~  rj  r>  r	  r  r#   rQ  r  r$   r   rR  rJ   r  r{  r   r4  r  r  r  r  r  r  r  r3  r#  r  r$  r%  r   r   rS  rH  r  rT  r   r  rU  rU   r  r  rV  rW  rE  r   rP   r   rX  rM   r   r  r  r  s$                                       r   career_apply_jobrA    s   @
((1LL$$V,	  )!!(+mm''1M9eV[IJ34 
 '$h/	
 +2299# : 
 "((-- ''++
''/3??LN ))*;<3@@MN##%9: 
 ",/EJ!!m,/0	

 44^D#008&(>>{?O?OP$&::;G	*22@@'mmL9TY__S=QRS=T&]];;!'"$, A 	
	1 .55<<)<T==!&6&=&=&?#9)G:L#M * !7!E!%99%+N	 -:,M,MMM23-
) &/mmJ&CN#,5MM:JB,ON).7mm<NPR.SN+2;--@VXZ2[N/)2}b)IN&+4=="+MN('0}}["'EN$,5MM:JB,ON)&/mmJ&CN#)7N&)3N&4=MMBZ4[N15V1\` D ,4N((6N%! ,33::&mmGR0"z26(}}-=rB!*/A2!F%.]]3I2%N%MM-<'mmOR@*#--R8(}}-=rB"z26&'0}}5M'N9Z5`d&)+ ; J0!>z?S?S!T(,,Y; )-LLV)@DVQW4W0W"W- [t[4*& )-LLV)BdVSW4W0W"W- [t[4)% !33T^c d$-$8$8#99;N;N:O!P!...G{{(22#19@\9Z
 5 5`e #G	 #   6 G# 00"7 % 	$ll	
 D)#66c  f  
	01
 	

s^   B%[  (C/[  E[  1H+[  '[ F[  	[[[  [[   	\)#\\\c           
         t        t        |dg d      }|j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  d	}t        |      S )NrN  r  )ra  r   r   )	r   rg  r  r   r  r   r   rh  ri  )r_   r   r   rg  r  r   r  r   r   rh  ri  r   )r   ra  r  r   s       r   career_job_detailrC    sv     
6	
B \\!55KK~~//#%#=#=$&$?$?
D D>r   c                   "    e Zd ZdedefdZd Zy)SearchPatternResetButtonr	  r  c                     t        j                  dd|t         j                        j                         }t	        j
                  |      S r5  r8  r<  s      r   r>  z.SearchPatternResetButton.extract_json_from_llm  r?  r   c                    |j                   j                  d      }|st        ddid      S t               }ddd}t        j
                  j                  |	      }|j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  d
}t+        j,                  |g      }|j/                  |d   |d          |j1                  |      }| j3                  |j4                        }	t7        |j4                         |	r,|	d   j                  d      |_        |j;                  dg       t        d|	d   j                  d      id      S )Nra  r  r  r)  r   rj  rk  rl  rP  r`  rm  rn  r   ro  rV  r  )r   r   r   rz  r   r   ra  rZ  r   r   r  rb  r   r   rc  rd  re  rf  rg  rh  ri  rI   r|  r}  r~  r>  r	  r  ro  r   )
r   r   ra  r  r  r  r  r|  r  r  s
             r   r   zSearchPatternResetButton.get  s   $$((1W&9:3GG!,/EJ'//33u3= !&&(66$..#,,#.#B#B(66&22 + < <#.#B#B*5*P*P/:/Z/Z%00'44'2'J'J(3(L(L
$ $.#F#FHXGY#Z !!m,.?@Q.R	
 (445MN001E1EFo""#)4Q););<L)MK&,<+=>k!n001AB
 	r   N)r   r   r   rw  rh  r>  r   r   r   r   rE  rE    s    ## #$ #-r   rE  c           
      .   	 | j                   j                  d      }| j                   j                  d      }t        t        |      }t        t        |      }|j
                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  j                  dz   |j                  j                  z   |j                  j                  d	}t!        |      S # t"        $ r t!        ddid	
      cY S w xY w)Nra  rH  r  r  r-  )	r   rg  r  r   r  r   r   rD  candidate_mailr  Data Extraction Failedr  r   )r   r   r_   r   r$   r   rg  r  r   r  r   r   r2  r  r  r  r   r  )r   ra  rH  r  r  r   s         r   interview_job_candidate_detailrK    s    I  )LL$$[1	#

 (
 !**#00#*#>#>((#00".. ' 8 8)66AACG+JbJbJlJll)66<<

 ~ I":;CHHIs   C6C9 9DDc                    	 | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|r|st        ddid      S t        j                  t        |      j                               j                         }t        j                  |t        j                        }|j                         }t        d	d
      \  }}|j                  ||t        j                  dz        }t        |||t        j                   d d
      }	|	rt        ddid      S t        ddid      S # t"        $ r.}
dd l} |j&                          t        ddid      cY d }
~
S d }
~
ww xY w)NrD  rI  rH  r  z Email and Resume ID are requiredr)  r   r  interview_otp_mailFrA  <   )rD  r  r  rK  r   zOTP sent successfullyr  zOTP sent failedr  r   rJ  r  )r   r   r   r  r   rw  r  rq  r  r  r   INTERVIEW_OTP_EXPIRY_SECONDSrm   rU   rV  rP   rX  r  r  r  )r   rD  rI  rH  r  r  r  rc  rd  otp_sentr  r  s               r   interview_send_otprQ  *  sv   .
 ))*:; ))*:;LL$$[1	YW&HIRUVVs9~4467>>@jjx'L'LM779*+?O{{) ==C  
 #$$
 34 
 -. 
  
	./
 	

s+   A#E	 &CE	 :E	 		F #E;5F ;F c                    	 | j                   j                  d      }| j                   j                  d      }|r|st        ddid      S t        j                  t        |      j                               j                         }t        j                  |t        j                        }|j                  |d	      rt        d
did      S t        ddid      S # t        $ r t        ddid      cY S w xY w)NrH  otpr  zResume ID and OTP are requiredr)  r   r  r   r  r   zOTP Verified Successfullyr  zInvalid or Expired OTPzOTP Verification Failedr  )r   r   r   r  r   rw  r  rq  r  r  r   rO  r  r  )r   rH  otp_enteredr  totps        r   interview_verify_otprV  ]  s    JLL$$[1	ll&&u-W&FGPSTTs9~4467>>@zz#(M(MN;;{;3Y(CDSQQ":;CHH J";<SIIJs   AC B C C C65C6c                 N   	 | j                   j                  d      }| j                   j                  d      }|r|st        ddid      S t        j                  j                  ||      j                         }|st        ddid      S t        j                  j                  |d	d
i      \  }}|s4d
|_	        |j                  d	g       d|_	        |j                  d	g       t        |      }t        |j                   d      S # t        $ r t        ddid      cY S w xY w)NrH  ra  r  z Resume ID and JD ID are requiredr)  r   r  zMatched Profile is Missingr   Started)matched_profiler1  rV  Start L0 Interviewr  zL0 Interview Starting Failedr  )r   r   r   r'   r   r   rs  r)   rQ  r   r   r:   r  )r   rH  ra  rY  interview_objr\  r   s          r   l0_interview_startr\  r  sC   %OLL$$[1	  )<= 
 -44;; < 
 %' 	
 67  "1!8!8!F!F+	* "G "
w
 #,M hZ8%9O"  z :.}=

44 O"@A#NNOs   AD	 ?D	 A=D	 	D$#D$c                 r   	 |j                  d      \  }}t        j                  j                  ||      j	                         }|st        ddid      S t        j                  j                  d      j                  |      }t        j                  j                  |	      }|j                  }|j                  j                   d
|j                  j                   j                         }ddh}	h d}
t        |j                  |j                   |j"                  |j$                  |j&                  ||j(                  |j*                  |j,                  |j.                  |j0                  |j2                  |	v |j2                  |
v d      S # t4        $ r"}t        dt7        |      id      cY d }~S d }~ww xY w)NrT  r  r  Invalid tokenr)  r   r2  r  r  r-  rR  
Interested>   rR  r_  Additional Info)r  current_salaryexpected_salaryvisa_statusremarksrD  r   rg  r  countrycurrencyalready_submitted	submitted)r  r'   r   r   rs  r   r$   r  r   r   r[  r2  r  r  r  r  ra  rb  rc  rd  r   rg  r  re  
currenciesr   r  rw  )r   rZ  rH  ra  rR  r  r  r  rD  SUBMITTED_STATUSES	SUBMITTEDr  s               r   shortlist_detailsrl    s   07 ;;s+	5$,,33 4 
 %' 	
 Wo6sCC#++::>JNN O 
 &&***7''"//::;1V=P=P=Z=Z<[\bbd 


	 $22%44&66"..,OO#%#9#9"**#..!(3E!E 94
  	   7#a&)#667s%   AF D4F 	F6F1+F61F6c                 &   	 | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|st        ddid      S t        j                  j                  |      j                         }|r|j                  }|j                  }n'd	|vrt        dd
id      S |j                  d	      \  }}t        j                  j                  ||      j                         }|st        ddid      S t        j                  | d| d      }||_        d|_        |j                          t!        |       t        ddi      S # t"        $ r"}	t        dt%        |	      id      cY d }	~	S d }	~	ww xY w)NrZ  rq  timer  Missing tokenr)  r   rZ  rT  Invalid token formatr  Matched profile not foundr-  z%Y-%m-%d %H:%ML0 Interview Scheduledr   z'Availability saved & recruiter notified)r   r   r   r+   r   r   rs  rH  ra  r  r'   rB   strptimeinterview_datetimer   r   rS   r  rw  )
r   rZ  rq  rn  linkrH  ra  rR  interview_dtr  s
             r   save_candidate_availabilityrx    s~   *7  )||'||'Wo6sCC!!((u(5;;=IJJE %*@ A#NN${{3/Iu$,,33 4 
 %' 	
 W&AB3OO((fAdV

 &2"1&w/$MNOO 7#a&)#667s3   A!E% $AE%  AE% AE% %	F.FFFc                    	 | j                   j                  d      }|st        ddid      S t        j                  j                  |      j                         }|r|j                  }|j                  }n'd|vrt        ddid      S |j                  d      \  }}t        j                  j                  ||	      j                         }|st        dd
id      S t        |j                  |j                  |j                  |j                  |j                   g       }| j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        t"        j                  j                  |      }|j$                  }| j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        | j                   j                  d      |_        |rd|_        |j+                          |j+                  g d       t        d| d      S # t,        $ r"}	t        dt/        |	      id      cY d }	~	S d }	~	ww xY w)NrZ  r  ro  r)  r   rp  rT  rq  r  rr  r  ra  rb  rc  rd  r  r`  )r  ra  rb  rc  notesrV  zDetails saved successfully)r   is_edit)r   r   r   r+   r   r   rs  rH  ra  r  r'   r  r  ra  rb  rc  rd  r$   r2  rz  r   r   r  rw  )
r   rZ  rv  rH  ra  rR  is_first_submissionr_  r  r  s
             r   submit_shortlist_detailsr}     s   D7  )Wo6sCC!!((u(5;;=IJJE %*@ A#NN${{3/Iu$,,33 4 
 %' 	
 W&AB3OO"%!!""##OO'
 #  !( 0 0 A!(!1!12B!C"),,"2"23D"E%ll..}=!,,**95'//33i3H
"//&-ll&6&6&G#'.||'7'78H'I$(/(8(89J(K%$+LL$4$4]$C!%ll..y9.GN *
 	 3..
  	
  7#a&)#667s1   +K	 AK	 
AK	 G*K	 		K4K/)K4/K4c           	      T    ddddddddddd	dd
ddddddddg}t        |      S )Nr  INR)re  rf  zUnited StatesUSDzUnited KingdomGBPCanadaCAD	AustraliaAUD	SingaporeSGDGermanyEURr
   )r   r   s     r   country_currency_listr  I  sO     /#7$%8%0U3U351D D>r   c                    	 | j                   j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }|st        ddid	      S |r|r|st        dd
id	      S t        j
                  j                  |      j                         }|st        ddid	      S t        j
                  j                  |      j                         }|st        ddid	      S t        j                  dd      5 }|j                         D ]  }	|j                  |	        |j                  }
d d d        
j                  dd      }|
j                  dd      }t        j                   j#                  t$        j&                  dd      }d}	 t)        j*                  ddd|
d|dddddddd d!d"d#d$d%d&d'd(d)d*|gd+t(        j,                  t(        j,                  ,       t)        j*                  ddd|d-d.d/d0d1d2d&|gd+t(        j,                  t(        j,                  ,       t/        j0                         }t/        j2                  |      5 }|j5                  |      }d d d        	 |j7                        j9                         }tG        |d5      5 }tI        ||j                  6      }tJ        j
                  jM                  |||||7      }d d d        |
||fD ]7  }t        j                   jA                  |      s#t        jB                  |       9 	 |d9k(  rNd:|_'        tQ        jR                         |_*        |jW                  d;d<g=       d>|_'        |jW                  d;g=       tY              }t        |j                  d?	      S # 1 sw Y   WxY w# 1 sw Y   <xY w# t.        j:                  $ r d}Y 7t.        j<                  $ rd}t        dd3t?        |       id4	      cY d }~|
||fD ]7  }t        j                   jA                  |      s#t        jB                  |       9 S d }~wtD        $ rd}t        dd3t?        |       id4	      cY d }~|
||fD ]7  }t        j                   jA                  |      s#t        jB                  |       9 S d }~ww xY w# 1 sw Y   xY w# tD        $ r[}t        dd8| id4	      cY d }~|
||fD ]7  }t        j                   jA                  |      s#t        jB                  |       9 S d }~ww xY w# |
||fD ]7  }t        j                   jA                  |      s#t        jB                  |       9 w xY w# tD        $ r1}d@d l-} |j\                          t        ddA| id4	      cY d }~S d }~ww xY w)BNvideol0_interviewrY  questionsaving_typer  zVideo file is requiredr)  r   z8Interview ID, Matched Profile, and Question are requiredr  Interview not foundr  Matched Profile not foundFz.webm)r  suffixz_wm.webmz.wav
app_imagesimplementor_logo.webpr  ffmpeg-y-i-filter_complexzW[0:v]scale=640:-2[scaled];[1:v]scale=100:-1[logo];[scaled][logo]overlay=W-w-20:20[outv]-map[outv]z0:a?-c:vz
libvpx-vp9-crf30z-b:v0z-row-mt1-c:alibopus-b:a96kT)checkstdoutstderr-vnz-acodec	pcm_s16le-ar16000-aczAudio Processing failed: r  rb)r  )rY  r  recorded_videor  answerzAudio Processing failed submitr  r   interview_completed_timerV  r/  r  r   zVideo Upload Failed )/r   r   r   r   r)   r   r   rs  r'   tempfileNamedTemporaryFilechunksr:  r  r2  r'  r*  r  r   r)  
subprocessrunDEVNULLsr
Recognizer	AudioFilerecordrecognize_googler  UnknownValueErrorRequestErrorrw  rR  r  r  r9  ro   r*   r   r   r   rm   r  r   r;   r  r  )r   
video_filel0_interview_idmatched_profile_idquestion_textr  r[  matched_profile_obj
temp_videochunktemp_video_pathwatermarked_video_pathtemp_audio_pathimplementor_logoextracted_answer_text
recognizerr.  
audio_datar  r*  r  interview_video_filesession_objr   r  s                            r   l0_interview_record_videor  V  s   K
]]&&w/
!,,**>:$\\--.?@((4ll&&}523 
 &8TU 
 (//66/6JPPRW(;=cJJ088??CU?V\\^"W(ACCPP((gF 	.*#**, (  '( )ooO		. "1!8!8*!M)11'6B77<<(;(;\Kbc "P	$NNd/*%< HFLDCsIE*!$ !))!))+0 NNd0{73# !))!))$ Jo. 7&'..v6
7(2(C(CJ(O(U(U(W% ,d3 	q'+AJOO'D$199@@$7!.#7*0 A 	  )*@/R $77>>$'IIdO$ ("#.M 5=\\^M2h8R-ST)7&$$H:$>2;?

44Q	. 	.p7 7
 '' +(*%??  9#a&BC 4 )*@/R $77>>$'IIdO$-   9#a&BC * )*@/R $77>>$'IIdO$-	 	  	4QC89 
 )*@/R $77>>$'IIdO$	 )*@/R $77>>$'IIdO$  
	,QC01
 	
	
sA  BV V />V .>V -V 3O7AV B8S/ OS/ (O& S/ <S"S/ 'V ?BV OV O#S/ &S;S/ >SQ/*S+S/ /'V V /S;SSS/ 'V V SS/ "S,'S/ /	U8U	U
U 'V 6V UU (V?VV 	W#&W	WWc                    	 | j                   j                  d      }| j                   j                  d      }t        j                  j	                  ||      j                         }|st        ddid      S |j                  dv rt        j                  j	                  |	      j                         }d
}|sd}n:|j                  }t        j                  j	                  |      j                         }t        d||dd      S t        ddid      S # t        $ r1}d
d l} |j                          t        dd| id      cY d }~S d }~ww xY w)Nra  rH  r  r  Fr  r   )rs  r`  rZ  rY  r   zNot Startedr  T)r  status_dataquestions_completedr  zEligibility Check API Failed r  )r   r   r'   r   r   rs  r   r   r)   r*   r  r  r  r  )	r   ra  rH  rY  l0_interview_datar  r  r  r  s	            r   l0_interview_eligibility_checkr    so   1
$$((1((,,[9	,44;; < 
 %' 	
 Z/<<!! &
 
 !0 7 7 > > / !? !eg  #$$+/66&9&A&A&H&H!2 'I '%' $  $#.+>
   U+C88 
	5aS9:
 	

s+   A5D 8BD D 	E&E
E
Ec                   d     e Zd ZegZeZej                  j                         Z
g dZ fdZ xZS )L0InterviewListViewSet)r   r  r  c                     t         |          }| j                  j                  j	                  d      }|r|j                  |      }|S )NrY  r  rU  )r   r   rY  r   s      r   r   z#L0InterviewListViewSet.get_queryset#  sC    7'),,33778IJGHr   )r   r   r   r   ri   r:   r   r)   r   r   r   http_method_namesr   r   r   s   @r   r  r    s6    )*0&&**,H2 r   r  c                   R    e Zd ZegZeZej                  j                         Z
ddgZy)L0InterviewSessionsViewSetr   r  N)r   r   r   r   ri   r;   r   r*   r   r   r   r  r   r   r   r  r  -  s0    )*4"**..0H)r   r  c                     t         j                  j                  |       d   }|dz   }ddd| dddd	d
dd|g}t        j                  |t        j
                  t        j
                  d       |S )Nr   z	_temp.wavr  r  r  r  r  r  r  r  r  r  T)r  r  r  )r'  r*  r=  r  r  r  )
video_pathbasewav_pathcmds       r   convert_video_to_wavr  4  ss    77J'*Dk!H 	$jswC NN3z11*:L:LTXYOr   c                    t        |       }dd|dddddg}t        j                  |t        j                  t        j                  d	      }|j                  }g }g }|j                  d
      D ]  }d|v r:|j                  t        |j                  d      d   j                                      d|v sF|j                  t        |j                  d      d   j                  d      d   j                                       t        j                  j                  |      rt        j                  |       |sy|d   }	|r|d   nd }
t        d|	|z
        }|
r|
|z   nd }||fS )Nr  r  z-afzsilencedetect=n=-20dB:d=0.5z-fnull-T)r  r  r	  r+  silence_startzsilence_start:r   silence_endzsilence_end:|r   r  r  )r  r  r  PIPEr  r  r  r  r  r'  r*  rR  r  r  )r  buffer_secondsr  r  r#  outputsilence_startssilence_endsr/  speech_start
speech_end
trim_starttrim_ends                r   get_speech_trim_pointsr  G  sa   #J/H 	h,fC ^^	F ]]FNLT" d"!!%

3C(DQ(G(M(M(O"PQD djj0399#>qAGGIJ 
ww~~h
		(   ?L (6#4J Q~56J.8zN*dHxr   c                    t         j                  j                  t        j                  dt        | j                  j                              }t        j                  |d       g }t         j                  j                  t        j                  dd      }t         j                  j                  t        j                  dd      }	 t        |d      D ]  \  }}t         j                  j                  |d	| d
      }	t        |j                  d      }
|
j                  |	       t         j                  j                  |d	| d      }t        j                  g ddddd|d|	d|ddddddddddddddd d!d"d#d$d%d&d'd(d)d*|       |j                  |       |j                   st         j                  j                  |d+| d,      }t#        |j                   j                  t        j$                  -      \  }}ddd|j                   j                  d.t        |      g}|r|d/t        |      gz  }|d0d1dddd d#d$d%d&d'd(|gz  }t        j                  |d2       |j                  |        |}t         j                  j                  ||      }g }g }t        |      D ]$  \  }}|d|gz  }|j                  d3| d4| d5       & d6j                  |      d7t'        |       d8z   }ddg|d|dddd9dddd:dd;dd d!d<d#d$d%d&d'd(d)d*|}t        j                  |       t)        |d=      5 }| j*                  j                  |t-        |             d d d        | j                          | j*                  j.                  t         j                  j1                  |      rt3        j4                  |d>       S S # 1 sw Y   fxY w# t6        $ r}|d }~ww xY w# t         j                  j1                  |      rt3        j4                  |d>       w w xY w)?Ntemp_l0Tr  r  zAI_profile_image.pngr  r   )startqz.mp3en)r	  langz
_video.mp4r  r  z-loopr  r  r  z[0:v]scale=500:500:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2:color=white[bg];[2:v]scale=100:-1[logo];[bg][logo]overlay=W-w-20:20[outv]r  r  z1:az	-shortestr  libx264z-presetmediumr  23r  aacr  128kr  44100r  2z-pix_fmtyuv420pz	-movflagsz
+faststartansz_trimmed.mp4)r  z-ssz-toz-vfzscale=1280:720,fps=30)r  [z:v:0][z:a:0]r  z	concat=n=z:v=1:a=1[outv][outa]z[outa]fastr  r  r  )ignore_errors)r'  r*  r  r   r)  rw  rY  r  r(  	enumeratern   r  r   r  r  r  r  r  &INTERVIEW_VIDEO_SILENCE_BUFFER_SECONDSr   r9  interview_videoro   rf  rR  shutilrmtreer  )r  l0_interview_sessionsvideo_file_nametemp_dir
clip_pathsai_profile_imager  idxsessionquestion_audio_pathttsquestion_video_pathtrimmed_answer_pathr  r  ffmpeg_trim_cmdfinal_video_namefinal_video_pathffmpeg_inputsfilter_partsr%  clipconcat_filtercommandr  r  s                             r   !generate_l0_final_interview_videor  }  s   ww||H//C@Q@a@a@d@d<efHKK4(Jww||H$7$7G]^ww||H$7$7G^_q8%&;1E B	7LC"$'',,x1SE"GG,,48CHH()"$'',,x1SE9L"MNN   ' 	 *	
 
 ' "4  !     "  $  !  !" #" #$ %$ %& '& '( )( )* +* &+, -, *-. $/ 4 12%%&(ggllC55'# (>**//#+#R#R($
H d'00553z?# #s8}'==O2IE73	'$  d;!!"56EB	7F +77<<2BC , 	7GAtdD\)M!A3fQCu 56	7
 GGL!#j/**>?@ 	 d

 
  -
 	
 	

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 "
 
 &
 
$ 	w"D) 	NQ--223CT!WM	N 	  0044 77>>(#MM($7 $	N 	N   77>>(#MM($7 $sD   C>O7 FO7  &O+-O7 +O40O7 7	P PPP
 
9Qc                 L   t               }ddd}|j                  |d   |d          g }| D ],  }|j                  |j                  d}|j	                  |       . t        j                  |      }|j                  |      }t               j                  |j                        }|S )Nrj  rk  rl  rm  rn  )r  candidate_answer)rz  r}  r  r  r  rI   l0_interview_summary_promptr~  rj  r>  r	  )	r	  r  r  interview_datar   rH  r  r  r  s	            r   generate_l0_summary_reportr!    s    KM(+F-(+,
 N% % $
 	e$% 33NCF,,V4K"$::;;K;KLKr   c           	         	 | j                   j                  d      }| j                   j                  d      }t        j                  j	                  ||      j                         }|st        ddid      S t        j                  j	                  |      j                         }|st        dd	id      S t        j                  j	                  |
      j                  d      }t        ||d| d| d      }t        |      }||_        |j                  dg       d|_        ||_        |j                  ddg       t         j                  j                  |      }||_        ||_        |j                  ddg       t        ||d      S # t$        $ r7}	dd l}
 |
j(                          t        dt+        |	      id      cY d }	~	S d }	~	ww xY w)Nra  rH  r  r  r  r  r   r  r  r  r  L0_jd__resume_z_final_video.mp4r  rV  Generated L0 reportl0_interview_summaryr   rP  r  )summary_responsefinal_videor   r  )r   r   r'   r   r   rs  r   r)   r*   r   r  r!  r  r   r   r&  r$   r  r  r  r  rw  )r   ra  rH  rY  r  r	  final_video_urlfinal_summary_reportcandidate_resumer  r  s              r   l0_interview_final_summaryr,    s   27$$((1((,,[9	,44;; < 
 %' 	
 W&AB3OO+33::+ ; 

%' 	 !W&;<SII 3 ; ; B B* !C !

(4. 	 <!UG8I;.>?
  ::OP,@).?-@A!6/C,,BH+MN-5599Y9G->*0D--@BX,YZ 4*
  	
  7	#a&)#667s+   A5F 8>F 7CF 	G,GGGc                   R    e Zd ZegZeZej                  j                         Z
ddgZy)TblMatchedProfilesViewSetr   r  N)r   r   r   r   ri   r<   r   r'   r   r   r   r  r   r   r   r.  r.  I  s0    )*3!))--/H)r   r.  c                    | j                   j                  d      }| j                   j                  d      }|st        d      S 	 t        j                  |      }t        j                         j                         |d   kD  rt        d      S |j                  d      }|st        d| d	      S ||k7  rt        d
| d      S |d   }|j                  t        j                  d      j                  d      }t        j                  j!                  t        j"                  |      }t        j                  j%                  |      st        d      S t'        t)        |d            S # t        j
                  $ r t        d      cY S w xY w)NrZ  passwordz
            <h3>Invalid Access Link</h3>
            <p>The access link is missing or incomplete.</p>
            <p>Please use the original link shared with you or contact the administrator.</p>
        z
            <h3>Invalid or Tampered Link</h3>
            <p>This link appears to be invalid or has been modified.</p>
            <p>Please ensure you are using the correct link provided in the email.</p>
        expz
            <h3>Link or Password Expired</h3>
            <p>This secure access link has expired.</p>
            <p>Please contact the administrator to request a new access link.</p>
        pwda  
        <html>
        <head>
        <script>
            function askPassword(message) {
                var pwd = prompt(message || "Enter password to access this file:");
                
                if (pwd === null || pwd.trim() === "") {
                    alert("Incorrect password!");
                    askPassword("Enter password to access this file:");
                } else {
                    window.location.href = "?token=z&password=" + encodeURIComponent(pwd);
                }
            }

            askPassword();
        </script>
        </head>
        </html>
        ao  
        <html>
        <head>
        <script>
            function retry() {
                var pwd = prompt("Incorrect password! Please try again:");

                if (pwd === null || pwd.trim() === "") {
                    alert("Incorrect password!");
                    retry();
                } else {
                    window.location.href = "?token=z&password=" + encodeURIComponent(pwd);
                }
            }

            retry();
        </script>
        </head>
        </html>
        r  r  /zFile not found.r  )r  r   rr   rp   r;  BadSignaturer   rm   	timestamprs   r2  r   rB  lstripr'  r*  r  r)  rR  rq   r9  )r   rZ  entered_passwordr   correct_passwordr  relative_path	full_paths           r   secure_file_viewr;  P  s   KKOOG$E{{z2$ &  	}}U# ||~!DK/$ &  	 xx !4 5:7 ;	  	* ++ !4 5:7 ;	  	* [!I%%h&8&8"=DDSIMX00-@I77>>)$$%677Y-..I  $ &  	s   E$ $FFc                        e Zd ZegZddZd Zy)ClientSubmissionDatac                 ~   |syt        |d      r|j                  }nt        |t              r|}nydj	                  d t        d      D              }t        j                         t        |      z   j                         }|||d}t        j                  |      }t        d      }	|j                  |	 d	|       |fS )
N)r  r  rf  r  c              3      K   | ]8  }t        j                  t        j                  t        j                  z          : y wr   )secretsr  stringascii_lettersdigits)r  rT  s     r   r  zAClientSubmissionData.generate_secure_file_link.<locals>.<genexpr>  s)     bTU7>>&*>*>*NObs   >A r  r  )r  r1  r2  secure_filez?token=)rW  rf  ro  rw  r  r"  r   rm   rC   r5  rp   dumpsrt   build_absolute_uri)
r   r   file_objvalidity_daysr  r0  expiryr  rZ  
secure_urls
             r   generate_secure_file_linkz.ClientSubmissionData.generate_secure_file_link  s    8U# I#& I77bY^_`Yabb,,.9-#@@KKM #
 g&]+
))ZLw*GH(RRr   c                    |j                   j                  d      xs g }|j                   j                  d      xs g }|j                   j                  d      xs g }|j                   j                  d      xs g }t        j                  j                  |j                   j                  d            }|j                  }|j
                  }|j                   j                  d      }	t        j                  j                  |	      }
|j                   j                  dd	      }|j                   j                  d
d	      }dj                  |      }dj                  |      }dj                  |      }d}t        j                  j                  |
j                        j                  }t        j                  j                  |
      j                         }|r|j                  nd }| j                  |||      \  }}| j                  |||      \  }}d	j                  d |D              }d| d| d| d| d| d| d}d| d| d|j                   j                  dd	       d|j                   j                  dd	       d|j                   j                  dd	       d|j                   j                  d d	       d!|j                   j                  d"d	       d#|j                   j                  d$d	       d%|j                   j                  d&d	       d'|j                   j                  d(d	       d)|j                   j                  d*d	       d+|j                   j                  d,d	       d-|j                   j                  d.d	       d/|j                   j                  d0d	       d1}d2| d3| d4| d5| d6| d7}d8| d9| d9| d:}d;| d<| d=| }t!        |||t"        j$                  d d>?      }|r|d@|
_        |
j)                  dAgB       t*        j                  j                  |
j,                  C      }|j.                  dDv rdE|_        |j)                  dFgB       t1        d>dGdHdIJ      S t1        dKdLdHdMJ      S )NN	strengthsr4  r  tools_frameworksr   rP  r  r  r  position_appliedr     r  )rH  c              3   (   K   | ]
  }d | d  yw)z<li style="margin-bottom:4px;">z</li>Nr   )r  strengths     r   r  z,ClientSubmissionData.post.<locals>.<genexpr>  s!      !
 .hZu=!
s   z`
            <p>Dear,</p>
            <p>
                I am pleased to submit the profile of z%
                for the position of z.
            </p>
            <p>
                The candidate demonstrates strong capabilities with the following notable strengths:
            </p>
            <ul style="margin-top:5px; margin-bottom:15px;">
                zN
            </ul>
            <p>
                The Primary Skills include zh. 
            </p>
            <p>
                The Candidate also possesses Technical Expertise in z.
            </p>
            <p>
                The Candidate also has experience working with tools and frameworks such as z.
            </p>
        a<  
            <table border="1" cellpadding="4" cellspacing="0" width="100%" 
                style="border-collapse: collapse; font-size:14px;">
                
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Full Name</strong></td>
                    <td style="padding:6px;">z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Position Applied</strong></td>
                    <td style="padding:6px;">z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Current Location</strong></td>
                    <td style="padding:6px;">current_locationz</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Mobile Number</strong></td>
                    <td style="padding:6px;">r+  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Email</strong></td>
                    <td style="padding:6px;">r  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Total Experience</strong></td>
                    <td style="padding:6px;">total_experiencez</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Relevant Experience</strong></td>
                    <td style="padding:6px;">relevant_experiencez</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Domain Experience</strong></td>
                    <td style="padding:6px;">domain_experiencez</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Current Salary</strong></td>
                    <td style="padding:6px;">ra  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Expected Salary</strong></td>
                    <td style="padding:6px;">rb  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Notice Period</strong></td>
                    <td style="padding:6px;">r  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Visa Status</strong></td>
                    <td style="padding:6px;">rc  z</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>Nationality</strong></td>
                    <td style="padding:6px;">nationalityz</td>
                </tr>
                <tr>
                    <td style="width:30%; padding:6px;"><strong>LinkedIn ID</strong></td>
                    <td style="padding:6px;">r  z9</td>
                </tr>
            </table>
        aI  
            <p>
                For your review, please find the candidate's detailed resume and pre-screening interview recording below:
            </p>

            <p style= font-size:14px;">
                <strong>Note:</strong> These secure links are password protected and will remain valid for 
                <strong>z days</strong> from the date of this email.
            </p>

            <ul style="margin-top:10px; margin-bottom:15px;">
                <li>
                    <strong>Resume:</strong> 
                    <a href="z" target="_blank" style="color:#1a73e8;">
                        Click here to view the resume
                    </a>
                    <br/>
                    <strong>Password:</strong> z
                </li>

                <br/>

                <li>
                    <strong>Pre-Screening Interview Video:</strong> 
                    <a href="z" target="_blank" style="color:#1a73e8;">
                        Click here to watch the interview
                    </a>
                    <br/>
                    <strong>Password:</strong> z
                </li>
            </ul>

            <p>
                Kindly review the above details and let us know your feedback or if any additional information is required.
            </p>
        z?
            <html>
                <body>
                    z.
                    <br>
                    z5
                </body>
            </html>
        zProfile Submission: z for z | TrK  zL0-Submission to Clientr   rV  r  )r$  rY  r  r  zClient Submitionr\  zProfile Submitted Successfully)r  r   r  r   FzProfile Submitted Failedr  )r   r   r   r   r  r  r'   r  r$   rH  r#  r)   r   rs  r  rK  rP   r   rX  r   r   r   r  r\  r   ) r   r   rM  r4  r  rN  r  client_mailr  r  matched_profile_datarD  rO  primary_skills_texttechnical_skills_text
tools_textrH  r#  r[  interview_fileresume_linkresume_passwordinterview_video_linkvideo_passwordstrengths_htmlsummary_sectiontable_sectionlink_sectionsummary_email_bodysummary_email_subjectmail_submissionr  s                                    r   r  zClientSubmissionData.post  sv   LL$$[17R	 ))*:;Ar"<<++,>?E2"<<++,>?E2''++w||/?/?/I+J!66!--\\%%l3
199===L ))+r:"<<++,>C"ii7 $		*: ;YY/0
(0044#-- 5 

+ 	 (//660 7 

%' 	 ;H66T'+'E'Eg{jw'E'x$_/3/M/MgWe  vC/M  0D,n !
%!
 

7 8F6F G%%5$6 7    !, -@+@ AE F[D[ \] ^h\h i	'.. /=-= >. /?-? @. /6ll.>.>?QSU.V-W X. /6ll.>.>PR.S-T U. /6ll.>.>w.K-L M. /6ll.>.>?QSU.V-W X. /6ll.>.>?TVX.Y-Z [. /6ll.>.>?RTV.W-X Y. /6ll.>.>?OQS.T-U V. /6ll.>.>?PRT.U-V W. /6ll.>.>PR.S-T U. /6ll.>.>}b.Q-R S. /6ll.>.>}b.Q-R S. /6ll.>.>}b.Q-R S	u=~ ' ( *] +0 1@/@ A 33 40 1?/? @	9#J" %% &"O $!N #	
 #>"2%8H7I[MZ 	 **?ASU`bjbzbz  GK  UY  Z*C ' %%XJ%?"**..5I5P5P.QB{{JJ0zl3;  
 5  r   N)rP  )r   r   r   r   ri   rK  r  r   r   r   r=  r=    s    )*S4Jr   r=  c                     t         j                  j                  |      j                         }|st	        ddid      S t	        |j
                  |j                  |j                  d      S )Nrp  r  r^  r  r   )rH  ra  	link_type)r+   r   r   rs  r   rH  ra  rj  )r   rZ  rv  s      r   resolve_public_tokenrk    sb     $$5$1779D/23??^^^^  r   c                   P    e Zd Zd Zd Zd Zd ZddZd Zd Z	d Z
d	 Zd
 Zd Zy)!IdentifyMatchingProfilesCandidatec                    |sy|j                         j                         }t        j                  dd|      j                         }t        j                  d|      }|r4t        |j                  d            t        |j                  d            fS t        j                  d|      }|r*t        |j                  d            t        j                  fS t        j                  d|      }|r*t        |j                  d	            t        j                  fS t        j                  d
|      }|rdt        |j                  d	            fS t        j                  d|      }|r*t        t        |j                  d                  }||dz   fS yr	  r  r  s       r   r  z9IdentifyMatchingProfilesCandidate.normalize_jd_experience  r  r   c                    syj                         j                         t        fddD              ryt        fddD              ryt        j                  dd      t        j                  d	d      j                         t        j
                  d
      }|syt        |j                  d            }t        |      S )r  Nc              3   &   K   | ]  }|v  
 y wr   r   r  s     r   r  zSIdentifyMatchingProfilesCandidate.normalize_candidate_experience.<locals>.<genexpr>  r  r  r  r   c              3   &   K   | ]  }|v  
 y wr   r   r  s     r   r  zSIdentifyMatchingProfilesCandidate.normalize_candidate_experience.<locals>.<genexpr>  r   r  r!  r  r  r#  r  r   r$  r%  s    `  r   r&  z@IdentifyMatchingProfilesCandidate.normalize_candidate_experience  r'  r   c                     | j                  |      \  }}| j                  |      }||y||k  ry||t        j                  k7  r||kD  ryyr)  r*  r+  s         r   r1  z0IdentifyMatchingProfilesCandidate.is_years_match  r2  r   c                 >    |j                         j                         S r   r4  r5  s     r   r7  z1IdentifyMatchingProfilesCandidate.normalize_skill  r8  r   c           	         | j                  |      }|D ]  }| j                  |      }|j                         D ]$  \  }}|D ]  }|| j                  |      k(  s|} $ & ||k(  r yt        j                  j	                  t        t        |      t        |                  j                  d      d d }	|	r|	d   d   nd }
|
|
|k\  rt        d| d        yt        d| d        t        d	       y
)NTr<  r  r   r   r=  r>  r?  r@  F)	r7  r  r$   r   rg  rk   r[   r  r  )r   candidate_skillrV  rC  rD  r6  rF  rG  rH  rI  rJ  s              r   rK  z-IdentifyMatchingProfilesCandidate.skill_match  s>   ..? $	E((/E*;*A*A*C &
J' E 4 4U ;; * ' #**)eo.   r#  ;H}Q/6T+0@DU0U,-Q0 +,A/C$	L 	67r   c                 *   | j                  ||d   |      rt        d       y| j                  ||d   |      rt        d       y| j                  ||d   |      rt        d       y| j                  ||d   |      rt        d	       yt        d
       y)Nr4  rM  Tr  u#   ✅ MATCH FOUND IN SECONDARY SKILLSr  u   ✅ MATCH FOUND IN KEYWORDSr  u   ✅ MATCH FOUND IN SOFT SKILLSrN  F)rK  r  )r   ru  jd_search_patternrC  s       r   !candidate_skill_matches_candidatezCIdentifyMatchingProfilesCandidate.candidate_skill_matches_candidate  s    O->?O-PRcd56O->?Q-RTef78O->z-JL]^/0O->}-MO`a23;<r   c                 b   |sdddddS d}|D ]m  }| j                  |      }|j                         D ]$  \  }}|D ]  }	|| j                  |	      k(  s|} $ & | j                  ||j                  |      si|dz  }o t	        |      }
||
z  dz  }||
z  }||
t        |d      t        |d      dS )Nr   r  rQ  r   r  r  rS  )r7  r  rx  ro  r   r  )r   rB  r  rC  rR  ru  normalized_candidate_skillrF  rG  rH  rS  rT  rU  s                r   rX  z7IdentifyMatchingProfilesCandidate.calculate_skill_score.  s    !	  / 		O)-)=)=o)N&*;*A*A*C &
J' E1T5I5I%5PP*4 55ovG\G\^op1		 $%o,
% 
A.5!_	
 	
r   c                    |j                         D ci c]  \  }}||j                  |      || }}}|syt        |j                               }|j                         D ci c]  \  }}|||z   }}}d}|j                         D ]4  \  }}||   }	t	        |	t
              r|	rdnd}
nt        |	      }
||
|z  z  }6 t        |d      S c c}}w c c}}w rZ  r[  r]  s              r   rg  z9IdentifyMatchingProfilesCandidate.calculate_overall_scoreO  rh  ri  c                    |sy|j                         j                         }|dv ryt        j                  dd|      }d}d|v sd|v sd|v rd	}nd
|v sd|v sd|v rd}nd|v rd}t        j                  d|      }|sy|D cg c]  }t        |      |z   }}d|v sd|v sd|v r|d   t        d      fS d|v sd|v sd|v rd|d   fS t        |      dk\  rt        |      t        |      fS |d   |d   fS c c}w rk  rx  r{  s         r   r  z.IdentifyMatchingProfilesCandidate.parse_salaryl  r  r  c                 v    | j                  |      \  }}| j                  |      \  }}||y ||k  xs ||kD   S r   r  r  s          r   r  z.IdentifyMatchingProfilesCandidate.salary_match  r  r   c           	      T   |j                   j                  d      }|st        ddid      S i }t        j                  j                  |      }|j
                  }|j                  }|j                  }|j                  }|j                  }	|j                  }
|j                  }|j                  }t        j                  j                  |      }t               }|D ]  }|j                   dk7  st#        |j$                        }d|j                   i||<   |j                   d	k(  r#|j&                  j                  d
d      ||   d<   nd||   d<   |j)                  |        t*        j                  j-                  d      j-                  |      }i }t.        j0                  j3                  t4        j6                  dd      }t9        |dd      5 }t;        j<                  |      }d d d        g }|D ]X  }t#        |j>                        }| jA                  |jB                  |jD                        }|sA|jG                  |       ddi||<   Z |r|r|dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d<     nY|D ]T  }t#        |      }||vri ||<   t*        j                  j                  |      }| jI                  |||      }|d   ||   d<   V |r|r|dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d<     nY|D ]T  }t#        |      }||vri ||<   t*        j                  j                  |      }| jI                  |||      }|d   ||   d<   V |D ]  }t#        |      }||vri ||<   d ||   d<     t*        j                  j                  |      j-                  d       jK                  tM        tO        d      |            jK                  dtO        d       z
  !      j                  d"#      }|D ]B  }t#        |j>                        }|jP                  xs d$}tS        tU        |      d%      ||   d<   D |r|r|dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d&<     n|D ]  }t#        |      }||vri ||<   t*        j                  j                  |      }d'}|D ](  }| jW                  ||jX                  d&   |      s$|dz  }* t[        |      }||z  d(z  } ||z  }!||tS        | d)      tS        |!d%      d*}"|"d   ||   d&<    |r|	r|	dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d+<     n|D ]  }t#        |      }||vri ||<   t*        j                  j                  |      }d'}#|	D ](  }| jW                  ||jX                  d,   |      s$|#dz  }#* t[        |	      }$|#|$z  d(z  }%|#|$z  }&|#|$tS        |%d)      tS        |&d%      d*}'|'d   ||   d+<    |r|
r|
dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d-<     n|
xs g D (cg c]  }(d.|(v r
|(d.   r|(d.    })}(|D ]  }t#        |      }||vri ||<   t*        j                  j                  |      }d'}*|)D ](  }+| jW                  |+|jX                  d-   |      s$|*dz  }** t[        |)      },|*|,z  d(z  }-|*|,z  }.|*|,tS        |-d)      tS        |.d%      d*}/|/d   ||   d-<    |r|r|dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d/<     nh|D ]c  }t#        |      }||vri ||<   t*        j                  j                  |      }d"}0| jW                  ||jX                  d/   g|      rd}0|0||   d/<   e |r|r|dk(  r$|D ]  }t#        |      }||vri ||<   d ||   d0<     nd|D ]_  }t#        |      }||vri ||<   t*        j                  j                  |      }|jX                  d0   }1| j]                  |1|      }2|2||   d0<   a d1d2d3d3d4d4d4d4d5}3|j_                         D ]6  \  }4}5| ja                  |5|3      |5d6<   |5j                  d      r-d|5d<   d|5d<   8 tc        te        |j_                         d7 d8            }t        |t[        |      |d9      S # 1 sw Y   xY wc c}(w ):NrH  r  zresume_id is requiredr)  r   r  rR  r   r%  r  r  r   r  r   r  r  r  rl  r  r6  Tr  r4  r  rU  r  r   )r]  r]  r  r   r  r  Fr  r  rS  r  r   r  r  rQ  r  rc  r  r  r   r  g333333?rj  g?r  )r4  r   r  r  r  r  r   r  re  c                 ,    | d   j                  dd      S r  r  r  s    r   r  z8IdentifyMatchingProfilesCandidate.post.<locals>.<lambda>  r  r   r  )rH  total_jdr5  )3r   r   r   r$   r   r$  r  r4  r  r  r  r   r  r'   r   r  r   rw  r  r&  addr   r  r'  r*  r  r   r  r9  r:  r  ra  r1  r  r"  r  rX  rg  rj   rZ   r  r  r  rK  ro  r   r  r  rg  rh  r  )6r   r   rH  r  r  !resume_responibilities_embeddingsresume_technical_skillsresume_primary_skillsresume_tools_and_frameworksresume_domain_skillsresume_educationresume_locationresume_salarymatched_profilesprocessed_job_idsr  ra  r  rC  r  r  eligible_jd_idsr  r  r  r  technical_skills_scoresimilarity_jdsr  r  r6  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rT  r^  s6                                                         r   r  z&IdentifyMatchingProfilesCandidate.post  s
   LL$$[1	W&=>sKK(0044y4I,7,a,a)"-">"> + : :&1&F&F#*88&00%..#**-55<<y<QE' 
	-G~~*GNN+gnn" >>%::292N2N2R2RSprt2uGEN;/24GEN;/!%%e,
	- $++33 4 

''  
 	 GGLL!2!2I?RS	)S73 	-q $		!	-  	BME#22&&$$ 
  &&u- !$"	 (,A_,T) <BGEG+)+7;GEN#34	< * TBGEG+)+.66:::DF*.*D*D-)+' 8K77SGEN#34T *.C.V) >BGEG+)+9=GEN#56	> * YBGEG+)+.66:::DF-1-G-G/).* :PPW9XGEN#56Y " 	6BGEG#!#15GEN-.		6 %%VoV.W4W8X'565   X,-*,=   VV> 	  ! 	BME77>3J16j!12GEN-.		 .2MQ`2`) BBGEG+)+=AGEN#9:	B * ZBGEG+)+.66:::DF*+'!< 5++E63H3HI_3`bst/14/5 #&&A"BK(;k(IS'P$"5"CK $7!,&+,<a&@!&{A!6	+' >QQX=YGEN#9:1Z4 '+??+R) ;BGEG+)+6:GEN?3	; * YBGEG+&(.66:::DF+,(!5 6++E63H3HI^3_ars0A506 $'';#<L)=)LPS(S%#7,#FL $8!-&+,=q&A!&|Q!7	1- 7PPW6XGEN?31Y4 #'7?'J) 7BGEG+)+26GEN;/	7 "2!7R'4'DN N'# ' * XBGEG+)+.66:::DF./+%8 9	++Iv7L7L[7Y[lm3q839 '**=&>O,Co,UY\+\(&=&OO $;!0&+,@!&D!&!:	40 3Ow2WGEN;/1X4 "o&H) 6BGEG+)+15GEN:.	6 * @BGEG+)+.66:::DF%*N''&:O:OPZ:[9\^op)-1?GEN:.@  M_$D) :BGEG+)+59GEN>2	: * BBGEG+)+.66:::DF & 5 5n EI#00MJH9AGI&~6B " # $$("!	
 %]]_ 	-MAz*.*F*FzS_*`J'>>(+'0
8$*,
;'		- @
 "G
  	a	- 	-X's   b!b%b"Nr  )r   r   r   r  r&  r1  r7  rK  rx  rX  rg  r  r  r  r   r   r   rm  rm    s>    $L6%*X(
B':0&d6|r   rm  c                     t               }d| i}t        dt        fd|i      }|j                  |      }|j                  S )NrH  r  r   )rm  r  r	  r  r   )rH  r
  r  r  r  s        r   rS  rS    sL    ,.DYG y+ L yy&H==r   c                 v   | j                   j                  d      }| j                   j                  dd      }|sOt        j                  j	                  |      }|D ]K  }t
        j                  j	                  |j                        j                         r<|j                          M i }|D ]  }|j                  dk(  rct        j                  j	                  ||j                        j                         }|j                  j                  dd	      ||j                  <   ud ||j                  <    |j                         rst        |d
      }|j                   }	|	D ]  }
|j                  |
d         |
d<    t        d|	d      S t        j                  j	                  |      j                          t        |      }|j                  di       }|st        j                  j	                  |      }i }|D ]  }|j                  dk(  rct        j                  j	                  ||j                        j                         }|j                  j                  dd	      ||j                  <   ud ||j                  <    t        |d
      }|j                   }	|	D ]  }
|j                  |
d         |
d<    t        d|	d      S t!        |j#                         d d
      }i }|D ]  \  }}|j                  d      ||<   t        j                  j	                  ||      j                         rJt        j                  j%                  |||j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  dd
      |j                  dd	      	        t        j                  j	                  |      }t        |d
      }|j                   }	|	D ]$  }
t'        |
d         }|j                  |      |
d<   & t        d|	d      S )NrH  r  Fr  r  r%  r  r  r  Tr  ra  r   r  r  r5  r  c                     | d   d   S r  r   r  s    r   r  z0get_matched_profiles_candidate.<locals>.<lambda>1  r  r   r  )rH  ra  re  r   r4  r  r   r  r6  r   )	rH  ra  re  r  technicalskill_scorer!  r"  r#  r   )r   r   r,   r   r   r   ra  rR  r  r   r'   rs  r&  r=   r   rS  r  r  r   rw  )r   rH  r  r'  r(  l0_result_mapr   rY  r   response_datar  r`  r5  	sorted_jdra  rM  r*  s                    r   get_matched_profiles_candidater    s     -I<<##NE:L.66==	=R 	A$,,33!''3BIIK
	  	1D{{33'9'A'A'H'HS\eieoeo'H'p'v'v'x$,@,U,U,Y,YZwy{,|djj),0djj)	1 ??>xdSJ&OOM% E$1$5$5d7m$D[!E %  
 	$++22 	3 	

&(29=N  B/G.66==	=R 	1D{{33'9'A'A'H'HS\eieoeo'H'p'v'v'x$,@,U,U,Y,YZwy{,|djj),0djj)	1 ;8$O
"! 	AD - 1 1$w- @D	A !
  	
 +I M 
q uu[1e&..55 6 
 &( #++22%%3 uu%5q9!"'91!=#$55);Q#?!"'=q!AUU<6552& 	3 
	

* 188??)?TN6~DQJOOM 5DM")--e4[5   r   c                     t        t        |      }| j                  j                  dd      j	                         t        fddD              rt        | dd|i      S t        d|j                         S )	N)r:  HTTP_USER_AGENTr  c              3   &   K   | ]  }|v  
 y wr   r   )r  bot
user_agents     r   r  z#public_job_share.<locals>.<genexpr>_  s     
i3*
ir  )facebookexternalhitlinkedinbot
twitterbotwhatsappzmasters/public_job_share.htmljob,https://career.itconnectus.com/career/apply/)	r_   r   METAr   rF  r  r`   r^   ra  )r   rZ  r  r  s      @r   public_job_sharer  Z  sj    
-E
BC!!"3R8>>@J

i(h
iig>MMB399+NOOr   c                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j                  |      }|j	                  d      D cg c]  } |j
                          }}t        dd	      \  }}	|j                  |
      }|	j                  ||j                  |j                  d|j                         }	t        ||	t        j                  |d       t        j                  j                  ||||       t!        ddi      S c c}w )Nr  friend_emails	your_name
your_emailr  rB  job_share_emailFrA  )r  r  )r  r   rg  r  )fail_silently)r  r  r  r  r   z
Email sent)r   r   r   r   r  r  rU   rV  r   rg  ra  ru   r   rX  r-   r   r   )
r   r  r  r  r  r  r  
email_listrc  r   s
             r   email_share_jobr  d  sO    \\h'FLL$$_5M  -I!!,/J

#
#
'
'f
'
5C-:-@-@-EFE+%++-FJF)*;UKGWnn  G nn--%%@L	  G      #	 !  Y-..= Gs    E c                 
	   	 | j                   j                  d      }| j                   j                  d      }t        j                  j	                  ||      j                         }|j                  dk(  rt        ddid      S t        j                  j	                  d	|d
      j                  d      j                         }|j                  j                  }t        j                  j                  t        j                  dt!        |j"                              }t        j$                  |d       t        j                  j'                  |      d   }d| d| d| }t        j                  j                  ||      }	t        j                  j)                  |      rt+        j,                  ||	       t        j                  j                  dt!        |j"                        |      }
t        j                  j/                  |j                  |j0                  |j2                  ||
      }t4        j                  j	                  |      j                  d      }|D ]X  }|j6                  j                  }t        j                  j9                  |      }t        j                  j'                  |      d   }d|v r+|j;                  dd      d   }d|j=                  |d      z   }nd}d| d| | | }t        j                  j                  ||      }t        j                  j)                  |      rt+        j,                  ||       t        j                  j                  dt!        |j"                        |      }t4        j                  j/                  |j>                  |j@                  |||       [ d|_        |j0                  |_!        |jE                  ddg       tF        j                  j                  |      }||_$        |j0                  |_!        |jE                  ddg       t        ddid      S # tJ        $ r:}d d l&} |jN                          t        dd!t!        |      z   id"      cY d }~S d }~ww xY w)#Nra  rH  r  r%  r  zL0 Report Generatedr  r   r  Fr  r  l0_interview_videosTr  r   r#  r$  _final_video)r   r  r  rY  r  r  _Q_r  )r  r  r  rY  r  r&  r   rV  rP  r  r  r  r   zFailed r  )(r   r   r'   r   r   rs  r   r   r)   r   r  r*  r'  r  r   r)  rw  r  r(  r=  rR  r  copy2r   r  r  r*   r  rA  r  r2  r  r  r&  r   r$   r  r  r  r  )r   ra  rH  r  l0_exist_objfinal_source_path
new_folderfinal_file_extensionfinal_filenamer  relative_final_video_pathl0_created_objl0_exist_session_obj
l0_sessionsession_source_pathsession_video_filenamesession_file_extensionquestion_partsession_filenamesession_video_pathrelative_session_video_pathr+  r  r  s                           r   candidate_consent_approvalr    s   bC  )LL$$[1	(0077 8 
 %' 	
 !66.0 
 '..55'0$) 6 
 (=
!%%'	 	 )88==WW\\!


 	J.!ww//0AB1E!%<H\G]^77<<
NC77>>+,LL*,<=$&GGLL!%
! )0077&&(88%1%J%J'5 8 
  3::AA|A\eefst. 	J",";";"@"@%'WW%5%56I%J"%'WW%5%56L%Ma%P".. 6 < <UA Fq I %(=(=>TVX(Y Y "!'whyk-QgPhi!#j:J!Kww~~1202DE*,'',,%KNN# +'  ''..#,,!((: ++ / 1	@ 3+7+G+G((>'IJ-5599Y9G-;*0<0L0L--@BX,YZ9-c:: C	)c!f"45cBBCs%   BP? N9P? ?	R/Q=7R=Rc                    | j                   }t        j                  |      }t        j                  j                  |      j                  ddd      }|D cg c]  }|d   |d   |d   d }}t        j                  |      }t        j                  j                  |d      j                  dd	      }|D cg c]  }|d   |d	   d
 }	}t        j                  j                  dd      j                  dddd      }
|
D cg c]*  }|d   |d    d|d    j                         xs |d   d
, }}t        j                  j                  dd      j                  dddd      }|D cg c]*  }|d   |d    d|d    j                         xs |d   d
, }}t        |	|||d      S c c}w c c}w c c}w c c}w )Nr   ra  rZ  r   )r  rZ  r   r   )r~   r   r   r  )r  r  	RecruiterTr7  r  r  r  r   r-  r6  )rI  r  managersr  )r   rT   r   r   r   r   r  r   r   r   r  r   )r   r   r   r  r  jd_listr   rI  rL  client_listr  r  recruiter_listr  r(  manager_lists                   r   dashboard_filter_datar    s@    <<D..t4F

#
#
*
*V
*
<
C
C+C  	 W+0K	
G  66t<J&&  '  f[-(  
  K.m$	
K  $$  %  fT<j9  
  D''q;(89??ARQz]	
N  ||"", #  fT<j9  
  D''q;(89??ARQz]	
L  $ 	  e s   F-9F2/F7,/F<c                       e Zd ZegZd Zy)DashboardOverviewc                    |j                   }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }|j                  j                  d      }t	        j
                  |      }t        j                  j                  |      }	|r|	j                  |      }	|r|	j                  |      }	|r|	j                  |      }	|r|r|	j                  ||g	      }	|	j                         }	|	j                  d
d      }
|	j                  d      j                         }|	j                  d      j                         j                         }t        j                  j                  dd      j                         }t        j                  j                  dd      j                         }t        j                  j                  |
      j                         }|	j                  d      j                  t!        d
            }|	j                  ddd      j                  t!        d
            j#                  d      }t        j                  j                  |
      j                  d      j                  t!        d            }t%        |||||dt'        |      t'        |      t'        |      d      S )Nr   r   r  r   r   r   r}   )assignjd__user__id__inr   ra  Tr=  rN  r   r   r  r7  r6  )
job_id__inr\  r  client_id__client_namer   r  )
active_jdsrI  r  delivery_managersr  )statsjd_stage_dataclient_stagecandidate_funnel)r   r  r   r   rT   r   r   r   r   r
  rU  r  r  r   r'   rg  rX   r   r   rT  )r   r   r   r   r   r  r   r   authorized_jd_idsjd_queryjd_ids_listr  rI  r  r  r  r  r  funnels                      r   r   zDashboardOverview.get=  s   ||$$X.[[((6
++O<[[__\2
;;??:.)==dC$,,33' 4 
 8HZ@H'4 ' H ( *H5 ' H $$&**7*>__F_399;
//+.779??A\\(($ ) 
 %' 	
 !LL//0 0 
 %' 	
 (//66" 7 

%' 	
 OOJ'XE'NX+ 	 OOK)A:NXE'NX+X./ 	 &&V{V+VHXE$KX(	 	 ("(%6( "-0 . $V
  	r   Nr   r   r   r   r  r  :  s    )*Sr   r  c           
         | j                   }| j                  j                  d      }t        j                  j                  dd      }g }|D ]  }t        j                  |      }t        j                  j                  |      }|j                         }|j                  d      j                  t        d            }	t        t        |j                  dd	                  }
|j!                  |j"                  |j$                   d
|j&                   j)                         xs |j*                  ||
t        |	      d        |r |D cg c]  }t-        |d         |k(  r| }}t/        d|i      S c c}w )Ndm_idr6  Tr7  r   r\  r  r   r=  r-  )r  dm_namer  r   stagesr  )r   r   r   r   r   r   rT   r   r   r  r  rg  rX   rT  r  rU  r  r  r  r  r  r   rw  r   )r   r   r  dmsr  dmr   r  r  
stage_datar   r  s               r   delivery_manager_dashboardr    sv    <<D  $$W-E
,,

,  C
 M 2226''...@IIK	 JJz"uZ01 	
 $7

 	UU--",,8>>@OBKK"$:&
 	'6 $
1W:%' 
 

 ]  
s   E4c                    | j                   }| j                  j                  d      }t        j                  |      }t
        j                  j                  d|      }|r|j                  |      }g }|D ]  }t        j                  j                  |      }|j                         }|j                  d      j                  t        d            j                  d      }	|j                  dd      }
|j                  |j                  |j                   |t#        |	      |
D cg c]  }|d   |d   d	 c}d
        t%        d|i      S c c}w )zT
    Returns JD distribution stats for all Active Clients or a selected client.
    r   r   )r   r~   rP  r   r\  r  r   r  stager  r  r  r  
jd_detailsrI  )r   r   r   rT   r   r   r   r   r   r  r  rg  rX   r   r  r   r  rT  r   )r   r   r   client_ids_authrI  r  r   r  r  r  r  r  s               r   client_dashboard_statsr    s=    <<D$$((5I#;;DAO&&ho&VG..I..M ''...@IIK	 JJz"uZ01$ 	 ZZZ8
""&&":&YcdSUR_r*~Nd
 	* Y.// es   $Ec                    | j                   j                  d      }t        j                  j	                  dd      }|r|j	                  |      }g }|D ]	  }t        j                  |      }t        j                  j	                  |      }|j                         }|j                  d      j                  t        d            j                  d      }|j                  d	d      }	|j                  |j                  |j                   d
|j                    j#                         xs |j$                  |t'        |      |	D 
cg c]  }
|
d	   |
d   d c}
d        t)        d|i      S c c}
w )z;
    Returns JD distribution stats for all Recruiters.
    r  r  Tr7  rP  r   r\  r  r   r-  r  r  r  )r   r   r   r   r   rT   r   r   r  r  rg  rX   r   r  r  r  r  r  r   rT  r   )r   r  r  r  r  r   r  r  r  r  r  s              r   recruiter_dashboard_statsr    sZ    ''++N;L$$+$NJ&&,&7
M 	229=''...@IIK	 JJz"uZ01$ 	 ZZZ8
,, ++,Ai.A.A-BCIIKayOaOa":&YcdSUR_r*~Nd
 	, \=122 es   E/c                       e Zd Zd Zy)AuditLogViewc                 ~   |j                   j                  d      }|j                   j                  d      }t        |j                   j                  dd            }t        |j                   j                  dd            }t        j                  j                         j                  d      }|r3	 t        j                  j                  |      }|j                  |	      }|r|j                  |      }|j                         }|dz
  |z  }	|	|z   }
||	|
 }t        |d      }t        j                  j                  d       j                         j                  dd      j                         }t        |||t!        |      |j"                  d      S # t        j                  $ r t        d
did      cY S w xY w)Nmodelactorpager   	page_size   z
-timestamp)r  )rc  r  zInvalid model namer)  r   )actor__usernameTr  )r  r  r=  )r  r  r  usersr  )r  r   r   rw   r   r   r   rv   r   r  r   r  r>   r  rU  r
  rT  r   )r   r   
model_namer  r  r  logsrc  total_countr  endr   r  s                r   r   zAuditLogView.get   s   [[__W-
(7;;??61-.R89	##%..|<M*2266Z6H{{{= ;;u;5DjjlY&iE#'48
W4W XZ[*[6XZ 	  "%[!
  	- ++ M*> ?LLMs   12F "F<;F<Nr  r   r   r   r  r     s    )r   r  c                       e Zd Zd Zy)ScreenScrapeViewc                 ^   |j                   j                  d      }|j                   j                  d      }|r|st        ddit        j                        S 	 t        ||      }t        d|d      S # t        $ r0}t        dt        |      it        j                        cY d }~S d }~ww xY w)Nr   r0  r  zusername and password requiredr   zScraping completedr   )	r   r   r   r   r   rx   r  rw  HTTP_500_INTERNAL_SERVER_ERROR)r   r   r   r0  r   r  s         r   r  zScreenScrapeView.postB   s    <<##J/<<##J/x:;22 
	x2D/  
  	#a&!<< 	s   A3 3	B,<%B'!B,'B,N)r   r   r   r  r   r   r   r  r  A   s    r   r  r   )r  )rest_framework.viewsetsr   r   rest_framework.permissionsr   r   rest_framework.parsersr   r   r	   rest_framework.responser   rest_frameworkr   django.confr   django.contrib.auth.modelsr   r   rest_framework.viewsr   r  r   	django.dbr   django.core.files.storager   admin_users.modelsr   rf   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   serializersr.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r+  r  email.headerr?   email.utilsr@   rA   rB   rC   r'  r:  rw  rD   r  ai_settings.ai_gen_gcs_extrE   rz  r  r  r  r  masters.utilsrF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   django.core.mailrV   django.core.exceptionsrW   django.db.modelsrX   rY   rZ   r[   linkedin_servicer\   r]   django.shortcutsr^   r_   r`   r,  urllib.parsera   sklearn.feature_extraction.textrb   sklearn.metrics.pairwiserc   rest_framework.decoratorsrd   r  rest_framework.exceptionsre   django.utils.dateparserg   r  rh   ri   pgvector.djangorj   r0  r  django.contrib.postgres.searchrk   rest_framework.testrl   django.utils.timezonerm   r  r  speech_recognitionr  gttsrn   django.core.filesro   django.corerp   django.httprq   rr   rs   django.urlsrt   r@  rA  ru   "django.contrib.contenttypes.modelsrv   auditlog.modelsrw   scrapperrx   rz   r   r   r   r   r   r   rj  r  r  r  r  r  r  r  r1  rB  rF  rP  r_  rh  rm  rx  r  r  r  r  r  r  r  r  r  r+  r3  r8  r<  rA  rC  rE  rK  rQ  rV  r\  rl  rx  r}  r  r  r  r  r  r  r  r  r!  r,  r.  r;  r=  rk  rm  rS  r  r  r  r  r  r  r  r  r  r  r  r   r   r   <module>r&     s   F @ J J , !   2 ( ! ! 7 1 N  N  N  N  N  N ^  ^  ^  ^  ^   & 8 ( 	   	 @ $ $  I  I  I  I  I ) 5 / / . + @ @  " ; 6 , 	  5  1 , B *   < 1 %     "  I I    & : $ !N
L N
^0 1
 1
f?
\ ?
B0 0' 0
XL Xtnw n`!zw zx1
< 1
fq
7 q
f9 9MZG MZ^7 ;:w ;:xz
w z
zW "!g !F/9\ /9d+ +.6L 6&$7 $L; ;B3
l 3
j2
\ 2
hU
O' U
On 
5'XJ	%   	% 
6(XJ@
   @
DAw AF&w & 
5')% )%V 
6(V Vr 
6(0 0( 
6(U Un 
5'XJ   " 
6(XJA
   A
F 
5'XJ   ,4w 4l 
6(XJI   I@ 
6(XJ/
   /
b 
6(XJJ   J& 
6(XJ&O   &OP 
5'XJ17   17f 
6(XJ+7   +7Z 
6(XJE7   E7N 
5'
 
 
6(XJL
   L
\ 
5'XJ2
   2
h\ "* *&4 lz8x0 
5'XJ47   47l* *Q/fg7 gR 
5'XJ
   
I	 I	V 
6(e eNP 
6(XJ&/   &/P 
6(XJcC   cCJ 
5'_%&A ' AFV Vp 
5'_%&/ ' /b 
5'_%&$0 ' $0L 
5'_%&#3 ' #3J+7 +Zw r   