
    hR                         S SK JrJr  S SKJrJr  S SKJr  S SKJ	r	J
r
  S SKJrJrJrJr  S SKJrJrJr   " S S\5      r " S	 S
\5      rS rS rS rS rS rS\l        S rS rS r " S S5      rg)    )Counterdefaultdict)partialreduce)chain)
attrgetteror_)IntegrityErrorconnectionsmodelstransaction)query_utilssignalssqlc                   (   ^  \ rS rSrU 4S jrSrU =r$ )ProtectedError
   c                 0   > X l         [        TU ]	  X5        g N)protected_objectssuper__init__)selfmsgr   	__class__s      K/var/www/html/env/lib/python3.13/site-packages/django/db/models/deletion.pyr   ProtectedError.__init__   s    !20    )r   __name__
__module____qualname____firstlineno__r   __static_attributes____classcell__r   s   @r   r   r   
   s    1 1r   r   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )RestrictedError   c                 0   > X l         [        TU ]	  X5        g r   )restricted_objectsr   r   )r   r   r+   r   s      r   r   RestrictedError.__init__   s    "41r   )r+   r   r&   s   @r   r(   r(      s    2 2r   r(   c                 
   U R                  UUR                  R                  UR                  UR                  SS9  UR                  (       a7  [
        U   R                  R                  (       d  U R                  US U5        g g g )NF)sourcesource_attrnullablefail_on_restricted)	collectremote_fieldmodelnamenullr   featurescan_defer_constraint_checksadd_field_update	collectorfieldsub_objsusings       r   CASCADEr?      sn    !!''JJ    zz+e,55QQ""5$9 Rzr   c           	          [        SUR                  R                  R                  < SUS   R                  R                  < SUR
                  < S3U5      e)Nz'Cannot delete some instances of model 'z@' because they are referenced through a protected foreign key: 'r   .')r   r3   r4   r    r   r5   r:   s       r   PROTECTrC   "   sJ    
 $$--QK!!**JJ	
 		 	r   c                     U R                  X5        U R                  UR                  R                  UR                  5        g r   )add_restricted_objectsadd_dependencyr3   r4   r:   s       r   RESTRICTrG   /   s0    $$U5U//55u{{Cr   c                 f   ^  [        T 5      (       a  U 4S jnOU 4S jnSUl        U 4S jUl        U$ )Nc                 6   > U R                  UT" 5       U5        g r   r9   r;   r<   r=   r>   values       r   set_on_deleteSET.<locals>.set_on_delete7   s    &&uegx@r   c                 ,   > U R                  UTU5        g r   rJ   rK   s       r   rM   rN   <   s    &&ueX>r   Tc                     > ST 40 4$ )Nzdjango.db.models.SET )rL   s   r   <lambda>SET.<locals>.<lambda>A   s    )?%2(Nr   )callablelazy_sub_objsdeconstruct)rL   rM   s   ` r   SETrW   4   s/    	A
	? '+# NMr   c                 *    U R                  US U5        g r   rJ   r:   s       r   SET_NULLrY   E   s    udH5r   Tc                 D    U R                  XR                  5       U5        g r   )r9   get_defaultr:   s       r   SET_DEFAULTr\   L   s    u&7&7&98Dr   c                     g r   rQ   r:   s       r   
