o
    »•iG>  ã                   @   sÔ   d dl mZ d dl mZ d dlT d dlmZ d dl mZ d dlmZmZ d dlT ddl	T d dl
T d dlT G dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dƒZdS )é    )Úserializers)Úauthentication)Ú*)ÚlanguageKey)Ú
pagination)ÚdatetimeÚtimezoneé   c                   @   ó   e Zd Zdd„ ZdS )ÚAPIKeyAuthenticationc                 C   sL   |j  d¡}d}d}|d ur|tkrd}nd}nd}|rd S t d|gi¡‚)NÚapiKeyÚ FTzIn Valid API KeyzNo API Key FoundÚerror)ÚheadersÚgetÚ
SECRET_KEYr   ÚValidationError)ÚselfÚrequestr   ÚerrorMessageÚisRequestAuthenticated© r   úY/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/mama_care_api/common/views.pyÚauthenticate   s   z!APIKeyAuthentication.authenticateN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      ó    r   c                   @   r
   )ÚLoginAuthenticationc                 C   sœ   |j  d¡}|j  d¡}d}d}|d ur@|d ur@tjj|dj|d}| ¡ dkr=|d }|j|kr:|j|kr:d}nd	}nd
}nd}|rFd S t 	d|gi¡‚)NÚphoneÚpasswordr   F)r    )r!   r   Tzphone and password is invalidzNo Records foundzphone and password is requiredr   )
Úquery_paramsr   ÚParentÚobjectsÚfilterÚ__len__r    r!   r   r   )r   r   r    r!   r   ÚisValidÚselectedListÚparentr   r   r   r   "   s$   ÿz LoginAuthentication.authenticateNr   r   r   r   r   r   !   r   r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚDashboardTokenAuthenticationc                 C   ó   |d ur
|dkr
dS dS ©Nr   TFr   ©r   Útokenr   r   r   Ú
checkToken?   ó   z'DashboardTokenAuthentication.checkTokenc                 C   s   |dkrdS dS )NÚadminTFr   r-   r   r   r   ÚcheckAdminAuthE   s   z+DashboardTokenAuthentication.checkAdminAuthc                 C   sl   |j  d¡}|j  d¡}d}d}|  |¡dkr(|dkr'|  |¡dkr%d}nd}nd}|r.d S t d	|gi¡‚)
NÚisDashboardr.   Fr   TÚ1zIn Valid TokenúToken is requiredr   )r   r   r/   r2   r   r   )r   r   r3   r.   ÚisAuthenticatedr   r   r   r   r   J   s   €z)DashboardTokenAuthentication.authenticateN)r   r   r   r/   r2   r   r   r   r   r   r*   =   s    r*   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚMobileTokenAuthenticationc              	   C   s$   zt |ƒW S  ttfy   Y d S w )N)ÚintÚ
ValueErrorÚ	TypeError)r   Úvalr   r   r   ÚtryParseInt`   s
   
ÿz%MobileTokenAuthentication.tryParseIntc                 C   s*   |   |¡d urt|ƒ}|t|ƒkrdS dS )NTF)r<   ÚgetLanguageRecordr8   )r   Ú
languageIdr;   r   r   r   ÚcheckLanguagef   s
   z'MobileTokenAuthentication.checkLanguagec                 C   r+   r,   r   r-   r   r   r   r/   m   r0   z$MobileTokenAuthentication.checkTokenc                 C   sV   |   |¡d ur)tjj|d}| ¡ dkr'|d }|jjt|ƒkr'|j|kr'dS dS dS )N)r.   r   TF)	r<   ÚParentTokenr$   r%   r&   ÚparentIdÚidr8   r.   )r   r.   rA   ÚparentTokenListÚselectedr   r   r   ÚcheckParentAuths   s   z)MobileTokenAuthentication.checkParentAuthc                 C   s  |j  t¡}|j  d¡}|j di ¡}| d¡}|j d¡}d}d}d}	d }
|d ur/|}
d}	n|d ur5|}
|
d ur_|  |¡dkrU|  ||
¡dkrKd}nd}t d	|gi¡‚d
}t d	|gi¡‚d}t d	|gi¡‚|	dkrŠ|  	|¡}|r‚|rxd S d}t d	|gi¡‚t d	|gi¡‚d S )Nr.   ÚkwargsÚpkrA   Fr   TzInvalid Tokenr   r5   úParent Id is requiredzLanguage is required)
r   r   r   Úparser_contextr"   r/   rE   r   r   r?   )r   r   r>   r.   rF   Ú
parentIdPKÚ
parentIdFKr6   r   ÚisPKrA   ÚisLanguageValidr   r   r   r   }   sB   

z&MobileTokenAuthentication.authenticateN)r   r   r   r<   r?   r/   rE   r   r   r   r   r   r7   ^   s    
r7   c                   @   s   e Zd ZdZdZdZdS )ÚStandardSetPaginationé   Ú	page_sizeiè  N)r   r   r   rP   Úpage_size_query_paramÚmax_page_sizer   r   r   r   rN   «   s
    ÿrN   c                       sp   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡ fdd„Z
‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚSubscriptionPlanMixinc                 C   s   d|||gdœiS )NÚsubscriptionError)ÚcurrentPlanIdÚcurrentPlanKeyNamer   r   )r   rU   rV   r   r   r   r   ÚgetError´   s   ýÿzSubscriptionPlanMixin.getErrorc                 C   s&   t jjtd}| ¡ dkr|d S d S )N)ÚkeyNamer   )ÚSubscriptionPlanr$   r%   ÚbasicPlanKeyNamer&   )r   ÚselectedPlanListr   r   r   ÚgetBasicPlan½   s   ÿz"SubscriptionPlanMixin.getBasicPlanc                 C   sF   t |tƒrt |¡}n|}|jdu r|jtjd}t tj¡}||kS )z=
        expirationDate: ISO 8601 string or datetime
        N)Útzinfo)	Ú
isinstanceÚstrr   Úfromisoformatr]   Úreplacer   ÚutcÚnow)r   ÚexpirationDateÚexpiration_dtÚ
current_dtr   r   r   ÚcheckExpirationDateÂ   s   

z)SubscriptionPlanMixin.checkExpirationDatec                 C   s   t jj|djdd d S )N©ÚfinancialProfileId__parentIdT©Ú	isExpired)ÚSubscriptionOrderr$   r%   Úupdate)r   rA   r   r   r   ÚupdateSubscriptionOrderÔ   s
   ÿ
ÿz-SubscriptionPlanMixin.updateSubscriptionOrderc                 C   s€   | j j dd ¡}|d ur>tjj|djdd}| ¡ dkr5|  |d j¡dkr,|d j	S |  
|¡ |  ¡ S |  
|¡ |  ¡ S d S )NrA   rh   Frj   r   T)r   r"   r   rl   r$   r%   r&   rg   ÚendDateÚsubscriptionPlanIdrn   r\   )r   rA   ÚcurrentListr   r   r   ÚgetCurrentSubscriptionPlanØ   s   ÿþ


z0SubscriptionPlanMixin.getCurrentSubscriptionPlanc                 C   s,   | j j dd¡}| dd¡}|dkrdS dS )Nú/r   Ú_mobileÚaiTF©r   Úpathra   ©r   rw   r   r   r   ÚcheckAIModelPathé   s
   z&SubscriptionPlanMixin.checkAIModelPathc                 C   s4   | j j dd¡}| dd¡}|dks|dkrdS dS )Nrs   r   rt   ÚchildÚuser_deviceTFrv   rx   r   r   r   ÚcheckModelPathð   s
   z$SubscriptionPlanMixin.checkModelPathc                    s°  |   ¡ }d}d}d}d }tƒ  ¡ }|  ¡ rT| jj dd ¡}|d u r-d}t |  	d d |¡¡‚tƒ  ¡ j
}dd„ |j ¡ D ƒ}	d|	v rH|j|d}nd}t |  	d d |¡¡‚|  |¡}|d ur¥|jrr|  ¡ d	kro|jrld	}n=d}n:d	}n7|jr¢d	}|d urŽ| j}
|
jj}|d
krŽ|jrŒd	}nd}| jj ¡ dkr¡| jjd jdkr¡d	}nd}nd}d}|d	kr¾|d	kr¼| ¡ dkr¼|d gS |S |d urÎt |  	|j|j|¡¡‚t |  	d d |¡¡‚)Nr   FrA   rH   c                 S   ó   h | ]}|j ’qS r   ©Úname©Ú.0Úfr   r   r   Ú	<setcomp>  ó    z5SubscriptionPlanMixin.get_queryset.<locals>.<setcomp>©rA   úParent Id is not in this modelTr	   zPaging is not allowedr   éÿÿÿÿÚListAPIViewzReading is not allowedúNo Plan Found)rr   ÚsuperÚget_querysetr|   r   r"   r   r   r   rW   ÚmodelÚ_metaÚ
get_fieldsr%   Úpaginate_querysetÚisUnLimitedry   ÚisAllowAIAssistÚisAllowReadingÚ	paginatorÚpageÚnumberÚisAllowPagingÚ	__class__Ú	__bases__r&   r   rB   rX   )r   ÚcurrentPlanr   ÚisAllowÚisSingleRecordr”   ÚqsrA   rŒ   Úfield_namesr“   Úcurrent_page©r—   r   r   r‹   ÷   sf   

€
ÿz"SubscriptionPlanMixin.get_querysetc           
         sº  |   ¡ }d}d}d}| jj dd ¡}|d u r"d}t |  d d |¡¡‚|  ¡ rn| jj dd ¡}|d u r>d}t |  d d |¡¡‚tƒ  	¡ j
}dd„ |j ¡ D ƒ}d|v rbtƒ  	¡ }	|	j|d}	|	 ¡ }nd	}t |  d d |¡¡‚tƒ  	¡ }	|	 ¡ }|d ur±|jr|  ¡ d
kr|jrŠd
}n)d}n&d
}n#|jr®|  ¡ d
kr£|dk r d
}nd}n||jk r«d
}nd}nd}nd}|d
krÃ| ¡ rÁ| ¡  d S d S |d urÓt |  |j|j|¡¡‚t |  d d |¡¡‚)NFr   r   rA   rH   c                 S   r}   r   r~   r€   r   r   r   rƒ   Q  r„   z7SubscriptionPlanMixin.perform_create.<locals>.<setcomp>r…   r†   Té   zMaximum Adding is reachedzAdding is not allowedr‰   )rr   r   r"   r   r   r   rW   r|   rŠ   r‹   rŒ   r   rŽ   r%   Úcountr   ry   r‘   ÚisAllowAddingÚaddingRecordCountÚis_validÚsaverB   rX   )
r   Ú
serializerr™   rš   r   ÚrecordCountrA   rŒ   r   rœ   rŸ   r   r   Úperform_create?  sb   



ÿÿz$SubscriptionPlanMixin.perform_createc                 C   s¤   |   ¡ }d}d}|d ur*|jr!|  ¡ dkr|jrd}nd}nd}n|jr'd}nd}nd}|dkr<| ¡ r:| ¡  d S d S |d urIt |  	||¡¡‚t |  	d |¡¡‚)Nr   FTzEditing is not allowedr‰   )
rr   r   ry   r‘   ÚisAllowEditingr¤   r¥   r   r   rW   )r   r¦   r™   r   rš   r   r   r   Úperform_updateƒ  s.   ÿ
ÿz$SubscriptionPlanMixin.perform_updatec                 C   s”   |   ¡ }d}|d ur(|jr|  ¡ dkr|jrd}nd}nd}n|jr%d}nd}nd}|dkr4| ¡  d S |d urAt |  ||¡¡‚t |  d |¡¡‚)NFTzDeleting is not allowedr‰   )	rr   r   ry   r‘   ÚisAllowDeletingÚdeleter   r   rW   )r   rŒ   r™   rš   r   r   r   r   Úperform_destroy£  s(   
ÿz%SubscriptionPlanMixin.perform_destroy)r   r   r   rW   r\   rg   rn   rr   ry   r|   r‹   r¨   rª   r­   Ú__classcell__r   r   rŸ   r   rS   ³   s    	HD rS   N)Úrest_frameworkr   r   Úmama_care_api.settingsr   r   r   r   Úuser.modelsÚcommonÚsubscription.modelsÚbasic.modelsÚBaseAuthenticationr   r   r*   r7   ÚPageNumberPaginationrN   rS   r   r   r   r   Ú<module>   s     !M