o
    )iy                     @   s   d dl mZ d dlmZmZmZmZmZmZ 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 d d	lmZ d dlZd
d Zdd Zdd Zdd Zd#ddZdd Zd$ddZd#ddZdd Zdd Z	 	 dd  Z d!d" Z!dS )%    	send_mail)AirlinesClientFrequentFlyer	CountriesUserPermissionsStateCitiesN)models)	FieldFile)datetime)model_to_dict)	FileField)DjangoJSONEncoderc                   C   s   t tddS )Ni i?B )strrandomrandint r   r   I/var/www/html/myvaluetrips/my_value_trip_new/my_value_trip/users/utils.pygenerate_otp   s   r   c                 C   s    t dd| dd| gdd d S )NzYour Login OTP - My Value TripzYour OTP for login is: T 

 This OTP is valid for 5 minutes.  Please do not share this OTP with anyone else.zno-reply@myvaluetrip.comF)subjectmessage
from_emailrecipient_listfail_silentlyr   )emailotpr   r   r   send_login_otp   s   

r   c                 C   s(   d}d| d}t ||d| gdd d S )NzYour Password Reset OTPz%Use this OTP to reset your password: r   znoreply@myvaluetrip.comF)r   r   )r   r   r   r   r   r   r   send_forgot_password_otp   s
   
r   c                 C   s:   | r| j sg S ttjj| ddddddddS )zAReturn a safe, recursion-free list of menu dicts for the sidebar.)ref_userref_menuref_menu__menu_order_noref_menu__idref_menu__menu_nameref_menu__menu_urlref_menu__icon)is_authenticatedlistr   objectsfilterselect_relatedorder_byvalues)userr   r   r   get_assigned_menu    s    
r/   c                 C   s\   |pg }i }|j jD ]!}|j}||v rq
t| |d}t||d}||kr+||d||< q
|S )zO
    Compare old_instance and new_instance, return dict of changed fields.
    Noldnew)_metafieldsnamegetattr)old_instancenew_instanceexclude_fieldschangesfield
field_name	old_value	new_valuer   r   r   get_changed_fields3   s   r?   c                 C   s~  i ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&}i }|   D ]{\}}||v rP|||| < qA|d'rt|d(d) d*d+ }|d,d- d*d.}d/|vrvg |d/< t|d/ |kr|d/ i  t|d/ |ks~|d0kr||d/ | d1< qA|d2kr||d/ | d2< qA|d3kr| d4v |d/ | d5< qA|||< qA|S )6z~
    Convert incoming payload keys like clientDetails[firstName]
    into serializer-friendly keys like client_first_name
    zclientDetails[firstName]client_first_namezclientDetails[middleName]client_middle_namezclientDetails[lastName]client_last_namezclientDetails[salutation]client_salutationzclientDetails[dob]dobzclientDetails[gender]genderzclientDetails[occupation]
occupationzclientDetails[maritalStatus]marital_statuszclientDetails[reference]reference_fromzclientDetails[country]residential_countryzclientDetails[state]residential_statezclientDetails[city]residential_cityzclientDetails[pincode]residential_pincodezclientDetails[countryCode]country_codezclientDetails[contactNo]
contact_nozclientDetails[email]r   zclientDetails[preferredContact]preferred_contact_methodresidential_address
aadhaar_nopan_no)z!clientDetails[residentialAddress]zclientDetails[aadhaarNo]zclientDetails[panNo]z
companies[[   ]r   z][ 	companiescompanyref_company_iddesignationprimary)true1
is_primary)items
startswithintsplitreplacelenappendlower)data
client_map
normalizedkeyvalidxr;   r   r   r   normalize_client_payloadH   sx   	


rn   rootc                 C   s   dd l m  m  m  m} g }t| tr-|  D ]\}}|t|| d| 7 }q|S t| t	rJt
| D ]\}}|t|| d| d7 }q6|S z"ddlm} t| |s]| jjdrj||t| jf W |S W |S  tyv   Y |S w )Nr   .rS   rU   )Filer   )django.db.models.fields.filesdbr
   r4   files
