
:jfc           @   s9  d  d l  m Z m Z 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 d  d l 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 m Z m  Z  d  d l! m" Z" d  d l# Z# d  d l$ Z$ d  d l% Z% d  d l& m' Z' d  d l( Td  d l) Td  d l* Td  d l+ Td  d l, Td  d l- Td  d l. Td  d l/ Td  d l0 Td  d l1 Td  d l2 Td   Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 d   Z: e' d    Z; e' d    Z< d   Z= d   Z> e' d    Z? d S(   i(   t   rendert   render_to_responset   get_object_or_404(   t   RequestContextt   loadert   Context(   t   login_requiredt   permission_required(   t   Q(   t   Site(   t   settings(   t   HttpResponseRedirectt   Http404t   HttpResponse(   t   reverse(   t   Usert   Group(   t   messages(   t   slugify(   t   datetimet   datet   time(   t   chainN(   t   csrf_exempt(   t   *c         C   s  t  } | j s t St j   } | j r; | j | k r; t S| j rW | | j k rW t S| j j   r|  j	 j
   r ya t j j d |  j	  } | j r | | j k r t } q n  | j | j j   k r t } n  Wq |  j	 j s t } q q Xqt } n  | S(   Nt   user(   t   Truet   enabledt   FalseR   t   todayt
   start_datet   end_datet   limit_booking_to_member_typest   allR   t   is_authenticatedt   Membert   objectst   gett   expiry_datet   member_typet   limit_to_member_typest   is_superuser(   t   requestt   meetingt   allowedR   t   member(    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt
   get_access'   s,    			c         C   s  t  t d | } | j r t j j d | d t  j d d  j d d  } | j   | j k r t	 t
 d d | j g  Sn  y t j j d	 |  j  } Wn t } n Xt |  |  s t	 t
 d
 d | j g  St } |  j j d t  r0y/ t j j d |  j d  } t d |  } Wq0q0Xn  | sy` t j j d	 |  j  } t d i | j d 6| j d 6| j d 6| j j d 6| j d 6 } Wqt } t   } qXn  |  j rt |  j  } | j   ry< t j j d | d |  j d d t  } t j |  d  Wqt j k
 r| j d t  } t d t d  | _ | | _  | r| | _! | j" | _# | j" d k rd | _# n  | j$ | _$ | j% | _% | j& | _& | j' | _' | j( | _( | j) | _) | j* | _* | j+ | _+ | j, | _, | j   n|  j j d  ry% t j j d | j d | j  } Wqt j k
 ry t j j d | j-  } Wqt j. |  d t
 d d | j g  t } qXqXn, y t j j d | j-  } Wn t } n X| r| | _! d | _# | j$ | _$ | j% | _% | j& | _& | j' | _' | j( | _( | j) | _) | j* | _* | j+ | _+ | j, | _, | j" d k r| j/ d k r| j0 rd | _# qn  | j   nt }	 y t1 j j d | j-  }
 Wnv t1 j k
 r4t1 d | j- d | j d  | j  }
 t d! t1 d"  |
 _2 t3 d#  } |
 j4 |  |
 j   t }	 n Xt d	 |
 d$ d d | j d | j d | j  } |	 rw| | _5 n  t6   | _ | j   | | _! d | _# |	 | _7 | j   | j |  j d <t	 t
 d% d | j g  SXqn  t8 d& i | d 6| d' 6d( t9 |   S()   Nt   slugR+   t   completet   statust	   cancelledt   rejectedt   meeting_booking_fully_bookedt   argsR   t   meeting_booking_deniedt   meeting_booking_keyt
   unique_keyt   instancet   initialt   titlet   surnamet
   given_namet   email_addresst   membership_numbers0   Sorry, you have already booked onto this meetingt   commitid   s
   non-membert   delegatet   user__emails   A Member could not be found with the details you provided. You will be registered for this meeting as a Non-Member, if this is incorrect, please <a href="%s">Restart your booking</a> and try again.t   meeting_booking_meetingR-   t   currentt   emailt
   first_namet	   last_namei   t   usernamei
   t	   user_typet   meeting_booking_detailss   public/meeting_booking.htmlt   booking_formt   context_instance(:   R   t   Meetingt   limit_number_of_bookingst   MeetingBookingR$   t   filterR   t   excludet   countR   R   R/   R#   R%   R   R   R.   t   sessiont   MeetingBookingFormR;   R<   R=   RE   R?   t   POSTt   is_validR   t   errort   DoesNotExistt   savet   random_string_uniqueR8   R+   R-   RI   t   registrant_typet	   job_titlet   hospitalt	   address_1t	   address_2t   townt   countyt   countryt   postcodet	   telephoneR>   t   warningt   member_statust   approvedR   RH   t   generate_random_passwordt   set_passwordt   raw_passwordt   get_next_membership_numbert   user_registeredR   R   (   R*   t   meeting_slugR+   t   bookingst   current_membert   meeting_bookingRK   R-   t   booking_membert   new_userR   t   passwordRA   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyR+   J   s    	3
H	(		%&
		$

0
			
#c         C   s2   t  t d | } t d i | d 6d t |   S(   NR/   s   public/fully_booked.htmlR+   RL   (   R   RM   R   R   (   R*   Rm   R+   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   meeting_fully_booked   s    c         C   s2   t  t d | } t d i | d 6d t |   S(   NR/   s   public/access_denied.htmlR+   RL   (   R   RM   R   R   (   R*   Rm   R+   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   meeting_denied   s    c         C   s  t  t d | } t |  |  s= t t d d | j g  S|  j j d t  r t  t	 d |  j d } | j
 r |  j d =t j |  d  t t d d | j g  Sn, t j |  d  t t d d | j g  S| j } t d	 |  } t d	 |  } y t j j d
 d  } Wn t } n X|  j rZt |  j d	 | } t |  j d	 | } | j   rZ| j   rZ| j   | j   | j j s+| j | j _ | j | j _ | j | j _ | j | j _ | j | j _ | j | j _ | j | j _ | j | j _ | j | j _ | j j   n  t j |  d  t t d d | j g  Sn  t d i | d 6| d 6| d 6| d 6d t  |   S(   NR/   R6   R5   R7   R8   s3   Sorry, you have already registered for this meetingRC   s%   You have not started your booking yetR9   t   names   United Kingdons   Your details have been savedt   meeting_booking_ticketss   public/details.htmlR+   Rp   t    meeting_booking_institution_formt   meeting_booking_address_formRL   (!   R   RM   R.   R   R   R/   RS   R%   R   RO   R0   R   RW   R-   t   MeetingBookingInstitutionFormt   MeetingBookingAddressFormt   MemberCountryR$   RU   RV   RY   R^   R\   R]   R_   R`   Ra   Rb   Rc   Rd   t   successR   R   (   R*   Rm   R+   Rp   R-   Rx   Ry   Rb   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   details   sL    	
	
	

c      
   C   s.  t  t d | } t |  |  s= t t d d | j g  S|  j j d t  r t  t	 d |  j d } | j
 r |  j d =t j |  d  t t d d | j g  Sn t t d d | j g  S| j d k r$t j j t d	 t  t d
 | j j  Bd t d | d t j d  } n* t j j d | d	 t d t  j d  } t j j d |  j d  } t j j d | d t d t  j d  } t j j d | d t d t  j d  } |  j rt } |  j j d  st j |  d  t } qt j j d |  }	 x |	 D] }
 |
 j   qWt j j d |  } x | D] } | j   qFWyN t j j d |  j d  } | | _ | j | _ | j   | _  | j!   Wn t j |  d  t } n X| j j" r|  j j d  st j |  d  t } qn  | s| j j" r|  j j d  r|  j j# d  } x | D] } yq t j j d |  } | j d k r{| j$   } n | j   } t d | d | d | j d |  } | j!   Wq?q?Xq?Wqn  g  } |  j j d  r|  j j# d  } x | D] } yr t j j d |  } |  j j d |  } | rrt d | d | d | j d |  } | j% | _& | j!   n  Wqt j' k
 rqXqWn  t j( |  d  | j)   rt t d  d | j g  St t d! d | j g  Sqn  t* d" i | d 6| d 6| d# 6| d 6| d 6| d$ 6d% t+ |   S(&   NR/   R6   R5   R7   R8   s3   Sorry, you have already registered for this meetingRC   R-   t   limit_to_member_types__isnullR(   t   guests_onlyR+   R   t   ordert   day_bookings_onlyt   types    Please select a Registration FeeRp   t   ids   Could not save booking typet   meeting_dayss$   Please select a Day you wish to Bookt   meeting_dayt   meeting_day_namet
   price_paidt   social_eventss   social_event_quantity_%st   social_eventt   social_event_namet   quantitys.   Your Registration Fee details have been saved.t   meeting_booking_sessionst   meeting_booking_payments   public/tickets.htmlt   typest   social_events_daysRL   (,   R   RM   R.   R   R   R/   RS   R%   R   RO   R0   R   RW   R[   t   MeetingBookingTypeR$   RP   R   R   R-   R'   t   order_byt
   MeetingDayt   MeetingSocialEventRU   t   MeetingDayBookingt   deletet   MeetingSocialEventBookingR   Rv   t	   type_namet   get_costt
   type_priceRY   t   meeting_days_generatet   getlistt   get_member_costt   costR   RX   R}   t   get_sessionsR   R   (   R*   Rm   R+   Rp   R   R   R   R   t   errorst   day_bookingst   day_bookingt   social_bookingst   social_bookingt   meeting_booking_typet   posted_dayst   dayR   R   t   meeting_day_bookingt   social_event_bookingst   posted_social_eventst   social_event_idR   R   t   social_event_booking(    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   tickets  s    	
L***			
$$"c      	   C   s  t  t d | } t |  |  s= t t d d | j g  S|  j j d t  r t  t	 d |  j d } | j
 s t t d d | j g  S| j r |  j d =t j |  d  t t d d | j g  Sn t t d d | j g  St j j d	 |  j d
  } t j j d	 |  j d
  } | sLt t d d | j g  St j j d |  } g  } x | D] } | j | j  qnW|  j rx | D] } | j   qW|  j j d  }	 g  }
 g  } x*| D]"} | j r||  j j d | j  rh|  j d | j } | d k ryt j j d |  } t d | d | d | j  } | j | _ | j   qyq| j d |  qxt | j   D]f } t | j  |	 k ry8 t d | d | d | j  } | j | _ | j   Wqt  }
 qXqqWqWt j j d |  } g  } x | D] } | j | j  qW|
 rLt j |  d  q| rvxM | D] } t j |  |  qYWqt j! |  d  t t d d | j g  Sn  t" d i | d	 6| d 6| d 6| d 6| d 6| d 6d t# |   S(   NR/   R6   R5   R7   R8   Rw   s3   Sorry, you have already registered for this meetingRC   R+   R   R   Rp   t   sessionss   session_category_%st   notR   RS   t   session_names/   Please select an option for <strong>%s</strong>s4   Sorry, could not save the sessions, please try agains(   Your session information has been saved.s   public/sessions.htmlt   session_categoriest   session_bookingst   sessions_bookedRL   ($   R   RM   R.   R   R   R/   RS   R%   R   RO   R   R0   R   RW   t   MeetingSessionCategoryR$   RP   R   t   MeetingSessiont   MeetingSessionBookingt   appendRU   R   R   t   only_oneR   Rv   R   R   RY   R   t   strR   R}   R   R   (   R*   Rm   R+   Rp   R   R   R   R   t   session_bookingt   posted_sessionsR   t   session_errorst   categoryt   posted_sessionRS   t   session_error(    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyR   |  sp    		
		c         C   s  t  t d | } t |  |  s= t t d d | j g  S|  j j d t  r t  t	 d |  j d } | j
 s t t d d | j g  S| j r |  j d =t j |  d  t t d d | j g  Sn t t d d | j g  St j j d	 |  j d
  } t j j d	 | d t  } t j j d	 | d t  } t j j d	 |  } | j   } t d | j d d d	 |  }	 |	 j   t j rd |	 j |	 _ n d |	 j |	 _ |	 j   t j }
 t j } d t j t d d | j g f } d |
 | | |	 j | f } d d  l  } | j! |  j"   } t# d i
 | d 6| d	 6| d 6| d 6| d 6| d 6| d 6|	 d 6| d 6| d 6d t$ |   S(    NR/   R6   R5   R7   R8   Rw   s3   Sorry, you have already registered for this meetingRC   Rp   t   meeting_day__ordert   waiting_listR-   R   s   meeting-bookings   Meeting_Payment_%sDs   Meeting_Payment_%ss   %s%st   meeting_booking_payment_processs   %s:%s:%s:GBP:%s:%sis   public/payment.htmlR+   t   meeting_booking_daysR   t   waiting_list_sessionsR   t
   total_costt   paymentt   redirect_urlt   encrypted_stringRL   (%   R   RM   R.   R   R   R/   RS   R%   R   RO   R   R0   R   RW   R   R$   RP   R   R   R   R   t	   get_totalt   PaymentR-   RY   R
   t   DEVELOPMENTR   t   invoicet   WORLD_PAY_SECRET_KEYt   WORLD_PAY_INSTALLATION_IDt   PAYMENT_URLt   hashlibt   md5t	   hexdigestR   R   (   R*   Rm   R+   Rp   R   R   R   R   R   R   t
   secret_keyt   installation_idR   t   stringR   R   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyR     s<    		

	
		%c         C   s\  t  t d | } |  j s$ t  n  |  j j d d  } |  j j d d  } |  j j d d  } |  j j d d  } t  t d | } g  } t }	 | s | s | s | r t }	 | j d  n  | t	 j
 k r t }	 | j d	  n  | s| d
 k r| j d k rd | _ | | _ | j   | j }
 |
 j } t |
 _ t |
 _ | j |
 _ |
 j   d | } t d |
 j d |
 d d d | d | j d d  } t d t d  | _ | j   y t j d d  } | j d d d d  | j d t j   j d   t t j  j! t	 j" d | j#  d  } t$ j% d  i | d! 6 } | j& | |  | j'   d | j# | _( | j   Wn n Xt) j* d" d  d |
 d! |  n  d# t	 j, t- d$ d% | j. g f } qEd& | _ | j   d# t	 j, t- d' d% | j. g f } n8 d& | _ | j   d# t	 j, t- d' d% | j. g f } t/ |  d( i | d) 6 S(*   NR/   t
   callbackPWt    t   transStatust
   authAmountt   cartIdR   s   Missing a Required Variables   World Pay Password Incorrectt   Yt   pendingR0   s   %sR-   Rp   R   s   meeting-bookingRv   t   amount_paidt   payment_types   Credit Cardi   R8   t   calmdigitalt    dc33e7c4525620565185d0a00d90b8f0t   50t   0su   <p style="font-size:12px; text-align:center;">The Society for Vascular Technology of Great Britain and Ireland %s</p>s   %d/%m/%Ys'   members/receipts/meeting_booking_%s.pdft   wbs&   members/receipts/view-receipt-pdf.htmlt   receiptt   senders   %s%st    meeting_booking_payment_completeR5   R3   t   meeting_booking_payment_failures   public/payment-redirect.htmlt   next(0   R   RM   RU   R   R%   R   R   R   R   R
   t   WORLD_PAY_PASSWORDR1   t   amountRY   Rp   R+   R0   t   paidR   t   ReceiptR-   RZ   R8   t   pdfcrowdt   Clientt   setPageMarginst   setFooterHtmlR   R   t   strftimet   opent   ost   patht   joint
   MEDIA_ROOTR   R   t   render_to_stringt   convertHtmlt   closet   filet"   meeting_booking_confirmation_emailt   sendt   Nonet   URLR   R/   R    (   R*   Rm   R+   R   R   R   R   R   R   RW   Rp   t   receipt_nameR   t   clientt   output_filet   htmlR   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   payment_process   sj    				
				

