o
    g&                     @   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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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 G dd dejZdd 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 ))    )serializers)User)	smart_strDjangoUnicodeDecodeError)PasswordResetTokenGenerator   )r   CodeConfirmationusertypeN)render_to_string)
strip_tags)EmailMultiAlternatives)failed_response)status)urlsafe_base64_decode)	force_str)settings)	send_mailc                   @   s*   e Zd Ze ZG dd dZdd ZdS )UserRegistrationSerializerc                   @   s$   e Zd ZeZg dZdddiiZdS )zUserRegistrationSerializer.Meta)
first_name	last_nameusernameemailpasswordusertype_id
pan_numberphone_numberr   
write_onlyTN__name__
__module____qualname__r   modelfieldsextra_kwargs r$   r$   A/var/www/html/authentication-server/authentication/serializers.pyMeta   s
    r&   c                 C   sN   | d}tjj|d stdtjdtjj	di |}||_
|  |S )Nr   idzUser Type id doesn't existmsgcoder$   )popr	   objectsfilterexistsr   r   HTTP_404_NOT_FOUNDr   create_userr   save)selfvalidated_datar   userr$   r$   r%   create   s   
z!UserRegistrationSerializer.createN)r   r   r    r   IntegerFieldr   r&   r6   r$   r$   r$   r%   r      s    r   c                 C   sR   d}|dd}t d|}t|}tj}t|||| g}||d |jdd d S )NzVerify Your Emailz
30 minutes)r+   expiry_timeotp_email.html	text/htmlFfail_silently)r
   r   r   EMAIL_HOST_USERr   attach_alternativesend)r   r+   subjectcontexthtml_messageplain_message
from_emailemail_messager$   r$   r%   send_verification_email(   s   
rF   c                   @   s@   e Zd ZejdddZejdddZejddidZdd Z	d	S )
UserLoginSerializerFT)requiredallow_blank
input_typer   )stylec                 C   sJ   | d}| d}| d}|s|stdtjd|s#tdtjd|S )Nr   r   r   z$Either username or email is requiredr)   zPassword is required)getr   r   HTTP_400_BAD_REQUEST)r3   attrsr   r   r   r$   r$   r%   validateF   s   


zUserLoginSerializer.validateN)
r   r   r    r   	CharFieldr   
EmailFieldr   r   rO   r$   r$   r$   r%   rG   A   s
    rG   c                   @   J   e Zd ZejdddiddZejdddiddZG dd dZdd	 Zd
S )UserChangePasswordSerializer   rJ   r   T
max_lengthrK   r   c                   @      e Zd ZddgZdS )z!UserChangePasswordSerializer.Metar   	password2Nr   r   r    r"   r$   r$   r$   r%   r&   U       r&   c                 C   sL   | d}| d}| j d}||krtdtjd|| |  |S )Nr   rX   r5   z*Password and confirm password do not matchr+   )rL   rA   r   r   rM   set_passwordr2   )r3   rN   r   rX   r5   r$   r$   r%   rO   X   s   


z%UserChangePasswordSerializer.validateN	r   r   r    r   rP   r   rX   r&   rO   r$   r$   r$   r%   rS   Q   
    rS   c                   @   s.   e Zd ZejddZG dd dZdd ZdS ) SendPasswordResetEmailSerializerrT   rV   c                   @   s   e Zd ZdgZdS )z%SendPasswordResetEmailSerializer.Metar   NrY   r$   r$   r$   r%   r&   e   s    
r&   c                 C       t jj|d std|S Nr   z$User with this email does not exist.r   r-   r.   r/   r   ValidationErrorr3   valuer$   r$   r%   validate_emailh      
z/SendPasswordResetEmailSerializer.validate_emailN)r   r   r    r   rQ   r   r&   rh   r$   r$   r$   r%   r_   b   s    r_   c                   @   rR   )UserPasswordResetSerializerrT   rJ   r   TrU   c                   @   rW   )z UserPasswordResetSerializer.Metar   rX   NrY   r$   r$   r$   r%   r&   s   rZ   r&   c           	   
   C   s   zH| d}| d}| j d}| j d}||kr"tdtjdtt|}tjj |d}t	 
||s=tdtjd|| |  |W S  tyb } zt	 
|| td	tjd
d }~ww )Nr   rX   uidtokenz*Password and Confirm Password do not matchr[   r'   z"Token is either invalid or expiredzToken is not valid or expiredr)   )rL   rA   r   r   rM   r   r   r   r-   r   check_tokenr\   r2   r   )	r3   rN   r   rX   rk   rl   user_idr5   
identifierr$   r$   r%   rO   v   s&   


z$UserPasswordResetSerializer.validateNr]   r$   r$   r$   r%   rj   o   r^   rj   c                   @   sV   e Zd Ze ZejdddZeje	j
dZG dd dZdd Zd	d
 Zdd ZdS )SendCodeSerializer   T)rV   	read_onlychoicesc                   @      e Zd ZeZg dZdS )zSendCodeSerializer.Meta)r   r+   typeN)r   r   r    r   r!   r"   r$   r$   r$   r%   r&          r&   c                 C   ra   rb   rd   rf   r$   r$   r%   rh      ri   z!SendCodeSerializer.validate_emailc                 C   sn   |d }|d }t jj||ddjdd ttdd}||d	< t jj|||dd
