B
    ÒîbÀ  ã               @   s€   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 d dlmZ G dd	„ d	ejƒZG d
d„ dejƒZdS )é    )Úserializers)Úget_username_fieldÚPasswordField)Úapi_settings)Úgettext_lazy)ÚEmployee)ÚCompany)ÚMyUserc                   sD   e Zd Z‡ fdd„Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Z‡  Z	S )ÚAppAuthTokenSerializerc                s6   t t| ƒj||Ž t ¡ | j| j< tdd| jd< dS )zD
        Dynamically add the USERNAME_FIELD to self.fields.
        T)Ú
write_onlyÚpasswordN)Úsuperr
   Ú__init__r   Ú	CharFieldÚfieldsÚusername_fieldr   )ÚselfÚargsÚkwargs)Ú	__class__© úQG:\easytimepro\master/mysite/mobile\api_rest\serializers\auth_token_serializer.pyr      s    zAppAuthTokenSerializer.__init__c             C   s   t ƒ S )N)r   )r   r   r   r   r      s    z%AppAuthTokenSerializer.username_fieldc             C   s   | j S )N)Úvalidated_data)r   r   r   r   Úobject   s    zAppAuthTokenSerializer.objectc             C   s   t  |¡}t  |¡}|S )N)r   ÚJWT_PAYLOAD_HANDLERÚJWT_ENCODE_HANDLER)r   ÚempÚpayloadÚtokenr   r   r   Ú	get_token    s    

z AppAuthTokenSerializer.get_tokenc             C   sš   | j | | j ¡d| d¡i}| | j ¡}d|krDtjj|d ¡ }ntjj|d ¡ }|rˆ| | d¡¡rˆt|  |¡ƒ |  |¡|dœS t	 
ddi¡‚d S )Nr   ú@)Zemail)Úmobile)r   ÚuserÚmessagezusername or password error.)r   Úgetr   ÚobjectsÚfilterÚfirstÚcheck_passwordÚprintr   r   ÚValidationError)r   ÚattrsÚcredentialsÚusernamer   r   r   r   Úvalidate%   s    zAppAuthTokenSerializer.validate)
Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r   r   r.   Ú__classcell__r   r   )r   r   r
      s
   r
   c                   sD   e Zd Z‡ fdd„Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Z‡  Z	S )ÚAppAuthTokenCompanySerializerc                sD   t t| ƒj||Ž t ¡ | j| j< tdd| jd< t ¡ | jd< dS )zD
        Dynamically add the USERNAME_FIELD to self.fields.
        T)r   r   ÚcompanyN)r   r4   r   r   r   r   r   r   )r   r   r   )r   r   r   r   7   s    z&AppAuthTokenCompanySerializer.__init__c             C   s   t ƒ S )N)r   )r   r   r   r   r   @   s    z,AppAuthTokenCompanySerializer.username_fieldc             C   s   | j S )N)r   )r   r   r   r   r   D   s    z$AppAuthTokenCompanySerializer.objectc             C   s   t  |¡}t  |¡}|S )N)r   r   r   )r   r   r   r   r   r   r   r   H   s    

z'AppAuthTokenCompanySerializer.get_tokenc       	      C   s|  | j | | j ¡d| d¡d| d¡i}t| ¡ ƒrT|d }tjj|d ¡ }|shtdƒ}t	 
d|i¡‚y8tjj| | j ¡|jd}| | d¡¡rš|}nd }W nb tk
r   y2tjj| | j ¡|jd}| | d¡¡sâd }W n tk
rü   d }Y nX Y nX |r<|js(tdƒ}t	 
d|i¡‚|  |¡}||d	œS td
ƒ}t	 
d|i¡‚n$tdƒ}|j| j d}t	 
d|i¡‚d S )Nr   r5   )Zname__iexactz'Unable to log in with provided company.r#   )Zemp_codeZ
company_id)r-   Zcurrent_companyzUser account is disabled.)r   r"   z+Unable to log in with provided credentials.z=Must include "{username_field}" and "password" and "company".)r   )r   r$   ÚallÚvaluesr   r%   r&   r'   Ú_r   r*   r   Úidr(   Ú	Exceptionr	   Z	is_activer   Úformat)	r   r+   r,   r5   Zcompany_objÚmsgr   r"   r   r   r   r   r.   M   sB    

z&AppAuthTokenCompanySerializer.validate)
r/   r0   r1   r   r2   r   r   r   r.   r3   r   r   )r   r   r4   6   s
   	r4   N)Zrest_frameworkr   Zrest_framework_jwt.compatr   r   Zrest_framework_jwt.settingsr   Zdjango.utils.translationr   r8   Zmysite.personnel.modelsr   Zmysite.cloud.modelsr   Zmysite.accounts.modelsr	   Z
Serializerr
   r4   r   r   r   r   Ú<module>   s   '