
    Nwg:                     J   d dl mZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/  e0e	dd      Z1 G d dejd                        Z3y)    )AnySequence)http)apps)settings)admin)helpers)unquote)get_permission_codenameget_user_model)PermissionDenied)QuerySet)get_object_or_404render)re_pathreverse)	force_str)	mark_safe)capfirst)gettext   )HistoricalQuerySetHistoryManager)HistoricalChanges)HistoricalRecordContextHelper)get_history_manager_for_modelget_history_model_for_modelSIMPLE_HISTORY_EDITFc                       e Zd Zg ZdZdZdZ fdZddZde	de
ded	efd
Zd	ee
   fdZded	efdZ	 d dee   fdZd Z fdZddZd Zd Z fdZed        ZddZd fd	Zd fd	ZddZddZ ddZ!ddZ"ed        Z# xZ$S )!SimpleHistoryAdminz"simple_history/object_history.htmlz'simple_history/object_history_list.htmlz'simple_history/object_history_form.htmlc                     t         |          }| j                  }| j                  j                  }|j
                  |j                  f}t        d|j                  | j                        d|z        g}||z   S )z8Returns the additional urls used by the Reversion admin.z^([^/]+)/history/([^/]+)/$z%s_%s_simple_history)name)
superget_urls
admin_sitemodel_meta	app_label
model_namer   
admin_viewhistory_form_view)selfurlsr%   optsinfohistory_urls	__class__s         K/var/www/horilla/myenv/lib/python3.12/site-packages/simple_history/admin.pyr$   zSimpleHistoryAdmin.get_urls"   su    w!__
zz~~t.,%%d&<&<=+d2
 d""    c                 Z   | j                   j                  |_        | j                  }|j                  }|j
                  }|j                  j                  }t        ||j                  j                        }t        |      }| j                  ||||      }	| j                  |      }
	  | j                  |      j                  di ||i}| j)                  ||      st*        |
D ]8  }t        | |d      }|st-        |      s|	D ]  }t/        || ||              : | j1                  ||	       | j2                  j4                  j7                  t9                     }dj;                  |j
                  |j                        }| j=                  ||      | j>                  |	tA        tC        |jD                              |t        | j                   dd      ||||
| jG                  ||      d}|jI                  | j                   jK                  |             |jI                  |xs i        i } | jL                  || jN                  |fi |S # |j                  $ rN 	 |	j!                  d      j"                  }n-# |	j                  j                  $ r t$        j&                  w xY wY w xY w)z(The 'history' admin view for this model.history_dateNzadmin:{}_{}_change	root_path)titleobject_history_list_templatehistorical_recordsmodule_nameobjectr6   r(   r.   admin_user_viewhistory_list_displayrevert_disabled )(r%   r"   current_appr&   r'   r(   pkattnamegetattr simple_history_manager_attributer
   get_history_querysetget_history_list_displayget_querysetgetDoesNotExistlatestinstancer   Http404-has_view_history_or_change_history_permissionr   callablesetattrset_history_delta_changescontent_type_model_clsobjectsget_for_modelr   formathistory_view_titler8   r   r   verbose_name_pluralr>   updateeach_contextrender_history_viewobject_history_template)r,   request	object_idextra_contextr&   r.   r(   pk_namehistoryr9   r=   objhistory_list_entryvalue_for_entryrecordcontent_typer<   contextextra_kwargss                      r2   history_viewzSimpleHistoryAdmin.history_view1   s   "oo22

{{NN	''//%!M!MNI&	!66Wgy
  $<<WE	#0$##G,00HGY3GHC AA'3O"" #7 	Q%d,>EO8O#<0 QFF$68OPQ	Q 	&&w0BC22::HH
 /55""
 ,,Wc:,0,M,M"4#Id.F.F$GH +tD".$8#33GSA
 	t33G<=}*+'t''T117
>J
 	
U !! 	##(//?HH%++88 #ll"# 	#s*   '#I J*I98J*9*J##J*)J*history_managerr^   r\   returnc                      |j                   di ||i}t        |j                  j                  t              s|j                  d      }|j                         }|S )a  
        Return a ``QuerySet`` of all historical records that should be listed in the
        ``object_history_list_template`` template.
        This is used by ``history_view()``.

        :param request:
        :param history_manager:
        :param pk_name: The name of the original model's primary key field.
        :param object_id: The primary key of the object whose history is listed.
        history_userr?   )filter
isinstancer&   rk   propertyselect_related$_select_related_history_tracked_objs)r,   r[   rh   r^   r\   qss         r2   rE   z'SimpleHistoryAdmin.get_history_queryseto   sW     "8!7!7!O7I:N!O///<<hG"">2B446	r3   c                     | j                   S )z
        Return a sequence containing the names of additional fields to be displayed on
        the object history page. These can either be fields or properties on the model
        or the history model, or methods on the admin class.
        )r=   )r,   r[   s     r2   rF   z+SimpleHistoryAdmin.get_history_list_display   s     (((r3   historical_recordc                 .    t        | j                  |      S )z
        Return an instance of ``HistoricalRecordContextHelper`` for formatting
        the template context for ``historical_record``.
        )r   r&   )r,   r[   rs   s      r2   $get_historical_record_context_helperz7SimpleHistoryAdmin.get_historical_record_context_helper   s     -TZZ9JKKr3   r9   c                     d}|D ]D  }||}|j                  ||      }| j                  ||      }|j                  |      |_        |}F y)aF  
        Add a ``history_delta_changes`` attribute to all historical records
        except the first (oldest) one.

        :param request:
        :param historical_records:
        :param foreign_keys_are_objs: Passed to ``diff_against()`` when calculating
               the deltas; see its docstring for details.
        N)foreign_keys_are_objs)diff_againstru   context_for_delta_changeshistory_delta_changes)r,   r[   r9   rw   previouscurrentdeltahelpers           r2   rP   z,SimpleHistoryAdmin.set_history_delta_changes   sm     ) 	G"))/D * E >>wQF-3-M-Me-TH*H	r3   c                     | j                  ||      rt        st        d      t        |      z  S t        d      t        |      z  S )NzView history: %szChange history: %s)r>   r   _r   r,   r[   r`   s      r2   rU   z%SimpleHistoryAdmin.history_view_title   s?    -6I'(9S>99)*Ys^;;r3   c           	      R   d|j                   v rt        r|j                  j                  }t	        d      t        |      t        |      dz  }| j                  |dj                  |t	        d                   t        j                  |j                        S t        | 1  ||      S )N_change_historyz0The %(name)s "%(obj)s" was changed successfully.)r"   r`   z{} - {}zYou may edit it again below)POSTr   r'   verbose_namer   r   message_userrT   r   HttpResponseRedirectpathr#   response_change)r,   r[   r`   r   msgr1   s        r2   r   z"SimpleHistoryAdmin.response_change   s    ,1D9911LFG!,/ ~K C
 ))#q1N/OP ,,W\\::7*7C88r3   c                 d   | j                   j                  |_        | j                  j                  }t        | j                  | j                  j                  j                        j                  }t        |fi |j                  j                  |d|ij                  }d|j                  _        | j                  ||      st        t        rd}nd}d|j                   v r-t        r't#        |      }	|	j%                  |      j                  }g }
