3
g                 @   s   d dl Z d dl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
mZ d dlmZ d dlmZmZmZm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mZ d dlm Z  e! Z"f Z#dZ$d/Z%d)d* Z&d+d, Z'G d-d. d.Z(dS )0    N)defaultdict)apps)settings)FieldDoesNotExistImproperlyConfigured)connections)	AutoFieldManagerOrderWrtUniqueConstraint)PathInfo)ImmutableList
OrderedSet)cached_property)import_string)camel_case_to_spacesformat_lazy)overridezyThe return type of '%s' should never be mutated. If you want to manipulate this list for your own use, make a copy first.verbose_nameverbose_name_pluraldb_tableorderingunique_togetherpermissionsget_latest_byorder_with_respect_to	app_labeldb_tablespaceabstractmanagedproxy	swappableauto_createdindex_togetherr   default_permissionsselect_on_savedefault_related_namerequired_db_featuresrequired_db_vendorbase_manager_namedefault_manager_nameindexesconstraintsc             C   sb   yH| s
f S t | ttfst| d }t |ttfs8| f} tdd | D S  tk
r\   | S X dS )z
    option_together can be either a tuple of tuples, or a single
    tuple of two strings. Normalize it to a tuple of tuples, so that
    calling code can uniformly expect that.
    r   c             s   s   | ]}t |V  qd S )N)tuple).0Zot r/   V/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/models/options.py	<genexpr>5   s    z%normalize_together.<locals>.<genexpr>N)
isinstancer-   list	TypeError)Zoption_togetherZfirst_elementr/   r/   r0   normalize_together&   s    r5   c             C   s   t |t|  dS )N)warning)r   IMMUTABLE_WARNING)namedatar/   r/   r0   make_immutable_fields_list<   s    r:   c            	   @   s  e Zd Zddddddddd	h	Zd
ddhZeZdfddZedd Z	edd Z
edd Zedd Zdd Zdd Zdd Zdd Zd d! Zdgd#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Zed;d< Zed=d> Z ed?d@ Z!edAdB Z"edCdD Z#edEdF Z$edGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)dQdR Z*dSdT Z+dUdV Z,edWdX Z-dhdZd[Z.did\d]Z/djd^d_Z0ed`da Z1edbdc Z2eddde Z3dS )kOptionsfieldsmany_to_manyconcrete_fieldslocal_concrete_fields_forward_fields_mapmanagersmanagers_mapbase_managerdefault_managerrelated_objects
fields_map_relation_treeNc             C   s   i | _ g | _g | _g | _g | _d | _d | _d | _d | _d | _	d| _
g | _d| _g | _g | _g | _g | _d| _d| _g | _d | _|| _d | _d | _tj| _g | _d | _|| _d | _d | _d| _ d| _!d| _"d | _#d | _$d | _%i | _&d| _'g | _(| j)| _*d | _+d S )	N FaddchangedeleteviewT)rI   rJ   rK   rL   ),_get_fields_cachelocal_fieldslocal_many_to_manyprivate_fieldslocal_managersr)   r*   
model_namer   r   r   r   _ordering_clashr+   r,   r   r#   r%   r$   r   object_namer   r   r   r   ZDEFAULT_TABLESPACEr   r'   r(   metapkZ
auto_fieldr   r   r    proxy_for_modelconcrete_modelr!   parentsr"   Zrelated_fkey_lookupsdefault_appsr   r&   )selfrU   r   r/   r/   r0   __init__J   sR    zOptions.__init__c             C   s   d| j | jf S )Nz%s.%s)r   rT   )r[   r/   r/   r0   label   s    zOptions.labelc             C   s   d| j | jf S )Nz%s.%s)r   rR   )r[   r/   r/   r0   label_lower   s    zOptions.label_lowerc             C   s   | j jj| jS )N)r   Zapp_configsgetr   )r[   r/   r/   r0   
app_config   s    zOptions.app_configc             C   s
   | j d k	S )N)r`   )r[   r/   r/   r0   	installed   s    zOptions.installedc             C   s  ddl m} ddlm} | |_|| _|j| _| jj | _	t
| j| _i | _| jr| jjj }x | jjD ]}|jdrh||= qhW xhtD ]`}||krt| ||j| t| || j|< qt| j|rt| |t| j| t| || j|< qW t| j| _t| j| _t|jddsBx.dD ]&}t| |g }t| || j|| qW | jd kr\td	| j| _t| joj| j| _|i krt d
dj!| ntd	| j| _| `| j"sd| j#| j	f | _"|| j"|j$j% | _"d S )Nr   )
connection)truncate_name_r   Fr,   r+   z{}sz)'class Meta' got invalid attribute(s): %s,z%s_%s>   r+   r,   )&	django.dbrb   Zdjango.db.backends.utilsrc   _metamodel__name__rT   lowerrR   r   r   Zoriginal_attrsrU   __dict__copy
startswithDEFAULT_NAMESsetattrpopgetattrhasattrr5   r   r#   _format_names_with_classr   r   boolr   r   rS   r4   joinr   r   opsZmax_name_length)r[   clsr8   rb   rc   Z
meta_attrs	attr_nameobjsr/   r/   r0   contribute_to_class   sH    




zOptions.contribute_to_classc             C   sH   g }x>|D ]6}|j  }|j|jjj |jj d |_|j| q
W |S )z4App label/class name interpolation for object names.)r   class)cloner8   rg   r   rj   ri   append)r[   rw   ry   Znew_objsobjr/   r/   r0   rs      s    

z Options._format_names_with_classc             C   s   t | jdtj}| jr>| jjr>t| j}|j d|j d}nd}|sTt| dyt	|}W n< t
k
r } z | d| d}t||W Y d d }~X nX t|tstd| d	| d
|S )NZdefault_auto_field.z.default_auto_fieldDEFAULT_AUTO_FIELDz must not be empty.z refers to the module 'z' that could not be imported.zPrimary key 'z' referred by z must subclass AutoField.)rq   r`   r   r   Z!_is_default_auto_field_overriddentype
__module____qualname__r   r   ImportError
issubclassr   
ValueError)r[   Zpk_class_pathZapp_config_classsourcepk_classemsgr/   r/   r0   _get_default_pk_class   s&    

zOptions._get_default_pk_classc                s  | j r| j y$tfdd| jddD | _ W n& tk
rV   td| jf Y nX d| _tdd |jj	D s|j
dt  nd | _ | jd kr| jrtt| jj   fdd	| j	D }|r|d
  d _| j  n"| j }|dddd}|j
