
    EvgR                         d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZmZmZ d dlmZmZmZ  G d de          Z G d	 d
e          Zd Zd Zd Zd Zd Zde_        d Zd Zd Z G d d          ZdS )    )Counterdefaultdict)partialreduce)chain)
attrgetteror_)IntegrityErrorconnectionsmodelstransaction)query_utilssignalssqlc                        e Zd Z fdZ xZS )ProtectedErrorc                 Z    || _         t                                          ||           d S N)protected_objectssuper__init__)selfmsgr   	__class__s      V/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/db/models/deletion.pyr   zProtectedError.__init__   s,    !2/00000    __name__
__module____qualname__r   __classcell__r   s   @r   r   r   
   s8        1 1 1 1 1 1 1 1 1r   r   c                        e Zd Z fdZ xZS )RestrictedErrorc                 Z    || _         t                                          ||           d S r   )restricted_objectsr   r   )r   r   r&   r   s      r   r   zRestrictedError.__init__   s,    "4011111r   r   r"   s   @r   r$   r$      s8        2 2 2 2 2 2 2 2 2r   r$   c                     |                      ||j        j        |j        |j        d           |j        r0t
          |         j        j        s|                     |d |           d S d S d S )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   CASCADEr9      s    !'J      z :+e,5Q :""5$99999: : : :r   c           	      ~    t          d|j        j        j        d|d         j        j        d|j        d|          )Nz'Cannot delete some instances of model 'z@' because they are referenced through a protected foreign key: 'r   .')r   r-   r.   r   r   r/   r4   s       r   PROTECTr=   "   sP    
. $---QK!***JJJ	
 		 	 	r   c                 |    |                      ||           |                     |j        j        |j                   d S r   )add_restricted_objectsadd_dependencyr-   r.   r4   s       r   RESTRICTrA   /   s<    $$UH555U/5u{CCCCCr   c                 ^     t                     r fd}n fd}d|_         fd|_        |S )Nc                 F    |                      |             |           d S r   r3   r5   r6   r7   r8   values       r   set_on_deletezSET.<locals>.set_on_delete7   s'    &&ueeggx@@@@@r   c                 6    |                      ||           d S r   rD   rE   s       r   rG   zSET.<locals>.set_on_delete<   s!    &&ueX>>>>>r   Tc                      d fi fS )Nzdjango.db.models.SET )rF   s   r   <lambda>zSET.<locals>.<lambda>A   s    )?%2(N r   )callablelazy_sub_objsdeconstruct)rF   rG   s   ` r   SETrO   4   so     
+	A 	A 	A 	A 	A 	A
	? 	? 	? 	? 	? '+# N N N NMr   c                 4    |                      |d |           d S r   rD   r4   s       r   SET_NULLrQ   E   s     udH55555r   Tc                 X    |                      ||                                |           d S r   )r3   get_defaultr4   s       r   SET_DEFAULTrT   L   s,    ue&7&7&9&98DDDDDr   c                     d S r   rJ   r4   s       r   
DO_NOTHINGrV   P   s    Dr   c                 B    d |                      d          D             S )Nc              3   R   K   | ]"}|j         	|j        |j        s|j        |V  #d S r   )auto_createdconcrete
one_to_oneone_to_many).0fs     r   	<genexpr>z4get_candidate_relations_to_delete.<locals>.<genexpr>W   sd        > #$* 34, CD-	     r   T)include_hidden)
get_fields)optss    r   !get_candidate_relations_to_deleterc   T   s1     55   r   c                       e Zd ZddZddZddZd Zd Zd Zd	 Z	d
 Z
ddZd Z	 	 	 	 	 	 	 ddZd Zd Zd Zd ZdS )	CollectorNc                    || _         || _        t          t                    | _        t          t
                    | _        t          t          t          t                              | _        g | _	        t          t                    | _
        d S r   )r8   originr   setdatalistfield_updatesr   r&   fast_deletesdependencies)r   r8   rg   s      r   r   zCollector.__init___   sk    
$$	(.."-gk3.G.G"H"H  (,,r   Fc                     |sg S g }|d         j         }| j        |         }|D ]}||vr|                    |           |                    |           ||s|                     |||           |S )a7  
        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   Nreverse_dependency)r   ri   appendupdater@   )	r   objsr(   r*   rp   new_objsr.   	instancesobjs	            r   addzCollector.addt   s      	IQ!Ie$	 	% 	%C)##$$$""" hBTUUUr   c                     |r||}}| j         |j        j                                     |j        j                   | j                            || j                                                   d S r   )rm   _metaconcrete_modelrw   ri   
