o
    [h                     @   s  d dl mZ d dlmZ d dlmZmZ d dlm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	lmZ d d
lmZ ddlmZmZmZmZmZmZmZmZmZm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 Z0G d+d, d,ej Z1G d-d. d.ej Z2G d/d0 d0ej Z3d1S )2    )serializers)EmailMultiAlternatives)make_passwordcheck_passwordUser)Token)default_token_generator)urlsafe_base64_encodeurlsafe_base64_decode)force_bytes)	send_mail)settings   )
Module
PermissionRolePermissionRoleAdminMasterBannerGift
AppSettingFemaleRewardc                   @      e Zd ZG dd dZdS )ModuleSerializerc                   @      e Zd ZeZdZdS )zModuleSerializer.Meta__all__N)__name__
__module____qualname__r   modelfields r#   r#   Q/var/www/vedio/testing/chatpythonscript.ninositsolution.com/master/serializers.pyMeta       r%   Nr   r   r    r%   r#   r#   r#   r$   r          r   c                   @   r   )PermissionSerializerc                   @   r   )zPermissionSerializer.Metar   N)r   r   r    r   r!   r"   r#   r#   r#   r$   r%      r&   r%   Nr'   r#   r#   r#   r$   r)      r(   r)   c                   @   r   )PermissionRoleSerializerc                   @   r   )zPermissionRoleSerializer.Metar   N)r   r   r    r   r!   r"   r#   r#   r#   r$   r%      r&   r%   Nr'   r#   r#   r#   r$   r*      r(   r*   c                   @   s@   e Zd Zejdej ddZG dd dZ	dd Z
dd	 Zd
S )RoleSerializerTF)manyquerysetrequiredc                   @   r   )zRoleSerializer.Metar   N)r   r   r    r   r!   r"   r#   r#   r#   r$   r%   '   r&   r%   c                 C   s.   | dg }tjjdi |}|j| |S )Npermissionsr#   )popr   objectscreater/   set)selfvalidated_datapermissions_dataroler#   r#   r$   r2   +   s   zRoleSerializer.createc                 C   sJ   | dd }| D ]
\}}t||| q
|  |d ur#|j| |S )Nr/   )r0   itemssetattrsaver/   r3   )r4   instancer5   r6   attrvaluer#   r#   r$   update1   s   zRoleSerializer.updateN)r   r   r    r   PrimaryKeyRelatedFieldr   r1   allr/   r%   r2   r>   r#   r#   r#   r$   r+       s    r+   c                       s*   e Zd ZG dd dZ fddZ  ZS )AdminSerializerc                   @   s    e Zd ZeZdZdddiiZdS )zAdminSerializer.Metar   password
write_onlyTN)r   r   r    r   r!   r"   extra_kwargsr#   r#   r#   r$   r%   ?   s
    r%   c           	         s   | d}| d}| d}tjj|d|id\}}|r'|| |  nd}|r2|| d}|r>|j|kr>||_d}|rD|  |rLt||d< t 	|}|S )NrB   nameemail)usernamedefaultsFT)
getr   r1   get_or_createset_passwordr:   rF   r   superr2   )	r4   r5   rB   rE   rF   usercreatedupdatedadmin	__class__r#   r$   r2   F   s(   





zAdminSerializer.create)r   r   r    r%   r2   __classcell__r#   r#   rQ   r$   rA   >   s    rA   c                   @   r   )AdminUpdateSerializerc                   @      e Zd ZeZddgZdS )zAdminUpdateSerializer.MetarB   
admin_codeN)r   r   r    r   r!   excluder#   r#   r#   r$   r%   a       r%   Nr'   r#   r#   r#   r$   rT   `   r(   rT   c                   @   r   )AdminStatusUpdateSerializerc                   @      e Zd ZeZdgZdS )z AdminStatusUpdateSerializer.MetastatusN)r   r   r    r   r!   r"   r#   r#   r#   r$   r%   g       
r%   Nr'   r#   r#   r#   r$   rY   f   r(   rY   c                   @   r   )MasterSerializerc                   @   s   e Zd ZeZg dZdS )zMasterSerializer.Meta)idtyperE   value1r[   
created_at
updated_atNr   r   r    r   r!   r"   r#   r#   r#   r$   r%   m   rX   r%   Nr'   r#   r#   r#   r$   r]   l   r(   r]   c                   @   r   )MasterStatusUpdateSerializerc                   @   rZ   )z!MasterStatusUpdateSerializer.Metar[   Nrc   r#   r#   r#   r$   r%   s   r\   r%   Nr'   r#   r#   r#   r$   rd   r   r(   rd   c                   @   s(   e Zd Ze ZejddZdd ZdS )AdminLoginSerializerT)rC   c                 C   sd   | d}| d}z	tjj |d}W n tjy    tdw t||js,td||d< |S )NrF   rB   rF   zInvalid credentialsrP   )rI   r   r1   DoesNotExistr   ValidationErrorr   rB   )r4   datarF   rB   rP   r#   r#   r$   validate{   s   