d| d S )Nc             3   s&   | ]}|j  ks|j kr|V  qd S )N)r8   attname)r.   f)queryr/   r0   r1     s    z#Options._prepare.<locals>.<genexpr>F)reversez%s has no field named '%s'_orderc             s   s   | ]}t |tV  qd S )N)r2   r
   )r.   fieldr/   r/   r0   r1   
  s    c                s   g | ]}|j  j kr|qS r/   )r8   )r.   Zfld)r   r/   r0   
<listcomp>  s    z$Options._prepare.<locals>.<listcomp>r   TZID)r   primary_keyr"   id)r   )r   next_get_fieldsStopIterationr   rT   r   anyrg   rN   Zadd_to_classr
   rV   rY   itervaluesr   setup_pkr   )r[   rh   Zalready_createdr   autor/   )r   r   r0   _prepare   s,    "zOptions._preparec             C   s   | j j| | j  d S )N)rQ   r}   _expire_cache)r[   managerr/   r/   r0   add_manager!  s    zOptions.add_managerFc             C   s   |r| j j| n4|jr.|jr.tj| j| ntj| j| | j| |jrt	|j
dr|j
jry|j
jjjdd W n tk
r   Y nX | j  n| jdd d S )Nrh   F)forward)r   )rP   r}   is_relationr=   bisectinsortrO   rN   r   rr   remote_fieldrh   rg   r   AttributeError)r[   r   privater/   r/   r0   	add_field%  s    


zOptions.add_fieldc             C   s   | j  r|jr|| _ d|_d S )NF)rV   r   	serialize)r[   r   r/   r/   r0   r   C  s    zOptions.setup_pkc             C   s   |j j| _|| _|j j| _dS )zb
        Do the internal setup so that the current model is a proxy for
        "target".
        N)rg   rV   rW   r   )r[   targetr/   r/   r0   setup_proxyH  s    
zOptions.setup_proxyc             C   s
   d| j  S )Nz<Options for %s>)rT   )r[   r/   r/   r0   __repr__Q  s    zOptions.__repr__c             C   s   | j S )N)r^   )r[   r/   r/   r0   __str__T  s    zOptions.__str__c                s^   | j s| js| j rdS t tr*t   | jr<| j jkS | jrZt	 fdd| jD S dS )z
        Return True if the model can/should be migrated on the `connection`.
        `connection` can be either a real connection or a connection alias.
        Fc             3   s   | ]}t  j|d V  qdS )FN)rq   features)r.   Zfeat)rb   r/   r0   r1   c  s   z&Options.can_migrate.<locals>.<genexpr>T)
