
 Xv_                 @   s  d  d l  m 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 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 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' d d l( m) Z) d d d  Z* d d   Z+ d d   Z, Gd d   d e-  Z. Gd d   d e	  Z/ Gd  d!   d! e  Z0 Gd" d#   d# e-  Z1 d S)$    )unicode_literalsN)OrderedDict)contextmanager)	AppConfig)Appsapps)settings)models)OrderWrt)RECURSIVE_RELATIONSHIP_CONSTANT)DEFAULT_NAMESnormalize_together)make_model_tuple)six)RemovedInDjango20Warning)
force_text
smart_text)cached_property)import_string)get_docs_version   )InvalidBasesError c             C   sh   t  |  t j  rN |  j d d  } t |  d k r@ t |  S| | d f S|  j j |  j j f Sd  S)N.r      r   )	
isinstancer   string_typessplitlentuple_meta	app_label
model_name)modelr!   r    r$   H/home/ubuntu/projects/ifolica/build/django/django/db/migrations/state.py_get_app_label_and_model_name   s    *r&   c             C   s   d d   |  j    D } t   } xt |  j j d d d d  D]W } | j r> | j d k	 r> t | j t j  r> | j	 | j
  | j | j  q> q> W|  j } | j r |  | k r | j | j  n  | S)zO
    Return all models that have a direct relationship to the given model.
    c             S   s(   g  |  ] } t  | t j  r |  q Sr$   )
issubclassr	   Model).0subclassr$   r$   r%   
<listcomp>'   s   	 z'_get_related_models.<locals>.<listcomp>Zinclude_parentsTZinclude_hiddenN)__subclasses__setr    Z
get_fieldsis_relationrelated_modelr   r   r   addr#   appendproxyZconcrete_model)mrelated_modelsZrelated_fields_modelsfoptsr$   r$   r%   _get_related_models"   s    	".	r7   c             C   s   t    } t |   } xe | D]] } | j j | j j } } | | f | k rS q n  | j | | f  | j t |   q W| |  j j |  j j f h S)a  
    Return all models that have a direct or indirect relationship
    to the given model.

    Relationships are either defined by explicit relational fields, like
    ForeignKey, ManyToManyField or OneToOneField, or by inheriting from another
    model (a superclass is related to its subclasses, but not vice versa). Note,
    however, that a model inheriting from a concrete model is also related to
    its superclass through the implicit *_ptr OneToOneField on the subclass.
    )r-   r7   r    r!   r"   r0   extend)r#   seenqueueZrel_modrel_app_labelrel_model_namer$   r$   r%   get_related_models_recursive7   s    	r=   c               @   s   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z e	 d d    Z
 e d d    Z e d d    Z d d   Z d d   Z d S)ProjectStatez
    Represents the entire project's overall state.
    This is the item that is passed around - we do it here rather than at the
    app level so that cross-app FKs/etc. resolve properly.
    Nc             C   s"   | p	 i  |  _  | p g  |  _ d  S)N)r	   	real_apps)selfr	   r?   r$   r$   r%   __init__T   s    zProjectState.__init__c             C   sL   | j  | j } } | |  j | | f <d |  j k rH |  j | |  n  d  S)Nr   )r!   