| j'                  ||      }|j(                  dk(  r ||j                   |j*                  |      }|j-                         rx| j/                  ||d      }| j1                  |||d       |j3                          | j5                  ||| j7                  |||
             | j9                  ||      S  ||      }t;        j<                  || j?                  ||      | j@                  | jC                  ||      | 	      }|jD                  }| j                   j                  |jF                  |f}i d
| jI                  ||      d|d|d|dddtK        | jL                  |jL                  z         dt;        jN                  ||
      d|jF                  d|dtQ        d|z        dtQ        d|z  |j                  f      dtQ        d|z  |j                  f      d|d| jS                  ||      ddddd| jU                  |      | jW                  ||      | jY                  ||      | j[                  ||      ddd|j                  | j\                  j^                  ja                  | j                        jb                  | jd                  | jf                  t        | j                   d d       d!}|ji                  | j                   jk                  |             |ji                  |xs i        i } | jl                  || jn                  |fi |S )"N
history_idFTr   )rA   r   )rK   )change)model_adminr7   	adminformr\   originalis_popupmediaerrorsr(   original_optschangelist_urlz%s:%s_%s_changelist
change_urlz%s:%s_%s_change)argshistory_urlz%s:%s_%s_historychange_historyr>   addr   has_add_permission r6   )has_view_permissionhas_change_permissionhas_delete_permissionhas_file_fieldhas_absolute_urlform_urlr.   content_type_idsave_assave_on_topr6   )8r%   r"   r@   r&   r'   rC   rD   r   rA   rB   rK   _stateaddingrM   r   r   r   r   rH   get_formmethodFILESis_valid	save_form
save_modelsave_m2m
log_changeconstruct_change_messager   r	   	AdminFormget_fieldsetsprepopulated_fieldsget_readonly_fieldsr)   r(   history_form_view_titler   r   AdminErrorListr   r>   r   has_view_history_permissionhas_change_history_permissionr   rQ   rR   rS   idr   r   rW   rX   rY   object_history_form_template)r,   r[   r\   
version_idr]   r   r&   r`   r   r_   formsets
form_classform
new_object
admin_formr)   url_tripletre   rf   s                      r2   r+   z$SimpleHistoryAdmin.history_form_view   s8   "oo22

((JJ

((II

% 	  
#&&..	<T

( 	 "

AA'3O""!N"N,1D3C8G+++,55C]]7C0
>>V#gllGMMCHD}}!^^GT$^G
T$G11'4J
 ++GZ@@ s+D&&w,$$$$Wc2

 #--
oo**M,C,CZO 
T11'3? 
 
  
 	 

  
 YtzzJ,<,<<= 
 g,,T8< 
 00 
 ] 
 g&;k&IJ 
 '"3k"A	R 
 7#5#C366)T 
 n 
 t33GSA 
  5! 
" d# 
$ !$"9"9'"B% 
& $(#C#CGS#Q%)%G%GQT%U%)%?%?%M" %KK#::BBPP

 b||++ +tD? 
B 	t33G<=}*+'t''T66
CO
 	
r3   c                     | j                  ||      rt        d      t        |      z  S t        d      t        |      z  S )NzView %sz	Revert %s)r>   r   r   r   s      r2   r   z*SimpleHistoryAdmin.history_form_view_title%  s8    -Y<)C.00[>IcN22r3   c                     t        |||fi |S )z*Catch call to render, to allow overriding.)r   )r,   r[   templatere   kwargss        r2   rY   z&SimpleHistoryAdmin.render_history_view+  s    gx;F;;r3   c                 L    |j                   |_        t        |   ||||       y)z<Set special model attribute to user for reference after saveN)user_history_userr#   r   )r,   r[   r`   r   r   r1   s        r2   r   zSimpleHistoryAdmin.save_model/  s"    #LL7Cv6r3   c                 ,    t        j                  d      S )z$Returns the ContentType model class.zcontenttypes.contenttype)django_apps	get_modelr,   s    r2   rQ   z)SimpleHistoryAdmin.content_type_model_cls4  s     $$%?@@r3   c                 r    t        t        dd      ry| j                  ||      r| j                  ||      syy)zHIf `True`, hides the "Revert" button in the `submit_line.html` template.SIMPLE_HISTORY_REVERT_DISABLEDFT)rC   r   r   r   r   s      r2   r>   z"SimpleHistoryAdmin.revert_disabled9  s;    8=uE--S
44WcBr3   c                 $    t         |   ||      S N)r#   r   r,   r[   r`   r1   s      r2   r   z&SimpleHistoryAdmin.has_view_permissionC  s    w*7C88r3   c                 $    t         |   ||      S r   )r#   r   r   s      r2   r   z(SimpleHistoryAdmin.has_change_permissionF  s    w,Wc::r3   c                 N    | j                  ||      xs | j                  ||      S r   )r   r   r   s      r2   has_view_or_change_permissionz0SimpleHistoryAdmin.has_view_or_change_permissionI  s.    ''5 
9S9SS:
 	