d\}}| || |S )Nr   rv   T)r   rv   pendingF)rx   i i?B r+   )r+   rx   )r   rv   defaults)	r   r-   r.   updatestrrandomrandintupdate_or_createsend_otp_email)r3   r4   r   confirmation_typer+   code_confirmationcreatedr$   r$   r%   r6      s   
zSendCodeSerializer.createc                 C   sR   d}t dd|i}t|}tjd}t||||g}||d |jdd d S )NzYour OTP Coder9   r+   
EMAIL_USERr:   Fr;   )r
   r   osenvironrL   r   r>   r?   )r3   r   r+   r@   rB   rC   rD   rE   r$   r$   r%   r      s   z!SendCodeSerializer.send_otp_emailN)r   r   r    r   rQ   r   rP   r+   ChoiceFieldr   TYPE_CHOICESrv   r&   rh   r6   r   r$   r$   r$   r%   rp      s    rp   c                   @   sJ   e Zd Ze ZejddZeje	j
dZejddZdd Zdd	 Zd
S )VerifyCodeSerializerrq   r`   rs   F)rH   c                 C   s   | d}| d}| d}| d}|tjkr4||kr$tdtjdtjj|d	 r4tdtjdtjj||d	|d
	 sGtdtjd|S )Nr   r+   	new_emailrv   z1New email can't be the same as the current email.r)   rc   zNew email is already in use.Tr   r+   rx   rv   zInvalid code or email.)
rL   r   EMAIL_CHANGEr   r   rM   r   r-   r.   r/   )r3   datar   r+   r   r   r$   r$   r%   rO      s   




zVerifyCodeSerializer.validatec                 C   s   | j d }| j d }| j d }tjj||d|d}d|_|  |tjkr:| j d }tjj|d}||_|  |S |tj	krMtjj|d}d|_
|  |S )	Nr   r+   rv   Tr   Fr   rc   )r4   r   r-   rL   rx   r2   r   r   r   EMAIL_CONFIRMATION	is_active)r3   r   r+   r   confirmationr   r5   r$   r$   r%   r2      s&   





zVerifyCodeSerializer.saveN)r   r   r    r   rQ   r   rP   r+   r   r   r   rv   r   rO   r2   r$   r$   r$   r%   r      s    r   c                   @   s0   e Zd ZejddZe Zeje dZ	dS )EmailSerializerrT   r`   )childN)
r   r   r    r   rP   r@   message	ListFieldrQ   recipient_listr$   r$   r$   r%   r      s    
r   c                   @   s"   e Zd ZG dd dZdd ZdS )UserSerializerc                   @   s*   e Zd ZeZg dZddiddidZdS )zUserSerializer.Meta)r   r   middle_namer   r   profile_imgr   r   TrH   F)r   r   Nr   r$   r$   r$   r%   r&      s    
r&   c                 C   sD   | dd }| D ]
\}}t||| q
|r|| |  |S )Nr   )r,   itemssetattrr\   r2   )r3   instancer4   r   attrrg   r$   r$   r%   rz      s   
zUserSerializer.updateN)r   r   r    r&   rz   r$   r$   r$   r%   r      s    r   c                   @      e Zd ZG dd dZdS )UserTypeSerializerc                   @   s   e Zd ZeZddgZdS )zUserTypeSerializer.Metarv   subtypeN)r   r   r    r	   r!   r"   r$   r$   r$   r%   r&      rw   r&   Nr   r   r    r&   r$   r$   r$   r%   r          r   c                   @   r   )UserDetailSerializerc                   @   ru   )zUserDetailSerializer.Meta)r   r   r   r   r   verifiedr   N)r   r   r    r   r!   r"   r$   r$   r$   r%   r&     rw   r&   Nr   r$   r$   r$   r%   r     r   r   )+rest_frameworkr   authentication.modelsr   django.utils.encodingr   r   django.contrib.auth.tokensr   modelsr   r	   r   django.template.loaderr
   django.utils.htmlr   django.core.mailr   r|   utils.helperr   r   django.utils.httpr   r   django.confr   r   ModelSerializerr   rF   
SerializerrG   rS   r_   rj   rp   r   r   r   r   r   r$   r$   r$   r%   <module>   s:    &/