U
    j                     @   s   d dl 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 ddlmZmZmZ dd	lmZmZmZ ed
dedd Zed
ddd Zed
dedd ZdS )    N)login_required)JsonResponse)get_object_or_404)require_POST)	Mock_TestQuestion_Bank   )explanation_video_storagemock_video_folderparse_question_number)create_upload_jobffmpeg_binaryread_job_statusz/admin/login)	login_urlc              
   C   s   t t|d}| jd}t s.tddiddS zt||}W nt tk
rv } ztdt|idd W Y S d}~X Y n< t	k
r } ztd	t|d
dd W Y S d}~X Y nX tdd
|d |dd|dddS )zKAccept all numbered videos and process them sequentially in the background.idvideoserrorzMFFmpeg is not installed on the server. Install it or configure FFMPEG_BINARY.i  status  Nz!Could not queue the video upload.)r   detailsi  Tzx{0} videos queued for background compression and upload. {1} additional files shared a question tag and were not needed.totalZignored_tag_duplicatesr   )successmessagejob   )r   r   FILESgetlistr   r   r   
ValueErrorstr	Exceptionformatget)requestmock_test_id	mock_testr   r   exc r(   >/var/www/html/crackeverytest/admin_app/question_video_views.py'bulk_upload_question_explanation_videos   s:     ( 
r*   c                 C   s>   t |}|r t|dd|kr0tddiddS td|dS )	Nr%   r   r   zUpload job not found.i  r   T)r   r   )r   intr#   r   )r$   r%   Zjob_idr   r(   r(   r)   question_video_upload_status<   s    r,   c                 C   s  t t|d}ttjj|ddd}|s:tddiddS t }t	|}z|
|\}}W nF tk
r } z(td	t|gd
|ddd W Y S d}~X Y nX i }	t|ddD ]6\}
}t|jpd }|r| dkr|	||
 qi }g }|D ]}zt|\}}W n$ tk
r.   || Y qY nX d|  krLt|krn n||d  }t|jphd }| dkrd}|r|	||n|}||}|t|||d}|dks||kr|||< q|| qd}| D ]t\}}|d }|d r0|tjj||d dj|dj|d7 }n.|tjj||d  j|dj|dj|d7 }qtjj|ddjdd }ttjj|ddd}tdd|t|d
|t|||dd t|ddD |dS ) zFMatch manually uploaded Spaces videos to questions by question number.r   )test_id
section_idr   r   z This mock test has no questions.r   r   z'Could not read the DigitalOcean folder.z
media/{0}/)r   r   folderi  Nr   )start )nullnone)Zsource_numberstorage_nametagr   r4   r5   )r-   r5   explanation_video)r   r-   F)r-   explanation_video__isnullTz$Videos uploaded in mock: {0} of {1}.c                 S   s   g | ]\}}|j s|qS r(   r6   ).0numberquestionr(   r(   r)   
<listcomp>   s   z4sync_question_explanation_videos.<locals>.<listcomp>)r   r   r/   r   uploadedupdatedmissingignored_files)r   r   listr   objectsfilterorder_byr   r	   r
   listdirr!   r    r"   	enumerater5   striplower
setdefaultr   r   appendlenr#   	posixpathjoinitemsexcludeupdater   count)r$   r%   r&   	questionsstorager/   Z_directories	filenamesr'   Zfirst_number_by_tagr:   r;   r5   Zmatched_filesr@   filenameZquestion_numberZ
_extensionZcanonical_numberexisting	candidater>   matchr4   r=   Zrefreshed_questionsr(   r(   r)    sync_question_explanation_videosD   s    	

 






rY   )rL   django.contrib.auth.decoratorsr   django.httpr   django.shortcutsr   django.views.decorators.httpr   Zlearn.modelsr   r   Zquestion_video_servicer	   r
   r   Zquestion_video_jobsr   r   r   r*   r,   rY   r(   r(   r(   r)   <module>   s   $
