a
    d*                     @   s  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m
Z
mZ d dlmZmZ d dlmZ d d	lmZ e 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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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'ej"Z%G d(d) d)ej"Z&G d*d+ d+ej"Z'd,S )-    )get_user_model)RegexValidator)serializers)ValidationError)UniqueValidator)datedatetime	timedelta)CompanySubscription)Profile)Widgetc                   @   s   e Zd ZG dd dZdS )UserCompanySerializerc                   @   s   e Zd ZeZg dZdS )zUserCompanySerializer.Meta)idnameurlcurrency_symbolis_verifiedN)__name__
__module____qualname__r
   modelfields r   r   M/var/www/html/Ranjet/backend/verify_trusted/users/api/serializers/__init__.pyMeta   s   r   Nr   r   r   r   r   r   r   r   r      s   r   c                   @   s"   e Zd ZG dd dZdd ZdS )ProfileSerializerc                   @   s   e Zd ZeZg dZdS )zProfileSerializer.Meta)user
first_name	last_namephotoaddresslanguagedue_dateunsubscribe_emailconnect_tokenshopify_url	charge_idphone_numberactiveNr   r   r   r   r   r   r   r   r   r   r      s   r   c                 C   s4   t jj|d d |d< tf i |}|  |S )Nr   )pk)Userobjectsfilterfirstr   saveselfvalidated_dataprofiler   r   r   create   s    zProfileSerializer.createN)r   r   r   r   r6   r   r   r   r   r      s   r   c                   @   s|   e Zd Ze ZejddddZedddZ	e
 Ze
 Ze
 Ze
 ZG dd dZdd Zd	d
 Zdd Zdd ZdS )UserSerializerTF)	read_onlyrequireddefault)r9   manyc                   @   s   e Zd ZeZg dZdS )zUserSerializer.Meta)r   emailr   is_superuserr5   is_owner	companiesip	is_expireis_trialr$   	widget_idNr   r   r   r-   r   r   r   r   r   r   r   (   s   r   c                 C   s0   t jj|jjd }|d ur,|jt kS dS NZcompany__userF)	r   r.   r/   r5   r   r0   r$   r   todayr3   objsubscriptionr   r   r   get_is_expire9   s    zUserSerializer.get_is_expirec                 C   s(   t jj|jjd }|r$|jd u S dS rE   )r   r.   r/   r5   r   r0   paymentrH   r   r   r   get_is_trial?   s    zUserSerializer.get_is_trialc                 C   s$   t jj|jjd }|r |jS d S NrF   )r   r.   r/   r5   r   r0   r$   rH   r   r   r   get_due_dateC   s    zUserSerializer.get_due_datec                 C   s$   t jj|jjd }|r |jS d S rN   )r   r.   r/   r5   r   r0   r   )r3   rI   widgetr   r   r   get_widget_idG   s    zUserSerializer.get_widget_idN)r   r   r   r   r5   r   BooleanFieldr>   r   r?   SerializerMethodFieldrA   rB   r$   rC   r   rK   rM   rO   rQ   r   r   r   r   r7       s   r7   c                   @   s.   e Zd ZejddZG dd dZdd ZdS )CreateProfileSerializerNr:   c                   @   s   e Zd ZeZdZdS )zCreateProfileSerializer.Meta__all__Nr+   r   r   r   r   r   O   s   r   c                 C   s   t f i |}|  |S )N)r   r1   r2   r   r   r   r6   S   s    zCreateProfileSerializer.create)r   r   r   r   HiddenFieldr   r   r6   r   r   r   r   rT   L   s   rT   c                       s0   e Zd Ze ZG dd dZ fddZ  ZS )CreateUserSerializerc                   @   sV   e Zd ZeZg dZddddddddd	ddeej d
