o
    Eh1                     @   s  d dl mZmZmZ d dlmZmZmZmZ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 d dl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' G dd de
Z(G dd de
Z)G dd deZ*d dl+m,Z, d dlmZ eeddee,ddG dd de
Z-eeddee,ddG dd deZ.ee,dd Z/eee,dd  Z0d!S )"    )renderredirectget_object_or_404)ListView
DetailView
CreateView
UpdateView
DeleteViewViewTemplateView)messages)reverse_lazyreverse)JsonResponse)Count)require_POST)method_decorator)login_required)csrf_exempt)timezone   )
TShirtType
TShirtSizeApparelRequest)ApparelRequestFormVerificationForm)RegisteredUserc                   @   s    e Zd ZdZdd Zdd ZdS )ApparelFormViewzapparel/apparel_form.htmlc                 C   s   t  }d|i}t|| j|S )Nform)r   r   template_name)selfrequestr   context r#   ,/var/www/html/smartRegister/apparel/views.pyget   s   zApparelFormView.getc                 C   s   t |j}| rj|jdd}|jd }zEtjj|d}tjj	|d
 }|rD|jd |_|jd |_|jd |_|  t|d	 n||_|j|_|  t|d
 tdW S  tjyi   |dd Y nw d|i}t|| j|S )NF)commitregistration_coder'   registered_usertshirt_typetshirt_sizeadditional_notesz3Your apparel request has been updated successfully!z5Your apparel request has been submitted successfully!zapparel:thank_you6Invalid registration code. Please check and try again.r   )r   POSTis_validsavecleaned_datar   objectsr%   r   filterfirstr+   r,   r-   r   successr*   
event_namer   DoesNotExist	add_errorr   r   )r    r!   r   apparel_requestr'   userexisting_requestr"   r#   r#   r$   post   s0   


zApparelFormView.postN)__name__
__module____qualname__r   r%   r=   r#   r#   r#   r$   r      s    r   c                       s,   e Zd Zee fddZdd Z  ZS )VerifyRegistrationCodeViewc                    s   t  j|i |S )N)superdispatch)r    argskwargs	__class__r#   r$   rC   @   s   z#VerifyRegistrationCodeView.dispatchc              
   C   s   t |j}| re|jd }zEtjj|d}tjj|d	 }|d u}t
d|j|jt|dr1|jnd||rL|r;|jjnd |rB|jjnd |rH|jnddnd dd	W S  tjyd   t
d
dd Y S w t
d
ddS )Nr'   r(   r)   Tr7    )r+   r,   r-   )	full_nameemailr7   has_existing_requestr<   )r6   r;   Fr.   r6   messagez)Please provide a valid registration code.)r   r/   r0   r2   r   r3   r%   r   r4   r5   r   rI   rJ   hasattrr7   r+   idr,   r-   r8   )r    r!   r   r'   r;   r<   rK   r#   r#   r$   r=   D   s@   


zVerifyRegistrationCodeView.post)r>   r?   r@   r   r   rC   r=   __classcell__r#   r#   rF   r$   rA   ?   s    rA   c                   @   s   e Zd ZdZdS )ThankYouViewzapparel/thank_you.htmlN)r>   r?   r@   r   r#   r#   r#   r$   rQ   g   s    rQ   )staff_member_requiredrC   )namec                   @   s   e Zd ZdZdd ZdS )DashboardViewzapparel/dashboard.htmlc           
      C   s   t jdjtddd}t jdjtddd}t j }t jjdd }|| }|dkr:|| d nd}t jd	d
ddd d }|||||||d}	t	|| j
|	S )Ntshirt_type__namerO   )counttshirt_size__nameTis_fulfilledr   d   r+   r,   r*   z-created_at
   )total_requestsfulfilled_requestspending_requestsfulfillment_rate
type_stats
size_statsrecent_requests)r   r3   valuesannotater   order_byrV   r4   select_relatedr   r   )
r    r!   r`   ra   r\   r]   r^   r_   rb   r"   r#   r#   r$   r%   r   s    
	zDashboardView.getN)r>   r?   r@   r   r%   r#   r#   r#   r$   rT   m   s    rT   c                       s8   e Zd ZeZdZdZdZ fddZ fddZ	  Z
S )ApparelRequestListViewzapparel/apparel_list.htmlrequests   c                    s   t   }| jjd}| jjd}| jjd}| jjd}|r)|j|d}|r1|j|d}|r;|j|dkd}|rC|j|d	}|S )
Nr+   r,   rY   r7   )rU   )rW   TruerX   )r7   )rB   get_querysetr!   GETr%   r4   )r    querysetr+   r,   rY   r7   rF   r#   r$   rk      s   
z#ApparelRequestListView.get_querysetc                    sJ   t  jdi |}tj |d< tj |d< tjjddd |d< |S )Ntshirt_typestshirt_sizesr7   T)flatevent_namesr#   )	rB   get_context_datar   r3   allr   r   values_listdistinct)r    rE   r"   rF   r#   r$   rr      s
   z'ApparelRequestListView.get_context_data)r>   r?   r@   r   modelr   context_object_namepaginate_byrk   rr   rP   r#   r#   rF   r$   rg      s    rg   c                 C   s   i }t | d|S )z$View for scanning apparel collectionzapparel/scanner.html)r   )r!   r"   r#   r#   r$   scanner   s   ry   c                 C   s:  ddl }|t}| jdkr| jdd}|d|  |s,|d tdd	d
S zt	j
j|d}|d|j d|j  |j  dkre|d|j d|j d tdd|j dd
W S zttj
j|d}|d|jj d|jj d|j  |jr|d|j d tddd|j|j|j|j|jj|jj|jddW W S |d|j  d|_t |_|  tddd|j|j|j|j|jj|jj|jddW W S  tjy   |d|j d tdd d
 Y W S w  t	jy   |d!| d" tdd#d
 Y S w |d$ tdd$d
S )%z+Process barcode scan for apparel collectionr   Nr/   r'   rH   z Received scan request for code: zNo registration code providedFzRegistration code is requiredrL   r(   zFound user: z, payment status: PAIDzUser z payment status is z
, not PAIDzUser payment status is z&. Only PAID users can collect apparel.r)   zFound apparel request: Type: z, Size: z, Fulfilled: z$ has already collected their apparelTz(User has already collected their apparel)rI   rJ   r'   r7   r+   r,   payment_status)r6   already_collectedrM   r;   z&Marking apparel as collected for user z(Apparel collection recorded successfullyz has not requested any apparelz"User has not requested any apparelzUser with registration code z
 not foundzUser not foundzInvalid request method)logging	getLoggerr>   methodr/   r%   infowarningr   r   r3   rI   r{   stripupperr   r+   rS   r,   rY   rJ   r'   r7   r   now
updated_atr1   r8   )r!   r}   loggerr'   r;   r:   r#   r#   r$   scan_barcode   s   


&

r   N)1django.shortcutsr   r   r   django.views.genericr   r   r   r   r	   r
   r   django.contribr   django.urlsr   r   django.httpr   django.db.modelsr   django.views.decorators.httpr   django.utils.decoratorsr   django.contrib.auth.decoratorsr   django.views.decorators.csrfr   django.utilsr   modelsr   r   r   formsr   r   attendance.modelsr   r   rA   rQ   %django.contrib.admin.views.decoratorsrR   rT   rg   ry   r   r#   r#   r#   r$   <module>   s@    $.(



%