ó
P'—^c           @   s  d  d l  m Z d  d l m Z m Z d  d l m Z m Z d  d l Z d  d l	 m
 Z
 d  d l m Z d  d l m Z m Z d  d l m Z m Z d  d	 l m Z m Z m Z m Z y d  d
 l m Z Wn' e k
 ró d e f d „  ƒ  YZ n Xy d  d l m Z Wn' e k
 r1d e f d „  ƒ  YZ n Xy d  d l m Z Wn' e k
 rod e f d „  ƒ  YZ n Xd d l m  Z  d „  Z! d S(   iÿÿÿÿ(   t   product(   t   group_byt	   join_with(   t   lcatt   lmapN(   t   QuerySet(   t   OR(   t   Queryt
   ExtraWhere(   t   NothingNodet   SubqueryConstraint(   t   Lookupt   Exactt   Int   IsNull(   t   EverythingNodeR   c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyR      s   (   t   SubqueryR   c           B   s   e  Z RS(    (   R   R   (    (    (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyR      s   (   t   RawSQLR   c           B   s   e  Z RS(    (   R   R   (    (    (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyR      s   i   (   t   settingsc            s¾   t  ƒ  ‰  d	 d	 ˆ  t f g g ‰ d „  ‰ ‡ ‡ ‡ f d †  ‰ ‡  f d †  ‰ ‡ f d †  ‰ ‡ ‡ f d †  ‰ t j d
 k r­ |  j j r­ t t ‡ f d †  |  j j Dƒ ƒ Sˆ |  j ƒ Sd	 S(   s  
    Converts query condition tree into a DNF of eq conds.
    Separately for each alias.

    Any negations, conditions with lookups other than __exact or __in,
    conditions on joined models and subrequests are ignored.
    __in is converted into = or = or = ...
    c         S   s!   |  d |  d |  d |  d f S(   Ni    i   i   i   (    (   t   term(    (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt   negate-   s    c            s?  t  |  t ƒ r+t |  j d ƒ s% ˆ  St  |  j t t t t f ƒ rG ˆ  St  |  j j	 t
 j ƒ rc ˆ  S|  j j	 j } t  |  t ƒ r  |  j j | |  j t f g g St  |  t ƒ rÎ |  j j | d |  j f g g St  |  t ƒ r$t |  j ƒ t
 j k  r$g  |  j D]! } |  j j | | t f g ^ qÿ Sˆ  Snt  |  t ƒ rAg  g St  |  t ƒ rTg  St  |  t t f ƒ rmˆ  St |  ƒ d k r†g  g St ˆ |  j ƒ } t | ƒ d k r±g  g St | ƒ d k rÐ| d } n3 |  j t k rît | ƒ } n t t t | Œ  ƒ } |  j r7g  t | Œ  D] } t ˆ | ƒ ^ q} n  | Sd S(   s  
        Constructs DNF of where tree consisting of terms in form:
            (alias, attribute, value, negation)
        meaning `alias.attribute = value`
         or `not alias.attribute = value` if negation is False

        Any conditions other then eq are dropped.
        t   targeti    i   N(    t
   isinstanceR   t   hasattrt   lhst   rhsR   R   R   R   R   R   t   CACHEOPS_SKIP_FIELDSt   attnameR   t   aliast   TrueR   t   NoneR   t   lent   CACHEOPS_LONG_DISJUNCTIONR   R	   R   R
   R   t   childrent	   connectorR   R   R    t   negated(   t   whereR   t   vt   chilren_dnfst   resultt   p(   t	   SOME_TREEt   _dnfR   (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyR,   0   sD    	'/	+c            sr   i  } xe |  D]] \ } } } } | ˆ  k	 r | r | | k r | | k r] | | | k r] d  S| | | <q q W| S(   N(   R    (   t   conjt	   for_aliast   condsR   R   t   valuet   negation(   t   SOME(    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt
   clean_conjj   s    c            sk   g  |  D]" } | D] } ˆ  | | ƒ ^ q q } g  | D] } | d  k	 r6 | ^ q6 } t | ƒ sg i  g S| S(   N(   R    t   all(   t   treet   aliasesR-   R   t   cleaned(   R3   (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt	   clean_dnfu   s
    /%c            s‰   ‡ ‡ f d †  } ˆ ˆ j  ƒ ‰  ˆ j j j ‰ ‡ f d †  ˆ j j ƒ  Dƒ ˆ h d h B} t | | ƒ } ‡ ‡  f d †  | j ƒ  Dƒ S(   Nc            s   |  ˆ  k r |  Sˆ j  |  j S(   N(   t	   alias_mapt
   table_name(   R   (   t
   main_aliast   query(    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt	   table_for   s    c            s)   h  |  ] \ } } ˆ  j  | r | ’ q S(    (   t   alias_refcount(   t   .0R   t   join(   R<   (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pys	   <setcomp>Š   s   	 	t   django_content_typec            s(   i  |  ] \ } } ˆ  ˆ | ƒ | “ q S(    (    (   R?   t   tablet   table_aliases(   R8   t   dnf(    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pys
   <dictcomp>Ž   s   	 (   R&   t   modelt   _metat   db_tableR9   t   itemsR   (   R<   R=   R6   t   tables(   R,   R8   (   RD   R;   R<   s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt	   query_dnf€   s    i   i   c         3   s   |  ] } ˆ  | ƒ Vq d  S(   N(    (   R?   t   q(   RJ   (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pys	   <genexpr>‘   s    N(   i   i   (	   t   objectR    R   t   djangot   VERSIONR<   t   combined_queriesR   R   (   t   qs(    (   R2   R+   R,   R3   R8   R   RJ   s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt   dnfs!   s    			:#("   t	   itertoolsR    t   funcyR   R   t	   funcy.py3R   R   RM   t   django.db.models.queryR   t   django.db.models.sqlR   t   django.db.models.sql.queryR   R   t   django.db.models.sql.whereR	   R
   t   django.db.models.lookupsR   R   R   R   R   t   ImportErrorRL   t   django.db.modelsR   t   django.db.models.expressionsR   t   confR   RQ   (    (    (    s/   /tmp/pip-unpacked-wheel-CLcuMp/cacheops/tree.pyt   <module>   s,   "