
    .i                     J    d dl mZ d dlmZ d dlmZ d dlmZ  G d de      Zy)    )TokenObtainPairSerializer)serializers)UserCompanyRole)APIExceptionc                        e Zd Z fdZ xZS )CustomTokenObtainPairSerializerc                    t         |   |      }| j                  }|j                  dk(  r|S t        j
                  j                  |d      j                         }|st        d      |j                  j                  d      }|j                         st        d      |j                  r|j                  j                  s*|j                         }||_
        |j                  d	g
       |S )Nsuper_adminF)userrole__isnullz2Please ask your dealership admin to assign a role.)detailT)	is_activezINo active companies found for this user. Please contact dealership admin.active_company)update_fields)supervalidater   	user_typer   objectsfilterexistsr   	companiesr   r   firstsave)selfattrsdatar   has_roleactive_companiesfirst_active_company	__class__s          C/var/www/html/dp2/backend_v2.1/server/apps/core/serializers/auth.pyr   z(CustomTokenObtainPairSerializer.validate
   s    w&yy>>]*K"**11t%1PWWYK   >>0040@&&(jkk""$*=*=*G*G#3#9#9#; "6DII%5$6I7    )__name__
__module____qualname__r   __classcell__)r    s   @r!   r   r      s     r"   r   N)	$rest_framework_simplejwt.serializersr   rest_frameworkr   apps.permissions.modelsr   rest_framework.exceptionsr   r    r"   r!   <module>r,      s    J & 3 2&? r"   