name_lowerr	   __dict__reload_model)r@   model_stater!   r"   r$   r$   r%   	add_modelY   s    zProjectState.add_modelc             C   sF   |  j  | | f =d |  j k rB |  j j | |  |  j j   n  d  S)Nr   )r	   rC   r   unregister_modelclear_cache)r@   r!   r"   r$   r$   r%   remove_model_   s    zProjectState.remove_modelc             C   sA  d |  j  k r=y |  j j | |  } Wn t k
 rE t   } Yn Xt |  } |  j | | f } t   } xl | j D]a \ } } | j rx | j	 j
 t k r qx n  t | j |  \ }	 }
 | j |	 |
 j   f  qx qx W| j |  xU | D]M \ }	 }
 y |  j j |	 |
  } Wn t k
 r*Yq X| j t |   q W| j | | f  |  j j   / x' | D] \ }	 }
 |  j j |	 |
  qlWWd  QXg  } x< |  j j D]. } | j | j f | k r| j |  qqWxM | D]E \ }	 }
 y |  j |	 |
 f } Wn t k
 rYqX| j |  qW|  j j |  n  d  S)Nr   )rC   r   	get_modelLookupErrorr-   r=   r	   fieldsr.   remote_fieldr#   r   r&   r/   r0   lowerupdatebulk_updaterG   real_modelsr!   rB   r1   KeyErrorrender_multiple)r@   r!   r"   Z	old_modelr4   rE   Zdirect_related_modelsnamefieldr;   r<   Z	rel_modelZstates_to_be_renderedr$   r$   r%   rD   g   sH    		 zProjectState.reload_modelc             C   sV   t  d d d   |  j j   D d |  j  } d |  j k rR |  j j   | _ n  | S)z*Returns an exact copy of this ProjectStater	   c             S   s%   i  |  ] \ } } | j    |  q Sr$   )clone)r)   kvr$   r$   r%   
<dictcomp>   s   	 z&ProjectState.clone.<locals>.<dictcomp>r?   r   )r>   r	   itemsr?   rC   r   rV   )r@   Z	new_stater$   r$   r%   rV      s    zProjectState.clonec             C   s   t  |  j |  j  S)N)	StateAppsr?   r	   )r@   r$   r$   r%   r      s    zProjectState.appsc             C   s%   t  |  j |  j d d |  _ |  j S)Nignore_swappableT)r[   r?   r	   r   )r@   r$   r$   r%   concrete_apps   s    zProjectState.concrete_appsc             C   sR   i  } x? | j  d d  D]+ } t j |  } | | | j | j f <q W|  |  S)z7Takes in an Apps and returns a ProjectState matching itZinclude_swappedT)
get_models
ModelState
from_modelr!   rB   )clsr   Z
app_modelsr#   rE   r$   r$   r%   	from_apps   s
    zProjectState.from_appsc                sv   t  |  j j    t    j j    k r. d St  |  j  t    j  k rP d St   f d d   |  j j   D  S)NFc             3   s(   |  ] \ } } |   j  | k Vq d  S)N)r	   )r)   keyr#   )otherr$   r%   	<genexpr>   s    z&ProjectState.__eq__.<locals>.<genexpr>)r-   r	   keysr?   allrZ   )r@   rd   r$   )rd   r%   __eq__   s
    *zProjectState.__eq__c             C   s   |  | k S)Nr$   )r@   rd   r$   r$   r%   __ne__   s    zProjectState.__ne__)__name__
__module____qualname____doc__rA   rF   rI   rD   rV   r   r   propertyr]   classmethodrb   rh   ri   r$   r$   r$   r%   r>   M   s   =
	r>   c                   s:   e  Z d  Z d Z d Z   f d d   Z d d   Z   S)AppConfigStubzP
    Stubs a Django AppConfig. Only provides a label, and a dict of models.
    r   c                s&   | |  _  t t |   j | d   d  S)N)labelsuperrp   rA   )r@   rq   )	__class__r$   r%   rA      s    	zAppConfigStub.__init__c             C   s   | |  _  d  S)N)r	   )r@   
all_modelsr$   r$   r%   import_models   s    zAppConfigStub.import_models)rj   rk   rl   rm   pathrA   ru   r$   r$   )rs   r%   rp      s   rp   c                   sm   e  Z d  Z d Z d   f d d  Z e d d    Z d d   Z d	 d
   Z d d   Z	 d d   Z
   S)r[   zo
    Subclass of the global Apps registry class to better handle dynamic model
    additions and removals.
    Fc                sC  g  |  _  xS | D]K } t j |  } x3 | j   D]% } |  j  j t j | d d  q2 Wq Wd d   | j   D } d d   t | t	 |   D } t
 t |   j |  |  j t	 | j    |  j   d d l m }	 | r t t j  h n t   }
 |	 |  d	 |
 } | r?t d
 j d d   | D    n  d  S)Nexclude_relsTc             S   s   h  |  ] } | j   q Sr$   )r!   )r)   rE   r$   r$   r%   	<setcomp>   s   	 z%StateApps.__init__.<locals>.<setcomp>c             S   s   g  |  ] } t  |   q Sr$   )rp   )r)   rq   r$   r$   r%   r+      s   	 z&StateApps.__init__.<locals>.<listcomp>r   )_check_lazy_referencesignore
