
    siQF                     
   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZm Z m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 e  ed      d               Z8e  ed      d)d              Z9ddl:m;Z; e;e d               Z<d Z=e  ed      d               Z>e e e!d      d                      Z?e e e!d      d                      Z@e  e!d       d!               ZAd" ZBe e ed      d#                      ZCe ed$               ZDe  e!d%      d&               ZEe e e!d'      d(                      ZFy)*zO
surveys.py

This module is used to write views related to the survey features
    N)datetime)uuid4)messages)serializers)default_storage)SimpleUploadedFile)ProtectedError)HttpResponseHttpResponseRedirectJsonResponse)redirectrender)gettext_lazy)closest_numbersget_pagination)hx_request_requiredis_recruitment_managerlogin_requiredpermission_required)SurveyFilter)AddQuestionFormApplicationFormQuestionForm
SurveyFormSurveyPreviewFormTemplateForm)	CandidateJobPositionRecruitmentRecruitmentSurveyRecruitmentSurveyAnswerResumeStageSurveyTemplate)group_by_queryset)paginator_qryz!recruitment.add_recruitmentsurvey)permc                     | j                   d   }t        j                  j                  |      }t	        |      j
                  }t        | dd|i      S )z4
    This method is used to render survey wform
    recIdidrecruitmentzsurvey/form.htmlform)GETr   objectsgetr   r.   r   )requestrecruitment_idr-   r.   s       =/var/www/python-projects/worksol/recruitment/views/surveys.pysurvey_formr5   3   sO     [[)N%%))^)<K+.33D'-~>>    c                     | j                   j                  d      }t        j                  j                  |      }t	        |      j
                  }t        | d||d      S )5
    Used to render survey form to the candidate
    titler9   )templatezsurvey/survey_preview.html)r.   r;   )r/   r1   r$   r0   r   r.   r   )r2   pkr9   r;   r.   s        r4   survey_previewr=   ?   sZ     KKOOG$E%%)))6Hh/44D$8, r6   )csrf_exemptc                 $   | j                   dk(  r| j                  j                  d      }t        | j                  j                  d            }t        j
                  j                  |      }|j                  |kD  r|}|j                  |k  r|dz
  }t        j
                  j                  |      }|D ]  }|dz   |_        |j                           t        |      |_        |j                          t        ddd	      S t        d
did      S )NPOSTquestion_idnew_positionr*      )sequenceTz#Question order updated successfully)successmessageerrorzInvalid request methodi  )status)
methodr@   r1   intr    r0   rD   filtersaver   )r2   rA   rB   qsold_qsis         r4   question_order_updaterP   S   s     ~~ll&&}57<<++N;<&&**k*:;;%'L;;,&'!+L"**11<1H 	A%)AJFFH	 ,'
	)NO
 	
 ":;CHHr6   c                 
	   d}| j                   d   }t        j                  |      }|d   d   d   }|d   d   d   }t        j                  j                  |      }t        j                  j                  |      }|d   d   d   }||d   d   d<   ||d   d   d<   t        j                  j                  |      |d   d   d<   t        di |d   d   }	t        |	      j                  }
