a
    D=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	m
Z
 d dlmZ d dlZedd ZdS )	    )csrf_exempt)JsonResponse)settings)UexistFilesUexistTargetsUexistFilesTargetsUexistEntriesMatched)dateNc              	      s  | j dkr| jj | jd}t|d | jd}t|d |sTtddidd	S t	d
}d| d}tj
|dd g }g }|D ]}tj||j}t|d(}	| D ]}
|	|
 qW d    n1 s0    Y  td|j  d|j }|| ||j q fdd|D }tj| td g }|D ]0}tjj| dd }|r<|| q<tjj|d}g }|D ](}|D ]}|t|j|jd qqtj| td td||dS tddidd	S )NPOSTznames[]selected_optionszfiles[]uploaded_fileserrorzNo files uploadedi  )statusZENVz/var/www/html/hope-faith-php-z/app/uexistT)exist_okzwb+zFile saved at: zstatic/uploaded_files/c                    s   g | ]}t |t  d qS ))filer	   user_id)r   r	   today).0	file_namer    I/var/www/html/hope-faith-dev/hope-faith/src/hope_faith_harvester/views.py
<listcomp>*   s   z$file_upload_view.<locals>.<listcomp>z,Data write in UexistFiles table successfully)r   r   z-file_id)Zname__in)file_id	target_idz3Data write in UexistFilesTargets table successfullyzFiles uploaded successfully)message	file_urlsr   zInvalid request)methoduseridr
   getlistprintFILESr   osgetenvmakedirspathjoinnameopenchunkswriteappendr   objectsbulk_createfilterorder_byfirstr   r   r   r   )requestZselected_namesr   envZ
upload_dirr   Z
file_namesuploaded_file	file_pathfchunkZfile_urlfilesZlatest_filesfnameZlatest_filetargetsZfile_target_entriesr   targetr   r   r   file_upload_view	   sh    


*

r<   )django.views.decorators.csrfr   django.httpr   django.confr   Zhope_faith_harvester.modelsr   r   r   r   datetimer	   r#   r<   r   r   r   r   <module>   s   