3
g&M                 @   s   d dl 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mZmZ d dlmZmZmZ G dd	 d	eZG d
d deZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd dZdS )    N)Counterdefaultdict)partialreduce)chain)
attrgetter)IntegrityErrorconnectionstransaction)query_utilssignalssqlc                   s   e Zd Z fddZ  ZS )ProtectedErrorc                s   || _ t j|| d S )N)protected_objectssuper__init__)selfmsgr   )	__class__ W/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/models/deletion.pyr      s    zProtectedError.__init__)__name__
__module____qualname__r   __classcell__r   r   )r   r   r      s   r   c                   s   e Zd Z fddZ  ZS )RestrictedErrorc                s   || _ t j|| d S )N)restricted_objectsr   r   )r   r   r   )r   r   r   r      s    zRestrictedError.__init__)r   r   r   r   r   r   r   )r   r   r      s   r   c             C   sB   | j ||jj|j|jdd |jr>t| jj r>| j|d | d S )NF)sourcesource_attrnullablefail_on_restricted)	collectremote_fieldmodelnamenullr	   featuresZcan_defer_constraint_checksadd_field_update)	collectorfieldsub_objsusingr   r   r   CASCADE   s
    r,   c             C   s(   t d|jjj|d jj|jf |d S )NzoCannot delete some instances of model '%s' because they are referenced through a protected foreign key: '%s.%s'r   )r   r"   r#   r   r   r$   )r(   r)   r*   r+   r   r   r   PROTECT    s    r-   c             C   s"   | j || | j|jj|j d S )N)add_restricted_objectsadd_dependencyr"   r#   )r(   r)   r*   r+   r   r   r   RESTRICT*   s    r0   c                s4   t  r fdd}n fdd} fdd|_|S )Nc                s   | j |  | d S )N)r'   )r(   r)   r*   r+   )valuer   r   set_on_delete1   s    zSET.<locals>.set_on_deletec                s   | j | | d S )N)r'   )r(   r)   r*   r+   )r1   r   r   r2   4   s    c                  s   d fi fS )Nzdjango.db.models.SETr   r   )r1   r   r   <lambda>6   s    zSET.<locals>.<lambda>)callableZdeconstruct)r1   r2   r   )r1   r   SET/   s
    r5   c             C   s   | j |d | d S )N)r'   )r(   r)   r*   r+   r   r   r   SET_NULL:   s    r6   c             C   s   | j ||j | d S )N)r'   get_default)r(   r)   r*   r+   r   r   r   SET_DEFAULT>   s    r8   c             C   s   d S )Nr   )r(   r)   r*   r+   r   r   r   
DO_NOTHINGB   s    r9   c             C   s   dd | j ddD S )Nc             s   s,   | ]$}|j r|j r|js |jr|V  qd S )N)auto_createdZconcreteZ
one_to_oneZone_to_many).0fr   r   r   	<genexpr>J   s    z4get_candidate_relations_to_delete.<locals>.<genexpr>T)Zinclude_hidden)Z
get_fields)optsr   r   r   !get_candidate_relations_to_deleteF   s    r?   c               @   s   e Zd Zdd Zd"ddZd#ddZd	d
 Zdd Zdd Zdd Z	dd Z
d$ddZdd Zd%ddZdd Zdd Zdd Zd d! ZdS )&	Collectorc             C   sD   || _ tt| _tttt| _tttt| _g | _tt| _d S )N)	r+   r   setdatar   field_updatesr   fast_deletesdependencies)r   r+   r   r   r   r   P   s    
zCollector.__init__NFc       	      C   sl   |sg S g }|d j }| j| }x|D ]}||kr&|j| q&W |j| |dk	rh| rh| j|||d |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   rB   appendupdater/   )	r   objsr   r   rF   new_objsr#   	instancesobjr   r   r   addc   s    



zCollector.addc             C   s>   |r|| }}| j |jj j|jj | jj|| jj  d S )N)rE   _metaconcrete_modelrM   rB   
setdefaultdefault_factory)r   r#   
dependencyrF   r   r   r   r/   {   s    
zCollector.add_dependencyc             C   s.   |sdS |d j }| j| ||f j| dS )z
        Schedule a field update. 'objs' must be a homogeneous iterable
        collection of model instances (e.g. a QuerySet).
        Nr   )r   rC   rH   )r   r)   r1   rI   r#   r   r   r   r'      s    
