o
    ÌSe   ã                   @   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mZmZmZmZmZ d dlmZ d dlZd dlmZ d d	lmZ d d
lmZ dd„ Ze
dddd„ ƒZe
dddd„ ƒZe
dddd„ ƒZe
dddd„ ƒZe
dddd„ ƒZ e
dddd„ ƒZ!e
dddd„ ƒZ"edddde
dddd„ ƒƒZ#dS ) é    )ÚrenderÚredirect)ÚauthenticateÚloginÚlogout)ÚUser)Úlogin_required)ÚPostÚUserProfileÚCandidateProfileÚCandidatePostÚVoterÚVote)ÚmessagesN)Úcache_control)ÚHttpResponseRedirect)ÚCountc                 C   sò   d}| j jr
tdƒS | jdkro| j d¡ ¡  dd¡ ¡ }| j d¡}z	t	j
j|d}W n
   t | d	¡ Y t| ||d
}|d urit| |ƒ | j jjdkr[t | d|j ¡ tdƒS t| ƒ t | d¡ tdƒS t | d¡ d|i}t| d|ƒS )NÚsu_loginÚcreate_postÚPOSTÚusernameú Ú Úpassword)r   zUser does not exist)r   r   TzLogged in as zYou are not authorizedz"Username OR password does not exitÚpagezsu/login.html)ÚuserÚis_authenticatedr   Úmethodr   ÚgetÚlowerÚreplaceÚstripr   Úobjectsr   Úerrorr   r   ÚuserprofileÚis_superuserÚsuccessr   r   r   )Úrequestr   r   r   r   Úcontext© r)   ú"/var/www/alumnievoting/su/views.pyr      s,   

r   )Ú	login_urlc                 C   st   d}t j ¡ }| jdkr/t jj| j d¡ ¡  ¡ d t	 
| d| j d¡ ¡  dd¡ ¡  ¡ ||dœ}t| d	|ƒS )
Nr   r   Ú	post_name)r,   zPost added: r   r   )r   Ú	post_dataúsu/create-post.html)r	   r"   Úallr   Úcreater   r   Úupperr!   r   r&   r    r   )r'   r   r-   r(   r)   r)   r*   r   /   s   

ÿ(
r   c                 C   sv   d}t j ¡ }t jj|d}| jdkr/| j d¡ ¡  ¡ |_| 	¡  t
 | d|j ¡ tdƒS |||dœ}t| d|ƒS )	NÚupdate_post©Úidr   r,   zPost updated : r   ©r   r-   Úpost_single_datar.   )r	   r"   r/   r   r   r   r1   r!   r,   Úsaver   r&   r   r   ©r'   Úpkr   r-   r6   r(   r)   r)   r*   r2   >   s   

r2   c                 C   s`   d}t j ¡ }t jj|d}| jdkr$| ¡  t | d|j ¡ t	dƒS |||dœ}t
| d|ƒS )NÚdelete_postr3   r   zPost deleted : r   r5   r.   )r	   r"   r/   r   r   Údeleter   r&   r,   r   r   r8   r)   r)   r*   r:   O   s   

r:   c                 C   sÐ   d}t j ¡ }tj ¡ }| jdkr\tjj| j d¡ 	¡  
¡ | j d¡tj ¡ jd}| j d¡ 	¡  
¡ D ]}tjj|d}|j | j d¡¡ q4t | d| j d¡ 	¡  d	d
¡ 
¡  ¡ |||dœ}t| d|ƒS )NÚupload_candidatesr   Úcandidate_full_nameÚcandidate_photo)r=   r>   Úelection_yearÚcandidate_post)Úcandidate_profilezCandidate added: r   r   )r   r-   Úcandidate_post_dataúsu/upload.html)r	   r"   r/   r   r   r   r0   r   r   r1   r!   ÚFILESÚdatetimeÚdateÚtodayÚyearÚpostÚsetÚgetlistr   r&   r    r   )r'   r   r-   rB   Úcandidate_profile_saveÚdataÚcandidate_post_saver(   r)   r)   r*   r<   _   s"   




ýÿ(r<   c                 C   sŽ   d}t j ¡ }tj ¡ }tjj|d}|jjddd}| jdkr9| j d¡ 	¡  
¡ |_| j d¡|_tj ¡ j|_|||||d	œ}t| d
|ƒS )NÚupdate_candidatesr3   r,   T©Úflatr   r=   r>   ©r   r-   rB   Úcandidate_post_single_dataÚ#candidate_post_single_relation_datarC   )r	   r"   r/   r   r   rI   Úvalues_listr   r   r1   r!   r=   rD   r>   rE   rF   rG   rH   r?   r   ©r'   r9   r   r-   rB   rS   rT   r(   r)   r)   r*   rO   w   s   


rO   c                 C   s€   d}t j ¡ }tj ¡ }tjj|d}|jjddd}| jdkr2| ¡  t	 
| d|jj ¡ tdƒS |||||d	œ}t| d
|ƒS )NÚdelete_candidatesr3   r,   TrP   r   zCandidate deleted : r<   rR   rC   )r	   r"   r/   r   r   rI   rU   r   r;   r   r&   rA   r=   r   r   rV   r)   r)   r*   rW   ‰   s   


rW   c                 C   s¸   d}t j ¡ }tj ¡ }tj ¡ }tj ¡ }tjjdd}tj ¡ }t j ¡ }tj d¡ 	t
dƒ¡ ¡ jdd}	tj d¡ 	t
dƒ¡ ¡ jdd}
|||||	|||||
d	œ
}t| d
|ƒS )NÚelection_resultT)Úvoter_vote_flagÚcandidate_post_idr4   r   )Úid__count__gtrI   )
r   r-   rB   Ú	vote_dataÚresult_dataÚcandidate_countÚvoter_countÚ
vote_countÚ
post_countÚcandidate_count_filterzsu/results.html)r	   r"   r/   r   r   r   r
   ÚfilterÚvaluesÚannotater   Úorder_byr   )r'   r   r-   rB   r\   r^   r_   r`   ra   r]   rb   r(   r)   r)   r*   rX   ›   s   





""rX   T)Úno_cacheÚmust_revalidateÚno_storec                 C   s   t | ƒ t | d¡ tdƒS )NzYou are logged outÚsu)r   r   r#   r   )r'   r)   r)   r*   Ú	su_logout¯   s   rk   )$Údjango.shortcutsr   r   Údjango.contrib.authr   r   r   Údjango.contrib.auth.modelsr   Údjango.contrib.auth.decoratorsr   Úpresiding.modelsr	   r
   r   r   r   r   Údjango.contribr   rE   Údjango.views.decorators.cacher   Údjango.httpr   Údjango.db.modelsr   r   r   r2   r:   r<   rO   rW   rX   rk   r)   r)   r)   r*   Ú<module>   s8     "