| j                  d
k(  rft        j                  j                  |	j                  |	j                        j                         s|	j!                          nCt        j                  j                  |	j                  |	j                        j#                         }	|	j$                  j#                         
t'               n|	j$                  j#                         }|	|_        i }| j*                  j-                         D ]  \  }}|j/                  d      r8|j1                  dd      }|d   }| j*                  j3                  |      }|||<   O|j/                  d      rv|j1                  dd      }|d   }| j*                  j3                  |      }|s|d   dk7  s|D cg c]'  }t5        j6                  |d      j9                  d      ) }}|||<   |g||<    | j:                  j-                         D ]  \  }}| j:                  |   }|j<                  |kD  r1t?        j@                  | tC        d             tE        | d|
|	d      c S d|jF                   }tH        jJ                  jM                  tO        jJ                  |            }tH        jJ                  j                  |      stI        jP                  |       tO        jR                  |d      5 }|jU                         D ]  }|jW                  |        	 ddd       ||_,        |g||<     t        jZ                  |      |_.        |j!                          t?        j^                  | tC        d             tE        | d      S tE        | d|
|	d      S c c}w # 1 sw Y   xY w)r8   i  P 	candidater   fieldsr3   job_position_idr*   stage_idr,   r@   )emailr3   Nmultiple_choices__   date_rC    z%Y-%m-%dz%d-%m-%Yz1File size exceeds the limit. Maximum size is 5 MBz!survey/candidate_survey_form.html)r.   rR   zrecruitment_attachment/zwb+zYour answers are submitted.candidate/success.html )0sessionjsonloadsr   r0   r1   r   r#   r   r   r.   rI   rK   rV   r3   existsrL   firstrecruitmentsurveyanswer_setr!   candidate_idr@   items
startswithsplitgetlistr   strptimestrftimeFILESsizer   rG   rX   r   nameospathdirnamer   makedirsopenchunkswrite
attachmentdumpsanswer_jsonrE   )r2   MAX_FILE_SIZEcandidate_jsoncandidate_dictrec_idjob_idjobr-   rU   rR   r.   answeranswer_datakeyvaluepartsquestion_textselected_choicesdateformatted_datesru   attachment_pathattachment_dirdestinationchunks                            r4   candidate_surveyr   o   sR    $M__[1NZZ/NAx()9:FAx():;F



!
!V
!
,C%%))V)4Ka *:6H4?N1h 0158N1h 12.3mm.?.?8.?.LN1h
+8N1-h78I+.33D~~  ''//)2J2J ( 

&( NN!))00ooi6N6N 1 eg 
 44::<D $%66<<> 	
 ( !,,,,. 	+JC~~12		#q) %a#*<<#7#7#< -=M*(		#q) %a#*<<#7#7#< #(8(;r(A %5'  !))$
;DDZP'O ' 2AK.$)7C #	+$ "----/ 	1JC s+J.QRS 7!	: 
 !8
7HIOWW___-A-A/-RSN77>>.1N+ %%ou= -'..0 -E%%e,-- !0F /0K'	1( "ZZ4!$A"BCg788+I. ?',- -s   ,Q4'Q99R	"recruitment.view_recruitmentsurveyc           
      ^   t         j                  j                         }g }|D ]V  }|j                  j                         D ]7  }| j                  j
                  |k(  s|j                  |j                         9 X | j                  j                  d      rt        j                  j                         }n t        j                  j                  |      }t        |j                  d      j                         d| j                  j                  d      dt                     }g }|D ]  }|j                  |        t         j                  j                         }	|	j#                  dd	
      }
|j#                  dd	
      }t%        t'        |
      t'        |      z
        }g }|D ]  }|j                  |g dd        ||z   }t)        || j                  j                  d            }t)        |	| j                  j                  d            }	t+               }t-        j.                  t)        || j                  j                  d            j0                  D cg c]  }|j                   c}      }t3        | dt)        || j                  j                  d            ||	||d      S c c}w )z;
    This method is used to view the question template
    r   )recruitment_ids__inF)template_id__isnulltemplate_id__titletemplate_page)page	page_namerecords_per_pager9   T)flatr[   )grouperlistdynamic_namesurvey_template_pager   z#survey/view_question_templates.html)	questions	templatessurvey_templatesfrequests_ids)r   r0   allrecruitment_managersuseremployee_getappendr+   has_permr    rK   r%   distinctr/   r1   r   r$   values_listr   setr&   r   r_   rv   object_listr   )r2   recsidsrO   managerr   r   all_template_object_listr;   r   all_templatesused_templatesunused_templatesunused_groupstemplate_name
