o
    }	i  ã                   @   sr   d dl m Z  d dlZd dlZd dl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 G dd„ dejƒZdS )	é    )ÚdatetimeN)Úsettings)ÚVehicleMakeÚVehicleModelÚVehicleSubModel)ÚPartner)ÚPlacec                       s>  e Zd ZdZdZejdd�Zejddd�Z	ejddd�Z
ejdd�Zejdd�Zejdd�Zejdd�Zejdd�Zejdd�Zejdd�Zejedd	�Zejed
d	�Zejdd�Zejeejd�Zejdd�Zejeejd�Zejdd�Zejeejd�Zejdd�Zeje ejd�Z!ejdd�Z"eje#ejd�Z$ejdd�Z%e &e ¡ ¡Z'e &e ¡ ¡Z(e &e ¡ ¡Z)e &e ¡ ¡Z*eje+j,d�Z-eje+j,d�Z.ddgddœdgddœddgiddgigdœZ/dd„ Z0dd„ Z1dd„ Z2dd„ Z3dd„ Z4dd„ Z5d d!„ Z6d"d#„ Z7d$d%„ Z8d&d'„ Z9d(d)„ Z:d*d+„ Z;‡ fd,d-„Z<d.d/„ Z=‡  Z>S )0ÚVehicle)ÚPendingÚApprovedÚRejectedÚBooked)ÚActiveÚInactiveT)Úrequired)r   ÚuniqueN)Údefaultr
   )Úchoicesr   r   )Úreverse_delete_ruleÚvehicleÚchassis_number)Úfieldsr   Úengine_numberr   ÚpartnerÚpickup_point)Ú
collectionÚindexesc              	   C   sà   | j s|  ¡  tj tj|t| j ƒ¡}tj|dd� g }|D ]O}tj 	|j
¡d }|› dt ¡ j› |› �}tj ||¡}	t|	dƒ�}
| ¡ D ]}|
 |¡ qFW d   ƒ n1 sXw   Y  tj |t| j ƒ|¡}| |¡ q|S )NT)Úexist_oké   Ú_zwb+)ÚidÚsaveÚosÚpathÚjoinr   Ú
