ó
®â0_c           @  s  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d d l
 m Z e d d	 ƒ Z d
 e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d e	 j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d „ Z d „  Z d „  Z d S(   uî   
Various data structures used in query construction.

Factored out from django.db.models.query to avoid making the main module very
large and/or so that they can be used by other modules without getting into
circular import difficulties.
iÿÿÿÿ(   t   unicode_literalsN(   t
   namedtuple(   t
   LOOKUP_SEP(   t   tree(   t	   lru_cacheu   PathInfou5   from_opts to_opts target_fields join_field m2m directt   InvalidQueryc           B  s   e  Z d  Z RS(   uE   
    The query passed to raw isn't a safe query to use with raw.
    (   t   __name__t
   __module__t   __doc__(    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR      s   c         c  s<   |  Vx0 |  j  ƒ  D]" } x t | ƒ D] } | Vq% Wq Wd  S(   N(   t   __subclasses__t
   subclasses(   t   clst   subclasst   item(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR
      s    t   QueryWrapperc           B  s,   e  Z d  Z e Z d „  Z d d d „ Z RS(   u¢   
    A type that indicates the contents are an SQL fragment and the associate
    parameters. Can be used to pass opaque data to a where-clause, for example.
    c         C  s   | t  | ƒ f |  _ d  S(   N(   t   listt   data(   t   selft   sqlt   params(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   __init__.   s    c         C  s   |  j  S(   N(   R   (   R   t   compilert
   connection(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   as_sql1   s    N(   R   R   R   t   Falset   contains_aggregateR   t   NoneR   (    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR   '   s   	t   Qc           B  se   e  Z d  Z d Z d Z e Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 e d	 e e d „ Z RS(
   uf   
    Encapsulates filters as objects that can then be combined logically (using
    `&` and `|`).
    u   ANDu   ORc         O  s3   t  t |  ƒ j d t | ƒ t | j ƒ  ƒ ƒ d  S(   Nt   children(   t   superR   R   R   t   items(   R   t   argst   kwargs(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR   ?   s    c         C  sZ   t  | t ƒ s t | ƒ ‚ n  t |  ƒ ƒ  } | | _ | j |  | ƒ | j | | ƒ | S(   N(   t
   isinstanceR   t	   TypeErrort   typet	   connectort   add(   R   t   othert   connt   obj(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   _combineB   s    	c         C  s   |  j  | |  j ƒ S(   N(   R)   t   OR(   R   R&   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   __or__K   s    c         C  s   |  j  | |  j ƒ S(   N(   R)   t   AND(   R   R&   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   __and__N   s    c         C  s0   t  |  ƒ ƒ  } | j |  |  j ƒ | j ƒ  | S(   N(   R#   R%   R,   t   negate(   R   R(   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt
   __invert__Q   s    
c         C  s5   | j  |  | d | d t ƒ\ } } | j | ƒ | S(   Nt   allow_joinst
   split_subq(   t   _add_qR   t   promote_joins(   R   t   queryR0   t   reuset	   summarizet   for_savet   clauset   joins(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   resolve_expressionW   s    $N(   R   R   R   R,   R*   t   defaultR   R)   R+   R-   R/   R   t   TrueR   R:   (    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR   5   s   						t   DeferredAttributec           B  s,   e  Z d  Z d „  Z d d „ Z d „  Z RS(   u„   
    A wrapper for a deferred-loading field. When the value is read from this
    object the first time, the query is executed.
    c         C  s   | |  _  d  S(   N(   t
   field_name(   R   R>   t   model(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR   d   s    c         C  s›   | d k r |  S| j } | j |  j |  ƒ |  k r |  j | |  j ƒ } | d k r€ | j d |  j g ƒ t | |  j ƒ } n  | | |  j <n  | |  j S(   uz   
        Retrieves and caches the value from the datastore on the first lookup.
        Returns the cached value.
        t   fieldsN(   R   t   __dict__t   getR>   t   _check_parent_chaint   refresh_from_dbt   getattr(   R   t   instanceR   R   t   val(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   __get__g   s    	c         C  sS   | j  } | j | ƒ } | j | j ƒ } | j rO | | k rO t | | j ƒ Sd S(   uÁ   
        Check if the field value can be fetched from a parent field already
        loaded in the instance. This can be done if the to-be fetched
        field is a primary key field.
        N(   t   _metat	   get_fieldt   get_ancestor_linkR?   t   primary_keyRE   t   attnameR   (   R   RF   t   namet   optst   ft
   link_field(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyRC   y   s    	N(   R   R   R   R   R   RH   RC   (    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR=   _   s   	t   RegisterLookupMixinc           B  s‰   e  Z e d  „  ƒ Z e e d d	 ƒ d „  ƒ ƒ Z d „  Z d „  Z e	 d „  ƒ Z
 e d „  ƒ Z e d	 d „ ƒ Z e d	 d „ ƒ Z RS(
   c         C  s   |  j  ƒ  j | d  ƒ S(   N(   t   get_lookupsRB   R   (   R   t   lookup_name(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   _get_lookup‰   s    t   maxsizec         C  s>   g  t  j |  ƒ D] } | j j d i  ƒ ^ q } |  j | ƒ S(   Nu   class_lookups(   t   inspectt   getmroRA   RB   t   merge_dicts(   R   t   parentt   class_lookups(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyRS      s    1c         C  sn   d d l  m } |  j | ƒ } | d  k rJ t |  d ƒ rJ |  j j | ƒ S| d  k	 rj t | | ƒ rj d  S| S(   Niÿÿÿÿ(   t   Lookupu   output_field(   t   django.db.models.lookupsR\   RU   R   t   hasattrt   output_fieldt
   get_lookupt
   issubclass(   R   RT   R\   t   found(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyR`   “   s    c         C  sn   d d l  m } |  j | ƒ } | d  k rJ t |  d ƒ rJ |  j j | ƒ S| d  k	 rj t | | ƒ rj d  S| S(   Niÿÿÿÿ(   t	   Transformu   output_field(   R]   Rc   RU   R   R^   R_   t   get_transformRa   (   R   RT   Rc   Rb   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyRd   œ   s    c         C  s.   i  } x! t  |  ƒ D] } | j | ƒ q W| S(   u«   
        Merge dicts in reverse to preference the order of the original list. e.g.,
        merge_dicts([a, b]) will preference the keys in 'a' over those in 'b'.
        (   t   reversedt   update(   t   dictst   mergedt   d(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyRY   ¥   s    c         C  s(   x! t  |  ƒ D] } | j j ƒ  q Wd  S(   N(   R
   RS   t   cache_clear(   R   R   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   _clear_cached_lookups°   s    c         C  sN   | d  k r | j } n  d |  j k r3 i  |  _ n  | |  j | <|  j ƒ  | S(   Nu   class_lookups(   R   RT   RA   R[   Rk   (   R   t   lookupRT   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   register_lookupµ   s    
c         C  s&   | d k r | j } n  |  j | =d S(   un   
        Remove given lookup from cls lookups. For use in tests only as it's
        not thread-safe.
        N(   R   RT   R[   (   R   Rl   RT   (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   _unregister_lookup¿   s    N(   R   R   t   classmethodRU   R   R   RS   R`   Rd   t   staticmethodRY   Rk   Rm   Rn   (    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyRR   ‡   s   					c         C  sÐ   |  j  s t S|  j  j r$ | r$ t S| rc | rF |  j ƒ  | k rF t S| rc |  j | k rc t Sn  | rw |  j rw t S| rÌ |  j | k rÌ | rÉ |  j | k rÉ t d |  j j	 j
 |  j f ƒ ‚ qÉ qÌ n  t S(   uv  
    Returns True if this field should be used to descend deeper for
    select_related() purposes. Used by both the query construction code
    (sql.query.fill_related_selections()) and the model instance creation code
    (query.get_klass_info()).

    Arguments:
     * field - the field to be checked
     * restricted - a boolean field, indicating if the field list has been
       manually restricted using a requested clause)
     * requested - The select_related() dictionary.
     * load_fields - the set of fields to be loaded on this model
     * reverse - boolean, True if we are checking a reverse select related
    uX   Field %s.%s cannot be both deferred and traversed using select_related at the same time.(   t   remote_fieldR   t   parent_linkt   related_query_nameRN   t   nullRM   R   R?   RI   t   object_nameR<   (   t   fieldt
   restrictedt	   requestedt   load_fieldst   reverse(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   select_related_descendÊ   s"    	%c         C  si   x\ t  t |  ƒ d ƒ D]D } t j |  d | !ƒ } | | k r | | r | | |  | f Sq Wt d f S(   uó   
    A helper method to check if the lookup_parts contains references
    to the given annotations set. Because the LOOKUP_SEP is contained in the
    default annotation names we must check each prefix of the lookup_parts
    for a match.
    i   i    (    (   t   ranget   lenR   t   joinR   (   t   lookup_partst   annotationst   nt   level_n_lookup(    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   refs_expressionî   s
    c           s=   ‡  f d †  } | | ƒ p< t  | d t ƒ o< | | j j ƒ S(   uÞ   
    Check that self.model is compatible with target_opts. Compatibility
    is OK if:
      1) model and opts match (where proxy inheritance is removed)
      2) model is parent of opts' model or the other way around
    c           s=   ˆ  j  j |  j k p< |  j ˆ  j  j ƒ  k p< ˆ  |  j ƒ  k S(   N(   RI   t   concrete_modelt   get_parent_list(   RO   (   R?   (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   check  s    u   primary_key(   RE   R   R?   RI   (   R?   t   target_optsRv   R†   (    (   R?   s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   check_rel_lookup_compatibilityü   s    (   R   t
   __future__R    RW   t   collectionsR   t   django.db.models.constantsR   t   django.utilsR   t   django.utils.lru_cacheR   t   PathInfot	   ExceptionR   R
   t   objectR   t   NodeR   R=   RR   R   R{   Rƒ   Rˆ   (    (    (    s>   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/query_utils.pyt   <module>   s    		*(C$	