o
    m¡÷h/  ã                   @   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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 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 )é    )ÚAPIView)ÚResponse©Ústatusé   )ÚCountryÚCategoryÚQuestionAnswer)ÚCountrySerializerÚCategorySerializerÚQuestionAnswerSerializer)Ú
CustomUserN)ÚHttpResponse)ÚBytesIO)Údatetimec                   @   ó,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚCountryCRUDViewc                 C   ó&   t j ¡ }t|dd}t|jtjdS ©NT©Úmanyr   )r   ÚobjectsÚallr
   r   Údatar   ÚHTTP_200_OK)ÚselfÚrequestÚ	countriesÚ
serializer© r   ú#/var/www/spmcq_backend/app/views.pyÚget   ó   
zCountryCRUDView.getc                 C   ó<   t |jd}| ¡ r| ¡  t|jtjdS t|jtjdS ©N)r   r   )	r
   r   Úis_validÚsaver   r   ÚHTTP_201_CREATEDÚerrorsÚHTTP_400_BAD_REQUEST©r   r   r   r   r   r    Úpost   ó
   zCountryCRUDView.postc                 C   óz   z)t jj|d}t||jdd}| ¡ r!| ¡  t|jtj	dW S t|j
tjdW S  t jy<   tdditjd Y S w )N©ÚidT©r   Úpartialr   ÚerrorúCountry not found)r   r   r!   r
   r   r%   r&   r   r   r   r(   r)   ÚDoesNotExistÚHTTP_404_NOT_FOUND)r   r   Ú
country_idÚcountryr   r   r   r    Úput   ó   ÿzCountryCRUDView.putc                 C   óR   zt jj|d}| ¡  tdditjdW S  t jy(   tdditjd Y S w )Nr.   ÚmessagezCountry deletedr   r2   r3   )	r   r   r!   Údeleter   r   r   r4   r5   )r   r   r6   r7   r   r   r    r<   &   ó   ÿzCountryCRUDView.deleteN©Ú__name__Ú
__module__Ú__qualname__r!   r+   r8   r<   r   r   r   r    r      ó
    r   c                   @   r   )
ÚCategoryCRUDViewc                 C   r   r   )r   r   r   r   r   r   r   r   )r   r   Ú
categoriesr   r   r   r    r!   /   r"   zCategoryCRUDView.getc                 C   r#   r$   )	r   r   r%   r&   r   r   r'   r(   r)   r*   r   r   r    r+   4   r,   zCategoryCRUDView.postc                 C   r-   )Nr.   Tr0   r   r2   úCategory not found)r   r   r!   r   r   r%   r&   r   r   r   r(   r)   r4   r5   )r   r   Úcategory_idÚcategoryr   r   r   r    r8   ;   r9   zCategoryCRUDView.putc                 C   r:   )Nr.   r;   zCategory deletedr   r2   rE   )	r   r   r!   r<   r   r   r   r4   r5   )r   r   rF   rG   r   r   r    r<   F   r=   zCategoryCRUDView.deleteNr>   r   r   r   r    rC   .   rB   rC   c                   @   r   )
ÚImportQuestionsViewc                 C   sn  z|j  d¡}|r|j d¡stdditjdW S tj 	¡  
¡  tdƒ t |¡}t|ƒ d}| ¡ D ]Ö\}}| d¡}| d	¡}| d
¡}	| d¡}
| d¡}| d¡}| d¡}| d¡}| d¡}| d¡}t	|||	|
||||||g
ƒsyq4z	tjj|d}W n tjyœ   tdd|› ditjd Y   W S w z
tjj||d}W n tjyÄ   tdd|› d|› ditjd Y   W S w |dvrÝtdd|› d|d › itjd  W S |dvrötdd|› d|d › d itjd  W S tjj|||	|
||||||d!
 |d"7 }q4td#d$|› d%itjdW S  ty6 } ztdt|ƒitjdW  Y d }~S d }~ww )&NÚfileú.xlsxr2   z Please upload a valid Excel filer   zAll existing questions deletedr   r7   rG   ÚquestionÚoption_aÚoption_bÚoption_cÚoption_dÚcorrect_optionÚ
difficultyÚlanguage)Únamez	Country 'z' not found)r7   rS   z
Category 'z' not found for country 'ú')ÚAÚBÚCÚDzInvalid correct_option 'z' for question at row é   )ÚenÚhizInvalid language 'z	' at row z. Must be 'en' or 'hi'.)
r7   rG   Úquestion_textrL   rM   rN   rO   rP   rQ   rR   r   r;   z!Questions imported successfully. z questions imported.)ÚFILESr!   rS   Úendswithr   r   r)   r	   r   r   r<   ÚprintÚpdÚ
read_excelÚiterrowsr   r4   r   Úcreater   Ú	ExceptionÚstr)r   r   Ú
excel_fileÚdfÚimported_countÚindexÚrowÚcountry_nameÚcategory_namer\   rL   rM   rN   rO   rP   rQ   rR   r7   rG   Úer   r   r    r+   P   sl   










