
     hQ                         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
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 Zd Zd Z G d d          ZdS )    )Counterdefaultdict)partial)chain)
attrgetter)IntegrityErrorconnections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      U/var/www/html/Sam_Eipo/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   CASCADEr6      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,   r1   s       r   PROTECTr:   "   sP    
. $---QK!***JJJ	
 		 	 	r   c                 |    |                      ||           |                     |j        j        |j                   d S r   )add_restricted_objectsadd_dependencyr*   r+   r1   s       r   RESTRICTr>   /   s<    $$UH555U/5u{CCCCCr   c                 P     t                     r fd}n fd} fd|_        |S )Nc                 F    |                      |             |           d S r   r0   r2   r3   r4   r5   values       r   set_on_deletezSET.<locals>.set_on_delete7   s'    &&ueeggx@@@@@r   c                 6    |                      ||           d S r   rA   rB   s       r   rD   zSET.<locals>.set_on_delete<   s!    &&ueX>>>>>r   c                      d fi fS )Nzdjango.db.models.SET )rC   s   r   <lambda>zSET.<locals>.<lambda>?   s    )?%2(N r   )callabledeconstruct)rC   rD   s   ` r   SETrK   4   sh     ?	A 	A 	A 	A 	A 	A
	? 	? 	? 	? 	? !O N N NMr   c                 4    |                      |d |           d S r   rA   r1   s       r   SET_NULLrM   C   s     udH55555r   c                 X    |                      ||                                |           d S r   )r0   get_defaultr1   s       r   SET_DEFAULTrP   G   s,    ue&7&7&9&98DDDDDr   c                     d S r   rG   r1   s       r   
DO_NOTHINGrR   K   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>R   sd        > #$* 34, CD-	     r   T)include_hidden)
get_fields)optss    r   !get_candidate_relations_to_deleter_   O   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                 D   || _         || _        t          t                    | _        t          t          t          t                              | _        t          t          t          t                              | _        g | _        t          t                    | _	        d S r   )
r5   originr   setdatar   field_updatesr#   fast_deletesdependencies)r   r5   rc   s      r   r   zCollector.__init__Z   su    
$$	(c)B)BCC"-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   re   appendupdater=   )	r   objsr%   r'   rk   new_objsr+   	instancesobjs	            r   addzCollector.addo   s      	IQ!Ie$	 	% 	%C)##$$$""" hBTUUUr   c                     |r||}}| j         |j        j                                     |j        j                   | j                            || j                                                   d S r   )rh   _metaconcrete_modelrr   re   
setdefaultdefault_factory)r   r+   
dependencyrk   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                 x    |sdS |d         j         }| j        |         ||f                             |           dS )z
        Schedule a field update. 'objs' must be a homogeneous iterable
        collection of model instances (e.g. a QuerySet).
        Nr   )r   rf   rm   )r   r3   rC   rn   r+   s        r   r0   zCollector.add_field_update   sH    
  	FQ!5!%,/66t<<<<<r   c                 t    |r5|d         j         }| j        |         |                             |           d S d S )Nr   )r   r#   rm   )r   r3   rn   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 rG   rG   )rY   r3   itemsrn   s      r   
<dictcomp>z?Collector.clear_restricted_objects_from_set.<locals>.<dictcomp>   s3     . . . E5 ut|. . .r   )r#   r}   )r   r+   rn   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 rG   pk)rY   rn   rq   s      r   
<listcomp>zDCollector.clear_restricted_objects_from_queryset.<locals>.<listcomp>   sA        #'       r   )pk__in)r#   rd   filtervaluesr   )r   r+   qsrn   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.
        Frt   r+   _raw_deletec              3   $   K   | ]
}|k    V  d S r   rG   )rY   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   )r3   r*   	on_deleterR   )rY   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rY   r3   s     r   r[   z,Collector.can_fast_delete.<locals>.<genexpr>   sA         E#9::     r   )r*   r   r6   r   rt   r+   r   allru   parentsr   r_   anyprivate_fields)r   rn   r   r+   r^   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 rG   r,   r   s     r   r   z-Collector.get_del_batches.<locals>.<listcomp>   s    666euz666r      c                 *    g | ]}||z            S rG   rG   )rY   iconn_batch_sizern   s     r   r   z-Collector.get_del_batches.<locals>.<listcomp>   s8        Q_,,-  r   r   )maxr	   r5   opsbulk_batch_sizelenrange)r   rn   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 |r&t          |
j                                                  }t          t                    }t          t                    }t!          |
j                  D ]y}|r
|j        |v r|j        }|j
        j        t(          k    r,|j        }|                      ||          r||                             |           f|                     |	|g          }|D ]}|                     ||g|          }|j        j        si|                     |          sTt          t7          j        d	 t!          |j                  D                                 } |j        t=          |           }|rh	 |j
                            | ||| j                   # t@          $ r7}d
