o
    liv                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ ddlmZmZ ddlmZmZ d	d
 Zdd ZG dd dejZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    )viewsetsstatus)APIView)Response)IsAdminUserAllowAny)RegisteredMember   )MotionEvent
MotionVote)MotionEventSerializerMotionVoteSerializerc                 C   s0   | j d}|r|dd  S | j ddS )NHTTP_X_FORWARDED_FOR,r   REMOTE_ADDRunknown)METAgetsplitstrip)requestxff r   C/var/www/html/smartRegister/backend/apps/motion_secondment/views.py_get_client_ip   s   $r   c                 C   sT   | j }| j}|| }||||rt|| d dnd|r&t|| d ddS ddS )Nd   r	   r   )	yes_countno_counttotal_votesyes_pctno_pct)r   r   round)eventyesnototalr   r   r   _stats   s   r&   c                   @   s    e Zd Zej ZeZe	gZ
dS )MotionEventViewSetN)__name__
__module____qualname__r
   objectsallquerysetr   serializer_classr   permission_classesr   r   r   r   r'      s    

r'   c                   @      e Zd ZdZegZdd ZdS )PublicActiveMotionViewz:Returns the currently active motion with live vote counts.c                 C   sL   t jjddd }|stdditjdS t|j|j|j	|j
dt|S )NTF	is_activeis_archiveddetailNo active motion.r   )idnameslugdescription)r
   r+   filterfirstr   r   HTTP_404_NOT_FOUNDr8   r9   r:   r;   r&   )selfr   r"   r   r   r   r   '   s   zPublicActiveMotionView.getNr(   r)   r*   __doc__r   r/   r   r   r   r   r   r1   #       r1   c                   @   r0   )PublicMotionVoteViewz
    Public voting endpoint.
    - Verifies the registration code against RegisteredMember.
    - One vote per member per motion (idempotent: returns existing vote if already cast).
    - Returns updated live stats on every response.
    c                 C   s  |j dpd  }|j d}|j dpd  }|r"|s,tdddtjdS |d	vr:tdd
dtjdS ztjj|ddd}W n tj	yY   tdddtj
d Y S w z	tjj|d}W n tj	yx   tddddtj
d Y S w tjj||d }|rtdd|jp||jd|j ddt|S tjj|||j|t|d tdd|j|d| ddt|tjdS )Nregistration_code event_idvoteF)Registration code and event are required.successmessager7   )YESNOzVote must be YES or NO.Tr8   r3   r4   Voting is not currently open.registration_code__iexactBRegistration code not found. Please check your code and try again.	NOT_FOUNDrJ   rK   coder"   rD   zYou have already voted z on this motion.)rJ   already_voted	full_namerG   rK   )r"   rD   rX   rG   
ip_addresszYour vote (z) has been recorded. Thank you!)datar   r   upperr   r   HTTP_400_BAD_REQUESTr
   r+   DoesNotExistr>   r   r   r<   r=   rX   rG   r&   creater   HTTP_201_CREATED)r?   r   rD   rF   rG   r"   memberexistingr   r   r   post=   st   


	
zPublicMotionVoteView.postNr(   r)   r*   rA   r   r/   rb   r   r   r   r   rC   4   s    rC   c                   @   r0   )PublicMotionCheckViewu   
    Pre-vote check: verifies the registration code and returns whether
    the member has already voted — without casting a vote.
    c                 C   s  |j dpd  }|j d}|r|s tdddtjdS ztjj|ddd	}W n tj	y?   tdd
dtj
d Y S w z	tjj|d}W n tj	y^   tddddtj
d Y S w tjj||d }td|jt||ru|jnd d|rt|S i S )NrD   rE   rF   FrH   rI   r7   TrN   rO   rP   rR   rS   rT   rV   )rJ   rX   rW   rG   )rZ   r   r   r[   r   r   r\   r
   r+   r]   r>   r   r   r<   r=   rX   boolrG   r&   )r?   r   rD   rF   r"   r`   ra   r   r   r   rb      sD   



zPublicMotionCheckView.postNrc   r   r   r   r   rd   {   s    rd   c                   @   r0   )PublicMotionStatsViewu>   Lightweight polling endpoint — returns only live vote stats.c                 C   st   |j d}z|rtjj|dn	tjjddd }|stjW n tjy3   tdditj	d Y S w tt
|S )	NrF   )r8   TFr2   r5   r6   r7   )query_paramsr   r
   r+   r<   r=   r]   r   r   r>   r&   )r?   r   rF   r"   r   r   r   r      s   &zPublicMotionStatsView.getNr@   r   r   r   r   rf      rB   rf   c                   @   r0   )MotionVotesViewu2   Admin — list all votes for a given motion event.c                 C   sn   |j d}|rtjj|dd}ntjjddd }|r)tjj|ddntj }t	t
|ddjS )	NrF   )rF   z	-voted_atTFr2   )r"   )many)rg   r   r   r+   r<   order_byr
   r=   noner   r   rZ   )r?   r   rF   votesr"   r   r   r   r      s   "zMotionVotesView.getN)r(   r)   r*   rA   r   r/   r   r   r   r   r   rh      rB   rh   N)rest_frameworkr   r   rest_framework.viewsr   rest_framework.responser   rest_framework.permissionsr   r   apps.attendance.modelsr   modelsr
   r   serializersr   r   r   r&   ModelViewSetr'   r1   rC   rd   rf   rh   r   r   r   r   <module>   s    G+