3
g0                 @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ edd	ZG d
d deZG dd deedZdd ZG dd dejZG dd dZG dd dZd ddZdd Zdd ZG dd dZdS )!z
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.
    N)
namedtuple)FieldDoesNotExist
FieldError)
LOOKUP_SEP)tree)RemovedInDjango40WarningPathInfozGfrom_opts to_opts target_fields join_field m2m direct filtered_relationc                   s<   e Zd Zedd Zdd Z fddZ fddZ  ZS )	InvalidQueryTypec             C   s   t tfS )N)r   r   )self r   Z/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/models/query_utils.py_subclasses   s    zInvalidQueryType._subclassesc             C   s   t jdtdd d S )Nz\The InvalidQuery exception class is deprecated. Use FieldDoesNotExist or FieldError instead.   )category
stacklevel)warningswarnr   )r
   r   r   r   Z__warn   s    zInvalidQueryType.__warnc                s    | j   t|| jpt j|S )N)_InvalidQueryType__warn
isinstancer   super__instancecheck__)r
   instance)	__class__r   r   r   &   s    z"InvalidQueryType.__instancecheck__c                s    | j   t|| jpt j|S )N)r   
issubclassr   r   __subclasscheck__)r
   subclass)r   r   r   r   *   s    z"InvalidQueryType.__subclasscheck__)	__name__
__module____qualname__propertyr   r   r   r   __classcell__r   r   )r   r   r	      s   r	   c               @   s   e Zd ZdS )InvalidQueryN)r   r   r   r   r   r   r   r!   /   s   r!   )	metaclassc             c   s*   | V  x| j  D ]}t|E d H  qW d S )N)__subclasses__
subclasses)clsr   r   r   r   r$   3   s    r$   c                   sj   e Zd ZdZdZdZeZdZddd fdd	
Zd
d Z	dd Z
dd Zdd ZdddZdd Z  ZS )Qze
    Encapsulate filters as objects that can then be combined logically (using
    `&` and `|`).
    ANDORTNF)
_connector_negatedc               s"   t  j|t|j ||d d S )N)children	connectornegated)r   __init__sorteditems)r
   r)   r*   argskwargs)r   r   r   r.   D   s    z
Q.__init__c             C   s   t |tpt|dddks"t|| sBt|dr8|j S tj|S t |trn| rn| j \}}}t| ||S t|  }||_|j	| | |j	|| |S )NconditionalFTcopy)
r   r&   getattr	TypeErrorhasattrr4   deconstructtyper,   add)r
   otherconn_r1   r2   objr   r   r   _combineG   s    
z
Q._combinec             C   s   | j || jS )N)r?   r(   )r
   r;   r   r   r   __or__W   s    zQ.__or__c             C   s   | j || jS )N)r?   r'   )r
   r;   r   r   r   __and__Z   s    z	Q.__and__c             C   s$   t |  }|j| | j |j  |S )N)r9   r:   r'   negate)r
   r>   r   r   r   
__invert__]   s    
zQ.__invert__c             C   s&   |j | ||ddd\}}|j| |S )NF)allow_joinsZ
split_subqZcheck_filterable)Z_add_qZpromote_joins)r
   queryrD   reuseZ	summarizeZfor_saveZclauseZjoinsr   r   r   resolve_expressionc   s
    
zQ.resolve_expressionc             C   sf   d| j j| j jf }|jdr*|jdd}t| j}i }| j| jkrN| j|d< | j	r\d|d< |||fS )Nz%s.%szdjango.db.models.query_utilszdjango.db.modelsr)   Tr*   )
r   r   r   
startswithreplacetupler+   r,   defaultr-   )r
   pathr1   r2   r   r   r   r8   m   s    


zQ.deconstruct)NTNFF)r   r   r   __doc__r'   r(   rK   r3   r.   r?   r@   rA   rC   rG   r8   r    r   r   )r   r   r&   9   s   

r&   c               @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
DeferredAttributez
    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)field)r
   rO   r   r   r   r.      s    zDeferredAttribute.__init__Nc             C   sT   |dkr| S |j }| jj}||krL| j|}|dkrD|j|gd n|||< || S )zx
        Retrieve and caches the value from the datastore on the first lookup.
        Return the cached value.
        N)fields)__dict__rO   attname_check_parent_chainZrefresh_from_db)r
   r   r%   data
field_namevalr   r   r   __get__   s    
zDeferredAttribute.__get__c             C   s6   |j }|j| jj}| jjr2| j|kr2t||jS dS )z
        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)_metaZget_ancestor_linkrO   modelprimary_keyr5   rR   )r
   r   optsZ
link_fieldr   r   r   rS      s
    z%DeferredAttribute._check_parent_chain)N)r   r   r   rM   r.   rW   rS   r   r   r   r   rN   z   s   
rN   c               @   st   e Zd Zedd Zeejdddd Zdd Zd	d
 Z	e
