U
    hF                     @   sL   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 )	    )models)timezone)ContentTypec                       s    e Zd ZdZ fddZ  ZS )ActivityCategoryManagerzNCustom manager for ActivityCategory model that returns only active categories.c                    s   t   jddS )z*Override to return only active categories.T)	is_active)superget_querysetfilterself	__class__ 3/var/www/html/Focus/src/apps/activities/managers.pyr   	   s    z$ActivityCategoryManager.get_queryset)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s   r   c                   @   sZ   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
dd Zdd ZdS )ActivityManagerz
    Custom manager for Activity model with additional query methods.
    
    This manager provides convenient methods for querying activities
    with common filters and aggregations.
    c                 C   s   | j |dS )z
        Get activities for a specific user.
        
        Args:
            user: User instance to filter activities for
        
        Returns:
            QuerySet: Activities performed by the specified user
        )userr	   r   r   r   r   r   for_user   s    
zActivityManager.for_userc                 C   s
   |  |S )zAlias for for_user method.)r   r   r   r   r   by_user"   s    zActivityManager.by_userc                 C   s   t j|}| j||jdS )z
        Get activities for a specific object.
        
        Args:
            obj: Model instance to get activities for
        
        Returns:
            QuerySet: Activities related to the specified object
        )content_type	object_id)r   objectsget_for_modelr	   pk)r   objr   r   r   r   
for_object&   s    
zActivityManager.for_objectc                 C   s   | j |dS )z
        Get activities by category code.
        
        Args:
            category_code (str): Category code to filter by
        
        Returns:
            QuerySet: Activities in the specified category
        )Zcategory__coder   )r   Zcategory_coder   r   r   by_category3   s    
zActivityManager.by_category   c                 C   s    t  t j|d }| j|dS )z
        Get recent activities within the specified number of days.
        
        Args:
            days (int): Number of days to look back (default: 7)
        
        Returns:
            QuerySet: Activities from the last N days
        )days)created_at__gte)r   now	timedeltar	   )r   r$   cutoff_dater   r   r   recent?   s    
zActivityManager.recentc                 C   s   | j |dS )z
        Get activities by action type.
        
        Args:
            action (str): Action type to filter by
        
        Returns:
            QuerySet: Activities with the specified action
        )actionr   )r   r*   r   r   r   	by_actionL   s    
zActivityManager.by_actionc                 C   s   | j ddS )z
        Get only successful activities.
        
        Returns:
            QuerySet: Activities that completed successfully
        TZis_successfulr   r
   r   r   r   
successfulX   s    zActivityManager.successfulc                 C   s   | j ddS )zt
        Get only failed activities.
        
        Returns:
            QuerySet: Activities that failed
        Fr,   r   r
   r   r   r   faileda   s    zActivityManager.failedc                 C   s   | j ||dS )a  
        Get activities within a specific date range.
        
        Args:
            start_date: Start date for the range
            end_date: End date for the range
        
        Returns:
            QuerySet: Activities within the specified date range
        )r%   Zcreated_at__lter   )r   
start_dateend_dater   r   r   in_date_rangej   s    zActivityManager.in_date_rangeN)r#   )r   r   r   r   r   r   r!   r"   r)   r+   r-   r.   r1   r   r   r   r   r      s   
		r   N)		django.dbr   django.utilsr   "django.contrib.contenttypes.modelsr   Managerr   r   r   r   r   r   <module>   s   