o
    fsim                     @   s  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mZmZmZ d d	lmZmZ d
dlmZmZ d
dlmZmZmZmZmZ d dl m!Z! d dl"m#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j(Z)dS )    )	send_mail)settings)timezone)viewsetsstatusgenerics)action)Response)APIView)IsAuthenticatedIsAdminUserAllowAny)MultiPartParser
FormParser   )RegistrationPaymentProof)RegistrationSerializerPublicRegistrationSerializerPaymentProofSerializerVerifyCodeSerializer"UpdateRegistrationStatusSerializer)Event)EventListSerializerc                   @   s"   e Zd ZdZeZegZdd ZdS )PublicEventListViewz3Public endpoint: list events open for registration.c                 C   s   t jjdddddS )NTF)	is_activeis_archivedis_registerable
start_date)r   objectsfilterorder_byself r$   >/var/www/html/smartRegister/backend/apps/registration/views.pyget_queryset   s
   z PublicEventListView.get_querysetN)	__name__
__module____qualname____doc__r   serializer_classr   permission_classesr&   r$   r$   r$   r%   r      s
    r   c                   @   s&   e Zd ZdZegZdd Zdd ZdS )PublicRegistrationViewz'Public endpoint: register for an event.c                 C   sV   t |jd}| r#| }| | td|jdt |jdtjdS t|j	tj
dS )NdataTzRegistration successful.)successregistration_codemessager/   r   )r   r/   is_validsave_send_confirmation_emailr	   r1   r   HTTP_201_CREATEDerrorsHTTP_400_BAD_REQUEST)r#   request
serializerregistrationr$   r$   r%   post#   s   
zPublicRegistrationView.postc              	   C   sZ   z"t d|jj d|j d|jj d|j dtj|jgdd W d S  ty,   Y d S w )Nu   Registration Confirmed – zDear z,

Your registration for z/ has been received.
Your registration code is: z6

Please keep this code for reference.

Regards,
ICTAZT)subjectr2   
from_emailrecipient_listfail_silently)	r   eventname	full_namer1   r   DEFAULT_FROM_EMAILemail	Exception)r#   r<   r$   r$   r%   r6   0   s   
z/PublicRegistrationView._send_confirmation_emailN)r'   r(   r)   r*   r   r,   r=   r6   r$   r$   r$   r%   r-      s
    r-   c                   @   s   e Zd ZdZegZdd ZdS )VerifyCodeViewz,Public endpoint: verify a registration code.c              	   C   s   t |jd}| st|jtjdS |jd  }zt	j
dj|d}tdd|j|j|jj|j|jdW S  t	jy@   Y nw z(d	d
lm} |j
dj|d}tdd|j|j|jr`|jjnd|j|jdW S  |jys   Y nw tdddS )Nr.   r3   r1   rB   )r1   Tr<   )foundsourcerD   rF   
event_namer   payment_statusr   )RegisteredMember
attendance FzRegistration code not found.)rI   r2   )r   r/   r4   r	   r8   r   r9   validated_datastripr   r   select_relatedgetrD   rF   rB   rC   rL   DoesNotExistapps.attendance.modelsrM   )r#   r:   r;   coderegrM   memberr$   r$   r%   r=   F   sD   
	
	zVerifyCodeView.postN)r'   r(   r)   r*   r   r,   r=   r$   r$   r$   r%   rH   B   s    rH   c                   @   sh   e Zd ZdZeZegZdd Zdd Z	e
ddgegddd
dZe
ddgegeegddddZd	S )RegistrationViewSetz,Admin endpoint: full CRUD for registrations.c                 C   s   t jdd }| jjd}| jjd}| jjd}| jjd}|r-|j|d}|r5|j|d}|r=|j|d	}|rQ|j|d
|j|dB |j|dB }|S )NrB   payment_proofevent_idr   rL   search)r[   r3   )rL   )full_name__icontains)email__icontains)registration_code__icontains)r   r   rR   allr:   query_paramsrS   r    )r#   qsr[   status_filterrL   r\   r$   r$   r%   r&   t   s   $z RegistrationViewSet.get_querysetc                 C   s   | j dv r	t gS t gS )N)listretrieve)r   r   r   r"   r$   r$   r%   get_permissions   s   
z#RegistrationViewSet.get_permissionsTr=   )detailmethodsr,   Nc                 C   sn   |   }t|jd}| st|jtjdS d|jv r!|jd |_d|jv r,|jd |_	|
  tt|jS )Nr.   r3   r   rL   )
get_objectr   r/   r4   r	   r8   r   r9   rP   rL   r5   r   )r#   r:   pkr<   r;   r$   r$   r%   update_status   s   

z!RegistrationViewSet.update_status)rg   rh   r,   parser_classesc                 C   sb   |   }|jd}|stdditjdS tjj|||j	dddd\}}tt
|j	tjdS )	NfileerrorzNo file provided.r3   notesrO   )rm   ro   )r<   defaults)ri   FILESrS   r	   r   r9   r   r   update_or_creater/   r   r7   )r#   r:   rj   r<   rm   proof_r$   r$   r%   upload_payment   s   
z"RegistrationViewSet.upload_payment)N)r'   r(   r)   r*   r   r+   r   r,   r&   rf   r   r   rk   r   r   ru   r$   r$   r$   r%   rY   o   s    rY   N)*django.core.mailr   django.confr   django.utilsr   rest_frameworkr   r   r   rest_framework.decoratorsr   rest_framework.responser	   rest_framework.viewsr
   rest_framework.permissionsr   r   r   rest_framework.parsersr   r   modelsr   r   serializersr   r   r   r   r   apps.events.modelsr   apps.events.serializersr   ListAPIViewr   r-   rH   ModelViewSetrY   r$   r$   r$   r%   <module>   s"    #-