ó
Ô¬ÄVc           @   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
 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 d  d
 l m Z d  d l m Z d  d l Z d  d l Z d  d l m Z m Z m  Z  d  d l! Td  d l" Td  d l# Td  d l$ Td  d l% Z% e& d „  ƒ Z' e& d „  ƒ Z( e& d „  ƒ Z) d „  Z* e& d „  ƒ Z+ e& d „  ƒ Z, d S(   iÿÿÿÿ(   t   render_to_responset   get_object_or_404(   t   RequestContextt   loadert   Context(   t   messages(   t   login_requiredt   permission_required(   t   Q(   t   Site(   t   settings(   t   HttpResponseRedirectt   Http404t   HttpResponse(   t   reverse(   t   EmailMessage(   t
   serve_fileN(   t   datetimet   datet   time(   t   *c         C   s`   t  t d |  j ƒ} t j j d | d t ƒ j d ƒ } t d i | d 6| d 6d t	 |  ƒ ƒS(   Nt   usert   membert   completes
   -submitteds%   members/papers/paper-submissions.htmlt   submissionst   context_instance(
   R   t   MemberR   t   PaperSubmissiont   objectst   filtert   Truet   order_byR    R   (   t   requestR   R   (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   paper_submissions   s    $c         C   s/   t  j j ƒ  j d ƒ } t |  d i | d 6ƒ S(   Nt   names   members/papers/papers.htmlt
   categories(   t   PaperCategoryR   t   allR   t   render(   R    R#   (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   papers"   s    c         C   s‰  t  t d |  j ƒ} t  t d | d t ƒ} t j j d | d t ƒ } t j j d | ƒ } y t	 j j
 d | d | ƒ } Wn3 t	 j k
 r± t	 d | d | ƒ } | j ƒ  n Xt j j d | ƒ } |  j rMg  } |  j d d	 k r[t j ƒ  }	 xc |	 D]X }
 |
 j ƒ  } xC | D]; } | j r|  j j
 d
 | j ƒ sP| j | ƒ qPqqWqü Wn  | j ƒ  } x | D] } | j ƒ  qnWd } | j ƒ  } x¦| D]ž} | j d k r¤|  j j d
 | j ƒ r9|  j j d
 | j ƒ } d j | ƒ } | j j d ƒ } t d | d | d | j d | d | d t ƒ } | j ƒ  t | ƒ t | ƒ k rx2 | D] } | | k r\t | _  q\q\Wn	 t | _  | j ƒ  | d } q9q›|  j j
 d
 | j ƒ r›|  j d
 | j } t d | d | d | j d | d | ƒ } | j! ƒ  | j j! ƒ  k r"t | _  n  | j ƒ  | d } q›q›W|  j d d k rm| j ƒ  t" j# |  d ƒ qM| j$ d k r| j% ƒ  } | | j$ k  rt" j& |  d ƒ | j' ƒ  } x" | D] } t" j& |  d | ƒ qºWt( d i | d 6| d 6| d 6| d 6| d 6| d 6d t) |  ƒ ƒSn  t | _* t+ j, ƒ  | _- | j ƒ  yÎ t. j/ d d  ƒ } | j0 d! d! d! d! ƒ | j1 d" ƒ | j2 d# ƒ t3 t4 j5 j t6 j7 d$ | j ƒ d% ƒ } t8 j9 d& i | d 6| d 6d t) |  ƒ ƒ} | j: j; | ƒ | j< ƒ  d$ | j | _= | j ƒ  Wn n Xt> j? d' |  d | ƒ t@ tA d( d) | jB g ƒƒ Sn  t( d i | d 6| d 6| d 6| d 6| d 6d t) |  ƒ ƒS(*   NR   t   slugt   enabledt   papert   paper_section__isnullR   t
   submissiont   nextR   s   question_%si   s   multi-selectt   ,t   questiont   question_namet   answert   ordert   correctt   saves   Your answers have been saved.i    sR   You have not answered the required number of questions correctly, please try agains:   You have answered question <strong>%s</strong> Incorrectlys   members/papers/paper.htmlt   paper_questionst   paper_sectionst   paper_submissiont   question_submissionst   incorrect_questionsR   t   calmdigitalt    dc33e7c4525620565185d0a00d90b8f0t   50t   297mmt   210mms#   certificates/cpd_certificate_%s.pdft   wbs$   public/papers/paper-certificate.htmlR    t   papers_paper_completet   args(C   R   R   R   t   PaperR   t   PaperQuestionR   R   t   PaperSectionR   t   gett   DoesNotExistR4   t   PaperQuestionSubmissiont   POSTt
   assessmentt   get_sectionst   get_questionst   requiredt   idt   appendt   get_answerst   deletet   get_all_questionst   typet   getlistt   joinR1   t   splitR"   t   lent   FalseR3   t   lowerR   t   successt   pass_percentaget   get_percentaget   errort   get_incorrect_questionsR    R   R   R   t   nowt	   submittedt   pdfcrowdt   Clientt   setPageMarginst   setPageWidtht   setPageHeightt   opent   ost   pathR
   t   PRIVATE_FILESR   t   render_to_stringt   clientt   convertHtmlt   closet   certificatet   emailst   send_paper_submittedR   R   R(   (   R    t
   paper_slugR   R*   R5   R6   R7   R8   t   required_errorst   assessment_sectionst   assessment_sectiont	   questionsR/   t   answersR1   R2   t   all_paper_questionst   answer_textt   correct_answerst   paper_question_submissiont   correct_answert   submission_percentageR9   t   incorrect_questionRj   t   output_filet   certificate_html(    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyR*   )   sš    		0
	
*

F	
(,
c         C   s7   t  t d |  j ƒ} t  t d | ƒ} t |  | j ƒ S(   NR   R(   (   R   R   R   RB   R   t   pdf_file(   R    Rp   R   R*   (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   paper_download¤   s    c         C   s³   t  t d | ƒ} t  t d |  j ƒ} y% t j j d | d | d t ƒ } Wn= t j k
 r‹ t	 j
 |  d ƒ t t d d | j g ƒƒ SXt d	 i | d 6| d
 6d t |  ƒ ƒS(   NR(   R   R   R*   R   s,   Sorry, you have not completed this paper yett   papers_paperRA   s"   members/papers/paper-complete.htmlR7   R   (   R   RB   R   R   R   R   RE   R   RF   R   R\   R   R   R(   R    R   (   R    Rp   R*   R   R7   (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   paper_complete«   s    %c         C   sC   t  t d |  j ƒ} t  t d | d | ƒ} t |  | j d t ƒS(   NR   RM   R   t   save_as(   R   R   R   R   R   Rm   R   (   R    t   submission_idR   R,   (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   paper_download_certificate¹   s    (-   t   django.shortcutsR    R   t   django.templateR   R   R   t   django.contribR   t   django.contrib.auth.decoratorsR   R   t   django.db.modelsR   t   django.contrib.sites.modelsR	   t   django.confR
   t   django.httpR   R   R   t   django.core.urlresolversR   t   django.core.mailR   t   filetransfers.apiR   R`   Rf   R   R   R   t   modelst   modules.members.modelst   modules.core.decoratorst   modules.core.functionsRn   t   members_onlyR!   R'   R*   R€   R‚   R…   (    (    (    s9   /var/www/svtgbi.org/svtgbi/modules/papers/accountviews.pyt   <module>   s.   



{	