
    P1il                        d Z ddlZddlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ  ej                  e      Z e
       Z e       Z ee      d        Z ee      d	        Z ee      d
        Z ee      d        Zd Zd Zy)z
Django authentication signals for systematic user activity tracking.

This module uses Django's built-in authentication signals to automatically
track user activity for both web (session) and API (JWT) authentication.
    N)receiverSignal)user_logged_inuser_logged_outuser_login_failed)get_user_model)cache)timezonec                 b    t        |d       t        j                  d|j                   d       y)z
    Track user activity when user logs in via web interface.
    
    This signal is automatically fired by Django when a user logs in
    using the django.contrib.auth.login() function.
    	web_loginUser z logged in via web interfaceN)_update_user_activityloggerinfopksenderrequestuserkwargss       ./home/cursorai/projects/iching/main/signals.pytrack_user_login_activityr      s(     $,
KK%y <=>    c                     |r=|j                   r0t        |d       t        j                  d|j                   d       yyy)z
    Track user activity when user logs out via web interface.
    
    Note: We still update activity on logout to show the user was active
    at logout time.
    
web_logoutr   z logged out via web interfaceN)is_authenticatedr   r   r   r   r   s       r   track_user_logout_activityr   "   s:     %%dL1eDGG9$ABC &tr   c                 X    |j                  dd      }t        j                  d|        y)z
    Track failed login attempts.
    
    Note: This doesn't update last_active_date since login failed,
    but could be used for security monitoring.
    usernameunknownz#Failed login attempt for username: N)getr   warning)r   credentialsr   r   r   s        r   track_failed_login_attemptsr$   /   s(     z95H
NN8
CDr   c                 b    t        |d       t        j                  d|j                   d       y)z
    Track user activity when JWT token is validated.
    
    This custom signal should be fired whenever a JWT token is successfully
    validated in the authentication process.
    jwt_authr   z authenticated via JWTN)r   r   debugr   )r   r   r   r   s       r   !track_jwt_authentication_activityr(   ;   s(     $
+
LL5	!789r   c           	         	 d| j                    }d}t        j                  |      }t        j                         }|wt
        j                  j                  | j                         j                  |       t        j                  |||       t        j                  d| j                    d|        yy# t        $ r8}t        j                  d| j                    d	t        |              Y d}~yd}~ww xY w)
aC  
    Update user's last active date with intelligent caching.
    
    Uses cache to avoid updating the database on every request.
    Only updates if the last update was more than 5 minutes ago.
    
    Args:
        user: User instance
        activity_type: Type of activity ('web_login', 'web_logout', 'jwt_auth')
    user_activity_i,  N)r   )last_active_datez"Updated last_active_date for user z via z&Error updating user activity for user z: )r   r	   r!   r
   nowUserobjectsfilterupdatesetr   r'   	Exceptionerrorstr)r   activity_type	cache_keycache_timeoutlast_updater,   es          r   r   r   G   s    S$TWWI.	ii	*lln LL477+22C2H IIim4LL=dggYeM?[\   S=dggYbQQRRSs   B2B6 6	C7?.C22C7c                 <    t         j                  t        | |       y)a(  
    Helper function to manually fire the JWT authentication signal.
    
    This should be called from JWT authentication backends when a user
    is successfully authenticated via JWT token.
    
    Args:
        user: User instance that was authenticated
        request: Request object
    )r   r   r   N)jwt_authenticatedsendfire_jwt_authenticated_signal)r   r   s     r   r=   r=   h   s      ,  r   )__doc__loggingdjango.dispatchr   r   django.contrib.auth.signalsr   r   r   django.contrib.authr   django.core.cacher	   django.utilsr
   	getLogger__name__r   r-   r;   r   r   r$   r(   r   r=    r   r   <module>rH      s     , Z Z . # !			8	$ H  
.? ? 
/	D 	D 

E E 

: :SBr   