ó
ø¢TQc           @   sä   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
 d  d l m Z d  d l m Z d e f d „  ƒ  YZ d	 „  Z d
 „  Z d „  Z e d ƒ Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   wraps(   t
   attrgetter(   t   connectionst   transactiont   IntegrityError(   t   signalst   sql(   t
   SortedDict(   t   sixt   ProtectedErrorc           B   s   e  Z d  „  Z RS(   c         C   s&   | |  _  t t |  ƒ j | | ƒ d  S(   N(   t   protected_objectst   superR	   t   __init__(   t   selft   msgR
   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR      s    	(   t   __name__t
   __module__R   (    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR	   
   s   c         C   s_   |  j  | d | j j d | j d | j ƒ| j r[ t | j j r[ |  j | d  | ƒ n  d  S(   Nt   sourcet   source_attrt   nullable(
   t   collectt   relt   tot   namet   nullR   t   featurest   can_defer_constraint_checkst   add_field_updatet   None(   t	   collectort   fieldt   sub_objst   using(    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   CASCADE   s    c         C   s6   t  d | j j j | d j j | j f | ƒ ‚ d  S(   Nso   Cannot delete some instances of model '%s' because they are referenced through a protected foreign key: '%s.%s'i    (   R	   R   R   R   t	   __class__R   (   R   R   R   R    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   PROTECT   s    #c            s1   t  ˆ  ƒ r ‡  f d †  } n ‡  f d †  } | S(   Nc            s   |  j  | ˆ  ƒ  | ƒ d  S(   N(   R   (   R   R   R   R    (   t   value(    s2   ../Django//lib/python/django/db/models/deletion.pyt   set_on_delete"   s    c            s   |  j  | ˆ  | ƒ d  S(   N(   R   (   R   R   R   R    (   R$   (    s2   ../Django//lib/python/django/db/models/deletion.pyR%   %   s    (   t   callable(   R$   R%   (    (   R$   s2   ../Django//lib/python/django/db/models/deletion.pyt   SET    s    c         C   s   |  j  | | j ƒ  | ƒ d  S(   N(   R   t   get_default(   R   R   R   R    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   SET_DEFAULT-   s    c         C   s   d  S(   N(    (   R   R   R   R    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt
   DO_NOTHING1   s    c            s   t  ˆ  ƒ ‡  f d †  ƒ } | S(   Nc            sž   t  j d |  j ƒ s1 t  j d |  j ƒ t } n t } zC ˆ  |  | | Ž | rf t  j d |  j ƒ n t  j d |  j ƒ Wd  | r™ t  j d |  j ƒ n  Xd  S(   NR    (	   R   t
   is_managedR    t   enter_transaction_managementt   Truet   Falset   committ   commit_unless_managedt   leave_transaction_management(   R   t   argst   kwargst   forced_managed(   t   func(    s2   ../Django//lib/python/django/db/models/deletion.pyt	   decorated6   s    	(   R    (   R5   R6   (    (   R5   s2   ../Django//lib/python/django/db/models/deletion.pyt   force_managed5   s    t	   Collectorc           B   sƒ   e  Z d  „  Z d
 e e d „ Z d „  Z d „  Z d
 d „ Z d
 e e	 d
 e d „ Z
 d „  Z d „  Z d „  Z e d	 „  ƒ Z RS(   c         C   s:   | |  _  i  |  _ i  |  _ i  |  _ g  |  _ i  |  _ d  S(   N(   R    t   datat   batchest   field_updatest   fast_deletest   dependencies(   R   R    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR   J   s    					c   	      C   sÊ   | s
 g  Sg  } | d j  } |  j j | t ƒ  ƒ } x* | D]" } | | k r< | j | ƒ q< q< W| j | ƒ | d k	 rÆ | rÆ | r˜ | | } } n  |  j j | j j	 t ƒ  ƒ j
 | j j	 ƒ n  | S(   s9  
        Adds '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.

        Returns a list of all objects that were not already collected.
        i    N(   R"   R9   t
   setdefaultt   sett   appendt   updateR   R=   t   _metat   concrete_modelt   add(	   R   t   objsR   R   t   reverse_dependencyt   new_objst   modelt	   instancest   obj(    (    s2   ../Django//lib/python/django/db/models/deletion.pyRD   [   s    	%c         C   s/   |  j  j | i  ƒ j | t ƒ  ƒ j | ƒ d S(   s–   
        Schedules a batch delete. Every instance of 'model' that is related to
        an instance of 'obj' through 'field' will be deleted.
        N(   R:   R>   R?   RA   (   R   RH   R   RE   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt	   add_batchv   s    c         C   sL   | s
 d S| d j  } |  j j | i  ƒ j | | f t ƒ  ƒ j | ƒ d S(   s‰   
        Schedules a field update. 'objs' must be a homogenous iterable
        collection of model instances (e.g. a QuerySet).
        Ni    (   R"   R;   R>   R?   RA   (   R   R   R$   RE   RH   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR   }   s    	c      	      s  ˆ  r ˆ  j  j t k	 r t St | d ƒ o7 t | d ƒ s> t S| j } t j j | ƒ s} t j	 j | ƒ s} t j
 j | ƒ r t S| j } t ‡  f d †  | j j j j ƒ  Dƒ ƒ r¹ t Sx9 | j d t d t ƒ D] } | j j  j t k	 rÒ t SqÒ Wx! | j D] } | j  j sÿ t Sqÿ Wt S(   sÀ  
        Determines if the objects in the given queryset-like 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. Allows also
        skipping parent -> child -> parent chain preventing fast delete of
        the child.
        RH   t   _raw_deletec         3   s   |  ] } | ˆ  k Vq d  S(   N(    (   t   .0t   link(   t
   from_field(    s2   ../Django//lib/python/django/db/models/deletion.pys	   <genexpr>    s    t   include_hiddent   include_proxy_eq(   R   t	   on_deleteR!   R.   t   hasattrRH   R   t
   pre_deletet   has_listenerst   post_deletet   m2m_changedRB   t   anyRC   t   parentst   valuest   get_all_related_objectsR-   R   R*   t   many_to_manyt   through(   R   RE   RO   RH   t   optst   relatedt   relation(    (   RO   s2   ../Django//lib/python/django/db/models/deletion.pyt   can_fast_delete‰   s(    		+c         C   sí  |  j  | ƒ r# |  j j | ƒ d S|  j | | | d | ƒ} | sH d S| d j } | j j }	 xv t j |	 j j	 ƒ D]_ }
 |
 rw g  | D] } t
 | |
 j ƒ ^ qŠ } |  j | d | d |
 j j d t d t ƒqw qw W| réx£ | j j d t d t ƒ D]† } | j } | j j t k r#qü n  |  j | | ƒ } |  j  | d	 | ƒr]|  j j | ƒ qü | rü | j j |  | | |  j ƒ qü qü Wx` | j j D]O } | j j s“| j | |  j ƒ } |  j | d | d | j j d
 t ƒq“q“Wn  d S(   s  
        Adds 'objs' to the collection of objects to be deleted as well as all
        parent instances.  'objs' must be a homogenous 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.)
        NRF   i    R   R   t   collect_relatedRP   RQ   RO   R   (   Ra   R<   R@   RD   R"   RB   RC   R   t
   itervaluesRY   t   getattrR   R   R   t   related_nameR.   R-   R[   R   RR   R*   t   related_objectsR    R\   R]   t   bulk_related_objects(   R   RE   R   R   Rb   R   RF   RG   RH   RC   t   ptrRJ   t   parent_objsR_   R   R   R`   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR   ®   sD    	%	#c         C   s0   | j  j j |  j ƒ j i | d | j j 6  S(   s_   
        Gets a QuerySet of objects related to ``objs`` via the relation ``related``.

        s   %s__in(   RH   t   _base_managerR    t   filterR   R   (   R   R_   RE   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyRf   ð   s    c         c   sC   x< t  j |  j ƒ D]( \ } } x | D] } | | f Vq& Wq Wd  S(   N(   R   t	   iteritemsR9   (   R   RH   RI   RJ   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   instances_with_modelù   s    c         C   sù   g  } t  ƒ  } t |  j ƒ } x¥ t | ƒ t | ƒ k  rÅ t } xv | D]n } | | k r^ qF n  |  j j | j j ƒ } | oˆ | j	 | ƒ sF | j
 | ƒ | j | j j ƒ t } qF qF W| s! d  Sq! Wt g  | D] } | |  j | f ^ qÐ ƒ |  _ d  S(   N(   R?   t   listR9   t   lenR.   R=   t   getRB   RC   t
   differenceR@   RD   R-   R   (   R   t   sorted_modelst   concrete_modelst   modelst   foundRH   R=   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   sortþ   s"    	c         C   s^  x< |  j  j ƒ  D]+ \ } } t | d t d ƒ ƒ|  j  | <q W|  j ƒ  xK |  j ƒ  D]= \ } } | j j sV t j	 j
 d | d | d |  j ƒ qV qV Wx$ |  j D] } | j d |  j ƒ q¡ Wx‹ t j |  j ƒ D]w \ } } t j | ƒ } xY t j | ƒ D]H \ \ } } } | j g  | D] } | j ^ qi | | j 6|  j ƒ qü WqÑ Wx$ t j |  j  ƒ D] } | j ƒ  q_Wx{ t j |  j ƒ D]g \ } }	 t j | ƒ } xI t j |	 ƒ D]8 \ } } | j g  | D] } | j ^ qÊ|  j | ƒ q±Wq†Wx^ t j |  j  ƒ D]J \ } } t j | ƒ } g  | D] } | j ^ q&}
 | j |
 |  j ƒ qWxK |  j ƒ  D]= \ } } | j j s_t j j
 d | d | d |  j ƒ q_q_Wxj t j |  j ƒ D]V \ } } xG t j | ƒ D]6 \ \ } } } x! | D] } t | | j | ƒ qèWqÏWq³WxJ t j |  j  ƒ D]6 \ } } x' | D] } t | | j j j d  ƒ q3Wq Wd  S(   Nt   keyt   pkt   sendert   instanceR    (   R9   t   itemst   sortedR   Rv   Rm   RB   t   auto_createdR   RT   t   sendR    R<   RL   R   Rl   R;   R   t   UpdateQueryt   update_batchRx   R   Rc   t   reverseR:   t   DeleteQueryt   delete_batchRV   t   setattrt   attnameR   (   R   RH   RI   RJ   t   qst   instances_for_fieldvaluest   queryR   R$   R:   t   pk_listRz   (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   delete  sF    #
"4"N(   R   R   R   R   R.   RD   RK   R   Ra   R-   R   Rf   Rm   Rv   R7   RŠ   (    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyR8   I   s   			%	A				N(   t	   functoolsR    t   operatorR   t	   django.dbR   R   R   t   django.db.modelsR   R   t   django.utils.datastructuresR   t   django.utilsR   R	   R!   R#   R'   R   t   SET_NULLR)   R*   R7   t   objectR8   (    (    (    s2   ../Django//lib/python/django/db/models/deletion.pyt   <module>   s   				
			