zCollector.add_field_updatec             C   s&   |r"|d j }| j| | j| d S )Nr   )r   r   rH   )r   r)   rI   r#   r   r   r   r.      s    
z Collector.add_restricted_objectsc                s0   || j kr, fdd| j | j D | j |< d S )Nc                s   i | ]\}}|  |qS r   r   )r;   r)   items)rI   r   r   
<dictcomp>   s   z?Collector.clear_restricted_objects_from_set.<locals>.<dictcomp>)r   rS   )r   r#   rI   r   )rI   r   !clear_restricted_objects_from_set   s    

z+Collector.clear_restricted_objects_from_setc             C   s>   || j kr:t|jdd | j | j D d}| j|| d S )Nc             S   s   g | ]}|D ]
}|j qqS r   )pk)r;   rI   rL   r   r   r   
<listcomp>   s   zDCollector.clear_restricted_objects_from_queryset.<locals>.<listcomp>)Zpk__in)r   rA   filtervaluesrU   )r   r#   qsrI   r   r   r   &clear_restricted_objects_from_queryset   s
    
z0Collector.clear_restricted_objects_from_querysetc             C   s   t jj|pt jj|S )N)r   
pre_deleteZhas_listenerspost_delete)r   r#   r   r   r   _has_signal_listeners   s    zCollector._has_signal_listenersc                s    r j jtk	rdS t|dr(|jj}n t|drDt|drD|j}ndS | j|rVdS |j}t fdd|jjj	j
 D otdd t|D otd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.
        FrN   r#   _raw_deletec             3   s   | ]}| kV  qd S )Nr   )r;   link)
from_fieldr   r   r=      s    z,Collector.can_fast_delete.<locals>.<genexpr>c             s   s   | ]}|j jjtkV  qd S )N)r)   r"   	on_deleter9   )r;   relatedr   r   r   r=      s   c             s   s   | ]}t |d V  qdS )bulk_related_objectsN)hasattr)r;   r)   r   r   r   r=      s    )r"   rb   r,   re   rN   r#   r^   allrO   parentsrY   r?   anyprivate_fields)r   rI   ra   r#   r>   r   )ra   r   can_fast_delete   s    


 zCollector.can_fast_deletec                s^   dd |D }t t| j jj|d t krT fddtdt D S gS dS )zT
        Return the objs in suitably sized batches for the used connection.
        c             S   s   g | ]
}|j qS r   )r$   )r;   r)   r   r   r   rW      s    z-Collector.get_del_batches.<locals>.<listcomp>   c                s   g | ]}||   qS r   r   )r;   i)conn_batch_sizerI   r   r   rW      s   r   N)maxr	   r+   opsZbulk_batch_sizelenrange)r   rI   fieldsfield_namesr   )rm   rI   r   get_del_batches   s    zCollector.get_del_batchesTc	                s  | j |r| jj| dS | j||||d}	|	s4dS |	d j}
|s|
jj}xD|jjj D ]4  rX fdd|	D }| j	||
 j
jdddd qXW |sdS |rt|
jj }tt}tt}x6t|
jD ]&}|r|j|krq|j}|j
jtkrq|j}| j ||d	r|| j| q| j|	|g}x|D ]}| j||g|}|jjpR| j|sttjd
d t|jD }|jt| }|r,y|j
j| ||| j W nH t k
r } z*d|jj!|j"f }||  |j#7  < W Y dd}~X nX q,W qW |r"t d|
j!dj$|f ttj|j xL|j% D ]@\}}| j|	|}x(|D ] }| j|||}| jj| qFW q,W x>|
jj&D ]2}t'|drz|j(|	| j}| j	||
ddd qzW |rx$| j)j% D ]\}}| j*|| qW x| jD ]}| j+|j| qW | j,j rtt}xV| j,j% D ]H\}}x<|j% D ]0\}}|r.d|j!|j"f }||  |7  < q.W qW |rt-d|
j!dj$|f ttj|j 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.
        N)rF   r   c                s   g | ]}t | jqS r   )getattrr$   )r;   rL   )ptrr   r   rW      s    z%Collector.collect.<locals>.<listcomp>FT)r   r   collect_relatedrF   r    )ra   c             s   s    | ]}d d |j jD V  qdS )c             s   s   | ]}|j V  qd S )N)attname)r;   rfr   r   r   r=   #  s    z.Collector.collect.<locals>.<genexpr>.<genexpr>N)r)   Zforeign_related_fields)r;   relr   r   r   r=   #  s   z$Collector.collect.<locals>.<genexpr>z'%s.%s'zhCannot delete some instances of model %r because they are referenced through protected foreign keys: %s.z, rd   )r   r   r    ziCannot delete some instances of model %r because they are referenced through restricted foreign keys: %s.).rj   rD   rG   rM   r   rN   rO   rg   rY   r!   r"   Zrelated_namerA   Zget_parent_listr   listr?   r#   r)   rb   r9   related_modelrt   related_objectsqueryZselect_relatedr^   r   from_iterableonlytupler+   r   r   r$   r   joinrS   ri   re   rd   rB   rU   r[   r   r   )r   rI   r   r   rw   r   rF   Zkeep_parentsr    rJ   r#   rO   Zparent_objsrg   Zmodel_fast_deletesr   rc   r)   r|   Zbatchesbatchr*   Zreferenced_fieldserrorkeyrelated_fieldsrK   rZ   r   rr   r   )rv   r   r!      s    