c             s   s   |  ] } | j  Vq d  S)N)msg)r)   errorr$   r$   r%   re      s    z%StateApps.__init__.<locals>.<genexpr>)rQ   global_appsZget_app_configr^   r1   r_   r`   valuessortedlistrr   r[   rA   rS   Zdjango.core.checks.model_checksry   r   r   ZAUTH_USER_MODELr-   
ValueErrorjoin)r@   r?   r	   r\   r!   Zappr#   Z
app_labelsapp_configsry   rz   errors)rs   r$   r%   rA      s    	'# !zStateApps.__init__c             c   s6   |  j  } d |  _  z	 d  VWd  | |  _  |  j   Xd  S)NF)readyrH   )r@   r   r$   r$   r%   rP      s    				zStateApps.bulk_updatec             C   s   | s
 d  S|  j     | } x | r g  } xA | D]9 } y | j |   Wq3 t k
 rk | j |  Yq3 Xq3 Wt |  t |  k r t d | t   f   n  | } q  WWd  QXd  S)NzCannot resolve bases for %r
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
 in an app with no migrations; see https://docs.djangoproject.com/en/%s/topics/migrations/#dependencies for more)rP   renderr   r1   r   r   )r@   Zmodel_statesZunrendered_modelsZnew_unrendered_modelsr#   r$   r$   r%   rS     s     	zStateApps.render_multiplec             C   sI   t  g  i   } t j |  j  | _ t j |  j  | _ |  j | _ | S)zZ
        Return a clone of this registry, mainly used by the migration framework.
        )r[   copydeepcopyrt   r   rQ   )r@   rV   r$   r$   r%   rV   !  s
    zStateApps.clonec             C   s   | |  j  | | j j <| |  j k rO t |  |  j | <t   |  j | _ n  | |  j | j | j j <|  j |  |  j   d  S)N)	rt   r    r"   r   rp   r   r	   Zdo_pending_operationsrH   )r@   r!   r#   r$   r$   r%   register_model,  s    zStateApps.register_modelc             C   s<   y# |  j  | | =|  j | j | =Wn t k
 r7 Yn Xd  S)N)rt   r   r	   rR   )r@   r!   r"   r$   r$   r%   rG   5  s
    zStateApps.unregister_model)rj   rk   rl   rm   rA   r   rP   rS   rV   r   rG   r$   r$   )rs   r%   r[      s   	r[   c               @   s   e  Z d  Z d Z d d d d d  Z e d d    Z e d d d	   Z e d
 d    Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d S)r_   a  
    Represents a Django Model. We don't use the actual Model class
    as it's not designed to have its options changed - instead, we
    mutate this one and then render it into a Model as required.

    Note that while you are allowed to mutate .fields, you are not allowed
    to mutate the Field instances inside there themselves - you must instead
    assign new ones, as these are not detached during a clone.
    Nc             C   s  | |  _  t |  |  _ | |  _ | p* i  |  _ | p? t j f |  _ | pN g  |  _ t	 |  j t
  ru t d   n  x | D] \ } } t | d  r t d |   n  | j r t | j d  r t d |   n  | j r| t | j j d  r| t d |   q| q| Wd  S)NzCModelState.fields cannot be a dict - it must be a list of 2-tuples.r#   z7ModelState.fields cannot be bound to a model - "%s" is.r    z_ModelState.fields cannot refer to a model class - "%s.to" does. Use a string reference instead.zdModelState.fields cannot refer to a model class - "%s.through" does. Use a string reference instead.)r!   r   rT   rL   optionsr	   r(   basesmanagersr   dictr   hasattrr.   r/   Zmany_to_manyrM   Zthrough)r@   r!   rT   rL   r   r   r   rU   r$   r$   r%   rA   H  s(    		zModelState.__init__c             C   s   |  j  j   S)N)rT   rN   )r@   r$   r$   r%   rB   d  s    zModelState.name_lowerFc                sJ  g  } x  j  j D] } t | d d  r7 | r7 q n  t | t  rL q n  t | j d d } y | j | | j   f  Wq t	 k
 r } z$ t	 d |  j  j
 | f   WYd d } ~ Xq Xq W| sbx  j  j D] } t | j d d } y | j | | j   f  Wq t	 k
 rZ} z$ t	 d |  j  j | f   WYd d } ~ Xq Xq Wn  i  } x t D] } | d k rqoqo|  j  j k ro| d	 k r j  j d	 } t t |   | | <q| d
 k r j  j d
 }	 t t |	   | | <q j  j | | | <qoqoW|  j |  } | rfxb d	 d
 d g D] }
 |
 | k rC| |
 =qCqCWn2 | j d  d d    j  j D k r| d =n    f d d     t t      d  f d d   } t d d   | D  } t d d   | D  st j f } n  g  } t   } d } x  j  j D] } t | j  } | | k r^q7n | j rt j |  } | j   n` |  j k s|  j k r7t j   } | j  | _  | j | _ |  j k r| } qn q7| j! |  | j | | f  q7W| d | f g k r%g  } n  |   j  j"  j  j | | | |  S)zH
        Feed me a model, get a ModelState representing it out.
        rM   NZstrings_onlyTz'Couldn't reconstruct field %s on %s: %sz+Couldn't reconstruct m2m field %s on %s: %sr   r!   Zunique_togetherZindex_togetherZorder_with_respect_toc             S   s   h  |  ] } | j   q Sr$   )rT   )r)   rU   r$   r$   r%   rx     s   	 z(ModelState.from_model.<locals>.<setcomp>c                s\   g  } xO |  j  D]D } t | d  rG | j j rG | j   |   q | j |  q W| S)Nr    )	__bases__r   r    Zabstractr8   r1   )r#   r   base)flatten_basesr$   r%   r     s    z,ModelState.from_model.<locals>.flatten_basesrc   c                s     j  j |   S)N)__mro__index)x)r#   r$   r%   <lambda>  s    z'ModelState.from_model.<locals>.<lambda>c             s   s0   |  ]& } t  | d   r$ | j j n | Vq d S)r    N)r   r    Zlabel_lower)r)   r   r$   r$   r%   re     s   z(ModelState.from_model.<locals>.<genexpr>c             s   s3   |  ]) } t  | t j  p* t | t j  Vq d  S)N)r   r   r   r'   r	   r(   )r)   r   r$   r$   r%   re     s    Zobjects)zappsz	app_label)#r    Zlocal_fieldsgetattrr   r
   r   rT   r1   rV   	TypeErrorrq   Zlocal_many_to_manyZobject_namer   Zoriginal_attrsr-   r   force_text_recursivegetZprivate_fieldsr   r   anyr	   r(   r   Zuse_in_migrationsr   Z_set_creation_counterZ_base_managerZ_default_managerManagerr#   r0   r!   )ra   r#   rw   rL   rU   rT   er   utitrc   Zflattened_basesr   r   Zmanager_namesZdefault_manager_shimmanagerZmanager_nameZnew_managerr$   )r   r#   r%   r`   h  s    	$	'(
*						zModelState.from_modelc                s   t  | t j  r t |  St  | t  rB   f d d   | D St  | t  rn t   f d d   | D  St  | t  r t   f d d   | D  St  | t  r   f d d   | j   D S| S)Nc                s   g  |  ] }   j  |   q Sr$   )r   )r)   r   )ra   r$   r%   r+     s   	 z3ModelState.force_text_recursive.<locals>.<listcomp>c             3   s   |  ] }   j  |  Vq d  S)N)r   )r)   r   )ra   r$   r%   re     s    z2ModelState.force_text_recursive.<locals>.<genexpr>c             3   s   |  ] }   j  |  Vq d  S)N)r   )r)   r   )ra   r$   r%   re     s    c                s1   i  |  ]' \ } }   j  |    j  |   q Sr$   )r   )r)   rW   rX   )ra   r$   r%   rY     s   	z3ModelState.force_text_recursive.<locals>.<dictcomp>)	r   r   r   r   r   r   r-   r   rZ   )ra   valuer$   )ra   r%   r     s    
zModelState.force_text_recursivec             c   s   t  |  j d d d   } x | D]y \ } } t |  } | j   \ } } } } } | r{ t |  }	 | |	 j   f Vq" t |  }
 | |
 | |   f Vq" Wd S)z,Deep-clone the managers using deconstructionrc   c             S   s   |  d j  S)Nr   )Zcreation_counter)rX   r$   r$   r%   r     s    z/ModelState.construct_managers.<locals>.<lambda>N)r   r   r   deconstructr   