dgdddddZ	dS )zCreateUserSerializer.Metar<   r   r=   r5   FT       ZMSG001ZMSG003)blank
max_length
min_length)allow_blankr9   r^   r]   error_messagesMSG017)querysetmessageZMSG006MSG005)invalidr\   )r_   r9   
validatorsr`   )r   r<   N)
r   r   r   r-   r   r   r   r.   allextra_kwargsr   r   r   r   r   \   s(   r   c                    s\   |d |d< d|d< | dd }t |}t|d}|jdd ||jd< ||j |S )	Nr<   usernameT	is_activer5   dataraise_exceptionr   )popsuperr6   rT   is_validr4   )r3   r4   r5   r   profile_serializer	__class__r   r   r6   x   s    

zCreateUserSerializer.create)r   r   r   rT   r5   r   r6   __classcell__r   r   rs   r   rX   Y   s   rX   c                   @   s    e Zd Ze ZG dd dZdS )AdminUserSerializerc                   @   s   e Zd ZeZg dZdS )zAdminUserSerializer.Meta)r   ri   passwordr=   r<   r   r5   NrD   r   r   r   r   r      s   r   N)r   r   r   r   r5   r   r   r   r   r   rv      s   rv   c                   @   s   e Zd ZG dd dZdS )AdminUpdateProfileSerializerc                   @   s   e Zd ZeZdZdS )z!AdminUpdateProfileSerializer.Meta)r   N)r   r   r   r   r   excluder   r   r   r   r      s   r   Nr   r   r   r   r   rx      s   rx   c                       s0   e Zd Ze ZG dd dZ fddZ  ZS )AdminUpdateUserSerializerc                   @   s   e Zd ZeZdZdS )zAdminUpdateUserSerializer.MetarY   NrD   r   r   r   r   r      s   r   c                    s   | dd }t ||}t|d}|jdd tjj|jd}|jf i |j	 d|j	v r~|
 }|j	d |_|  ||_|S Nr5   rk   Trm   )user_idr!   )ro   rp   updateProfileUpdateSerializerrq   r   r.   r/   r   r4   r0   r!   r1   r5   r3   instancer4   r5   r   rr   user_profileZ
user_photors   r   r   r}      s    

z AdminUpdateUserSerializer.update)r   r   r   rx   r5   r   r}   ru   r   r   rs   r   rz      s   rz   c                   @   s   e Zd ZG dd dZdS )r~   c                   @   s.   e Zd ZeZdZedddZddegiiZdS )zProfileUpdateSerializer.Meta)r   r*   z^\+?1?\d{9,15}$ZMSG025)regexrc   phonerf   N)	r   r   r   r   r   ry   r   Zphone_regexrh   r   r   r   r   r      s   r   Nr   r   r   r   r   r~      s   r~   c                       sT   e Zd Ze ZejddZejddZG dd dZ	 fddZ
 fdd	Z  ZS )
UserUpdateSerializerNrU   Tc                   @   s   e Zd ZeZdZdS )zUserUpdateSerializer.Meta)r<   ri   rj   r   r5   NrD   r   r   r   r   r      s   r   c                    s|   d|v rXt |d dkrXtjj|d d }|d urBtddi|d |d< d|d< n|dd  |dd  t |S )Nr<   r   )r<   ra   ri   Trj   )	lenr-   r.   r/   r0   r   ro   rp   validate)r3   rl   r   rs   r   r   r      s    
zUserUpdateSerializer.validatec                    s   | dd }tt| ||}t|d}|jdd tjj|j	d}|jf i |j
 d|j
v r| }|j
d |_|  |j
 dd  |S r{   )ro   rp   r   r}   r~   rq   r   r.   r/   r   r4   r0   r!   r1   r   rs   r   r   r}      s    

zUserUpdateSerializer.update)r   r   r   r~   r5   r   rW   ri   rj   r   r   r}   ru   r   r   rs   r   r      s   r   c                   @   s"   e Zd Ze ZG dd dZdS )EmailVerificationSerializerc                   @   s   e Zd ZeZdgZdS )z EmailVerificationSerializer.MetatokenNrD   r   r   r   r   r      s   r   N)r   r   r   r   	CharFieldr   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZG dd dZdS )ResendEmailSerializerc                   @   s   e Zd ZeZdgZdS )zResendEmailSerializer.Metar<   NrD   r   r   r   r   r      s   r   Nr   r   r   r   r   r      s   r   c                   @   sd   e Zd ZejdddddidZejdddddidZejddddZejddd	Z	G d
d dZ
dS )ContactSerializerd   TFr\   rd   r]   r9   r_   r`   i  )r]   r9   r_   )r9   r_   c                   @   s   e Zd ZdZdS )zContactSerializer.MetarV   Nr   r   r   r   r   r   r   r   r     s   r   N)r   r   r   r   r   r   
EmailFieldr<   rc   websiter   r   r   r   r   r      s0   r   c                   @   s\   e Zd ZejdddddidZejdddddidZejdddddidZG dd	 d	Zd
S )AddShopifyUrlSerializerr   TFr\   rd   r      c                   @   s   e Zd ZdZdS )zAddShopifyUrlSerializer.MetarV   Nr   r   r   r   r   r   %  s   r   N)	r   r   r   r   r   r&   r'   shopify_tokenr   r   r   r   r   r     s,   	r   c                   @   sF   e Zd ZejdddddidZejdddddidZG dd	 d	Zd
S )LogoutShopifyUrlSerializerr   TFr\   rd   r   r   c                   @   s   e Zd ZdZdS )zLogoutShopifyUrlSerializer.MetarV   Nr   r   r   r   r   r   :  s   r   N)r   r   r   r   r   r&   r'   r   r   r   r   r   r   (  s   	r   c                   @   s0   e Zd ZejdddddidZG dd dZd	S )
GetUserByShopifySerializerr   TFr\   rd   r   c                   @   s   e Zd ZdZdS )zGetUserByShopifySerializer.MetarV   Nr   r   r   r   r   r   G  s   r   N)r   r   r   r   r   r'   r   r   r   r   r   r   =  s   	r   c                   @   s\   e Zd ZejdddddidZejdddddidZejdddddidZG dd dZd	S )
PurchasePlanSerializerr   TFr\   rd   r   c                   @   s   e Zd ZdZdS )zPurchasePlanSerializer.MetarV   Nr   r   r   r   r   r   e  s   r   N)	r   r   r   r   r   plan_id
company_idr(   r   r   r   r   r   r   K  s,   	r   N)(django.contrib.authr   Zdjango.core.validatorsr   rest_frameworkr   rest_framework.exceptionsr   rest_framework.validatorsr   r   r   r	   verify_trusted.companies.modelsr
   r   Zverify_trusted.users.modelsr   Zverify_trusted.widgets.modelsr   r-   ModelSerializerr   r   r7   rT   rX   rv   rx   rz   r~   r   r   r   
Serializerr   r   r   r   r   r   r   r   r   <module>   s4   ,+	$