
    vgF                        d 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.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 d Z7d Z8ddl9m:Z: 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!               Z@d" ZAee ed      d#                      ZBeed$               ZCe e d%      d&               ZDee e d'      d(                      ZEy))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_qryc                     | 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/horilla/recruitment/views/surveys.pysurvey_formr4   2   sO     [[)N%%))^)<K+.33D'-~>>    c                     t         j                  j                  t        |            }t	        |      j
                  }t        | d||d      S )5
    Used to render survey form to the candidate
    title)templatezsurvey/survey_preview.html)r-   r:   )r$   r/   r0   strr   r-   r   )r1   r9   r:   r-   s       r3   survey_previewr<   <   sL    
 %%))E
);Hh/44D$8, r5   )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?   r0   intr    r/   rC   filtersaver   )r1   r@   rA   qsold_qsis         r3   question_order_updaterO   N   s    ~~ll&&}57<<++N;<&&**k*:;;%'L;;,&'!+L"**11<1H 	A%)AJFFH	 ,'
	)NO
 	
 ":;CHHr5   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(  rt        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                   }tI        jJ                  |d      5 }|jM                         D ]  }|jO                  |        	 ddd       ||_(        |g||<    t        jR                  |      |_*        |j!                          t?        jV                  | tC        d             tE        | d      S tE        | d|
|	d      S c c}w # 1 sw Y   xY w)r7   i  P 	candidater   fieldsr2   job_position_idr)   stage_idr+   r?   )emailr2   Nmultiple_choices__   date_rB    z%Y-%m-%dz%d-%m-%Yz1File size exceeds the limit. Maximum size is 5 MBz!survey/candidate_survey_form.html)r-   rQ   zrecruitment_attachment/wb+zYour answers are submitted.candidate/success.html ),sessionjsonloadsr   r/   r0   r   r#   r   r   r-   rH   rJ   rU   r2   existsrK   firstrecruitmentsurveyanswer_setr!   candidate_idr?   items
startswithsplitgetlistr   strptimestrftimeFILESsizer   rF   rW   r   namer   openchunkswrite
attachmentdumpsanswer_jsonrD   )r1   MAX_FILE_SIZEcandidate_jsoncandidate_dictrec_idjob_idjobr,   rT   rQ   r-   answeranswer_datakeyvaluepartsquestion_textselected_choicesdateformatted_datesrq   attachment_pathdestinationchunks                           r3   candidate_surveyr   i   s    $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 %%ou= -'..0 -E%%e,-- !0F /0K!	1" "ZZ4!$A"BCg788+I. 9'&- -s   ,P('PP	"recruitment.view_recruitmentsurvey)permc           
      ^   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)flatrZ   )grouperlistdynamic_namesurvey_template_pager   z#survey/view_question_templates.html)	questions	templatessurvey_templatesfrequests_ids)r   r/   allrecruitment_managersuseremployee_getappendr*   has_permr    rJ   r%   distinctr.   r0   r   r$   values_listr   setr&   r   r_   rr   object_listr   )r1   recsidsrN   managerr   r   all_template_object_listr:   r   all_templatesused_templatesunused_templatesunused_groupstemplate_name
filter_objinstancer   s                     r3   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    r/   r0   r   rH   r?   is_validrK   r   r   cleaned_datarecruitment_idsr,   r   rD   rW   r
   r   contentdecode)r1   	survey_idr   r-   s       r3   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Nr5   z!recruitment.add_recruitmentsurveyc                     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   rH   r?   r   rK   r   r   r,   r   r   r   rD   rW   r
   r   r   r   )r1   r-   r   s      r3   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Gr5   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    r/   r0   deleter   rD   rW   DoesNotExistrF   r	   r   r   )r1   r   s     r3   delete_survey_questionr   8  s    F!!%%%3::<!$G"HI
 *++	 )) :w"7 89 *++  Fw"C DE*++Fs   AA 2C 'CCc                 	   t               }d}| j                  j                  d      }| j                  j                  d      }t        j                  j                  |      j                         }|r(d|r|j                  j                  ndi}t        |      }|@t        j                  j                  |      }|j                         r|j                         }| 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$                  }