as_manager)r@   Zsorted_managersZmgr_namer   r   Zmanager_pathZqs_pathargskwargsZqs_classZmanager_classr$   r$   r%   construct_managers  s    zModelState.construct_managersc             C   sR   |  j  d |  j d |  j d t |  j  d t |  j  d |  j d t |  j   S)z(Returns an exact copy of this ModelStater!   rT   rL   r   r   r   )	rs   r!   rT   r   rL   r   r   r   r   )r@   r$   r$   r%   rV     s    				zModelState.clonec                s  i |  j  d 6  d 6} | j |  j  t t d  t   |  } y& t   f d d   |  j D  } Wn( t k
 r t d |  j f   Yn Xd d   |  j	 D } | | d <d	 | d
 <| j |  j
    t j   1 t j d d t  t t |  j  | |  SWd QXd S)zACreates a Model object from our current state into the given appsr!   r   ZMetac             3   s6   |  ], } t  | t j  r*   j |  n | Vq d  S)N)r   r   r   rJ   )r)   r   )r   r$   r%   re     s   z$ModelState.render.<locals>.<genexpr>z(Cannot resolve one or more bases from %rc             S   s%   i  |  ] \ } } | j    |  q Sr$   )rV   )r)   rT   rU   r$   r$   r%   rY     s   	 z%ModelState.render.<locals>.<dictcomp>Z__fake__rk   rz   zDManagers from concrete parents will soon qualify as default managersN)r!   rO   r   typestrr   r   rK   r   rL   r   warningscatch_warningsfilterwarningsr   rT   )r@   r   Zmeta_contentsmetar   bodyr$   )r   r%   r     s*    