DO_NOTHINGr^   P   s    r   c                 .    S U R                  SS9 5       $ )Nc              3      #    U  HQ  nUR                   (       d  M  UR                  (       a  M)  UR                  (       d  UR                  (       d  MM  Uv   MS     g 7fr   )auto_createdconcrete
one_to_oneone_to_many).0fs     r   	<genexpr>4get_candidate_relations_to_delete.<locals>.<genexpr>W   s>      5A>> 	
"#** 	
23,,!-- 	
5s   AA A	AT)include_hidden)
get_fields)optss    r   !get_candidate_relations_to_deleterl   T   s    5 r   c                       \ rS rSrSS jrSS jrSS jrS rS rS r	S	 r
S
 rSS jrS r       SS jrS rS rS rS rSrg)	Collector^   Nc                     Xl         X l        [        [        5      U l        [        [
        5      U l        [        [        [        [        5      5      U l        / U l	        [        [        5      U l
        g r   )r>   originr   setdatalistfield_updatesr   r+   fast_deletesdependencies)r   r>   rq   s      r   r   Collector.__init___   sR    
$	(."-gk3.G"H  (,r   c                     U(       d  / $ / nUS   R                   nU R                  U   nU H  nX;  d  M
  UR                  U5        M     UR                  U5        Ub  U(       d  U R	                  X&US9  U$ )a  
Add 'objs' to the collection of objects to be deleted.  If the call is
the result of a cascade, 'source' should be the model that caused it,
and 'nullable' should be set to True if the relation can be null.

Return a list of all objects that were not already collected.
r   reverse_dependency)r   rs   appendupdaterF   )	r   objsr.   r0   r{   new_objsr4   	instancesobjs	            r   addCollector.addt   s}     IQ!!IIe$	C#$  	" hBTUr   c                    U(       a  X!p!U R                   UR                  R                     R                  UR                  R                  5        U R                  R                  X R                  R                  5       5        g r   )rw   _metaconcrete_modelr   rs   
setdefaultdefault_factory)r   r4   
dependencyr{   s       r   rF   Collector.add_dependency   s]     *:%++44599++	
 			Z)B)B)DEr   c                 B    U R                   X4   R                  U5        g)zq
Schedule a field update. 'objs' must be a homogeneous iterable
collection of model instances (e.g. a QuerySet).
N)ru   r|   )r   r<   rL   r~   s       r   r9   Collector.add_field_update   s    
 	5<(//5r   c                 t    U(       a1  US   R                   nU R                  U   U   R                  U5        g g )Nr   )r   r+   r}   )r   r<   r~   r4   s       r   rE    Collector.add_restricted_objects   s7    G%%E##E*5188> r   c                     XR                   ;   aC  U R                   U   R                  5        VVs0 s H
  u  p4X4U-
  _M     snnU R                   U'   g g s  snnf r   )r+   items)r   r4   r~   r<   r   s        r   !clear_restricted_objects_from_set+Collector.clear_restricted_objects_from_set   s_    +++ %)$;$;E$B$H$H$J.$JLE t|#$J.D##E* ,.s   Ac           
         XR                   ;   al  [        UR                  U R                   U   R                  5        VVs/ s H  nU  H  nUR                  PM     M     snnS95      nU R                  X5        g g s  snnf )N)pk__in)r+   rr   filtervaluespkr   )r   r4   qsr~   r   s        r   &clear_restricted_objects_from_queryset0Collector.clear_restricted_objects_from_queryset   s    +++		 %)$;$;E$B$I$I$K$KD#'C #' $K  D 225? ,s     A=c                     [         R                  R                  U5      =(       d    [         R                  R                  U5      $ r   )r   