setdefaultdefault_factory)r   r.   
dependencyrp   s       r   r@   zCollector.add_dependency   so     	2 *E:E%+4599+	
 	
 	
 		Z)B)B)D)DEEEEEr   c                 J    | j         ||f                             |           dS )z
        Schedule a field update. 'objs' must be a homogeneous iterable
        collection of model instances (e.g. a QuerySet).
        N)rk   rq   )r   r6   rF   rs   s       r   r3   zCollector.add_field_update   s*    
 	5%<(//55555r   c                 t    |r5|d         j         }| j        |         |                             |           d S d S )Nr   )r   r&   rr   )r   r6   rs   r.   s       r   r?   z Collector.add_restricted_objects   sG     	?G%E#E*5188>>>>>	? 	?r   c                     || j         v r5fd| j         |                                         D             | j         |<   d S d S )Nc                 "    i | ]\  }}||z
  S rJ   rJ   )r]   r6   itemsrs   s      r   
<dictcomp>z?Collector.clear_restricted_objects_from_set.<locals>.<dictcomp>   s3     . . . E5 ut|. . .r   )r&   r   )r   r.   rs   s     `r   !clear_restricted_objects_from_setz+Collector.clear_restricted_objects_from_set   sc    D+++. . . .$($;E$B$H$H$J$J. . .D#E*** ,+r   c                     || j         v rbt          |                    d | j         |                                         D                                 }|                     ||           d S d S )Nc                 &    g | ]}|D ]	}|j         
S rJ   pk)r]   rs   rv   s      r   
<listcomp>zDCollector.clear_restricted_objects_from_queryset.<locals>.<listcomp>   sA        #'       r   )pk__in)r&   rh   filtervaluesr   )r   r.   qsrs   s       r   &clear_restricted_objects_from_querysetz0Collector.clear_restricted_objects_from_queryset   s    D+++		 $($;E$B$I$I$K$K      D 225$????? ,+r   c                 ~    t           j                            |          pt           j                            |          S r   )r   
pre_deletehas_listenerspost_delete)r   r.   s     r   _has_signal_listenerszCollector._has_signal_listeners   s9    !//
 
 6 ..u55	6r   c                    rj         j        t          urdS t          |d          r|j        j        }n*t          |d          rt          |d          r|j        }ndS |                     |          rdS |j        }t          fd|j        j        j	        
                                D                       oDt          d t          |          D                       ot          d |j        D                        S )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.
        Fry   r.   _raw_deletec              3   $   K   | ]
}|k    V  d S r   rJ   )r]   link
from_fields     r   r_   z,Collector.can_fast_delete.<locals>.<genexpr>   s<         
"     r   c              3   F   K   | ]}|j         j        j        t          u V  d S r   )r6   r-   	on_deleterV   )r]   relateds     r   r_   z,Collector.can_fast_delete.<locals>.<genexpr>   sC         *4
B     r   c              3   6   K   | ]}t          |d           V  dS )bulk_related_objectsN)hasattrr]   r6   s     r   r_   z,Collector.can_fast_delete.<locals>.<genexpr>   sA         E#9::     r   )r-   r   r9   r   ry   r.   r   allrz   parentsr   rc   anyprivate_fields)r   rs   r   r.   rb   s     `  r   can_fast_deletezCollector.can_fast_delete   sR     	*1;7JJ54!! 	J$EET7## 	m(D(D 	JEE5%%e,, 	5 {     /5=DDFF        @FF       !%!4     	
r   c                    d |D             }t          t          | j                 j                            |          d          t                    k    r+fdt          dt                              D             S gS )zT
        Return the objs in suitably sized batches for the used connection.
        c                     g | ]	}|j         
