ó
£dYc           @   sî  d  d l  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  Z  d  d l! Td  d l" Td  d l# Td  d l$ Z$ d  d l% Z% d  d l& Z& d  d l' Td  d l( Td  d l) Td  d l* Td „  Z+ d „  Z, d „  Z- e. d „  ƒ Z/ e. d „  ƒ Z0 d „  Z1 e. d „  ƒ Z2 e. d „  ƒ Z3 e. d „  ƒ Z4 e. d „  ƒ Z5 d S(   iÿÿÿÿ(   t   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   EmailMessage(   t   messages(   t   datetimet   datet   timeN(   t   *c         C   sÈ   |  j  s( |  j d k s( |  j d k r, t St } | j j ƒ  re |  j | j j ƒ  k re t } qe n  | j j ƒ  rÄ t } x5 | j j ƒ  D]$ } |  | j	 j ƒ  k rŠ t } qŠ qŠ W| sÄ t } qÄ n  | S(   Nt   currentt   member(
   t   approvedt   member_statust	   user_typet   Falset   Truet   member_types_allowed_registert   allt   member_typet   member_groups_allowed_registert   members(   R   t   electiont   allowedt	   in_groupst   group(    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   can_register   s    (c         C   sð   t  } |  j s. |  j d k s. |  j d k r2 t St j ƒ  } |  j rZ |  j | k rZ t S| j j	 ƒ  r |  j
 | j j	 ƒ  k r t } q n  | j j	 ƒ  rì t } x5 | j j	 ƒ  D]$ } |  | j j	 ƒ  k r² t  } q² q² W| sì t } qì n  | S(   NR   R   (   R   R   R   R   R   R   t   todayt   expiry_datet   member_types_allowed_voteR   R   t   member_groups_allowed_voteR!   (   R   R"   R#   R'   R$   R%   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   can_vote5   s"    (c         C   sÈ   |  j  s( |  j d k s( |  j d k r, t St } | j j ƒ  re |  j | j j ƒ  k re t } qe n  | j j ƒ  rÄ t } x5 | j j ƒ  D]$ } |  | j	 j ƒ  k rŠ t } qŠ qŠ W| sÄ t } qÄ n  | S(   NR   R   (
   R   R   R   R   R   t   member_types_allowed_viewR   R   t   member_groups_allowed_viewR!   (   R   R"   R#   R$   R%   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   can_viewO   s    (c         C   sz  t  t d | ƒ} | j s; t d i | d 6d t |  ƒ ƒSy t j j d |  j ƒ } Wn t	 ‚ n Xt
 | | ƒ s– t d i | d 6d t |  ƒ ƒS| j ƒ  d k rÄ t t d d	 | j g ƒƒ S| j ƒ  d
 k rö t d i | d 6d t |  ƒ ƒS| j ƒ  d k r$t t d d	 | j g ƒƒ S| j ƒ  d k rVt d i | d 6d t |  ƒ ƒSt d i | d 6d t |  ƒ ƒSd  S(   Nt   slugs'   public/elections/election-disabled.htmlR"   t   context_instancet   users%   public/elections/election-denied.htmlt   registrationt   elections_election_registrationt   argss   registration-completes5   public/elections/election-registration-gathering.htmlt   votingt   elections_election_votet   completes'   public/elections/election-complete.html(   R   t   Electiont   enabledR    R   t   Membert   objectst   getR1   R   R.   t
   get_statusR
   R   R/   (   t   requestt   election_slugR"   R   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyR"   e   s$    	 
   c      
   C   s  t  t d | ƒ} y t j j d |  j ƒ } Wn t ‚ n Xt j ƒ  } | j	 ƒ  d k rx t
 t d d | j g ƒƒ S| j | ƒ } | j rç t d i | j d 6| j d 6| j d	 6| j j d
 6| j d 6| j d 6d | ƒ } nT t d i | j d 6| j d 6| j d	 6| j j d
 6| j d 6| j d 6d | ƒ } t | | ƒ sR| rrt d i | d 6d t |  ƒ ƒS|  j rç| j r¢t |  j |  j d | ƒ} n t |  j |  j d | ƒ} | j ƒ  rÔt } yB | j d } t j j d | d | ƒ }	 t  j! |  d ƒ t" } Wn t j# k
 r'n X|  j j d ƒ |  j j d ƒ k ret  j$ |  d ƒ t" } n  y7 t j j d |  j j d ƒ d t" d d d d ƒ } Wn* t j# k
 rÈt  j$ |  d ƒ t" } n Xy7 t j j d |  j j d ƒ d t" d d d d ƒ } Wn* t j# k
 r,t  j$ |  d ƒ t" } n X| sä| j% d t ƒ }	 | |	 _& | |	 _' |	 j% ƒ  t( ƒ  }
 |
 |	 _) |	 j% ƒ  t( ƒ  }
 |
 |	 _* |	 j% ƒ  t+ j, d d  d  |  d! |	 ƒ t
 t d" d | j g ƒƒ Sqçt  j$ |  d# ƒ n  t d$ i | d 6| d% 6d t |  ƒ ƒS(&   NR/   R1   R2   t   elections_electionR4   t   initialt   titlet
   given_namet   surnamet   emailt	   telephonet   hospital_clinict   position_choicess2   public/elections/election-registration-denied.htmlR"   R0   t   positionR   s.   You have already registered for this position.t   sponsor_email_1t   sponsor_email_2s5   Please enter two unique email addresses as referrers.t   user__emailR   R   R   R   sG   Please enter the email address of an existing member for your Proposer.sG   Please enter the email address of an existing member for your Seconder.t   committ   senderR>   t   election_candidatet(   elections_election_registration_completes:   Please ensure you have completed all fields and try again.s+   public/elections/election-registration.htmlt   election_registration_form(.   R   R8   R:   R;   R<   R1   R   R   R'   R=   R
   R   R/   t!   get_positions_registration_membert   show_dobt   ElectionRegistrationDOBFormRB   RC   RD   RE   RF   t   hospitalt   ElectionRegistrationFormR&   R    R   t   POSTt   FILESt   is_validR   t   cleaned_datat   ElectionCandidateR   t   warningR   t   DoesNotExistt   errort   saveR   R"   t   random_string_unique_sponsort   sponsor_key_1t   sponsor_key_2t   election_registeredt   sendt   None(   R>   R?   R"   R   R'   RH   RQ   t   errorsRI   RO   t
   unique_key(    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_registration€   sn    
	WT 		
$	7
7
		
		
		
c         C   s  t  t d | ƒ} t } t } y t j j d | ƒ } d } Wn0 y t j j d | ƒ } d } Wqp qp Xn X| r| rt j ƒ  } | d k r³ | | _ t	 | _
 | j ƒ  n+ | d k rÞ | | _ t	 | _ | j ƒ  n  t d i | d 6| d 6d	 t |  ƒ ƒSt ‚ d  S(
   NR/   Ra   i   Rb   i   s4   public/elections/election-registration-approved.htmlR"   RO   R0   (   R   R8   R   R[   R;   R<   R   t   nowt   sponsor_time_1R   t   sponsor_confirmed_1R_   t   sponsor_time_2t   sponsor_confirmed_2R    R   R   (   R>   R?   t   approval_keyR"   t   slotRO   Ri   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_registration_approveâ   s0    

				'c         C   s2   t  t d | ƒ} t d i | d 6d t |  ƒ ƒS(   NR/   s4   public/elections/election-registration-complete.htmlR"   R0   (   R   R8   R    R   (   R>   R?   R"   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_registration_complete  s    c         C   s4  t  t d | ƒ} y t j j d |  j ƒ } Wn t ‚ n Xt j ƒ  } | j	 ƒ  d k rx t
 t d d | j g ƒƒ St | | ƒ s§ t d i | d 6d t |  ƒ ƒS| j | ƒ } g  } xR | D]J } t j j d	 | d
 | ƒ } | j ƒ  }	 | rÃ |	 rÃ | j | ƒ qÃ qÃ W|  j rt }
 xˆ| D]€} | j d k r”|  j j d | j ƒ r§|  j j d | j ƒ }	 t |	 ƒ | j k rtt j j d	 | d
 | ƒ } | rÄt }
 t j |  d | j ƒ n  xÇ |	 D]¢ } t j j d | d | ƒ } | j  s| j! | k rt j |  d ƒ qn  |
 sËy# t d	 | d | ƒ } | j" ƒ  Wqmt j |  d | j ƒ t }
 qmXqËqËWq‘t }
 t j |  d | ƒ q§q'|  j j d | j ƒ r'|  j d | j } t j j d | d | ƒ } | j  s| j! | k rt j |  d ƒ t }
 qn  t j j d	 | d
 | ƒ } | rQt }
 t j |  d | j ƒ n  |
 s§y# t d	 | d | ƒ } | j" ƒ  Wq¤t j |  d | j ƒ t }
 q¤Xq§q'q'W|
 sÍt
 t d d | j g ƒƒ St j j d	 | d | ƒ } x | D] } | j# ƒ  qïWn  t d i | d 6| d 6| d 6d t |  ƒ ƒS(   NR/   R1   R5   R@   R4   s,   public/elections/election-voting-denied.htmlR"   R0   R   t   candidate__positioni   s   position_%ss-   Sorry, you have already voted for position %st   idRI   s#   Sorry, you cannot vote for yourselft	   candidates   Could not vote for position %ssP   Sorry you have selected too many candidates for the position <strong>%s</strong>t    elections_election_vote_completet   candidate__elections%   public/elections/election-voting.htmlt	   positionst   votable_positions($   R   R8   R:   R;   R<   R1   R   R   R'   R=   R
   R   R/   R+   R    R   t   get_positions_voting_membert   ElectionVotet   filtert   get_approved_candidatest   appendRW   R   t	   num_votest   getlistRs   t   lenR   R   R^   t   nameR[   t   vote_for_selfR   R_   t   delete(   R>   R?   R"   R   R'   Rw   Rx   RI   t   votest
   candidatesRf   t   election_votest   candidate_idRt   t   vote(    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_vote  sˆ    
 			c         C   s2   t  t d | ƒ} t d i | d 6d t |  ƒ ƒS(   NR/   s.   public/elections/election-voting-complete.htmlR"   R0   (   R   R8   R    R   (   R>   R?   R"   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_vote_completep  s    c         C   sæ   t  t d | ƒ} t  t d | d | ƒ} y t j j d |  j ƒ } Wn t ‚ n Xt j	 ƒ  } | j
 ƒ  d k r t t d d | j g ƒƒ St | | ƒ s¿ t d i | d 6d	 t |  ƒ ƒSt d
 i | d 6| d 6d	 t |  ƒ ƒS(   NR/   R"   Rs   R1   R5   R@   R4   s,   public/elections/election-voting-denied.htmlR0   s(   public/elections/election-candidate.htmlRt   (   R   R8   R[   R:   R;   R<   R1   R   R   R'   R=   R
   R   R/   R+   R    R   (   R>   R?   R‡   R"   Rt   R   R'   (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   election_vote_candidatex  s    
 (6   t   django.shortcutsR    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.core.mailR   t   django.contribR   R   R   R   t   matht   modelst   modules.members.modelst   formst   randomt   csvt   stringt   modules.core.functionst   modules.core.decoratorst	   functionst   signalsR&   R+   R.   t   members_onlyR"   Rh   Rp   Rq   R‰   RŠ   R‹   (    (    (    s5   /var/www/svtgbi.org/svtgbi/modules/elections/views.pyt   <module>   s<   


$



			b	"d