a
    xd                     @   s\   d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	 G dd dZ
G dd dejZd	S )
    )
connectionmodels)OuterRefSubquery)timezone)get_change_reason_from_objectc                   @   s   e Zd Zdd Zdd ZdS )HistoryDescriptorc                 C   s
   || _ d S N)model)selfr
    r   N/var/www/html/Ranjet/env/lib/python3.9/site-packages/simple_history/manager.py__init__	   s    zHistoryDescriptor.__init__c                 C   s   |d u rt | jS t | j|S r	   )HistoryManagerr
   )r   instanceownerr   r   r   __get__   s    
zHistoryDescriptor.__get__N)__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c                       sT   e Zd Zd fdd	Z fddZdd Zdd	 Zd
d Zdd ZdddZ	  Z
S )r   Nc                    s   t t|   || _|| _d S r	   )superr   r   r
   r   )r   r
   r   	__class__r   r   r      s    zHistoryManager.__init__c                    s   t t|  S r	   )r   r   get_queryset)r   r   r   r   get_super_queryset   s    z!HistoryManager.get_super_querysetc                 C   sb   |   }| jd u r|S t| jjjtjr:| jjjjd }n| jjjj}|   jf i || jjiS )N_id)	r   r   
isinstance_metapkr   
ForeignKeynamefilter)r   qsZkey_namer   r   r   r      s    
zHistoryManager.get_querysetc                 C   s   | j std| jjjg }t| jdg }| j jjD ]:}|j|v rFq6t	|t
jrd||jd  q6||j q6t|}z|  j| d }W n( ty   | j d| j jj Y n0 | j jf i |S )zX
        Returns the most recent copy of the instance available in the history.
        z.Can't use most_recent() without a {} instance._history_excluded_fieldsr   r   z%s has no historical record.)r   	TypeErrorformatr
   r   object_namegetattrfieldsr    r   r   r   appendtupler   values
IndexErrorDoesNotExistr   )r   tmpZexcluded_fieldsfieldr(   r+   r   r   r   most_recent&   s,    

zHistoryManager.most_recentc                 C   s|   | j s| |S |  j|d}z|d }W n( tyT   | j d| j jj Y n0 |jdkrv| j d| j jj |j S )zGet a snapshot as of a specific date.

        Returns an instance, or an iterable of the instances, of the
        original model with all the attributes set according to what
        was present on the object on the date provided.
        Zhistory_date__lter   z%s had not yet been created.-z%s had already been deleted.)	r   
_as_of_setr   r!   r,   r-   r   r&   history_type)r   datequerysetZhistory_objr   r   r   as_ofC   s    


zHistoryManager.as_ofc                 c   s  t |  j}|jjj}|  j|d}tj	}|dkr|i }|
ddD ]"}t|||vrF|j|t||< qF|j| d}nr|dkr|
|dd|jddd	}	|j|	d}n>|jf i |t|i
dddd d
 }	|jt|	d}|jdd
|}
|
D ]}|jV  qd S )Nr1   Zmysqlz-history_datez-pk)Zhistory_id__inZ
postgresqlr   T)Zflat   r2   )r4   )typer
   r   r   r   r    r   r!   r   vendorZorder_byr'   r+   ZdistinctZvalues_listr   r   exclude)r   r5   r
   Zpk_attrr6   backendZhistory_idsitemZlatest_historicsZlatest_pk_attr_historic_idsadjustedZhistoric_itemr   r   r   r3   Y   sD    
zHistoryManager._as_of_setF c                    s   d}|rd}g }|D ] t  d|p,j }	jf t  d|pFt |	t pT||d fdd jjD }
tjdr j	|
_
||
 qjjj||d	S )
z
        Bulk create the history for the objects specified by objs.
        If called by bulk_update_with_history, use the update boolean and
        save the history_type accordingly.
        +~Z_history_userZ_history_date)Zhistory_datehistory_userZhistory_change_reasonr4   c                    s*   i | ]"}|j jjvr|jt |jqS r   )r    r
   r#   Zattnamer'   ).0r/   r   r   r   r   
<dictcomp>   s   z6HistoryManager.bulk_history_create.<locals>.<dictcomp>Zhistory_relation)
batch_size)r'   r
   Zget_default_history_userr   nowr   r   r(   hasattrr   Zhistory_relation_idr)   ZobjectsZbulk_create)r   objsrF   updateZdefault_userZdefault_change_reasonZdefault_dater4   Zhistorical_instancesrB   rowr   rD   r   bulk_history_create~   s:    z"HistoryManager.bulk_history_create)N)NFNr?   N)r   r   r   r   r   r   r0   r7   r3   rL   __classcell__r   r   r   r   r      s   (     r   N)Z	django.dbr   r   Zdjango.db.modelsr   r   Zdjango.utilsr   Zsimple_history.utilsr   r   Managerr   r   r   r   r   <module>   s
   