zModelState.renderc             C   sG   x' |  j  D] \ } } | | k r
 | Sq
 Wt d | |  j f   d  S)NzNo field called %s on model %s)rL   r   rT   )r@   rT   fnamerU   r$   r$   r%   get_field_by_name2  s    zModelState.get_field_by_namec             C   s   d |  j  |  j f S)Nz<ModelState: '%s.%s'>)r!   rT   )r@   r$   r$   r%   __repr__8  s    zModelState.__repr__c             C   s   |  j  | j  k o |  j | j k o t |  j  t | j  k o t d d   t |  j | j  D  o |  j | j k o |  j | j k o |  j | j k S)Nc             s   sY   |  ]O \ \ } } \ } } | | k oP | j    d  d  | j    d  d  k Vq d S)r   N)r   )r)   Zk1f1Zk2f2r$   r$   r%   re   @  s   z$ModelState.__eq__.<locals>.<genexpr>)	r!   rT   r   rL   rg   zipr   r   r   )r@   rd   r$   r$   r%   rh   ;  s    zModelState.__eq__c             C   s   |  | k S)Nr$   )r@   rd   r$   r$   r%   ri   G  s    zModelState.__ne__)rj   rk   rl   rm   rA   r   rB   ro   r`   r   r   rV   r   r   r   rh   ri   r$   r$   r$   r%   r_   =  s   	}"r_   )2
__future__r   r   r   collectionsr   
contextlibr   Zdjango.appsr   Zdjango.apps.registryr   r   r~   Zdjango.confr   Z	django.dbr	   Zdjango.db.models.fields.proxyr
   Zdjango.db.models.fields.relatedr   Zdjango.db.models.optionsr   r   Zdjango.db.models.utilsr   Zdjango.utilsr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   Zdjango.utils.versionr   
exceptionsr   r&   r7   r=   objectr>   rp   r[   r_   r$   r$   r$   r%   <module>   s4   ~`