isinstancedictr`   _find_non_serializablesr(   	enumeratedjango.core.files.baserq   	__class____name__endswithrf   type	Exception)objpath
filefieldsissueskvirq   r   r   r   rw   ~   s*   

rw   c              	   C   s   t d i }|p	g }| jjD ]6}|j|v rqt| |j}t|tr?z|r*|jr*|jnd ||j< W q ty>   d ||j< Y qw |||j< q|S )Nz!>>> Using SAFE serialize_instance)	printr3   r4   r5   r6   ru   r   url
ValueError)instancer9   rh   r;   valuer   r   r   serialize_instance   s   

r   c           
      C   s(  i }| du r|S t |  t | B D ]}}| |}||}t|trHt|trHtdd |D }tdd |D }||krG||d||< q||kr|du rT|dks|dkr\|du s|dv srd| v srd	| v srd
| v r|rv|nd}|r||nd}	||	kr||	d||< q||d||< q|S )z
    Compare old_data and new_data dictionaries, handling FKs, files, and related fields robustly.
    Return a dictionary of changed fields in the format {"field": {"old": old_value, "new": new_value}}.
    Nc                 S      g | ]
}t j|d tdqS T)	sort_keysclsjsondumpsr   .0itemr   r   r   
<listcomp>1      z!build_changes.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r   2  r   r0   rW   )aadhaar_card_filepan_card_filefiledocument
photograph)setkeysgetru   r(   sortedrg   )
old_datanew_datar:   rk   r=   r>   	old_items	new_itemsold_filenew_filer   r   r   build_changes   s,   

(,r   c                 C   sd   t | trdd |  D S t | trdd | D S t | tjr%| dS t | tjr0| dS | S )zFRecursively convert datetime/date objects to string in desired format.c                 S   s   i | ]	\}}|t |qS r   convert_datetime)r   r   r   r   r   r   
<dictcomp>F  s    z$convert_datetime.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r   r   )r   r   r   r   r   r   H  s    z$convert_datetime.<locals>.<listcomp>z%Y-%m-%d %H:%M:%Sz%Y-%m-%d)ru   rv   r`   r(   r   strftimedate)r   r   r   r   r   C  s   



r   c              	   C   s  i }g d}t t ttttd}ddddddd}| jjD ]}t| |j}|j|v rt| |j d}|rt| |jd }|rb||jd}	zt||	t	|||j< W q t
ya   t	|||j< Y qw ||j }
z|
jj|d}||jd}	t||	t	|||j< W q |
jy   d ||j< Y qw d ||j< qt|tjr|rt|dr|jr|jnd ||j< q|jr|jr|j|vr|r|jnd ||j< q|||j< qdd	 | j D |d
< dd	 | j D |d< dd	 | j D |d< dd	 | j D |d< dd	 | j D |d< |S )N)rM   rI   rJ   rK   ref_preferred_airlineref_airlinerM   r5   airline_idpkr   c                 S   .   g | ]}|j |j|j|jr|jjnd dqS )N)idpassport_nopassport_expiry_datepassport_file)r   r   r   r   r   r   r   r   r   r         z1serialize_client_with_related.<locals>.<listcomp>client_passportc              	   S   s6   g | ]}|j |j|j|j|j|jr|jjnd dqS )N)r   ref_visa_country_id	visa_typevisa_from_datevisa_to_datepassport_size_photograph)r   r   r   r   r   r   r   r   r   r   r   r     s    client_visac                 S   r   )N)r   insurance_from_dateinsurance_to_dateinsurance_document)r   r   r   r   r   r   r   r   r   r     r   travel_insurancec                 S   s*   g | ]}|j |j|jr|jjnd dqS )N)r   other_document_nameother_document)r   r   r   r   r   r   r   r   r     s    client_documentsc                 S   s.   g | ]}|j |jtjj|jd j|jdqS ))r   )r   ref_airline_idr   ff_no)r   r   r   r)   r   r   r   r   r   r   r   r     s    frequent_flyers)r   r   r	   r   r3   r4   r6   r5   r   r   AttributeErrorr)   DoesNotExistru   r
   r   hasattrr   is_relationmany_to_oner   r   allr   r   r   r   )clientrh   	fk_fields	fk_modelsdisplay_field_mapr;   r   fk_valuerelated_instancedisplay_fieldrelated_modelr   r   r   serialize_client_with_related]  sv   	


&





	r   c              	   C   s  i }g d}t t t ttd}dddddd}| jjD ]}t| |j}|j|v rt| |j d}|rt| |jd }|r`||jd}	zt||	t|||j< W q t	y_   t|||j< Y qw ||j }
z|
j
j|d}||jd}	t||	t|||j< W q |
jy   d ||j< Y qw d ||j< qt|tjr|rt|dr|jr|jnd ||j< q|jr|jr|j|vr|r|jnd ||j< q|||j< q|S )N)%account_concerned_person_country_codecompany_countrycompany_statecompany_cityz%travel_concerned_person_country_code )r   $travel_concerned_person_country_coder   r   r   rM   r5   r   r   r   )r   r   r	   r3   r4   r6   r5   r   r   r   r)   r   ru   r
   r   r   r   r   r   r   )rY   rh   r   r   r   r;   r   r   r   r   r   r   r   r   serialize_company_with_related  sT   

&r   )N)ro   )"django.core.mailr   users.modelsr   r   r   r   r   r	   r   	django.dbr
   rr   r   r   django.forms.modelsr   django.db.modelsr   django.core.serializers.jsonr   r   r   r   r   r/   r?   rn   rw   r   r   r   r   r   r   r   r   r   <module>   s4     	

6
 # wb