filter_objinstancer   s                     r4   view_question_templater      sy    ""$D
C !--113 	!G||((G3

144 	!! ||AB%--113	%--444M	!U3<<>[[___-!')I  " 2 ''12 &--113$00t0DM**+?d*KNC.^1DDEM) 
( "	

  8-G '++///"BI %'++//*@A J:: *7;;??62k		
 KK	
L -&y'++//&2IJ" 0(	

 
	
s   J*z$recruitment.change_recruitmentsurveyc                 h   t         j                  j                  |      }t        |      }| j                  dk(  rt        | j
                  |      }|j                         r|j                  d      }|j                          |j                  j                  |j                  d          |j                  j                  |j                         t        j                  | t        d             t!        t#        | dd	|i      j$                  j'                  d
      dz         S t#        | dd	|i      S )z>
    This view method is used to update question template
    r*   r   r@   Fcommittemplate_idzNew survey question updated.z survey/template_update_form.htmlr.   utf-8#<script>location.reload();</script>)r    r0   r1   r   rI   r@   is_validrL   r   r   cleaned_datarecruitment_idsr-   r   rE   rX   r
   r   contentdecode)r2   	survey_idr   r.   s       r4   update_question_templater   
  s    !((,,	,:HD ~~GLL8<==?yyy.HMMO  $$T%6%6}%EF$$(()9)9:Wa(F&GH?&$'&&/78  '=~NNr6   c                     t               }| j                  dk(  rt        | j                        }|j                         r|j	                  d      }|j	                          |j
                  j                  |j                         |j                  j                  |j                  d          t        j                  | t        d             t        t        | dd|i      j                  j!                  d      d	z         S t        | dd|i      S )
z>
    This view method is used to create question template
    r@   Fr   r   zNew survey question created.zsurvey/template_form.htmlr.   r   r   )r   rI   r@   r   rL   r   r   r-   r   r   r   rE   rX   r
   r   r   r   )r2   r.   r   s      r4   create_question_templater   '  s     >D~~GLL)==?yyy.HMMO$$(()9)9:  $$T%6%6}%EFWa(F&GH864.'&&/78  '6GGr6   z$recruitment.delete_recruitmentsurveyc                    	 t         j                  j                  |      j                          t	        j
                  | t        d             t        t              S # t         j                  $ r0 t	        j                  | t        d             Y t        t              S t        $ r0 t	        j                  | t        d             Y t        t              S w xY w)z;
    This method is used to delete the survey instance
    r*   z!Question was deleted successfullyzQuestion not found.zYou cannot delete this question)r    r0   r1   deleter   rE   rX   DoesNotExistrG   r	   r   r   )r2   r   s     r4   delete_survey_questionr   A  s    F!!%%%3::<!$G"HI
 *++	 )) :w"7 89 *++  Fw"C DE*++Fs   AA 2C 'CCc                 l   d}| j                   j                  d      }| j                   j                  d      }t        j                  j	                  |      j                         }| j                  dk(  r,|s*t        j                  | t        d             t        d      S 	 t        j                  j	                  |      j                         }|s*t        j                  | t        d             t        d      S 	 | j                  rd
| j                  vr[|rY|rW|j                   rK|j                   j#                         }t%        |j                   j&                  |d      }|| j                  d
<   t)        | j                  | j                        }|j+                         r|j-                  d      }|j.                  }	|	j0                  j3                         }
|
j	                  d      j5                         r&|
j	                  d      j                         |_        n$|
j9                  d      j                         |_        t        j:                  | t        d             t=        j>                  d|g      | j@                  d<   tB        j                  j	                  |      j5                         rz	 | jD                  jF                  }| jD                  jI                  d      r8||jJ                  j3                         vs|j0                  j	                  |      st        tL              S 	 |j-                          |rd|_'        |j-                          tQ        | d      S |jR                  j.                  jT                  j3                         |jV                  d   _,        n(|rd
|j                   jZ                  ini }t)        |      }tQ        | d|||d      S # t        t        f$ r- t        j                  | t        d	             t        d      cY S w xY w#  t        tL              cY S xY w)z@
    This method renders candidate form to create candidate
    NrecruitmentIdresumeIdr*   r/   zRecruitment ID is missingzopen-recruitmentszRecruitment not foundzInvalid Recruitment IDresumezapplication/pdf)content_typeFr   applied)