dd Zedd ZedddZedddZdS )RegisterLookupMixinc             C   s   | j  j|d S )N)get_lookupsget)r%   lookup_namer   r   r   _get_lookup   s    zRegisterLookupMixin._get_lookupN)maxsizec             C   s   dd t j| D }| j|S )Nc             S   s   g | ]}|j jd i qS )class_lookups)rQ   r^   ).0parentr   r   r   
<listcomp>   s    z3RegisterLookupMixin.get_lookups.<locals>.<listcomp>)inspectgetmromerge_dicts)r%   rb   r   r   r   r]      s    zRegisterLookupMixin.get_lookupsc             C   sP   ddl m} | j|}|d kr4t| dr4| jj|S |d k	rLt|| rLd S |S )Nr   )Lookupoutput_field)django.db.models.lookupsri   r`   r7   rj   
get_lookupr   )r
   r_   ri   foundr   r   r   rl      s    
zRegisterLookupMixin.get_lookupc             C   sP   ddl m} | j|}|d kr4t| dr4| jj|S |d k	rLt|| rLd S |S )Nr   )	Transformrj   )rk   rn   r`   r7   rj   get_transformr   )r
   r_   rn   rm   r   r   r   ro      s    
z!RegisterLookupMixin.get_transformc             C   s$   i }xt | D ]}|j| qW |S )z
        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'.
        )reversedupdate)Zdictsmergeddr   r   r   rh      s    zRegisterLookupMixin.merge_dictsc             C   s    xt | D ]}|jj  q
W d S )N)r$   r]   cache_clear)r%   r   r   r   r   _clear_cached_lookups   s    z)RegisterLookupMixin._clear_cached_lookupsc             C   s4   |d kr|j }d| jkri | _|| j|< | j  |S )Nrb   )r_   rQ   rb   ru   )r%   lookupr_   r   r   r   register_lookup   s    

z#RegisterLookupMixin.register_lookupc             C   s   |dkr|j }| j|= dS )zn
        Remove given lookup from cls lookups. For use in tests only as it's
        not thread-safe.
        N)r_   rb   )r%   rv   r_   r   r   r   _unregister_lookup   s    z&RegisterLookupMixin._unregister_lookup)N)N)r   r   r   classmethodr`   	functools	lru_cacher]   rl   ro   staticmethodrh   ru   rw   rx   r   r   r   r   r\      s   			r\   Fc             C   s   | j s
dS | j jr| rdS |rH|r4| j |kr4dS | rH| j|krHdS | rX| jrXdS |r| j|kr|r| j|krd| jjj| jf }t	|dS )au  
    Return 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
    FzXField %s.%s cannot be both deferred and traversed using select_related at the same time.T)
Zremote_fieldZparent_linkZrelated_query_namenamenullrR   rY   rX   Zobject_namer   )rO   Z
restricted	requestedZload_fieldsreversemsgr   r   r   select_related_descend   s$    
r   c             C   sZ   xPt dt| d D ]:}tj| d| }||kr|| r|| | |d fS qW df fS )z
    Check if the lookup_parts contains references to the given annotations set.
    Because the LOOKUP_SEP is contained in the default annotation names, check
    each prefix of the lookup_parts for a match.
       r   NF)rangelenr   join)Zlookup_partsannotationsnZlevel_n_lookupr   r   r   refs_expression
  s
    r   c                s,    fdd}||p*t |ddo*||jjS )z
    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)rX   Zconcrete_modelZget_parent_list)r[   )rY   r   r   check  s    z-check_rel_lookup_compatibility.<locals>.checkrZ   F)r5   rY   rX   )rY   Ztarget_optsrO   r   r   )rY   r   check_rel_lookup_compatibility  s    r   c               @   s@   e Zd ZdZe dddZdd Zdd Zd	d
 Zdd Z	dS )FilteredRelationz7Specify custom filtering in the ON clause of SQL joins.)	conditionc            C   s:   |st d|| _d | _t|ts*t d|| _g | _d S )Nzrelation_name cannot be empty.z*condition argument must be a Q() instance.)
ValueErrorrelation_namealiasr   r&   r   rL   )r
   r   r   r   r   r   r.   6  s    
zFilteredRelation.__init__c             C   s4   t || jstS | j|jko2| j|jko2| j|jkS )N)r   r   NotImplementedr   r   r   )r
   r;   r   r   r   __eq__@  s
    zFilteredRelation.__eq__c             C   s,   t | j| jd}| j|_| jd d  |_|S )N)r   )r   r   r   r   rL   )r
   cloner   r   r   r   I  s    zFilteredRelation.clonec             O   s   t ddS )zz
        QuerySet.annotate() only accepts expression-like arguments
        (with a resolve_expression() method).
        z0FilteredRelation.resolve_expression() is unused.N)NotImplementedError)r
   r1   r2   r   r   r   rG   O  s    z#FilteredRelation.resolve_expressionc             C   s&   |j }|j| jt| jd}|j|S )N)rF   )rE   Zbuild_filtered_relation_qr   setrL   compile)r
   compiler
connectionrE   wherer   r   r   as_sqlV  s    zFilteredRelation.as_sqlN)
r   r   r   rM   r&   r.   r   r   rG   r   r   r   r   r   r   3  s   
	r   )F)rM   r4   rz   rf   r   collectionsr   Zdjango.core.exceptionsr   r   Zdjango.db.models.constantsr   Zdjango.utilsr   Zdjango.utils.deprecationr   r   r9   r	   	Exceptionr!   r$   Noder&   rN   r\   r   r   r   r   r   r   r   r   <module>   s(   
A(C
%