MEDIA_ROOTÚstrÚmakedirsÚsplitextÚnameÚuuidÚuuid4ÚhexÚopenÚchunksÚwriteÚappend)ÚselfÚfilesÚfolder_nameÚprefixÚfolderÚsaved_pathsÚfÚextÚfilenameÚfilepathÚdestÚchunkÚrelative_path© r>   ú</var/www/html/Moveonwheels/move_on_wheels/vehicles/models.pyÚ_save_filesE   s"   ÿÿzVehicle._save_filesc                 C   ó&   |   |dd¡}tt| j| ƒƒ| _|S )Nú
vehicle/rcÚrc)r@   ÚlistÚsetÚrc_details_images©r1   r2   Úpathsr>   r>   r?   Úsave_rc_imagesW   ó   zVehicle.save_rc_imagesc                 C   rA   )Núvehicle/vehicle_imagesr   )r@   rD   rE   Úvehicle_imagesrG   r>   r>   r?   Úsave_vehicle_images\   rJ   zVehicle.save_vehicle_imagesc                 C   rA   )Núvehicle/insuranceÚ	insurance)r@   rD   rE   Úinsurance_imagesrG   r>   r>   r?   Úsave_insurance_imagesa   rJ   zVehicle.save_insurance_imagesc                 C   rA   )Núvehicle/pucÚpuc)r@   rD   rE   Ú
puc_imagesrG   r>   r>   r?   Úsave_puc_imagesf   rJ   zVehicle.save_puc_imagesc           	      C   sf   t | |g ƒ}|D ]}tj tj|¡}tj |¡rt |¡ qt| |g ƒ |  	|||¡}t| ||ƒ |S )z,Delete old images and replace with new ones.)
Úgetattrr"   r#   r$   r   r%   ÚexistsÚremoveÚsetattrr@   )	r1   r2   r3   r4   Ú
field_nameÚ
old_imagesr#   Úabs_pathÚ	new_pathsr>   r>   r?   Úreplace_filesl   s   
€zVehicle.replace_filesc                 C   ó   |   |ddd¡S )NrB   rC   rF   ©r^   ©r1   r2   r>   r>   r?   Úreplace_rc_imagesx   ó   zVehicle.replace_rc_imagesc                 C   r_   )NrK   r   rL   r`   ra   r>   r>   r?   Úreplace_vehicle_images{   rc   zVehicle.replace_vehicle_imagesc                 C   r_   )NrN   rO   rP   r`   ra   r>   r>   r?   Úreplace_insurance_images~   rc   z Vehicle.replace_insurance_imagesc                 C   r_   )NrR   rS   rT   r`   ra   r>   r>   r?   Úreplace_puc_images�   rc   zVehicle.replace_puc_imagesc              
   C   s°   dD ]S}t | |g ƒ}|D ]!}tj tj|¡}tj |¡r-zt |¡ W q ty,   Y qw qtj tj| 	d¡d t
| jƒ¡}tj |¡rOt |¡sOt |¡ t| |g ƒ qdS )z2Delete all image files from disk and clear arrays.©rF   rL   rP   rT   r   r   N)rV   r"   r#   r$   r   r%   rW   rX   Ú	ExceptionÚsplitr&   r    ÚisdirÚlistdirÚrmdirrY   )r1   rZ   Úimagesr=   r\   r5   r>   r>   r?   Údelete_all_images…   s    ÿý"
ôzVehicle.delete_all_imagesc                 C   sŒ   | j r| js| j j| _| jr| js| jj| _| jr!| js!| jj| _| jr,| js,| jj	| _| j
r7| js7| j
j| _| jdkrAd| _dS d| _dS )zHFill reference names if missing and update status based on verification.r   r   r   N)ÚmakeÚ	make_namer)   ÚmodelÚ
model_nameÚsubmodelÚsubmodel_namer   Úpartner_nameÚ
first_namer   Úpickup_point_nameÚverification_statusÚstatus)r1   r>   r>   r?   Úclean–   s   







zVehicle.cleanc                    s$   t  ¡ | _|  ¡  tƒ j|i |¤ŽS )N)r   ÚutcnowÚ
updated_atrz   Úsuperr!   )r1   ÚargsÚkwargs©Ú	__class__r>   r?   r!   ª   s   
zVehicle.savec                 C   s0   g }dD ]}t | |g ƒ}|dd„ |D ƒ7 }q|S )z)Return full URLs for all uploaded images.rg   c                 S   s   g | ]
}t j tj|¡‘qS r>   )r"   r#   r$   r   Ú	MEDIA_URL)Ú.0r#   r>   r>   r?   Ú
<listcomp>µ   s    z.Vehicle.get_all_image_urls.<locals>.<listcomp>)rV   )r1   ÚurlsÚfieldrm   r>   r>   r?   Úget_all_image_urls°   s
   zVehicle.get_all_image_urls)?Ú__name__Ú
__module__Ú__qualname__ÚVERIFICATION_STATUS_CHOICESÚSTATUS_CHOICESÚmeÚDateTimeFieldÚregistration_dateÚStringFieldr   r   Ú
owner_nameÚcolourÚdoiÚdoeÚvehicle_classÚaddressÚtax_uptorx   ry   ÚreasonÚReferenceFieldr   ÚNULLIFYro   rp   r   rq   rr   r   rs   rt   r   r   ru   r   r   rw   Ú	ListFieldrF   rL   rP   rT   r   r{   Ú
created_atr|   Úmetar@   rI   rM   rQ   rU   r^   rb   rd   re   rf   rn   rz   r!   r‡   Ú__classcell__r>   r>   r€   r?   r	      sv    þþ

üþr	   )r   Úmongoenginer�   r"   r*   Údjango.confr   Úvehiclemakemodels.modelsr   r   r   Úaccounts.modelsr   ÚexplorePlaces.modelsr   ÚDocumentr	   r>   r>   r>   r?   Ú<module>   s    