r3   c                     | j                   r&| j                  ||      xs | j                  ||      S | j                  ||      S r   )enforce_history_permissionsr   r   r   r   s      r2   rM   z@SimpleHistoryAdmin.has_view_history_or_change_history_permissionN  sO    ++33 B33GSAB 11'3??r3   c                     | j                   rUt        | j                        j                  }t	        d|      }|j
                  j                  |j                   d|       S | j                  ||      S )Nview.)	r   r   r&   r'   r   r   has_permr(   r   )r,   r[   r`   opts_historycodename_view_historys        r2   r   z.SimpleHistoryAdmin.has_view_history_permissionU  so    ++6tzzBHHL$;FL$Q!<<(())*!,A+BC  ''55r3   c                     | j                   rUt        | j                        j                  }t	        d|      }|j
                  j                  |j                   d|       S | j                  ||      S )Nr   r   )	r   r   r&   r'   r   r   r   r(   r   )r,   r[   r`   r   codename_change_historys        r2   r   z0SimpleHistoryAdmin.has_change_history_permission^  so    ++6tzzBHHL&=h&U#<<(())*!,C+DE  ))'377r3   c                 $    t        t        dd      S )N0SIMPLE_HISTORY_ENFORCE_HISTORY_MODEL_PERMISSIONSF)rC   r   r   s    r2   r   z.SimpleHistoryAdmin.enforce_history_permissionsg  s    H%
 	
r3   r   )T)%__name__
__module____qualname__r=   rZ   r8   r   r$   rg   r   strr   r   rE   r   rF   r   r   ru   rP   rU   r   r+   r   rY   r   rn   rQ   r>   r   r   r   rM   r   r   r   __classcell__)r1   s   @r2   r    r       s    B#L #L #<
|(6ADQT	*)8C= )L*;L	&L #	 %%67:<9"Z
x3<7
 A A9;

@68 
 
r3   r    N)4typingr   r   djangor   django.appsr   r   django.confr   django.contribr   django.contrib.adminr	   django.contrib.admin.utilsr
   django.contrib.authr   r   django.core.exceptionsr   django.db.modelsr   django.shortcutsr   r   django.urlsr   r   django.utils.encodingr   django.utils.htmlr   django.utils.textr   django.utils.translationr   r   managerr   r   modelsr   template_utilsr   utilsr   r   rC   r   
ModelAdminr    r?   r3   r2   <module>r      se       +     ( . G 3 % 6 ( + ' & 1 7 % 9 Mh(=uE P
)) P
r3   