o
    ]h                     @   s8   d dl mZ d dlmZ d dlmZ G dd deZdS )    )BaseCommand)ApparelRequest)Maxc                   @   s   e Zd ZdZdd ZdS )CommandzRRemoves duplicate apparel requests, keeping only the most recent one for each userc                 O   s   t jjddd }t j }| jd|  d}|D ];}|d u r#qt jj|dd}| dkrW|	 }|j
|jd	}	| jd
| d|	  d ||	 7 }|	  qt j }
| j| jd| d | j| jd|
  d S )Nregistered_user_idT)flatz-Total apparel requests before deduplication: r   )r   z-created_at   )idzUser ID z has z) duplicate requests that will be removed.z Deduplication complete. Removed z duplicate requests.z,Total apparel requests after deduplication: )r   objectsvalues_listdistinctcountstdoutwritefilterorder_byfirstexcluder	   deletestyleSUCCESS)selfargsoptionsuser_idstotal_beforeduplicates_removeduser_iduser_requestsmost_recent	to_deletetotal_after r"   W/var/www/html/smartRegister/apparel/management/commands/deduplicate_apparel_requests.pyhandle   s$   

zCommand.handleN)__name__
__module____qualname__helpr$   r"   r"   r"   r#   r      s    r   N)django.core.management.baser   apparel.modelsr   django.db.modelsr   r   r"   r"   r"   r#   <module>   s    