.
zCollector.collectc                s.   t tj fdd|D }|jj| jj|S )zQ
        Get a QuerySet of the related model to objs via related fields.
        c             3   s$   | ]}t jf d |j  iV  qdS )z%s__inN)r   Qr$   )r;   Zrelated_field)rI   r   r   r=   _  s   z,Collector.related_objects.<locals>.<genexpr>)r   operatoror_Z_base_managerr+   rX   )r   r|   r   rI   	predicater   )rI   r   r}   Z  s    

zCollector.related_objectsc             c   s4   x.| j j D ] \}}x|D ]}||fV  qW qW d S )N)rB   rS   )r   r#   rK   rL   r   r   r   instances_with_modeld  s    
zCollector.instances_with_modelc                s   g }t  }t j}xrt|t|k rd}xP|D ]H}||kr>q0 jj|jj}|oZ|j|s0|j	| |j
|jj d}q0W |sd S qW  fdd|D  _d S )NFTc                s   i | ]} j | |qS r   )rB   )r;   r#   )r   r   r   rT   y  s    z"Collector.sort.<locals>.<dictcomp>)rA   r{   rB   rp   rE   getrN   rO   
differencerG   rM   )r   Zsorted_modelsZconcrete_modelsmodelsfoundr#   rE   r   )r   r   sorti  s     


zCollector.sortc             C   s  x,| j j D ]\}}t|tdd| j |< qW | j  t }t| j dkrt|dkrt|d }| j|rt	j
| j tj|j|jg| j}W d Q R X t||jjjd  ||jj|ifS t	j| jddz x0| j D ]$\}}|jjstjj||| jd qW x:| jD ]0}|j| jd}|r||jjj  |7  < qW x^| jj D ]P\}}xD|j D ]8\\}	}
}tj|}|jd	d
 |D |	j |
i| j q^W qLW x| j j! D ]}|j"  qW x| j j D ]x\}}tj|}dd
 |D }|j|| j}|r||jj  |7  < |jjsx"|D ]}tj#j||| jd q"W qW W d Q R X xN| jj! D ]@}x8|j D ],\\}	}
}x|D ]}t||	j|
 q|W qjW q\W x:| j j D ],\}}x |D ]}t||jjjd  qW qW t$|j! t%|fS )NrV   )r   rk   r   F)r+   Z	savepoint)Zsenderinstancer+   )r+   c             S   s   g | ]
}|j qS r   )rV   )r;   rL   r   r   r   rW     s    z$Collector.delete.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )rV   )r;   rL   r   r   r   rW     s    )&rB   rS   sortedr   r   r   rp   r{   rj   r
   Zmark_for_rollback_on_errorr+   r   ZDeleteQueryZdelete_batchrV   setattrrN   rx   labelZatomicr   r:   r   r\   sendrD   r_   r#   rC   ZUpdateQueryZupdate_batchr$   rY   reverser]   sumdict)r   r#   rK   Zdeleted_counterr   countrL   rZ   Zinstances_for_fieldvaluesr)   r1   r~   Zpk_listr   r   r   delete{  sZ    
"



$
 
zCollector.delete)NFF)F)N)NFTNFFT)r   r   r   r   rM   r/   r'   r.   rU   r[   r^   rj   rt   r!   r}   r   r   r   r   r   r   r   r@   O   s$   



$  
 
r@   )r   collectionsr   r   	functoolsr   r   	itertoolsr   r   Z	django.dbr   r	   r
   Zdjango.db.modelsr   r   r   r   r   r,   r-   r0   r5   r6   r8   r9   r?   r@   r   r   r   r   <module>   s"   	
	