S rJ   r/   r   s     r   r   z-Collector.get_del_batches.<locals>.<listcomp>   s    666euz666r      c                 *    g | ]}||z            S rJ   rJ   )r]   iconn_batch_sizers   s     r   r   z-Collector.get_del_batches.<locals>.<listcomp>   s8        Q_,,-  r   r   )maxr   r8   opsbulk_batch_sizelenrange)r   rs   fieldsfield_namesr   s    `  @r   get_del_batcheszCollector.get_del_batches   s     76v666
#'77TJJA
 
 t99&&    q#d))_==   
 6Mr   Tc	           
      	   |                      |          r| j                            |           dS |                     ||||          }	|	sdS |	d         j        }
|sb|
j        j        }|j        j                                        D ]7r3fd|	D             }| 	                    ||
j
        j        ddd           8|sdS t          t                    }t          t                    }t          |
j                  D ]}|r|j        |
j        j        v r|j        }|j
        j        }|t&          k    r8|j        }|                      ||          r||                             |           r|                     |	|g          }|D ]}|                     ||g|          }|j        j        si|                     |          sTt5          t7          j        d	 t          |j                  D                                 } |j        t=          |           }t?          |d
d          s|rY	  || ||| j                    # tB          $ r7}d|j        j"        d|j#        d}||xx         |j$        z  cc<   Y d}~d}~ww xY w|r`tC          d|
j"        dd%                    |          dt5          t7          j        |                                                              |&                                D ]Q\  }}|                     |	|          }|D ]3}|                     |||          }| j                            |           4R|
j        j'        D ]F}tQ          |d          r4|)                    |	| j                   }| 	                    ||
dd           G|rG| j*        &                                D ]\  }}| +                    ||           | j        D ]}| ,                    |j        |           | j-                                        rt          t                    }| j-        &                                D ]D\  }}|&                                D ]*\  }}|r#d|j"        d|j#        d}||xx         |z  cc<   +E|rdt]          d|
j"        dd%                    |          dt5          t7          j        |                                                              dS dS dS )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.
        Nro   r   c                 :    g | ]}t          |j                  S rJ   )getattrr/   )r]   rv   ptrs     r   r   z%Collector.collect.<locals>.<listcomp>(  s%    "N"N"Nc73#9#9"N"N"Nr   FT)r(   r)   collect_relatedrp   r+   )r   c              3   B   K   | ]}d  |j         j        D             V  dS )c              3   $   K   | ]}|j         V  d S r   )attname)r]   rfs     r   r_   z.Collector.collect.<locals>.<genexpr>.<genexpr>Q  s$      SSBRZSSSSSSr   N)r6   foreign_related_fields)r]   rels     r   r_   z$Collector.collect.<locals>.<genexpr>P  sJ       , , # TS#)2RSSS, , , , , ,r   rM   r<   r;   z&Cannot delete some instances of model z= because they are referenced through protected foreign keys: z, r   )r(   r*   r+   z> because they are referenced through restricted foreign keys: )/r   rl   rq   rw   r   ry   rz   r   r   r,   r-   related_namer   rj   rc   r.   all_parentsr6   r   rV   related_modelr   related_objectsqueryselect_relatedr   rh   r   from_iterableonlytupler   r8   r   r   r/   r   joinr   r   r   r   ri   r   r   r&   r$   )r   rs   r(   r*   r   r)   rp   keep_parentsr+   rt   r.   rz   parent_objsmodel_fast_deletesr   r   r6   r   r   batchesbatchr7   referenced_fieldserrorkeyrelated_fieldsru   r   r&   r   r   s                                 @r   r,   zCollector.collect   s>   D %% 	$$T***F88&(7I  
 
  	F% 	 #[7N%+3::<< 
 
 	"N"N"N"NX"N"N"NKLL#$$'$4$A(-+/+0 !     	F(..'--8EE '	J '	JG 1H H HME*4IJ&&#1M##Me#DD "=188???**8eW==G  J J//wNN N1H11-@@H ),+ , ,'H - 3( (, , ,  ) )%  -x}e4E.F.FGH9ou== J JJ!	$xDDDD) J J J J+0;+?+?+?L)#...%2II........JJ+J6  		 . NNNII/0000	 E'(9(@(@(B(BCCDD   .@-E-E-G-G 	3 	3)M>**8^DDG  3 3//~uUU!((22223 [/ 	 	Eu455  55h
KKUTe      	 -1IOO,=,= Q Q(y66}iPPPP' J J;;BHbIIII&--// %0%6%6"-1-D-J-J-L-L < <)M6'-||~~ < <t < </</E/E/Euzzz"RC.s333t;333< & 
)/
 "NNN II&89999 E/0B0I0I0K0KLLMM	 	 		 	 
 
s   /I
J-I??Jc                     t           j                            fd|D             t           j        j                  }|j                            | j                                      |          S )zQ
        Get a QuerySet of the related model to objs via related fields.
        c                 (    g | ]}|j          d fS )__inr   )r]   related_fieldrs   s     r   r   z-Collector.related_objects.<locals>.<listcomp>  s*    UUU]#)))40UUUr   )	connector)r   QcreateOR_base_managerr8   r   )r   r   r   rs   	predicates      ` r   r   zCollector.related_objects  si      M((UUUUnUUU!m& ) 
 
	 *00<<CCINNNr   c              #   ^   K   | j                                         D ]\  }}|D ]}||fV  	d S r   )ri   r   )r   r.   ru   rv   s       r   instances_with_modelzCollector.instances_with_model  sW       $	 1 1 	! 	!E9  ! !Sj    !	! 	!r   c                     g }t                      }t           j                  }t          |          t          |          k     rd}|D ]x}||v r j                            |j        j                  }|r|                    |          s6|	                    |           |
                    |j        j                   d}y|sd S t          |          t          |          k      fd|D              _        d S )NFTc                 ,    i | ]}|j         |         S rJ   )ri   )r]   r.   r   s     r   r   z"Collector.sort.<locals>.<dictcomp>  s"    HHHUDIe,HHHr   )rh   rj   ri   r   rm   getry   rz   
