
TQc           @  s%  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	 d  d l
 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 m Z d  d l m Z m Z m Z m Z d   Z d! Z  e d e! f d      Y Z" d S("   i(   t   unicode_literalsN(   t   bisect(   t   settings(   t   RelatedObject(   t   ManyToManyRel(   t	   AutoFieldt   FieldDoesNotExist(   t   OrderWrt(   t
   get_modelst   app_cache_ready(   t   six(   t
   SortedDict(   t
   force_textt
   smart_textt   python_2_unicode_compatible(   t   activatet   deactivate_allt   get_languaget   string_concatc         C  s   t  j d d |   j   j   S(   Nu&   (((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))u    \1(   t   ret   subt   lowert   strip(   t
   class_name(    (    s1   ../Django//lib/python/django/db/models/options.pyt   <lambda>   s    u   verbose_nameu   verbose_name_pluralu   db_tableu   orderingu   unique_togetheru   permissionsu   get_latest_byu   order_with_respect_tou	   app_labelu   db_tablespaceu   abstractu   managedu   proxyu	   swappableu   auto_createdu   index_togethert   Optionsc           B  s  e  Z d# d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z e e  Z d   Z e e  Z d   Z e e  Z d   Z d   Z d   Z e e  Z d   Z d   Z e d  Z d   Z d   Z d   Z d   Z d   Z d   Z  e! e! e! d  Z" e! e! e! d  Z# d   Z$ e! d  Z% d   Z& d   Z' d   Z( d    Z) d!   Z* d"   Z+ RS($   c         C  s6  g  g  |  _  |  _ g  |  _ d \ |  _ |  _ d  |  _ d |  _ g  |  _ g  |  _	 g  |  _
 g  |  _ d  | |  _ |  _ d  |  _ d  |  _ t j |  _ d  |  _ | |  _ d  |  _ t d  |  _ |  _ t |  _ t |  _ t |  _ d  |  _ d  |  _ d  |  _ t    |  _! i  |  _" t |  _# g  |  _$ g  |  _% g  |  _& d  S(   Nu    (   NN('   t   local_fieldst   local_many_to_manyt   virtual_fieldst   Nonet   module_namet   verbose_namet   verbose_name_pluralt   db_tablet   orderingt   unique_togethert   index_togethert   permissionst   object_namet	   app_labelt   get_latest_byt   order_with_respect_toR   t   DEFAULT_TABLESPACEt   db_tablespacet   admint   metat   pkt   Falset   has_auto_fieldt
   auto_fieldt   abstractt   Truet   managedt   proxyt   proxy_for_modelt   concrete_modelt	   swappableR   t   parentst   duplicate_targetst   auto_createdt   abstract_managerst   concrete_managerst   related_fkey_lookups(   t   selfR-   R'   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   __init__   s:    																						c         C  s6  d d l  m } d d l m } |  | _ t j d d | j  t j	 k |  _
 | j |  _ |  j j   |  _ t |  j  |  _ |  j r|  j j j   } x- |  j j D] } | j d  r | | =q q Wxg t D]_ } | | k r t |  | | j |   q t |  j |  r t |  | t |  j |   q q W| j d |  j  } | rst | d t t f  rs| f } n  | |  _ |  j d  k rt  |  j d	  |  _ n  | i  k rt! d
 d j" | j#      qn t  |  j d	  |  _ |  ` |  j$ s2d |  j% |  j f |  _$ | |  j$ | j& j'    |  _$ n  d  S(   Ni(   t
   connection(   t   truncate_nameu	   \.models$u    u   _u   unique_togetheri    u   su)   'class Meta' got invalid attribute(s): %su   ,u   %s_%s((   t	   django.dbRA   t   django.db.backends.utilRB   t   _metaR   R   t
   __module__R   t   INSTALLED_APPSt	   installedt   __name__R&   R   R   t   get_verbose_nameR   R-   t   __dict__t   copyt
   startswitht   DEFAULT_NAMESt   setattrt   popt   hasattrt   getattrR#   t
   isinstancet   tuplet   listR    R   R   t	   TypeErrort   joint   keysR!   R'   t   opst   max_name_length(   R?   t   clst   nameRA   RB   t
   meta_attrst	   attr_namet   ut(    (    s1   ../Django//lib/python/django/db/models/options.pyt   contribute_to_classJ   s<    	$	# 	%	c   
      C  s  |  j  r= |  j |  j   |  _  d	 |  _ | j d t    n	 d  |  _  |  j d  k r |  j r t t	 j
 |  j   } g  |  j D] } | j | j k r | ^ q } | r | d } n  t | _ |  j |  q t d d d t d t  } | j d |  n  i  } x\ t	 j |  j  D]H \ } } y | | j |  Wqt k
 r^t | g  | | <qXqWi  |  _ x_ t	 j
 |  D]N }	 t |	  d k rq|n  x- |	 D]% } |	 j t | g   |  j | <qWq|Wd  S(
   Nu   _orderi    R   u   IDt   primary_keyR;   u   idi   (   u   _order(   R)   t	   get_fieldR"   t   add_to_classR   R   R.   R9   t   nextR
   t
   itervaluesR   R\   R3   Ra   t   setup_pkR   t	   iteritemsR:   t   addt   KeyErrort   sett   lent
   difference(
   R?   t   modelt   fieldt   fldt   already_createdt   autot   collectionst   columnt   targett   elt(    (    s1   ../Django//lib/python/django/db/models/options.pyt   _prepare}   s8    				.			c         C  s   | j  rU t | j  t  rU |  j j t |  j |  |  t |  d  r |  ` q nJ |  j j t |  j |  |  |  j	 |  t |  d  r |  `
 |  ` n  t |  d  r |  ` n  d  S(   Nu
   _m2m_cacheu   _field_cacheu	   _name_map(   t   relRS   R   R   t   insertR   RQ   t
   _m2m_cacheR   Rf   t   _field_cachet   _field_name_cachet	   _name_map(   R?   Rn   (    (    s1   ../Django//lib/python/django/db/models/options.pyt	   add_field   s    	c         C  s   |  j  j |  d  S(   N(   R   t   append(   R?   Rn   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   add_virtual_field   s    c         C  s,   |  j  r( | j r( | |  _  t | _ n  d  S(   N(   R.   Ra   R/   t	   serialize(   R?   Rn   (    (    s1   ../Django//lib/python/django/db/models/options.pyRf      s    	c         C  s   |  j  j |  j  S(   uU   
        Returns the index of the primary key field in the self.fields list.
        (   t   fieldst   indexR.   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   pk_index   s    c         C  s+   | j  j |  _ | |  _ | j  j |  _ d S(   ud   
        Does the internal setup so that the current model is a proxy for
        "target".
        N(   RE   R.   R6   R!   (   R?   Rt   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   setup_proxy   s    	c         C  s   d |  j  S(   Nu   <Options for %s>(   R&   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   __repr__   s    c         C  s    d t  |  j  t  |  j  f S(   Nu   %s.%s(   R   R'   R   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   __str__   s    c         C  s-   t    } t   t |  j  } t |  | S(   u   
        There are a few places where the untranslated verbose name is needed
        (so that we get the same value regardless of currently active
        locale).
        (   R   R   R   R   R   (   R?   t   langt   raw(    (    s1   ../Django//lib/python/django/db/models/options.pyt   verbose_name_raw   s
    	
c         C  s   |  j  r d |  j |  j j   f } t t |  j  d  } | r y | j d  \ } } Wn t k
 rm | SXd | | j   f d | f k r | Sq n  d S(   u!  
        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.
        u   %s.%su   .N(	   R8   R'   R&   R   RR   R   R   t   splitt
   ValueError(   R?   t   model_labelt   swapped_fort   swapped_labelt   swapped_object(    (    s1   ../Django//lib/python/django/db/models/options.pyt   _swapped   s    	"
c         C  s0   y |  j  Wn t k
 r( |  j   n X|  j  S(   u  
        The getter for self.fields. This returns the list of field objects
        available to this model (including through parent models).

        Callers are not permitted to modify this list, since it's a reference
        to this instance (not a copy).
        (   R{   t   AttributeErrort   _fill_fields_cache(   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   _fields   s
    c         C  s0   y |  j  Wn t k
 r( |  j   n X|  j  S(   u   
        Returns a sequence of (field, model) pairs for all fields. The "model"
        element is None for fields on the current model. Mostly of use when
        constructing queries so that we know which model a field belongs to.
        (   Rz   R   R   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_fields_with_model  s
    c         C  s   g  } x` |  j  D]U } xL | j j   D]; \ } } | rN | j | | f  q& | j | | f  q& Wq W| j g  |  j D] } | d  f ^ qy  t |  |  _ g  | D] \ } } | ^ q |  _	 d  S(   N(
   R9   RE   R   R~   t   extendR   R   RT   Rz   R{   (   R?   t   cachet   parentRn   Rm   t   ft   xt   _(    (    s1   ../Django//lib/python/django/db/models/options.pyR     s    )c         C  s6   y |  j  Wn t k
 r( |  j   n Xt |  j   S(   N(   Ry   R   t   _fill_m2m_cacheRU   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   _many_to_many'  s
    c         C  s?   y |  j  Wn t k
 r( |  j   n Xt t j |  j    S(   uF   
        The many-to-many version of get_fields_with_model().
        (   Ry   R   R   RU   R
   Rg   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_m2m_with_model/  s
    c         C  s   t    } xN |  j D]C } x: | j j   D]) \ } } | rH | | | <q) | | | <q) Wq Wx |  j D] } d  | | <qd W| |  _ d  S(   N(   R   R9   RE   R   R   R   Ry   (   R?   R   R   Rn   Rm   (    (    s1   ../Django//lib/python/django/db/models/options.pyR   9  s    	c         C  s`   | r |  j  |  j p |  j  } x! | D] } | j | k r& | Sq& Wt d |  j | f   d S(   uY   
        Returns the requested field by name. Raises FieldDoesNotExist on error.
        u   %s has no field named %rN(   R   t   many_to_manyR\   R   R&   (   R?   R\   R   t	   to_searchR   (    (    s1   ../Django//lib/python/django/db/models/options.pyRb   E  s
    c         C  si   y8 y |  j  | SWn" t k
 r6 |  j   } | | SXWn* t k
 rd t d |  j | f   n Xd S(   u6  
        Returns the (field_object, model, direct, m2m), where field_object is
        the Field instance for the given name, model is the model containing
        this field (None for local fields), direct is True if the field exists
        on this model, and m2m is True for many-to-many relations. When
        'direct' is False, 'field_object' is the corresponding RelatedObject
        for this field (since the field doesn't have an instance associated
        with it).

        Uses a cache internally, so after the first access, this is very fast.
        u   %s has no field named %rN(   R|   R   t   init_name_mapRi   R   R&   (   R?   R\   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_field_by_nameO  s    c         C  se   y |  j  } Wn t k
 r, |  j   } n Xt | j    } g  | D] } | j d  sF | ^ qF S(   u
  
        Returns a list of all field names that are possible for this model
        (including reverse relation names). This is used for pretty printing
        debugging output (a list of choices), so any internal-only field names
        are not included.
        u   +(   R|   R   R   t   sortedRX   t   endswith(   R?   R   t   namest   val(    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_all_field_namese  s    c         C  s  i  } x9 |  j    D]+ \ } } | | t t f | | j j   <q Wx9 |  j   D]+ \ } } | | t t f | | j j   <qO Wx3 |  j   D]% \ } } | | t t f | | j <q Wx3 |  j   D]% \ } } | | t t f | | j <q Wt	   r | |  _
 n  | S(   uE   
        Initialises the field name -> field object mapping.
        (   t&   get_all_related_m2m_objects_with_modelR/   R3   Rn   t   related_query_namet"   get_all_related_objects_with_modelR   R\   R   R	   R|   (   R?   R   R   Rm   (    (    s1   ../Django//lib/python/django/db/models/options.pyR   u  s    ##	c         C  s   d |  j  j   S(   Nu   add_%s(   R&   R   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_add_permission  s    c         C  s   d |  j  j   S(   Nu	   change_%s(   R&   R   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_change_permission  s    c         C  s   d |  j  j   S(   Nu	   delete_%s(   R&   R   (   R?   (    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_delete_permission  s    c         C  s5   g  |  j  d | d | d |  D] \ } } | ^ q S(   Nt
   local_onlyt   include_hiddent   include_proxy_eq(   R   (   R?   R   R   R   t   kt   v(    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_all_related_objects  s    c           s   y |  j  Wn t k
 r( |  j   n Xg  } | rH | j d    n  | sa | j d    n  | rp |  j n |  j  } g  | j   D](   t   f d   | D  r   ^ q S(   un   
        Returns a list of (related-object, model) pairs. Similar to
        get_fields_with_model().
        c         S  s   | S(   N(    (   R   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s    c         S  s   |  j  j j   S(   N(   Rn   Rw   t	   is_hidden(   R   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s    c         3  s   |  ] } |     Vq d  S(   N(    (   t   .0t   p(   t   t(    s1   ../Django//lib/python/django/db/models/options.pys	   <genexpr>  s    (   t   _related_objects_cacheR   t   _fill_related_objects_cacheR~   t   _related_objects_proxy_cachet   itemst   all(   R?   R   R   R   t
   predicatesR   (    (   R   s1   ../Django//lib/python/django/db/models/options.pyR     s    	c   	   	   C  s  t    } |  j   } x |  j D] } xv | j j d t  D]_ \ } } | j j d k  sh | j j j	 r} | j
 | k r} q; n  | s | | | <q; | | | <q; Wq W| j   } x t d t d t  D] } | j j s x | j j D] } | j r t | j j t j  r |  | j j j k r[d  | t | j j | |  <d  | t | j j | |  <q|  j | j j j j k rd  | t | j j | |  <qq q Wq q W| |  _ | |  _ d  S(   NR   i    t   include_auto_createdt   only_installed(   R   t   get_parent_listR9   RE   R   R3   Rn   t   creation_counterRw   t   parent_linkRm   RL   R   R/   t   swappedR   RS   t   toR
   t   string_typesR   R   R7   R   R   (	   R?   R   t   parent_listR   t   objRm   t   proxy_cachet   klassR   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s*    	"0"-	c         C  sf   y |  j  } Wn t k
 r, |  j   } n X| r\ g  | j   D] \ } } | s@ | ^ q@ St |  S(   N(   t   _related_many_to_many_cacheR   t    _fill_related_many_to_many_cacheR   RU   (   R?   R   R   R   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyt$   get_all_related_many_to_many_objects  s    )c         C  s@   y |  j  } Wn t k
 r, |  j   } n Xt t j |   S(   ur   
        Returns a list of (related-m2m-object, model) pairs. Similar to
        get_fields_with_model().
        (   R   R   R   RU   R
   Rg   (   R?   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s
    c      
   C  s<  t    } |  j   } xu |  j D]j } xa | j j   D]P \ } } | j j d k  rh | j | k rh q5 n  | s{ | | | <q5 | | | <q5 Wq Wx t d t	  D] } | j j
 s xm | j j D]\ } | j r t | j j t j  r |  | j j j k r d  | t | j j | |  <q q Wq q Wt   r8| |  _ n  | S(   Ni    R   (   R   R   R9   RE   R   Rn   R   Rm   R   R/   R   R   Rw   RS   R   R
   R   R   R   R	   R   (   R?   R   R   R   R   Rm   R   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s&    	!	*	c         C  s}   |  j  s d S| |  j  k r# | g Sx= |  j  D]2 } | j j |  } | r- | j d |  | Sq- Wt d | j j   d S(   u   
        Returns a list of parent classes leading to 'model' (order from closet
        to most distant ancestor). This has to handle the case were 'model' is
        a granparent or even more distant relation.
        Ni    u#   %r is not an ancestor of this model(   R9   RE   t   get_base_chainRx   RV   R   (   R?   Rm   R   t   res(    (    s1   ../Django//lib/python/django/db/models/options.pyR     s    	c         C  sD   t    } x4 |  j D]) } | j |  | j | j j    q W| S(   u   
        Returns a list of all the ancestor of this model as a list. Useful for
        determining if something is an ancestor, regardless of lineage.
        (   Rj   R9   Rh   t   updateRE   R   (   R?   t   resultR   (    (    s1   ../Django//lib/python/django/db/models/options.pyR      s
    	c         C  s[   | |  j  k r |  j  | Sx: |  j  D]/ } | j j |  } | r$ |  j  | pR | Sq$ Wd S(   u^  
        Returns 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.

        Returns None if the model isn't an ancestor of this one.
        N(   R9   RE   t   get_ancestor_link(   R?   t   ancestorR   R   (    (    s1   ../Django//lib/python/django/db/models/options.pyR     s    	c         C  s(   t  |  d  s! g  } | |  _ n  |  j S(   uO   Returns a list of Options objects that are ordered with respect to this object.u   _ordered_objects(   RQ   t   _ordered_objects(   R?   t   objects(    (    s1   ../Django//lib/python/django/db/models/options.pyt   get_ordered_objects  s    N(,   RI   RF   R   R@   R`   Rv   R}   R   Rf   R   R   R   R   R   t   propertyR   R   R   R   R   R   R   R   R   R   R3   Rb   R   R   R   R   R   R   R/   R   R   R   R   R   R   R   R   R   R   (    (    (    s1   ../Django//lib/python/django/db/models/options.pyR      sT   -	3	,															
	
													(   u   verbose_nameu   verbose_name_pluralu   db_tableu   orderingu   unique_togetheru   permissionsu   get_latest_byu   order_with_respect_tou	   app_labelu   db_tablespaceu   abstractu   managedu   proxyu	   swappableu   auto_createdu   index_together(#   t
   __future__R    R   R   t   django.confR   t   django.db.models.relatedR   t   django.db.models.fields.relatedR   t   django.db.models.fieldsR   R   t   django.db.models.fields.proxyR   t   django.db.models.loadingR   R	   t   django.utilsR
   t   django.utils.datastructuresR   t   django.utils.encodingR   R   R   t   django.utils.translationR   R   R   R   RJ   RN   t   objectR   (    (    (    s1   ../Django//lib/python/django/db/models/options.pyt   <module>   s(   "	    