o
    iM                     @   s   d dl Z d dlZd dlmZ d dlT d dlmZmZ d dlT d dlmZ d dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZ d d	lmZ d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )"    N)serializers)*)SubscriptionOrderSubscriptionTransaction)datetime)Image)InMemoryUploadedFile)LanguagebasicPlanKeyName)SubscriptionPlanc           
      C   s\  | j dr| j d jdd }| j d jdd }| j d jdd }d}|d u r-d}n|d u r4d}n|d u r:d}t|d	krHtd
|gitjj	|d
 }|dkrtjj|d}|jjt|krtjj	|d
 }|dkrtjj|d}	|	jjt|kr|	jjt|kr|t|krdS d}nd}nd}nd}nd}t|d	krtd
|gidS )NrequestparentIdchildIdmedicalProfileId zParent Id is requiredzChild Id is requiredzMedical Profile Id is requiredr   erroridTr   zInvalid Medical Profile Idz!Medical Profile Id does not existzInvalid Parent IdzChild Id does not exits)context__contains__query_paramsgetlenr   ValidationErrorChildobjectsfilterexistsr   r   intMedicalProfiler   )
selfselectedMedicalProfileIdr   r   r   errorMessageisExistChildselectedChildisExistMedicalProfileselectedMedicalProfile r(   Z/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/mama_care_api/common/common.pyisValidData   sP   r*   c                 C   sF   | j dr| j d jdd }|S | j dr| j d }|S d }|S )Nr   isDashboard)r   r   headersr   )r!   r+   r(   r(   r)   getIsDashboardB   s   
r-   c                 C   s&   | d urt jj| d}|jr|jS d S )Nr   )r	   r   r   	isEnabledr   )
languageIdselectedLanguager(   r(   r)   getLanguageRecordK   s
   r1   c                 C   sR   | j dr| j d jtd }t|S | j tr#| j t }t|S d }t|S )Nr   )r   r   r,   r   languageKeyr1   )r!   r/   r(   r(   r)   getLanguageR   s   
r3   c                 C   s   t | dko	|  S )N   )r   isdigit)passwordr(   r(   r)   validatePassword\   s   r7   c                 C   s   t | }|S )N)secrets	token_hex)r   tokenr(   r(   r)   generateToken_   s   
r;   c                 C   s(   | j dr| j d jdd }|S d S )Nr   r   )r   r   r   r   )r!   r   r(   r(   r)   getParentIdc   s   r<   c                 C   s\   d}t | }|d ur|d ur|t|krdS d}nd}nd}t|dkr,td|gidS )Nr   TzParent Id is not matchedzParent Id is Requiredr   r   F)r<   r   r   r   r   )r!   parentIdDatar#   parentIdRequestr(   r(   r)   validateParentIdh   s   r?   c                 C   s   d}d }d }d}t jj|jd}| dkrN|d }tjj|d }tjj|dd}| dkrN|d }	tjj|	jd}
|
 dkrNd}|	jj	}|
d j
}|dkrX|||dS tjjtd	}| dkrn|d j	d
|dS d S )NFr   )r   )financialProfileId)r@   	isExpired)subscriptionOrderIdT)keyNametransactionIdcount)rC   r   )FinancialProfiler   r   r   __len__r   rE   r   subscriptionPlanIdrC   rD   r   r
   )r!   instanceisCheckrD   rC   soCount
finAccListfinAccsubscriptionOrderListselectedOrder	transListsubListr(   r(   r)   getSubscriptionInfo{   sH   
rR   c                 C   sp   t jj|jd }|dkrt jj|jd}t|jS t	 t	 |jd}t|d}|
 r6|  |jS d S )Nr   T)createdDateupdatedDater   )data)r    r   r   r   r   r   MedicalProfileSerializerrU   r   nowis_validsave)r!   rI   isExistsselectedmedicalProfileMapmedicalProfileSerializerr(   r(   r)   getMedicalProfile   s   

r^   c                 C   sX   t  }t|jt|j t|j t|j t|j t|j }t| d | }|S )N_)	r   rW   stryearmonthdayhourminutesecond)
weekNumberdtdtStringnamer(   r(   r)   getImageName   s   rk   c           	   	   C   s   | j d jd}| j d jd}t|d}t|}t	|}t }|j
|| d t|d|d | | d d|  d	}|S )
Nr   imageBase64imageExtensionzUTF-8)formatimage.zimage/)file
field_namerj   sizecharsetcontent_type)r   rU   r   base64	b64decodeencodeioBytesIOr   openrY   upperr   tell)	r!   	imageNamerl   rm   rU   bufimgimg_ionewImager(   r(   r)   uploadImage   s    



r   )r8   r   rest_frameworkr   mama_care_api.settingssubscription.modelsr   r   user_profile.serializersrv   ry   PILr   django.core.files.uploadedfiler   basic.modelsr	   r
   r   r*   r-   r1   r3   r7   r;   r<   r?   rR   r^   rk   r   r(   r(   r(   r)   <module>   s2    3	
(