r    swappedr   r2   strr   r(   vendorr'   all)r[   rb   r/   )rb   r0   can_migrateW  s    
zOptions.can_migratec          	   C   s   t d t| jS Q R X dS )z%Return the untranslated verbose name.N)r   r   r   )r[   r/   r/   r0   verbose_name_rawg  s    
zOptions.verbose_name_rawc             C   s^   | j rZtt| j d}|rZy|jd\}}W n tk
r>   |S X d||j f | jkrZ|S dS )a!  
        Has this model been swapped out for another? If so, return the model
        name of the replacement; otherwise, return None.

        For historical reasons, model name lookups using get_model() are
        case insensitive, so we make sure we are case insensitive here.
        Nr   z%s.%s)r!   rq   r   splitr   rj   r^   )r[   Zswapped_forZswapped_labelZswapped_objectr/   r/   r0   r   m  s    	zOptions.swappedc             C   s   g }t  }dd | jj D }xbt|D ]V\}}xL|jjD ]@}|j|krJq:tj|}| j|_|j|j |j	||j
|f q:W q(W tddd t|D S )Nc             s   s   | ]}t |d r|V  qdS )rg   N)rr   )r.   br/   r/   r0   r1     s    z#Options.managers.<locals>.<genexpr>rA   c             s   s   | ]}|d  V  qdS )   Nr/   )r.   mr/   r/   r0   r1     s    )setrh   mro	enumeraterg   rQ   r8   rl   rI   r}   Zcreation_counterr:   sorted)r[   rA   Zseen_managersbasesdepthbaser   r/   r/   r0   rA     s    

zOptions.managersc             C   s   dd | j D S )Nc             S   s   i | ]}||j qS r/   )r8   )r.   r   r/   r/   r0   
<dictcomp>  s    z(Options.managers_map.<locals>.<dictcomp>)rA   )r[   r/   r/   r0   rB     s    zOptions.managers_mapc             C   s   | j }|sFx:| jj dd  D ]$}t|dr|jjdkr@|jj}P qW |r|y
| j| S  tk
rz   td| j	|f Y nX t
 }d|_| j|_d|_|S )N   rg   _base_managerz%s has no manager named %rT)r)   rh   r   rr   r   r8   rB   KeyErrorr   rT   r	   r"   )r[   r)   parentr   r/   r/   r0   rC     s(    

zOptions.base_managerc             C   s   | j }| rD| j rDx.| jj dd  D ]}t|dr(|jj }P q(W |rzy
| j| S  tk
rx   td| j	|f Y nX | j
r| j
d S d S )Nr   rg   z%s has no manager named %rr   )r*   rQ   rh   r   rr   rg   rB   r   r   rT   rA   )r[   r*   r   r/   r/   r0   rD     s     

zOptions.default_managerc                s<   dd dd dd  t d fdd	| jd
dD S )a1  
        Return a list of all forward fields on the model and its parents,
        excluding ManyToManyFields.

        Private API intended only to be used by Django itself; get_fields()
        combined with filtering of field properties is the public API for
        obtaining this field list.
        c             S   s   | j o