3
 (
(	
(	
%c         C   s0  t  t d | } y t j j d d  } Wn t } n X|  j j d t  r t  t d |  j d } | j   d k r | j	 s t
 t d d | j g  St | _ t | _ d | _ | j   t j d d  d	 | d
 t  q n  |  j j d t  r	|  j d =n  t d i | d 6| d 6d t |   S(   NR/   s   meeting-payment-completeR7   R8   i    Rw   R5   R   Rp   R   s   public/payment_complete.htmlR+   t   content_blockRL   (   R   RM   t   ContentBlockR$   R%   R   RS   RO   R   R   R   R   R/   R   R0   R   R   RY   R   R   R   R   R   (   R*   Rm   R+   R  Rp   (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   payment_completeR  s$    
				
"c         C   sg   t  t d | } y t j j d d  } Wn t } n Xt j |  d  t t	 d d | j
 g  S(   NR/   s   meeting-payment-failuresk   Sorry, your payment could not be taken at this time. If you wish to retry your payment, please click below.R   R5   (   R   RM   R  R$   R%   R   R   RW   R   R   R/   (   R*   Rm   R+   R  (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   payment_failurep  s    
c         C   s  t  t d | } t |  |  s= t t d d | j g  S|  j j d t  r t  t	 d |  j d } | j
 s t t d d | j g  S| j r |  j d =t j |  d  t t d d | j g  Sn t t d d | j g  St t d	 d | j g  St   } |  j rt |  j  } | j   r| j d
 t  } | | _ | j   t | _ | j   | _ | j   t j d d  d |  d |  t t d d | g  Sn  t d i | d 6| d 6| d 6d t |   S(   NR/   R6   R5   R7   R8   Rw   s3   Sorry, you have already registered for this meetingRC   R   R@   R   R*   Rp   t    meeting_booking_invoice_completes   public/invoice.htmlR+   t   invoice_formRL   (   R   RM   R.   R   R   R/   RS   R%   R   RO   R   R0   R   RW   t   MeetingBookingInvoiceFormRU   RV   RY   Rp   R   R   R   t   meeting_booking_invoice_emailR   R   R   R   (   R*   Rm   R+   Rp   R	  t   meeting_booking_invoice(    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyR   |  s4    		
			
	
c         C   s   t  t d | } y t j j d d  } Wn t } n Xt |  |  sf t t d d | j	 g  St
 d i | d 6| d 6d t |   S(	   NR/   s   meeting-invoice-completeR6   R5   s   public/invoice_complete.htmlR+   R  RL   (   R   RM   R  R$   R%   R   R.   R   R   R/   R   R   (   R*   Rm   R+   R  (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   invoice_complete  s    
(@   t   django.shortcutsR    R   R   t   django.templateR   R   R   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.contrib.auth.modelsR   R   t   django.contribR   t   django.template.defaultfiltersR   R   R   R   t	   itertoolsR   t   cPicklet   base64R   t   django.views.decorators.csrfR   t   modules.core.decoratorst   modules.core.functionst   modules.members.functionst   modules.payments.functionst   signalst   formst   modules.members.formst   modelst   modules.resources.modelst   modules.members.modelst   modules.payments.modelsR.   R+   Rt   Ru   R~   R   R   R   R  R  R  R   R  (    (    (    s;   /var/www/svtgbi.org/svtgbi/modules/meeting_booking/views.pyt   <module>   sP   










	#				8	h	U	/R		+