ó
#Ü[c           @   s^  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 d  d l Td  d l Td  d l Td  d l Td  d l m Z d  d	 l Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z e d
 ƒ d „  ƒ Z  e d
 ƒ d „  ƒ Z! e d
 ƒ d „  ƒ Z" e d
 ƒ d „  ƒ Z# e d
 ƒ d „  ƒ Z$ e d
 ƒ d „  ƒ Z% e d
 ƒ d „  ƒ Z& e d
 ƒ d „  ƒ Z' e d
 ƒ d „  ƒ Z( e d
 ƒ d „  ƒ Z) d	 S(   iÿÿÿÿ(   t   rendert   get_object_or_404(   t   HttpResponseRedirectt   Http404t   HttpResponse(   t   reverse(   t   messages(   t   slugify(   t   permission_required(   t   *(   t
   serve_fileNs(   admin_users.can_access_exam_applicationsc         C   sH   t  j j d t ƒ } t  j j d t ƒ } t |  d i | d 6| d 6ƒ S(   Nt   enableds"   admin/theory_exams/exam-forms.htmlt   enabled_exam_formst   disabled_exam_forms(   t   TheoryExamFormt   objectst   filtert   Truet   FalseR    (   t   requestR   R   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   theory_exams   s    c         C   s˜   t  ƒ  } |  j r t  |  j ƒ } | j ƒ  r | j d t ƒ } t | j t ƒ | _ | j ƒ  t	 j
 |  d ƒ t t d ƒ ƒ Sn  t |  d i | d 6ƒ S(   Nt   commits   Exam Form has been Createdt   admin_theory_examss%   admin/theory_exams/add-exam-form.htmlt   exam_form_form(   t   TheoryExamFormFormt   POSTt   is_validt   saveR   t   slugify_uniquet   nameR   t   slugR   t   successR   R   R    (   R   R   t	   exam_form(    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   add_theory_exam   s    		
c         C   s·   t  t d | ƒ} t d | ƒ } |  j r™ t |  j d | ƒ} | j ƒ  r™ y. | j ƒ  t j |  d ƒ t t	 d ƒ ƒ SWq– t j
 |  d ƒ q– Xq™ n  t |  d i | d 6| d 6ƒ S(	   Nt   idt   instances   Exam Form has been UpdatedR   s   Could not update Exam Forms%   admin/theory_exams/add-exam-form.htmlR    R   (   R   R   R   R   R   R   R   R   R   R   t   errorR    (   R   t   exam_idR    R   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   edit_theory_exam3   s    	
c         C   sZ   t  t d | ƒ} y | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t d ƒ ƒ S(   NR"   s   Exam form has been deleteds   Could not delete Exam FormR   (   R   R   t   deleteR   R   R$   R   R   (   R   R%   R    (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   delete_theory_examH   s    
c         C   sc   t  t d | ƒ} y' t | _ | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t	 d ƒ ƒ S(   NR"   s   Exam form has been enableds   Could not enable Exam FormR   (
   R   R   R   R   R   R   R   R$   R   R   (   R   R%   R    (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   enable_theory_examV   s    	
c         C   sc   t  t d | ƒ} y' t | _ | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t	 d ƒ ƒ S(   NR"   s   Exam form has been disableds   Could not disable Exam FormR   (
   R   R   R   R   R   R   R   R$   R   R   (   R   R%   R    (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   disable_theory_examd   s    	
c         C   sE   t  t d | ƒ} t j j d | ƒ } t |  d i | d 6| d 6ƒ S(   NR"   R    s+   admin/theory_exams/locations/locations.htmlt	   locations(   R   R   t   ExamLocationR   R   R    (   R   R%   R    R+   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyR+   r   s    c         C   sÒ   t  t d | ƒ} t ƒ  } |  j r´ t |  j ƒ } | j ƒ  r´ yU | j d t ƒ } | | _ | j ƒ  t j	 |  d ƒ t
 t d d | j g ƒƒ SWq± t j |  d ƒ q± Xq´ n  t |  d i | d 6| d	 6ƒ S(
   NR"   R   s   Location has been Createdt   admin_theory_exam_locationst   argss   Could not create Exam Forms.   admin/theory_exams/locations/add-location.htmlR    t   location_form(   R   R   t   ExamLocationFormR   R   R   R   R    R   R   R   R   R"   R$   R    (   R   R%   R    R/   t   location(    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   add_locationz   s    			
 c         C   sÜ   t  t d | ƒ} t  t d | ƒ} t d | ƒ } |  j r· t |  j d | ƒ} | j ƒ  r· y: | j ƒ  t j |  d ƒ t	 t
 d d | j g ƒƒ SWq´ t j |  d ƒ q´ Xq· n  t |  d i | d 6| d	 6| d
 6ƒ S(   NR"   R#   s   Location has been UpdatedR-   R.   s   Could not update Locations/   admin/theory_exams/locations/edit-location.htmlR    R1   R/   (   R   R   R,   R0   R   R   R   R   R   R   R   R"   R$   R    (   R   R%   t   location_idR    R1   R/   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   edit_location’   s    	
 c         C   sx   t  t d | ƒ} t  t d | ƒ} y | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t d d | j	 g ƒƒ S(   NR"   s   Exam form has been deleteds   Could not delete Exam FormR-   R.   (
   R   R   R,   R'   R   R   R$   R   R   R"   (   R   R%   R3   R    R1   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   delete_location¨   s    
c         C   sE   t  t d | ƒ} t j j d | ƒ } t |  d i | d 6| d 6ƒ S(   NR"   R    s,   admin/theory_exams/exam_choices/choices.htmlt   exam_choices(   R   R   t
   ExamChoiceR   R   R    (   R   R%   R    R6   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyR6   ·   s    c         C   sÒ   t  t d | ƒ} t ƒ  } |  j r´ t |  j ƒ } | j ƒ  r´ yU | j d t ƒ } | | _ | j ƒ  t j	 |  d ƒ t
 t d d | j g ƒƒ SWq± t j |  d ƒ q± Xq´ n  t |  d i | d 6| d	 6ƒ S(
   NR"   R   s   Location has been Createdt   admin_theory_exam_choicesR.   s   Could not create Exam Forms/   admin/theory_exams/exam_choices/add-choice.htmlR    t   choice_form(   R   R   t   ExamChoiceFormR   R   R   R   R    R   R   R   R   R"   R$   R    (   R   R%   R    t   exam_choice_formt   exam_choice(    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   add_exam_choice¿   s    			
 c         C   sÜ   t  t d | ƒ} t  t d | ƒ} t d | ƒ } |  j r· t |  j d | ƒ} | j ƒ  r· y: | j ƒ  t j |  d ƒ t	 t
 d d | j g ƒƒ SWq´ t j |  d ƒ q´ Xq· n  t |  d i | d 6| d	 6| d
 6ƒ S(   NR"   R#   s   Location has been UpdatedR8   R.   s   Could not update Locations0   admin/theory_exams/exam_choices/edit-choice.htmlR    R<   R9   (   R   R   R7   R:   R   R   R   R   R   R   R   R"   R$   R    (   R   R%   t	   choice_idR    R<   R;   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   edit_exam_choice×   s    	
 c         C   sx   t  t d | ƒ} t  t d | ƒ} y | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t d d | j	 g ƒƒ S(   NR"   s   Exam form has been deleteds   Could not delete Exam FormR8   R.   (
   R   R   R7   R'   R   R   R$   R   R   R"   (   R   R%   R>   R    R<   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   delete_exam_choiceí   s    
c         C   s›   t  t d | ƒ} t j j d | d t d t ƒ } t j j d | d t d t ƒ } t j j d | d t ƒ } t |  d i | d 6| d 6| d 6| d	 6ƒ S(
   NR"   t   examt   completet   paids-   admin/theory_exams/applicants/applicants.htmlR    t   complete_applicantst   unpaid_applicantst   incomplete_applicants(   R   R   t   TheoryExamApplicationR   R   R   R   R    (   R   R%   R    RD   RE   RF   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   exam_applicantsü   s
    !!c   
      C   s.  t  t d | ƒ} t j j d | d t d t ƒ } d d d d d	 d
 d d d d d d d d d d d d d d d d d d g } x* t j j d | ƒ D] } | j | ƒ q— Wt d d ƒ } d  | d! <t	 j
 | ƒ } | j | ƒ xD| D]<} | j s| j j | _ | j ƒ  n  | j | j | j | j | j | j | j | j | j | j | j | j | j | j | j | j | j | j | j  | j! | j" | j# | j$ g } xO t j j d | ƒ D]8 } | | j% j& ƒ  k ræ| j d" ƒ q»| j d# ƒ q»W| j g  | D] }	 t' |	 ƒ j( d$ ƒ ^ qƒ qê W| S(%   NR"   RA   RB   RC   t	   Submitteds   Membership Numbert   Titles
   First Namet   Surnames   Email Addresss   Contact Numbert   DOBs	   Job Titles   Current Employers   Employment Start Dates   Work Address 1s   Work Address 2s	   Work Towns   Work Countys   Work Postcodes   Work Countrys   Other Sites Workeds   Mailing Address 1s   Mailing Address 2s   Mailing Towns   Mailing Countys   Mailing Postcodes   Mailing CountryR    t   content_types   text/csvs(   attachment; filename=exam_applicants.csvs   Content-Dispositiont   Yt   Ns   utf-8()   R   R   RG   R   R   R   R7   t   appendR   t   csvt   writert   writerowt   membership_numbert   memberR   t   completed_timet   titlet
   first_namet   surnamet   email_addresst   contact_numbert   dobt	   job_titlet   current_employert   employment_start_datet   work_address_1t   work_address_2t	   work_townt   work_countyt   work_postcodet   work_countryt   mailing_address_1t   mailing_address_2t   mailing_townt   mailing_countyt   mailing_postcodet   mailing_countryR6   t   allt   unicodet   encode(
   R   R%   R    RD   t   fieldst   choicet   responseRR   t	   applicantt   s(    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   download_applicants  s(    !N
	3c         C   s†  t  t d | ƒ} t j j d | d t d t ƒ } d d d d d	 d
 d d d d d d d d d d d g } t d d ƒ } d | d <t j | ƒ } | j	 | ƒ xÞ | D]Ö } d } x2 | j
 j ƒ  D]! }	 | rÙ d } qÄ |	 j ƒ  } qÄ W| j ƒ  | j | j d | j d | j | j | j | j | j | j | j | j | j | j | g } | j	 g  | D] }
 t |
 ƒ j d ƒ ^ q\ƒ q¨ W| S(   NR"   RA   RB   RC   t   Amounts   Salutation/Titles
   First Names   Middle Names	   Last Names   Gender (M/F)s   Birth date (dd/mm/yyyy)s   Employer (Optional)t   Addresst   Citys   State/Countys   Zip/Postal Codes   Phone Numbert   Countryt   Emails   SVTGBI ID #s   Physics/Tech/BothRM   s   text/csvs1   attachment; filename=exam_applicants_external.csvs   Content-Dispositiont    t   Boths   utf-8(   R   R   RG   R   R   R   R   RQ   RR   RS   R6   Rl   t   get_type_displayt   get_payment_amountRW   RX   RY   R\   R^   R`   Rb   Rc   Rd   R[   Re   RZ   RT   Rm   Rn   (   R   R%   R    RD   Ro   Rq   RR   Rr   t	   exam_typeRp   Rs   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   download_external)  s     !9
	f3c         C   sB   t  t d | ƒ} t  t d | ƒ} t |  d i | d 6| d 6ƒ S(   NR"   s3   admin/theory_exams/applicants/view-application.htmlR    t   application(   R   R   RG   R    (   R   R%   t   application_idR    R€   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   view_applicationD  s    c         C   sx   t  t d | ƒ} t  t d | ƒ} y | j ƒ  t j |  d ƒ Wn t j |  d ƒ n Xt t d d | j	 g ƒƒ S(   NR"   s   Application has been deleted.s   Could not delete application.t   admin_theory_exam_applicantsR.   (
   R   R   RG   R'   R   R   R$   R   R   R"   (   R   R%   R   R    R€   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   delete_applicationL  s    
c         C   sp   t  t d | ƒ} t  t d | ƒ} t | _ | j ƒ  t |  | ƒ t j |  d ƒ t	 t
 d d | j g ƒƒ S(   NR"   s#   Application has been marked as paidRƒ   R.   (   R   R   RG   R   RC   R   t   theory_application_form_paidR   R   R   R   R"   (   R   R%   R   R    R€   (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   mark_paid_application[  s    	
(*   t   django.shortcutsR    R   t   django.httpR   R   R   t   django.core.urlresolversR   t   django.contribR   t   django.template.defaultfiltersR   t   django.contrib.auth.decoratorsR   t   modules.core.functionst   formst   modelst   emailst   filetransfers.apiR
   RQ   R   R!   R&   R(   R)   R*   R+   R2   R4   R5   R6   R=   R?   R@   RH   Rt   R   R‚   R„   R†   (    (    (    s<   /var/www/svtgbi.org/svtgbi/modules/theory_exam/adminviews.pyt   <module>   s>   



!