| j S )N)r   r=   )r   r/   r/   r0   is_not_an_m2m_field  s    z+Options.fields.<locals>.is_not_an_m2m_fieldc             S   s   | j o
| j S )N)r   Zone_to_many)r   r/   r/   r0   is_not_a_generic_relation  s    z1Options.fields.<locals>.is_not_a_generic_relationc             S   s$   | j o | jo t| jdo| jj  S )Nrh   )r   Zmany_to_onerr   r   rh   )r   r/   r/   r0   is_not_a_generic_foreign_key  s    z4Options.fields.<locals>.is_not_a_generic_foreign_keyr<   c             3   s*   | ]"}|r|r |r|V  qd S )Nr/   )r.   r   )r   r   r   r/   r0   r1     s    z!Options.fields.<locals>.<genexpr>F)r   )r:   r   )r[   r/   )r   r   r   r0   r<     s    zOptions.fieldsc             C   s   t ddd | jD S )a  
        Return a list of all concrete fields on the model and its parents.

        Private API intended only to be used by Django itself; get_fields()
        combined with filtering of field properties is the public API for
        obtaining this field list.
        r>   c             s   s   | ]}|j r|V  qd S )N)concrete)r.   r   r/   r/   r0   r1     s    z*Options.concrete_fields.<locals>.<genexpr>)r:   r<   )r[   r/   r/   r0   r>     s    	zOptions.concrete_fieldsc             C   s   t ddd | jD S )z
        Return a list of all concrete fields on the model.

        Private API intended only to be used by Django itself; get_fields()
        combined with filtering of field properties is the public API for
        obtaining this field list.
        r?   c             s   s   | ]}|j r|V  qd S )N)r   )r.   r   r/   r/   r0   r1     s    z0Options.local_concrete_fields.<locals>.<genexpr>)r:   rN   )r[   r/   r/   r0   r?     s    	zOptions.local_concrete_fieldsc             C   s   t ddd | jddD S )a  
        Return a list of all many to many fields on the model and its parents.

        Private API intended only to be used by Django itself; get_fields()
        combined with filtering of field properties is the public API for
        obtaining this list.
        r=   c             s   s   | ]}|j r|jr|V  qd S )N)r   r=   )r.   r   r/   r/   r0   r1     s    z'Options.many_to_many.<locals>.<genexpr>F)r   )r:   r   )r[   r/   r/   r0   r=     s    	zOptions.many_to_manyc             C   s$   | j dddd}tddd |D S )aw  
        Return all related objects pointing to the current model. The related
        objects can come from a one-to-one, one-to-many, or many-to-many field
        relation type.

        Private API intended only to be used by Django itself; get_fields()
        combined with filtering of field properties is the public API for
        obtaining this field list.
        FT)r   r   include_hiddenrE   c             s   s"   | ]}|j  s|jjr|V  qd S )N)hiddenr   r=   )r.   r~   r/   r/   r0   r1   *  s    z*Options.related_objects.<locals>.<genexpr>)r   r:   )r[   Zall_related_fieldsr/   r/   r0   rE     s    zOptions.related_objectsc             C   sP   i }| j dd}x:|D ]2}|||j< y|||j< W q tk