|j        j!        d|j"        d
}||xx         |j#        z  cc<   Y d}~d}~ww xY w{|r`tA          d|
j!        dd$                    |          dt          t7          j        |                                                              |%                                D ]Q\  }}|                     |	|          }|D ]3}|                     |||          }| j                            |           4R|
j        j&        D ]F}tO          |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t          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.
        Nrj   r   c                 :    g | ]}t          |j                  S rG   )getattrr,   )rY   rq   ptrs     r   r   z%Collector.collect.<locals>.<listcomp>&  s%    "N"N"Nc73#9#9"N"N"Nr   FT)r%   r&   collect_relatedrk   r(   )r   c              3   B   K   | ]}d  |j         j        D             V  dS )c              3   $   K   | ]}|j         V  d S r   )attname)rY   rfs     r   r[   z.Collector.collect.<locals>.<genexpr>.<genexpr>P  s$      SSBRZSSSSSSr   N)r3   foreign_related_fields)rY   rels     r   r[   z$Collector.collect.<locals>.<genexpr>O  sJ       , , # TS#)2RSSS, , , , , ,r   r9   r8   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   rg   rl   rr   r   rt   ru   r   r   r)   r*   related_namerd   get_parent_listr   listr_   r+   r3   r   rR   related_modelr   related_objectsqueryselect_relatedr   r   from_iterableonlytupler5   r   r   r,   r   joinr}   r   r   r   re   r   r   r#   r!   )r   rn   r%   r'   r   r&   rk   keep_parentsr(   ro   r+   ru   parent_objsr   model_fast_deletesr   r   r3   r   batchesbatchr4   referenced_fieldserrorkeyrelated_fieldsrp   r   r#   r   r   s                                 @r   r)   zCollector.collect   sJ   D %% 	$$T***F88&(7I  
 
  	F% 	 #[7N%+3::<< 
 
 	"N"N"N"NX"N"N"NKLL#$$'$4$A(-+/+0 !     	F 	9%+557788G(..'--8EE &	J &	JG  8 8ME!+z99#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 JJ*44T5(DJWWWW) 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'-JJc                     t          j        fd|D             dt           j        j        i}|j                            | j                                      |          S )zQ
        Get a QuerySet of the related model to objs via related fields.
        c              3   0   K   | ]}|j          d fV  dS )__inNr   )rY   related_fieldrn   s     r   r[   z,Collector.related_objects.<locals>.<genexpr>  s4      VVm-$***D1VVVVVVr   
_connector)r   QOR_base_managerr5   r   )r   r   r   rn   	predicates      ` r   r   zCollector.related_objects  sc      MVVVV~VVV
"}'
 
	 *00<<CCINNNr   c              #   ^   K   | j                                         D ]\  }}|D ]}||fV  	d S r   )re   r}   )r   r+   rp   rq   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 rG   )re   )rY   r+   r   s     r   r~   z"Collector.sort.<locals>.<dictcomp>  s"    HHHUDIe,HHHr   )rd   r   re   r   rh   getrt   ru   
differencerl   rr   )r   sorted_modelsconcrete_modelsmodelsfoundr+   rh   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 ]c\  }}|                                D ]I\  \  }	}
}t          j        |          }|                     d	 |D             |	j!        |
i| j                   Jd| 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 ]#\  \  }	}
}|D ]}t!          ||	j        |
           $;| j                                         D ]*\  }}|D ]"}t!          ||j        j        j        d            #+tK          |"                                          tM          |          fS )Nr   )r   r   r   F)r5   	savepoint)senderinstancer5   rc   )r5   c                     g | ]	}|j         
S rG   r   rY   rq   s     r   r   z$Collector.delete.<locals>.<listcomp>  s    555C555r   c                     g | ]	}|j         
S rG   r   r   s     r   r   z$Collector.delete.<locals>.<listcomp>  s    777c36777r   )'re   r}   sortedr   r   r   r   r   r   r
   mark_for_rollback_on_errorr5   r   DeleteQuerydelete_batchr   setattrrt   r   labelatomicr   rU   r   r   sendrc   rg   r   r+   rf   UpdateQueryupdate_batchr,   r   reverser   sumdict)r   r+   rp   deleted_counterr   countrq   r   instances_for_fieldvaluesr3   rC   r   pk_lists                r   deletezCollector.delete  s,    $	 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 594F4L4L4N4N  001J1P1P1R1R  -NUEIOE22E&&559555
E7JDJ    "Y--// $ $	!!#### %)IOO$5$5   y..77Y777**7DJ?? @#EK$5666%?666{/ (  +00#(%("&*#';	 1    =,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	^ *.);)B)B)D)D 	7 	7%-F-L-L-N-N 7 7)	$ 7 7CC666677 !%	 1 1 	@ 	@E9% @ @%+."8$????@?))++,,d?.C.CCCs%   4DDD,G;M44M8;M8r   )NFF)F)NFTNFFT)r   r   r   r   rr   r=   r0   r<   r   r   r   r   r   r)   r   r   r   r   rG   r   r   ra   ra   Y   sB       - - - -*   0F F F F= = =? ? ?
  @ @ @6 6 6
*
 *
 *
 *
X  &  Y Y Y YvO O O! ! !
I I I$MD MD MD MD MDr   ra   N)collectionsr   r   	functoolsr   	itertoolsr   operatorr   	django.dbr   r	   r
   django.db.modelsr   r   r   r   r!   r6   r:   r>   rK   rM   rP   rR   r_   ra   rG   r   r   <module>r     s   , , , , , , , ,                   > > > > > > > > > > 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	 	 	  cD cD cD cD cD cD cD cD cD cDr   