stage_typerD   zApplication saved.r_   rR   )r   zperms.recruitment.add_candidate)r3   Tr\   rT   initialzcandidate/application_form.html)r.   r-   r   ).r/   r1   r"   r0   rK   rb   rI   r   rG   rX   r   r   
ValueErrorOverflowErrorr@   rk   filereadr   rm   r   r   rL   r3   	stage_setr   ra   rU   order_byrE   r   	serializer^   r    r   r   r   r   r   is_candidater   r   open_positionsrS   queryseturl)r2   r-   r3   	resume_id
resume_objfile_contentpdf_filer.   candidate_objrecruitment_objstagesemployeeinitial_datas                r4   application_formr   Q  ss    K[[___5N
+I&&)&4::<J~~~w"= >?+,,-!))00N0CIIKNN7A&=$>?/00  ||7==(Yjoo)335-OO((,EV +3h'w||W]];==? IIUI3M+::O$..224F}}	}299;)/))L)R)R)T&)/)D)J)J)L&Wa(<&=>+6+@+@,GOOK( !((// . 0 fh	6&||88H#LL112ST#;+K+K+O+O+QQ'11888T'(899  U
  *.
'!'#;<<MM((77;;= 	%&/
 ;E*//"5"56"|4)kZH o & -w": ;<+,,-L6#$455s    #AO! 9A7P  !9PP P3c                    t         j                  j                  |      }| j                  j                  d      }d|i}|r3t	        j
                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )zF
    This view method is used to single view of question template
    r*   instances_idsquestionpreviousnextr   z survey/view_single_template.html)r    r0   r1   r/   r_   r`   r   r   )r2   r   r   requests_ids_jsoncontextr   previous_idnext_ids           r4   single_surveyr     s     !((,,	,:H88$Gzz"34.|YGW)
!"3'=wGGr6   c                 N   | j                   j                  d      s<| j                   j                  d      s!t        j                  | d       t	        d      S | j
                  j                  d      }d}|r.t        j                  j                  |      j                         }t        |      }| j                  d	k(  rXt        | j                  |      }|j                         r1|j                          t        j                   | d
       t	        d      S t#        | dd|i      S )z(
    Create question template views
    zrecruitment.add_surveytemplate!recruitment.change_surveytemplatezYou dont have permission.)<script>window.location.reload()</script>r9   Nr:   r   r@   zTemplate savedzsurvey/main_form.htmlr.   )r   r   r   infor
   r/   r1   r$   r0   rK   rb   r   rI   r@   r   rL   rE   r   )r2   r9   r   r.   s       r4   create_templater     s     	>?<<  !DEg:;GHHKKOOG$EH!))00u0=CCE*D~~GLL8<==?IIKW&67 KLL'2VTNCCr6   z!recruitment.delete_surveytemplatec                 T   | j                   j                  d      }t        j                  j	                  t        |            j                          |dk(  rt        j                  | d       nt        j                  | d       t        | j                  j                  dd            S )zA
    This method is used to delete the survey template group
    r9   r:   Nonez%This template group cannot be deletedzTemplate group deletedHTTP_REFERER/)r/   r1   r$   r0   rK   strr   r   r   rE   r   META)r2   r9   s     r4   delete_templater     s}     KKOOG$E!!E
!3::<gFG":; 0 0 EFFr6   r   c                    d}| j                   j                  d      }|r*t        j                  j	                  |      j
                  }t        d|i      }| j                  dk(  rVt        | j                        }|j                         r1|j                          t        j                  | d       t        d      S t        | d	d
|i      S )zE
    This method is used to add survey question to the templates
    Nr9   r:   template_idsr   r@   zQuestion addedr   zsurvey/add_form.htmlr.   )r/   r1   r$   r0   rK   rb   r   rI   r@   r   rL   r   rE   r
   r   )r2   r;   r9   r.   s       r4   question_addr     s     HKKOOG$E!))00u0=CCNH#=>D~~w||,==?IIKW&67 KLL'1FD>BBr6   )N)G__doc__r_   rn   r   uuidr   django.contribr   django.corer   django.core.files.storager   django.core.files.uploadedfiler   django.db.modelsr	   django.httpr
   r   r   django.shortcutsr   r   django.utils.translationr   rX   base.methodsr   r   horilla.decoratorsr   r   r   r   recruitment.filtersr   recruitment.formsr   r   r   r   r   r   recruitment.modelsr   r   r   r    r!   r"   r#   r$   recruitment.pipeline_grouperr%   recruitment.views.paginator_qryr&   r5   r=   django.views.decorators.csrfr>   rP   r   r   r   r   r   r   r   r   r   r   r]   r6   r4   <module>r     s    	   # # 5 = + H H - 6 8  - 	 	 	 ; 9 @A? B ? @A B  5 I  I4Qh ABB C BJ @AO B  O4 =>H ?  H. @A, B ,M` ABH C  H  D  D4 89G : G 89C :  Cr6   