$ÿ*ÿ(*ö
"€ÿzImportQuestionsView.postc                 C   sz   z	t jj|d}W n t jy   tdditjd Y S w t||jdd}| 	¡ r5| 
¡  t|jtjdS t|jtjdS )Nr.   r2   úQuestion not foundr   Tr0   )r	   r   r!   r4   r   r   r5   r   r   r%   r&   r   r(   r)   )r   r   Úquestion_idrK   r   r   r   r    r8   ‘   s   ÿzImportQuestionsView.putc                 C   r:   )Nr.   r;   zQuestion deleted successfullyr   r2   rn   )	r	   r   r!   r<   r   r   r   r4   r5   )r   r   ro   rK   r   r   r    r<      r=   zImportQuestionsView.deletec                 C   sz   |j  d¡}|j  d¡}|j  d¡}tj ¡ }|r|j|d}|r'|j|d}|r/|j|d}t|dd}t|jt	j
d	S )
Nr7   rG   rR   ©Úcountry__name©Úcategory__name)rR   Tr   r   )ÚGETr!   r	   r   r   Úfilterr   r   r   r   r   )r   r   rk   rl   rR   Ú	questionsr   r   r   r    r!   ¥   s   
zImportQuestionsView.getN)r?   r@   rA   r+   r8   r<   r!   r   r   r   r    rH   O   s
    ArH   c                   @   s   e Zd Zdd„ ZdS )ÚExportQuestionsViewc                 C   s°  zº|j  d¡}|j  d¡}tj dd¡ ¡ }|r|j|d}|r&|j|d}g }|D ]}| |jj	|j
j	|j|j|j|j|j|j|j|jdœ
¡ q*|sedddd	d
ddddddœ
dddddddddddœ
g}t |¡}g d¢}|j|d}tƒ }	tj|	dd}
|j|
ddd W d   ƒ n1 s‘w   Y  |	 d¡ t ¡  d¡}d |› d!}t|	 ¡ d"d#}d$|› d%|d&< |W S  ty× } zt d't!|ƒit"j#d(W  Y d }~S d }~ww ))Nr7   rG   rp   rr   )
r7   rG   rK   rL   rM   rN   rO   rP   rQ   rR   ÚSampleCountry1ÚSampleCategory1z&What is the capital of SampleCountry1?zOption AzOption BzOption CzOption DrU   ÚeasyrZ   un   à¤¨à¤®à¥‚à¤¦à¤¾ à¤¶à¥à¤°à¥‡à¤£à¥€ 2 à¤®à¥‡à¤‚ à¤¸à¤¬à¤¸à¥‡ à¤¬à¤¡à¤¼à¤¾ à¤—à¥à¤°à¤¹ à¤•à¥Œà¤¨ à¤¸à¤¾ à¤¹à¥ˆ?u   à¤ªà¥ƒà¤¥à¥à¤µà¥€u   à¤®à¤‚à¤—à¤²u   à¤¬à¥ƒà¤¹à¤¸à¥à¤ªà¤¤à¤¿u   à¤¶à¥à¤•à¥à¤°rW   Úhardr[   )ÚcolumnsÚopenpyxl)ÚengineÚ	QuestionsF)Ú
sheet_nameri   r   z%Y%m%d_%H%M%SÚquestions_export_rJ   zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet)Úcontent_typezattachment; filename="ú"zContent-Dispositionr2   r   )$rt   r!   r	   r   Úselect_relatedr   ru   Úappendr7   rS   rG   r\   rL   rM   rN   rO   rP   rQ   rR   r`   Ú	DataFrameÚreindexr   ÚExcelWriterÚto_excelÚseekr   ÚnowÚstrftimer   Úreadrd   r   re   r   r)   )r   r   rk   rl   rv   r   rK   rg   r|   ÚoutputÚwriterÚ	timestampÚfilenameÚresponserm   r   r   r    r!   ¶   s„   
öööó
ÿ
þ"€ÿzExportQuestionsView.getN)r?   r@   rA   r!   r   r   r   r    rw   µ   s    rw   )Úrest_framework.viewsr   Úrest_framework.responser   Úrest_frameworkr   Úmodelsr   r   r	   Úserializersr
   r   r   Úusers.modelsr   Úpandasr`   Údjango.httpr   Úior   r   r   rC   rH   rw   r   r   r   r    Ú<module>   s    !f