|
j&                  j)                         }|j                  d      j                         r&|j                  d      j                         |	_        n$|j-                  d      j                         |	_        t/        j0                  | t3        d             | j                  j                  d      }|r\d|j                   }t5        j6                  |d      5 }|j9                         D ]  }|j;                  |        	 ddd       ||	_        	 | j                  d   r| j                  d   nd}d|	j                  j?                  dd       d|j                   dtA                }t5        j6                  |d      5 }|j9                         D ]  }|j;                  |        	 ddd       ||	_!        tE        jF                  d|	g      | jH                  d<   tJ        j                  j                  |      j                         rz	 | jL                  jN                  }| jL                  jQ                  d      r8||jR                  j)                         vs|j&                  j                  |      stU        tV              S 	 |	j#                          |rd|_,        |j#                          t[        | d      S |j\                  j$                  j^                  j)                         |j`                  d   _1        t[        | d|||d      S # 1 sw Y   xY w# 1 sw Y   pxY w#  Y qxY w#  tU        tV              cY S xY w)z@
    This method renders candidate form to create candidate
    NrecruitmentIdresumeIdr)   resumeinitialzapplication/pdf)content_typeFr   applied)
stage_typerC   zApplication saved.zrecruitment/resume/r[   profilezrecruitment/profile/ rW   r_   rQ   )r   zperms.recruitment.add_candidate)r2   Tr\   rS   zcandidate/application_form.html)r-   r,   r   )2r   r.   r0   r"   r/   rJ   rb   fileurlr   ra   r?   rk   readr   rm   r   rK   r2   	stage_setr   rT   order_byr   rD   rW   r   rn   ro   rp   r   replacer   r   r   	serializer^   r    r   r   r   r   r   r   is_candidater   r   open_positionsrR   queryset)r1   r-   r,   r2   	resume_id
resume_objinitial_datafile_contentpdf_filecandidate_objrecruitment_objstagesr   resume_pathr   r   r   profile_pathemployees                      r3   application_formr   H  s9    DK[[___5N
+I&&)&4::<J *//"5"5N|4!!))00N0C%++-K||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(<&=>]]&&x0F 3FKK=A$))+u= 1!' 1#))%011 (3$6=mmI6N'--	2TX!5m6H6H6P6PQTVY6Z5[[\]d]i]i\jjklqlsktu$)),> 1+!(!1 1#))%011 )5% ,7+@+@,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;;= 	%&/ )kZH Q1 11 16#$455s>   'R5?A-S ,'SS 6A7S 5R?SS SS*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    r/   r0   r.   r_   r`   r   r   )r1   r   r   requests_ids_jsoncontextr   previous_idnext_ids           r3   single_surveyr     s     !((,,	,:H88$Gzz"34.|YGW)
!"3'=wGGr5   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   Nr8   r   r?   zTemplate savedzsurvey/main_form.htmlr-   )r   r   r   infor
   r.   r0   r$   r/   rJ   rb   r   rH   r?   r   rK   rD   r   )r1   r9   r   r-   s       r3   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Cr5   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   r8   Nonez%This template group cannot be deletedzTemplate group deletedHTTP_REFERER/)r.   r0   r$   r/   rJ   r;   r   r   r   rD   r   META)r1   r9   s     r3   delete_templater     s}     KKOOG$E!!E
!3::<gFG":; 0 0 EFFr5   r   c                    d}| j                   j                  d      }|r t        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   r8   template_idsr   r?   zQuestion addedr   zsurvey/add_form.htmlr-   )r.   r0   r$   r/   rJ   r   rH   r?   r   rK   r   rD   r
   r   )r1   r:   r9   r-   s       r3   question_addr     s     HKKOOG$E!))00u0=NH#=>D~~w||,==?IIKW&67 KLL'1FD>BBr5   )F__doc__r_   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   rW   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&   r4   r<   django.views.decorators.csrfr=   rO   r   r   r   r   r   r   r   r   r   r   r]   r5   r3   <module>r     s      # # 5 = + H H - 6 8  - 	 	 	 ; 9? 5 I I4Nb ABB C BJ @AO B  O4 =>H ?  H. @A, B ,Wt ABH C  H  D  D4 89G : G 89C :  Cr5   