zAdminLoginSerializer.validateN)	r   r   r    r   
EmailFieldrF   	CharFieldrB   rj   r#   r#   r#   r$   re   w   s    re   c                   @   s$   e Zd Ze Zdd Zdd ZdS )#AdminRequestPasswordResetSerializerc                 C   s    t jj|d std|S )Nrf   zNo admin found with this email.)r   r1   filterexistsr   rh   )r4   r=   r#   r#   r$   validate_email   s   
z2AdminRequestPasswordResetSerializer.validate_emailc                 C   s   | j d }tjj|d}ddlm} z
|jj|jd}W n |jy'   Y d S w tt	|j
}t|}tj d| d| d| }d	}tj}	|g}
d
| }d| d| d}t|||	|
}||d |  d S )NrF   rf   r   r   )rG   z/resetpassword?uid=z&token=z&email=zAdmin Password Resetz.Click the link below to reset your password:

zY
            <p>Click the link below to reset your password:</p>
            <p><a href="z">z</a></p>
        z	text/html)r5   r   r1   rI   django.contrib.auth.modelsr   rE   rg   r
   r   pkr	   
make_tokenr   FRONTEND_URLDEFAULT_FROM_EMAILr   attach_alternativesend)r4   rF   rP   r   rM   uidtoken	reset_urlsubject
from_emailrecipient_listtext_contenthtml_contentmsgr#   r#   r$   r:      s.   


z(AdminRequestPasswordResetSerializer.saveN)r   r   r    r   rk   rF   rp   r:   r#   r#   r#   r$   rm      s    rm   c                   @   s8   e Zd Ze Ze ZejddZdd Zdd Z	dS )#AdminConfirmResetPasswordSerializer   )
min_lengthc                 C   s^   zt |d  }tjj|d| _W n ty   tdw t	
| j|d s-td|S )Nrx   )rr   zInvalid UIDry   zInvalid or expired token)r   decoder   r1   rI   rM   	Exceptionr   rh   r	   check_token)r4   ri   rx   r#   r#   r$   rj      s   

z,AdminConfirmResetPasswordSerializer.validatec                 C   sb   | j d }| j| | j  ztjj| jjd}t||_	|  W d S  tj
y0   Y d S w )Nnew_password)rE   )r5   rM   rK   r:   r   r1   rI   rG   r   rB   rg   )r4   rB   rP   r#   r#   r$   r:      s   


z(AdminConfirmResetPasswordSerializer.saveN)
r   r   r    r   rl   rx   ry   r   rj   r:   r#   r#   r#   r$   r      s    r   c                   @   r   )BannerSerializerc                   @   r   )zBannerSerializer.Metar   Nr   r   r    r   r!   r"   r#   r#   r#   r$   r%      r&   r%   Nr'   r#   r#   r#   r$   r      r(   r   c                   @   r   )BannerStatusSerializerc                   @   r   )zBannerStatusSerializer.Meta)r[   Nr   r#   r#   r#   r$   r%      r&   r%   Nr'   r#   r#   r#   r$   r      r(   r   c                   @   r   )BannerImageTypeUpdateSerializerc                   @   rU   )z$BannerImageTypeUpdateSerializer.Metar_   imageNr   r#   r#   r#   r$   r%      rX   r%   Nr'   r#   r#   r#   r$   r      r(   r   c                   @   r   )GiftSerializerc                   @   r   )zGiftSerializer.Metar   N)r   r   r    r   r!   r"   r#   r#   r#   r$   r%      r&   r%   Nr'   r#   r#   r#   r$   r      r(   r   c                   @   r   )AppSettingSerializerc                   @   s    e Zd ZeZg dZg dZdS )zAppSettingSerializer.Meta)r^   rE   coder=   notesra   rb   )r^   ra   rb   N)r   r   r    r   r!   r"   read_only_fieldsr#   r#   r#   r$   r%      s    r%   Nr'   r#   r#   r#   r$   r      r(   r   c                   @   s*   e Zd Ze ZG dd dZdd ZdS )FemaleRewardSerializerc                   @   r   )zFemaleRewardSerializer.Metar   N)r   r   r    r   r!   r"   r#   r#   r#   r$   r%      r&   r%   c                 C   s   |j  d|j S )Nz - )	start_minend_min)r4   objr#   r#   r$   get_duration   s   z#FemaleRewardSerializer.get_durationN)r   r   r    r   SerializerMethodFielddurationr%   r   r#   r#   r#   r$   r      s    r   N)4rest_frameworkr   django.core.mailr   django.contrib.auth.hashersr   r   rq   r   rest_framework.authtoken.modelsr   django.contrib.auth.tokensr	   django.utils.httpr
   r   django.utils.encodingr   r   django.confr   modelsr   r   r   r   r   r   r   r   r   r   ModelSerializerr   r)   r*   r+   rA   rT   rY   r]   rd   
Serializerre   rm   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   s:    0"%