rF   Y qX qW |S )NF)r   )r   r8   r   r   )r[   resr<   r   r/   r/   r0   r@   -  s    


zOptions._forward_fields_mapc             C   sR   i }| j ddd}x:|D ]2}|||j< y|||j< W q tk
rH   Y qX qW |S )NFT)r   r   )r   r8   r   r   )r[   r   r<   r   r/   r/   r0   rF   <  s    


zOptions.fields_mapc             C   sp   y
| j | S  tk
r8   | jjs4td| j|f Y nX y
| j| S  tk
rj   td| j|f Y nX dS )zW
        Return a field instance given the name of a forward or reverse field.
        z%s has no field named '%s'. The app cache isn't ready yet, so if this is an auto-created related field, it won't be available yet.z%s has no field named '%s'N)r@   r   r   Zmodels_readyr   rT   rF   )r[   
field_namer/   r/   r0   	get_fieldK  s    

zOptions.get_fieldc             C   sN   | j s
g S || j kr|gS x.| j D ]$}|jj|}|r"|jd| |S q"W g S )z
        Return a list of parent classes leading to `model` (ordered from
        closest to most distant ancestor). This has to handle the case where
        `model` is a grandparent or even more distant relation.
        r   )rY   rg   get_base_chaininsert)r[   rh   r   r   r/   r/   r0   r   d  s    
zOptions.get_base_chainc             C   s@   t | j}x,| jD ]"}x|jj D ]}|j| q"W qW t|S )z
        Return all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        )r   rY   rg   get_parent_listrI   r3   )r[   resultr   ancestorr/   r/   r0   r   u  s
    
zOptions.get_parent_listc             C   sF   || j kr| j | S x,| j D ]"}|jj|}|r| j | p<|S qW dS )a\  
        Return the field on the current model which points to the given
        "ancestor". This is possible an indirect link (a pointer to a parent
        model, which points, eventually, to the ancestor). Used when
        constructing table joins for model inheritance.

        Return None if the model isn't an ancestor of this one.
        N)rY   rg   get_ancestor_link)r[   r   r   parent_linkr/   r/   r0   r     s    	

zOptions.get_ancestor_linkc             C   s   | j |krg S | j}g }| }x^| j|D ]P}||kr<|j}q(|j| }|jj f}|j}|jt|j j|||dddd q(W |S )z
        Return a list of PathInfos containing the path from the current
        model to the parent model, or an empty list if parent is not a
        parent of the current model.
        FTN)Z	from_optsZto_optsZtarget_fieldsZ
join_fieldZm2mdirectZfiltered_relation)	rh   rX   r   rg   rY   r   Zget_related_fieldr}   r   )r[   r   Zproxied_modelpathoptsZ	int_modelZfinal_fieldtargetsr/   r/   r0   get_path_to_parent  s(    

zOptions.get_path_to_parentc       	      C   s~   | j |krg S | j}|jj|}|j  |j| g }xBt|dd D ].\}}||d  }|jj|}|j|j	  qHW |S )z
        Return a list of PathInfos containing the path from the parent
        model to the current model, or an empty list if parent is not a
        parent of the current model.
        Nr   )
rh   rX   rg   r   r   r}   r   r   extendZget_reverse_path_info)	r[   r   rh   chainr   ir   childlinkr/   r/   r0   get_path_from_parent  s    

zOptions.get_path_from_parentc       	      C   s   t t}| jjdd}xl|D ]d}|j}|jr.qdd |jdddD }x8|D ]0}t|jj	t
sL|jj	jjjj}|| j| qLW qW x(|D ] }||jjjj }||jjd< qW | jjdtS )a%  
        This method is used by each model to find its reverse objects. As this
        method is very expensive and is accessed frequently (it looks up every
        field in a model, in every app), it is computed on first access and then
        is set as a property on every model.
        T)Zinclude_auto_createdc             s   s"   | ]}|j r|jd k	r|V  qd S )N)r   Zrelated_model)r.   r   r/   r/   r0   r1     s    z<Options._populate_directed_relation_graph.<locals>.<genexpr>F)r   include_parentsrG   )r   r3   r   Z