differencerq   rw   )r   sorted_modelsconcrete_modelsr   foundr.   rm   s   `      r   sortzCollector.sort  s   %%di-  3v;;..E ! !M))#044U[5OPP$ !)@)@)Q)Q !!((///#''(BCCC E  -  3v;;.. IHHH-HHH			r   c           	         | j                                         D ]+\  }}t          |t          d                    | j         |<   ,|                                  t                      }t          | j                   dk    rt          |          dk    rt          |          d         }|                     |          rt          j
        | j                  5  t          j        |                              |j        g| j                  }d d d            n# 1 swxY w Y   t!          ||j        j        j        d            ||j        j        |ifS t          j        | j        d          5  |                                 D ]>\  }}|j        j        s-t.          j                            ||| j        | j                   ?| j        D ]>}|                    | j                  }|r||j        j        j        xx         |z  cc<   ?| j                                        D ]\  \  }}	}
g }g }|
D ]N}t?          |t@          j!                  r|j"        |#                    |           9|$                    |           O|r)tK          tL          |          } |j'        di |j(        |	i |r[|d         j)        }t          j*        |          }|+                    t          d	 |D                       |j(        |	i| j                   | j         ,                                D ]}|-                                 | j                                         D ]\  }}t          j        |          }d
 |D             }|                    || j                  }|r||j        j        xx         |z  cc<   |j        j        s2|D ]/}t.          j.                            ||| j        | j                   0	 d d d            n# 1 swxY w Y   | j                                         D ]*\  }}|D ]"}t!          ||j        j        j        d            #+t_          |,                                          ta          |          fS )Nr   )r   r   r   F)r8   	savepoint)senderinstancer8   rg   )r8   c                     h | ]	}|j         
S rJ   r   r]   rv   s     r   	<setcomp>z#Collector.delete.<locals>.<setcomp>  s    555cf555r   c                     g | ]	}|j         
S rJ   r   r   s     r   r   z$Collector.delete.<locals>.<listcomp>  s    777c36777r   rJ   )1ri   r   sortedr   r   r   r   rj   r   r   mark_for_rollback_on_errorr8   r   DeleteQuerydelete_batchr   setattrry   r   labelatomicr   rY   r   r   sendrg   rl   r   r.   rk   
isinstancer   QuerySet_result_cacherq   extendr   r	   rr   r/   r   UpdateQueryupdate_batchr   reverser   sumdict)r   r.   ru   deleted_counterr   countrv   r   r6   rF   instances_listupdatesrs   combined_updatesr   pk_lists                   r   deletezCollector.delete  sE    $	 1 1 	G 	GE9%iZ5E5EFFFDIe
 			!)) ty>>Q3y>>Q#6#6Iq)H##H-- 9 ;DJGG  OE22??!tz E               %+."8$???u{0%888djEBBB :	 :	"7799  
s{/ &++$!$"j#{	 ,    ' C CTZ88 C#BHN$8999UB999 372D2J2J2L2L  .!/ / /I"9fo>>/%3;y1111I.... C'-c7';';$+$+BBuz5.ABBB  G-EOE22E&&5555566U8KTZ  
 "Y--// $ $	!!#### %)IOO$5$5   y..77Y777**7DJ?? @#EK$5666%?666{/ (  +00#(%("&*#';	 1    Y:	 :	 :	 :	 :	 :	 :	 :	 :	 :	 :	 :	 :	 :	 :	x !%	 1 1 	@ 	@E9% @ @%+."8$????@?))++,,d?.C.CCCs%   4DDD,I=O66O:=O:r   )NFF)F)NFTNFFT)r   r   r    r   rw   r@   r3   r?   r   r   r   r   r   r,   r   r   r   r  rJ   r   r   re   re   ^   sB       - - - -*   0F F F F6 6 6? ? ?
  @ @ @6 6 6
*
 *
 *
 *
X  &  X X X XtO O O! ! !
I I I$VD VD VD VD VDr   re   N) collectionsr   r   	functoolsr   r   	itertoolsr   operatorr   r	   	django.dbr
   r   r   r   django.db.modelsr   r   r   r   r$   r9   r=   rA   rO   rQ   rM   rT   rV   rc   re   rJ   r   r   <module>r     s   , , , , , , , , % % % % % % % %       $ $ $ $ $ $ $ $ F F F F F F F F F F F F 6 6 6 6 6 6 6 6 6 61 1 1 1 1^ 1 1 12 2 2 2 2n 2 2 2	: 	: 	:
 
 
D D D
  "6 6 6  E E E	 	 	  hD hD hD hD hD hD hD hD hD hDr   