pre_deletehas_listenerspost_delete)r   r4   s     r   _has_signal_listenersCollector._has_signal_listeners   s7    !!//
 6  ..u5	6r   c                 Z  ^ T(       a  TR                   R                  [        La  g[        US5      (       a  UR                  R
                  nO0[        US5      (       a  [        US5      (       a  UR
                  nOgU R                  U5      (       a  gUR                  n[        U4S jUR                  R                  R                  R                  5        5       5      =(       aC    [        S [        U5       5       5      =(       a!    [        S UR                   5       5      (       + $ )a  
Determine if the objects in the given queryset-like or single object
can be fast-deleted. This can be done if there are no cascades, no
parents and no signal listeners for the object class.

The 'from_field' tells where we are coming from - we need this to
determine if the objects are in fact to be deleted. Allow also
skipping parent -> child -> parent chain preventing fast delete of
the child.
Fr   r4   _raw_deletec              3   .   >#    U  H
  nUT:H  v   M     g 7fr   rQ   )re   link
from_fields     r   rg   ,Collector.can_fast_delete.<locals>.<genexpr>   s      FD 
"Fs   c              3   n   #    U  H+  nUR                   R                  R                  [        L v   M-     g 7fr   )r<   r3   	on_deleter^   )re   relateds     r   rg   r      s,      FG **44
BF   35c              3   :   #    U  H  n[        US 5      v   M     g7f)bulk_related_objectsN)hasattr)re   r<   s     r   rg   r      s"      !4 E#9::!4s   )r3   r   r?   r   r   r4   r   allr   parentsr   rl   anyprivate_fields)r   r~   r   r4   rk   s     `  r   can_fast_deleteCollector.can_fast_delete   s     *11;;7J4!!JJ$$ET7##m(D(DJJE%%e,, {{  //55==DDF    @F   !%!4!4  	
r   c                 2   U Vs/ s H  o3R                   PM     nn[        [        U R                     R                  R                  XA5      S5      n[        U5      U:  a+  [        S[        U5      U5       Vs/ s H	  nXXe-    PM     sn$ U/$ s  snf s  snf )zD
Return the objs in suitably sized batches for the used connection.
   r   )r5   maxr   r>   opsbulk_batch_sizelenrange)r   r~   fieldsr<   field_namesconn_batch_sizeis          r   get_del_batchesCollector.get_del_batches   s     066vezzv6

#''77JA
 t9& q#d)_==A ,-= 
 6M 7
s   B9Bc	           
      @   U R                  U5      (       a  U R                  R                  U5        gU R                  XX6S9n	U	(       d  gU	S   R                  n
U(       d  U
R
                  R                  nUR
                  R                  R                  5        HY  nU(       d  M  U	 Vs/ s H  n[        XR                  5      PM     nnU R                  UU
UR                  R                  SSSS9  M[     U(       d  g[        [        5      n[        [        5      n[!        U
R
                  5       GHy  nU(       a&  UR"                  U
R
                  R$                  ;   a  M1  UR&                  nUR                  R(                  nU[*        :X  a  M_  UR,                  nU R                  UUS9(       a  UU   R                  U5        M  U R/                  U	U/5      nU H  nU R1                  UU/U5      nUR2                  R4                  (       dg  U R7                  U5      (       dQ  [9        [:        R<                  " S [!        UR
                  5       5       5      5      nUR>                  " [A        U5      6 n[        US	S5      (       d	  U(       d  M   U" U UUU RB                  5        M     GM|     U(       aY  [E        SU
RF                  < SSRK                  U5      < S3[9        [:        R<                  " UR                  5       5      5      5      eURM                  5        HO  u  nnU R/                  U	U5      nU H1  nU R1                  UUU5      nU R                  R                  U5        M3     MQ     U
R
                  RN                   HC  n[Q        US5      (       d  M  URS                  XRB                  5      nU R                  UU
SSS9  ME     U(       Gah  U RT                  RM                  5        H  u  nnU RW                  UU5        M     U R                   H  nU RY                  UR"                  U5        M!     U RZ                  R                  5       (       a  [        [        5      nU RZ                  RM                  5        HU  u  nnURM                  5        H;  u  nnU(       d  M  S
URF                  < SUR                  < S
3nUU==   U-  ss'   M=     MW     U(       aY  []        SU
RF                  < SSRK                  U5      < S3[9        [:        R<                  " UR                  5       5      5      5      egggs  snf ! [D         aL  nS
UR"                  RF                  < SUR                  < S
3nUU==   URH                  -  ss'    SnAGM  SnAff = f)a  
Add 'objs' to the collection of objects to be deleted as well as all
parent instances.  'objs' must be a homogeneous iterable collection of
model instances (e.g. a QuerySet).  If 'collect_related' is True,
related objects will be handled by their respective on_delete handler.

If the call is the result of a cascade, 'source' should be the model
that caused it and 'nullable' should be set to True, if the relation
can be null.

If 'reverse_dependency' is True, 'source' will be deleted before the
current model, rather than after. (Needed for cascading to parent
models, the one case in which the cascade follows the forwards
direction of an FK rather than the reverse direction.)

If 'keep_parents' is True, data of parent model's will be not deleted.

If 'fail_on_restricted' is False, error won't be raised even if it's
prohibited to delete such objects due to RESTRICT, that defers
restricted object checking in recursive calls where the top-level call
may need to collect more objects to determine whether restricted ones
can be deleted.
Nrz   r   FT)r.   r/   collect_relatedr{   r1   )r   c              3   \   #    U  H"  nS  UR                   R                   5       v   M$     g7f)c              3   8   #    U  H  oR                   v   M     g 7fr   )attname)re   rfs     r   rg   .Collector.collect.<locals>.<genexpr>.<genexpr>Q  s     S2RBZZ2Rs   N)r<   foreign_related_fields)re   rels     r   rg   $Collector.collect.<locals>.<genexpr>P  s.      ,( T#))2R2RSS(s   *,rU   rB   rA   z&Cannot delete some instances of model z= because they are referenced through protected foreign keys: z, r   )r.   r0   r1   z> because they are referenced through restricted foreign keys: )/r   rv   r|   r   r   r   r   r   r   getattrr5   r2   r3   related_namer   rt   rl   r4   all_parentsr<   r   r^   related_modelr   related_objectsqueryselect_relatedr   rr   r   from_iterableonlytupler>   r   r    r   joinr   r   r   r   rs   r   r   r+   r(   ) r   r~   r.   r0   r   r/   r{   keep_parentsr1   r   r4   r   ptrr   parent_objsmodel_fast_deletesr   r   r<   r   r   batchesbatchr=   referenced_fieldserrorkeyrelated_fieldsr   r   r+   r   s                                    r   r2   Collector.collect   s   D %%$$T*88(  
 %% #[[77N%++33::<3EM"NXc73#9XK"NLL#$$'$4$4$A$A(-+/+0 !  = (.'-8EG1H1H HMME**44IJ&#11M##Me#D"=188?**8eW=G //wN NN1111-@@(+++ ,'H - 3 3(, )%  (}}e4E.FGH9ou==J!$xD/ ! FP   NNII/0	 E''(9(@(@(BCD  .@-E-E-G)M>**8^DG //~uU!!((2 ! .H
 [[//Eu455 55h

KUTe  	 0  -1IIOO,=(y66}iP ->'';;BHHbI (&&--//%0%6"-1-D-D-J-J-L)M6'-||~t4/</E/Euzz"RC.s3t;3 (6 .M
 &)
 "NN II&89 E//0B0I0I0KLM	 	 & 0 [ #Of * J+0;;+?+?L)#.%2I2II..Js   +UU
VA VVc                    [         R                  R                  U Vs/ s H  oDR                   S3U4PM     sn[         R                  R                  S9nUR
                  R                  U R                  5      R                  U5      $ s  snf )zA
Get a QuerySet of the related model to objs via related fields.
__in)	connector)r   Qcreater5   OR_base_managerr>   r   )r   r   r   r~   related_field	predicates         r   r   Collector.related_objects  s{      MM((FTUn]##$D)40nU!mm&& ) 
	 **00<CCINN Vs   B
c              #   n   #    U R                   R                  5        H  u  pU H  nX4v   M
     M     g 7fr   )rs   r   )r   r4   r   r   s       r   instances_with_modelCollector.instances_with_model  s/      $		 1E j  ! !2r   c                 >   / n[        5       n[        U R                  5      n[        U5      [        U5      :  a  SnU H  nXQ;   a  M
  U R                  R                  UR                  R                  5      nU(       a  UR                  U5      (       a  MX  UR                  U5        UR                  UR                  R                  5        SnM     U(       d  g [        U5      [        U5      :  a  M  U Vs0 s H  oUU R                  U   _M     snU l        g s  snf )NFT)rr   rt   rs   r   rw   getr   r   
differencer|   r   )r   sorted_modelsconcrete_modelsr   foundr4   rw   s          r   sortCollector.sort  s    %dii- 3v;.E)#0044U[[5O5OP$)@)@)Q)Q!((/#''(B(BC E    - 3v;. ;HH-DIIe,,-H	Hs   8Dc           
      
   U R                   R                  5        H%  u  p[        U[        S5      S9U R                   U'   M'     U R	                  5         [        5       n[        U R                   5      S:X  a  [        W5      S:X  a  [        U5      S   nU R                  U5      (       a  [        R                  " U R                  5         [        R                  " W5      R                  UR                  /U R                  5      nS S S 5        [!        UWR"                  R                  R$                  S 5        WUR"                  R&                  U04$ [        R(                  " U R                  SS9   U R+                  5        HV  u  pUR"                  R,                  (       a  M"  [.        R0                  R3                  UUU R                  U R4                  S9  MX     U R6                   HO  nUR9                  U R                  S9nU(       d  M%  X7R:                  R"                  R&                  ==   U-  ss'   MQ     U R<                  R                  5        GH  u  u  pn
/ n/ nU
 HS  n[?        U[@        RB                  5      (       a   URD                  c  URG                  U5        MB  URI                  U5        MU     U(       a.  [K        [L        U5      nURN                  " S	0 URP                  U	0D6  U(       d  M  US   RR                  n[        RT                  " U5      nURW                  [        U Vs1 s H  ofR                  iM     sn5      URP                  U	0U R                  5        GM     U R                   RY                  5        H  nUR[                  5         M     U R                   R                  5        H  u  p[        R                  " U5      nU Vs/ s H  ofR                  PM     nnUR                  XR                  5      nU(       a   X1R"                  R&                  ==   U-  ss'   UR"                  R,                  (       a  M  U H7  n[.        R\                  R3                  UUU R                  U R4                  S9  M9     M     S S S 5        U R                   R                  5        H8  u  pU H-  n[!        XAR"                  R                  R$                  S 5        M/     M:     [_        URY                  5       5      [a        U5      4$ ! , (       d  f       GN= fs  snf s  snf ! , (       d  f       N= f)
Nr   )r   r   r   F)r>   	savepoint)senderinstancer>   rq   )r>   rQ   )1rs   r   sortedr   r   r   r   rt   r   r   mark_for_rollback_on_errorr>   r   DeleteQuerydelete_batchr   setattrr   r   labelatomicr   ra   r   r   sendrq   rv   r   r4   ru   
isinstancer   QuerySet_result_cacher|   extendr   r	   r}   r5   r   UpdateQueryupdate_batchr   reverser   sumdict)r   r4   r   deleted_counterr   countr   r   r<   rL   instances_listupdatesr~   combined_updatesr   pk_lists                   r   deleteCollector.delete  s    $		 1E%iZ5EFDIIe !2 			!) tyy>Q3y>Q#6Iq)H##H-- ;;DJJGOOE2??!tzzE H %++.."8"8$?u{{00%888djjEB"779
{{///&&++$!$"jj#{{	 ,  : ''TZZ85#HHNN$8$89UB9 ( 372D2D2J2J2L.!/I"9foo>>%33;y1I. "0 '-c7';$$++Buzz5.AB4 G--EOOE2E&&5ff56U8KTZZ# 3M, "YY--/	!!# 0 %)IIOO$5 .-67Yc66Y7**7JJ?#KK$5$56%?6{{///(++00#(%("&**#';;	 1   ) %6Y Cx !%		 1E%++.."8"8$? & !2 ?))+,d?.CCCM HGV 6 8] CBsS   
<T2/T/%A%T/C(T/:9T/3T%BT/T*/AT/AT/
T"%
T//
T=)rs   rw   rv   ru   rq   r+   r>   r   )NFF)F)NFTNFFT)r    r!   r"   r#   r   r   rF   r9   rE   r   r   r   r   r   r2   r   r   r   r  r$   rQ   r   r   rn   rn   ^   sm    -*0F6?
@6
*
X&  XtO!
I$VDr   rn   N) collectionsr   r   	functoolsr   r   	itertoolsr   operatorr   r	   	django.dbr
   r   r   r   django.db.modelsr   r   r   r   r(   r?   rC   rG   rW   rY   rU   r\   r^   rl   rn   rQ   r   r   <module>r     sw    , %  $ F F 6 61^ 12n 2	:
D
"6  E	hD hDr   