get_modelsrg   r   r   r2   r   rh   r   rX   r]   r}   rk   r_   EMPTY_RELATION_TREE)	r[   Zrelated_objects_graphZ
all_modelsrh   r   Zfields_with_relationsr   Zremote_labelrE   r/   r/   r0   !_populate_directed_relation_graph  s    


z)Options._populate_directed_relation_graphc             C   s   | j  S )N)r   )r[   r/   r/   r0   rG     s    zOptions._relation_treeTc             C   sb   |r(x"| j D ]}|| jkrt| | qW |rX| j rXx"| jD ]}|| jkr<t| | q<W i | _d S )N)FORWARD_PROPERTIESrk   delattrr   REVERSE_PROPERTIESrM   )r[   r   r   	cache_keyr/   r/   r0   r     s    

zOptions._expire_cachec             C   s   |dkrt }| j||dS )a  
        Return a list of fields associated to the model. By default, include
        forward and reverse fields, fields derived from inheritance, but not
        hidden fields. The returned fields can be changed using the parameters:

        - include_parents: include fields derived from inheritance
        - include_hidden:  include fields that have a related_name that
                           starts with a "+"
        F)r   r   )PROXY_PARENTSr   )r[   r   r   r/   r/   r0   
get_fields  s    
zOptions.get_fieldsc             C   sn  |ddt fkrtd|f |dk}|r.t }|j| j |||||f}y
| j| S  tk
rf   Y nX g }|dk	rxt| jD ]j}	|	|krq||	jj	| j	kr|t krq|xB|	jj
|||||dD ](}
t|
dd s|
j| j	kr|j|
 qW q|W |o| j r,| j}x,|D ]$}|s|jj r|j|j qW |rV|| j7 }|| j7 }|rV|| j7 }td|}|| j|< |S )a  
        Internal helper function to return fields of the model.
        * If forward=True, then fields defined on this model are returned.
        * If reverse=True, then relations pointing to this model are returned.
        * If include_hidden=True, then fields with is_hidden=True are returned.
        * The include_parents argument toggles if fields from parent models
          should be included. It has three values: True, False, and
          PROXY_PARENTS. When set to PROXY_PARENTS, the call will return all
          fields defined for the current model or any of its parents in the
          parent chain to the model's concrete model.
        TFz(Invalid argument for include_parents: %sN)r   r   r   r   seen_modelsr   zget_fields())r   r4   r   rI   rh   rM   r   rY   rg   rX   r   rq   r}   r    rG   r   r   rN   rO   rP   r:   )r[   r   r   r   r   r   Ztopmost_callr   r<   r   r~   Z
all_fieldsr   r/   r/   r0   r     sJ    






zOptions._get_fieldsc             C   s   dd | j D S )z
        Return a list of total unique constraints. Useful for determining set
        of fields guaranteed to be unique for all rows.
        c             S   s$   g | ]}t |tr|jd kr|qS )N)r2   r   	condition)r.   
constraintr/   r/   r0   r   g  s   z4Options.total_unique_constraints.<locals>.<listcomp>)r,   )r[   r/   r/   r0   total_unique_constraints`  s    z Options.total_unique_constraintsc             C   sB   g }x4t | jD ]&}tj| j|}t|tr|j| qW t|S )zAReturn a set of the names of the properties defined on the model.)dirrh   inspectgetattr_staticr2   propertyr}   	frozenset)r[   namesr8   attrr/   r/   r0   _property_namesl  s    
zOptions._property_namesc             C   s   dd | j ddtdD S )z
        Private API intended only to be used by Django itself.
        Fields to be returned after a database insert.
        c             S   s   g | ]}t |d dr|qS )Zdb_returningF)rq   )r.   r   r/   r/   r0   r   }  s    z/Options.db_returning_fields.<locals>.<listcomp>TF)r   r   r   )r   r   )r[   r/   r/   r0   db_returning_fieldsv  s    zOptions.db_returning_fields)N)F)TT)TF)TTTFN)4ri   r   r   r   r   r   rZ   r\   r   r]   r^   r`   ra   rz   rs   r   r   r   r   r   r   r   r   r   r   r   r   rA   rB   rC   rD   r<   r>   r?   r=   rE   r@   rF   r   r   r   r   r   r   r   rG   r   r   r   r   r   r   r/   r/   r/   r0   r;   @   sb   

9=%
	"%

 
S
r;   )r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r   r$   r%   r&   r'   r(   r)   r*   r+   r,   ))r   rl   r   collectionsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.exceptionsr   r   rf   r   Zdjango.db.modelsr   r	   r
   r   Zdjango.db.models.query_utilsr   Zdjango.utils.datastructuresr   r   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   Zdjango.utils.textr   r   Zdjango.utils.translationr   objectr   r   r7   rn   r5   r:   r;   r/   r/   r/   r0   <module>   s6         