ó
r±E^c           @  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 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 m Z m Z d  d l m Z m Z m Z d  d l m  Z  d  d l! m" Z" d  d l# m$ Z% m& Z& d e' f d „  ƒ  YZ( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d e f d „  ƒ  YZ0 d „  Z1 d d „ Z3 d d „ Z4 d „  Z5 d e6 d  „ Z7 d! „  Z8 d" „  Z9 e6 d# „ Z: d$ e' f d% „  ƒ  YZ; d& „  Z< d' „  Z= d( „  Z> d) „  Z? d S(*   iÿÿÿÿ(   t   unicode_literalsN(   t   defaultdict(   t   get_permission_codename(   t   FieldDoesNotExist(   t   models(   t
   LOOKUP_SEP(   t	   Collector(   t   QUERY_TERMS(   t   pretty_name(   t   NoReverseMatcht   reverse(   t   formatst   sixt   timezone(   t	   force_strt
   force_textt
   smart_text(   t   format_html(   t   capfirst(   t   overridet	   ungettextt   FieldIsAForeignKeyColumnNamec           B  s   e  Z d  Z RS(   u/   A field is a foreign key attname, i.e. <FK>_id.(   t   __name__t
   __module__t   __doc__(    (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyR      s   c         C  s°   | j  t ƒ } | d t k r, | d  } n  x} | D]u } | d k rT |  j j } n  |  j | ƒ } t | d ƒ r3 | j ƒ  } | d j }  t	 d „  | Dƒ ƒ r¨ t
 Sq3 q3 Wt S(   uU   
    Returns True if 'distinct()' should be used to query the given lookup path.
    iÿÿÿÿu   pku   get_path_infoc         s  s   |  ] } | j  Vq d  S(   N(   t   m2m(   t   .0t   path(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pys	   <genexpr>.   s    (   t   splitR   R   t   pkt   namet	   get_fieldt   hasattrt   get_path_infot   to_optst   anyt   Truet   False(   t   optst   lookup_patht   lookup_fieldst
   field_namet   fieldt	   path_info(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   lookup_needs_distinct   s    c         C  sX   |  j  d ƒ r! | j d ƒ } n  |  j  d ƒ rT | j ƒ  d k rK t } qT t } n  | S(   uK   
    Returns a lookup value prepared to be used in queryset filtering.
    u   __inu   ,u   __isnullu    u   falseu   0(   u    u   falseu   0(   t   endswithR   t   lowerR%   R$   (   t   keyt   value(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   prepare_lookup_value4   s    		c         C  sy   t  |  t j ƒ s |  St |  ƒ } xG t t | ƒ ƒ D]3 } | | } | d k r5 d t | ƒ | | <q5 q5 Wd j | ƒ S(   u"  
    Ensure that primary key values do not confuse the admin URLs by escaping
    any '/', '_' and ':' and similarly problematic characters.
    Similar to urllib.quote, except that the quoting is slightly different so
    that it doesn't get automatically unquoted by the Web browser.
    u   :/_#?;@&=+$,"[]<>%
\u   _%02Xu    (   t
   isinstanceR   t   string_typest   listt   ranget   lent   ordt   join(   t   st   rest   it   c(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   quoteD   s    
c         C  s¿   t  } t } |  j d ƒ } | d g } | j } | d =xw | D]o } | d d !r  y) | | | | d  d ƒ ƒ | d ƒ Wq® t k
 rœ | d | ƒ q® Xq? | d | ƒ q? Wd j | ƒ S(   uI   
    Undo the effects of quote(). Based heavily on urllib.unquote().
    u   _i    i   i   i   u    (   t   chrt   intR   t   appendt
   ValueErrorR8   (   R9   t   mychrt   myatoiR4   R:   t   myappendt   item(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   unquoteU   s    	)c         C  sM   g  } x@ |  D]8 } t  | t t f ƒ r8 | j | ƒ q | j | ƒ q W| S(   uN   Returns a list which is a single level of flattening of the
    original list.(   R2   R4   t   tuplet   extendR@   (   t   fieldst   flatR*   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   flattenj   s    c         C  s8   g  } x+ |  D]# \ } } | j  t | d ƒ ƒ q W| S(   u@   Returns a list of field names from an admin fieldsets structure.u   fields(   RH   RK   (   t	   fieldsetst   field_namesR   R&   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   flatten_fieldsetsv   s
    c           s”   t  d | ƒ } | j |  ƒ t ƒ  ‰ ‡  ‡ ‡ f d †  } | j | ƒ } g  | j D] } | | ƒ ^ qS }	 d „  | j j ƒ  Dƒ }
 | |
 ˆ |	 f S(   u  
    Find all objects related to ``objs`` that should also be deleted. ``objs``
    must be a homogeneous iterable of objects (e.g. a QuerySet).

    Returns a nested list of strings suitable for display in the
    template with the ``unordered_list`` filter.
    t   usingc           sò   |  j  ˆ  j k } |  j } d t | j ƒ t |  ƒ f } | rê y; t d ˆ  j | j | j	 f d  t |  j ƒ  ƒ f ƒ } Wn t k
 r� | SXd | j t d | ƒ f } ˆ j | ƒ sÎ ˆ j | j ƒ n  t d t | j ƒ | |  ƒ S| Sd  S(   Nu   %s: %su   %s:%s_%s_changeu   %s.%su   deleteu   {}: <a href="{}">{}</a>(   t	   __class__t	   _registryt   _metaR   t   verbose_nameR   R
   R   t	   app_labelt
   model_namet   NoneR=   t   _get_pk_valR	   R   t   has_permt   addR   (   t   objt	   has_adminR&   t   no_edit_linkt	   admin_urlt   p(   t
   admin_sitet   perms_neededt   user(    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   format_callbackŒ   s,    	
	c         S  s+   i  |  ]! \ } } t  | ƒ | j j “ q S(    (   R6   RR   t   verbose_name_plural(   R   t   modelt   objs(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pys
   <dictcomp>¯   s   	 (   t   NestedObjectst   collectt   sett   nestedt	   protectedt
   model_objst   items(   Re   R&   Ra   R_   RO   t	   collectorRb   t	   to_deleteRZ   Rj   t   model_count(    (   R_   R`   Ra   s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   get_deleted_objects€   s    	 "Rf   c           B  sP   e  Z d  „  Z d „  Z d d d „ Z d „  Z d „  Z d d „ Z d „  Z	 RS(   c         O  sA   t  t |  ƒ j | | Ž  i  |  _ t ƒ  |  _ t t ƒ |  _ d  S(   N(   t   superRf   t   __init__t   edgesRh   Rj   R   Rk   (   t   selft   argst   kwargs(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRr   µ   s    	c         C  s    |  j  j | g  ƒ j | ƒ d  S(   N(   Rs   t
   setdefaultR@   (   Rt   t   sourcet   target(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   add_edge»   s    c         K  sä   xŽ | D]† } | rc | j  d ƒ rc | i | j j d 6| j j d 6} |  j t | | ƒ | ƒ n |  j d  | ƒ |  j | j j j	 | ƒ q Wy# t
 t |  ƒ j | d | | �SWn) t j k
 rß } |  j j | j ƒ n Xd  S(   Nu   +u   classu	   app_labelt   source_attr(   R-   RR   RU   RT   Rz   t   getattrRV   Rk   Rd   RY   Rq   Rf   Rg   R   t   ProtectedErrorRj   t   updatet   protected_objects(   Rt   Re   Rx   R{   Rv   RZ   t   related_namet   e(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRg   ¾   s    #c         C  s.   t  t |  ƒ j | | ƒ } | j | j j ƒ S(   N(   Rq   Rf   t   related_objectst   select_relatedR*   R   (   Rt   t   relatedRe   t   qs(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyR‚   Î   s    c         C  sš   | | k r g  S| j  | ƒ g  } x9 |  j j | d ƒ D]" } | j |  j | | | ƒ ƒ q9 W| rw | | ƒ g } n	 | g } | r– | j | ƒ n  | S(   N(    (   RY   Rs   t   getRH   t   _nestedR@   (   Rt   RZ   t   seenRb   t   childrent   childt   ret(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyR‡   Ò   s     	c         C  sO   t  ƒ  } g  } x9 |  j j d d ƒ D]" } | j |  j | | | ƒ ƒ q% W| S(   u4   
        Return the graph as a nested list.
        N(    (   Rh   Rs   R†   RV   RH   R‡   (   Rt   Rb   Rˆ   t   rootst   root(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRi   á   s
    	 c         O  s   t  S(   u�   
        We always want to load the objects into memory so that we can display
        them to the user in confirm page.
        (   R%   (   Rt   Ru   Rv   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   can_fast_deleteë   s    N(
   R   R   Rr   Rz   RV   Rg   R‚   R‡   Ri   RŽ   (    (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRf   ´   s   				
c         C  sx   t  |  t j t j j f ƒ r* |  j } n* t  |  t j j ƒ rN |  j j } n |  } i t	 | j
 ƒ d 6t	 | j ƒ d 6S(   uË   
    Return a `dict` with keys 'verbose_name' and 'verbose_name_plural',
    typically for use with string formatting.

    `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
    u   verbose_nameu   verbose_name_plural(   R2   R   t   Modelt   baset	   ModelBaseRR   t   queryt   QuerySetRd   R   RS   Rc   (   RZ   R&   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   model_format_dictó   s    c         C  ss   t  |  t j j ƒ r< | d k r0 |  j ƒ  } n  |  j }  n  t |  ƒ } | d | d } } t | | | po d ƒ S(   u-  
    Return the appropriate `verbose_name` or `verbose_name_plural` value for
    `obj` depending on the count `n`.

    `obj` may be a `Model` instance, `Model` subclass, or `QuerySet` instance.
    If `obj` is a `QuerySet` instance, `n` is optional and the length of the
    `QuerySet` is used.
    u   verbose_nameu   verbose_name_plurali    N(	   R2   R   R’   R“   RV   t   countRd   R”   R   (   RZ   t   nt   dt   singulart   plural(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   model_ngettext  s    	c         C  sÿ   | j  } y t | |  ƒ } Wn¾ t t f k
 rÜ t |  ƒ rS |  } | | ƒ } n€ | d  k	 r¦ t | |  ƒ r¦ |  d k r¦ |  d k r¦ t | |  ƒ } | | ƒ } n- t | |  ƒ } t | ƒ rÍ | ƒ  } n | } d  } n Xd  } t | |  ƒ } | | | f S(   Nu   __str__u   __unicode__(   RR   t   _get_non_gfk_fieldR   R   t   callableRV   R    R|   (   R   RZ   t   model_adminR&   t   ft   attrR0   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   lookup_field  s*    	
c         C  s�   |  j  | ƒ } | j r@ | j r+ | j s4 | j r@ t ƒ  ‚ n  | j r} | j r} t | d ƒ r} | j | k r} t	 ƒ  ‚ n  | S(   u  
    For historical reasons, the admin app relies on GenericForeignKeys as being
    "not found" by get_field(). This could likely be cleaned up.

    Reverse relations should also be excluded as these aren't attributes of the
    model (rather something like `foo_set`).
    u   attname(
   R   t   is_relationt   many_to_onet   related_modelt   one_to_manyR   t   many_to_manyR    t   attnameR   (   R&   R   R*   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyR›   5  s    	1c         C  s,  d	 } yF t | j |  ƒ } y | j } Wn  t k
 rJ | j j j } n XWnÅt k
 rñ|  d k r† t | j j ƒ } t j	 } q|  d k r­ t
 | j j ƒ } t } qt |  ƒ rÂ |  } n� | d	 k	 rï t | |  ƒ rï t | |  ƒ } nc t | |  ƒ rt | |  ƒ } nB d |  | j j f } | rF| d | j j f 7} n  t | ƒ ‚ t | d ƒ rm| j } qt | t ƒ r¬t | d ƒ r¬t | j d ƒ r¬| j j } qt | ƒ râ| j d k rÐd } qît | j ƒ } qt |  ƒ } n# t k
 rt |  ƒ } |  } n X| r$| | f S| Sd	 S(
   us  
    Returns a sensible label for a field name. The name can be a callable,
    property (but not created with @property decorator) or the name of an
    object's attribute, as well as a genuine fields. If return_attr is
    True, the resolved attribute (which could be a callable) is also returned.
    This will be None if (and only if) the name refers to a field.
    u   __unicode__u   __str__u   Unable to lookup '%s' on %su    or %su   short_descriptionu   fgetu   <lambda>u   --N(   RV   R›   RR   RS   t   AttributeErrorR£   R   R   R   t	   text_typeR   t   bytesRœ   R    R|   t   object_nameRP   R   t   short_descriptionR2   t   propertyt   fgetR   R   (   R   Rd   R�   t   return_attrRŸ   R*   t   labelt   message(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   label_for_fieldJ  sR    			

c         C  s[   d } y t  | j |  ƒ } Wn t t f k
 r5 n Xt | d ƒ rQ | j } n  t | ƒ S(   Nu    u	   help_text(   R›   RR   R   R   R    t	   help_textR   (   R   Rd   R²   R*   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   help_text_for_field…  s    c         C  sR  d d l  m } t | d d  ƒ r; t | j ƒ j |  | ƒ St | t j	 ƒ s_ t | t j
 ƒ ri | |  ƒ S|  d  k ry | St | t j ƒ r¡ t j t j |  ƒ ƒ St | t j t j f ƒ rÉ t j |  ƒ St | t j ƒ rî t j |  | j ƒ St | t j t j f ƒ rt j |  ƒ St | t j ƒ rA|  rAt d |  j |  ƒ St |  | ƒ Sd  S(   Niÿÿÿÿ(   t   _boolean_iconu   flatchoicesu   <a href="{}">{}</a>(   t,   django.contrib.admin.templatetags.admin_listR´   R|   RV   t   dictt   flatchoicesR†   R2   R   t   BooleanFieldt   NullBooleanFieldt   DateTimeFieldR   t   localizeR   t   template_localtimet	   DateFieldt	   TimeFieldt   DecimalFieldt   number_formatt   decimal_placest   IntegerFieldt
   FloatFieldt	   FileFieldR   t   urlt   display_for_value(   R0   R*   t   empty_value_displayR´   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   display_for_field‘  s$    $
c         C  sæ   d d l  m } | r  | |  ƒ S|  d  k r0 | St |  t j ƒ rX t j t j |  ƒ ƒ St |  t j	 t j
 f ƒ r€ t j |  ƒ St |  t j t j t f ƒ r¬ t j |  ƒ St |  t t f ƒ rØ d j d „  |  Dƒ ƒ St |  ƒ Sd  S(   Niÿÿÿÿ(   R´   u   , c         s  s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R   t   v(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pys	   <genexpr>¸  s    (   Rµ   R´   RV   R2   t   datetimeR   R»   R   R¼   t   datet   timeR   t   integer_typest   decimalt   Decimalt   floatRÀ   R4   RG   R8   R   (   R0   RÇ   t   booleanR´   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRÆ   ª  s    
t   NotRelationFieldc           B  s   e  Z RS(    (   R   R   (    (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyRÒ   ½  s   c         C  s-   t  |  d ƒ r# |  j ƒ  d j j St ‚ d  S(   Nu   get_path_infoiÿÿÿÿ(   R    R!   R"   Rd   RÒ   (   R*   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   get_model_from_relationÁ  s    c         C  sð   g  } |  } | j  t ƒ } x¿ | D]· } | j j | ƒ } t | ƒ t | ƒ d k r| y t | ƒ Wq| t k
 rx Pq| Xn  | j r´ | j o• | j	 r´ | j
 ƒ  } | j j } n | j j } | j } | j d | ƒ q" W| t j | ƒ f S(   u«    Create a reversed field path.

    E.g. Given (Order, "user__groups"),
    return (Group, "user__order").

    Final field must be a related model, not a data field.
    i   i    (   R   R   RR   R   R6   RÓ   RÒ   R¡   t   auto_createdt   concretet   related_query_namet   remote_fieldRd   R*   R   R£   t   insertR8   (   Rd   R   t   reversed_patht   parentt   piecest   pieceR*   R€   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   reverse_field_pathÈ  s"    	c         C  sb   | j  t ƒ } g  } xF | D]> } | r; t | d ƒ } n |  } | j | j j | ƒ ƒ q W| S(   u;   Return list of Fields given path relative to model.

    e.g. (ModelX, "user__groups__name") -> [
        <django.db.models.fields.related.ForeignKey object at 0x...>,
        <django.db.models.fields.related.ManyToManyField object at 0x...>,
        <django.db.models.fields.CharField object at 0x...>,
    ]
    iÿÿÿÿ(   R   R   RÓ   R@   RR   R   (   Rd   R   RÛ   RI   RÜ   RÚ   (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   get_fields_from_pathç  s    	c   	      C  sc  g  } | r# | j  i i  d 6ƒ n* |  j rM | j  i i |  j d 6d 6ƒ n  | r_t d ƒ �û xó | D]ë } xE | j D]: } | j  i i t | j j ƒ d 6t | ƒ d 6d 6ƒ qw WxR | j D]G \ } } | j  i i t | j j ƒ d 6t | ƒ d 6| d 6d 6ƒ q¿ WxE | j	 D]: } | j  i i t | j j ƒ d 6t | ƒ d 6d 6ƒ qWqg WWd QXn  | S(   uÊ   
    Construct a JSON structure describing changes from a changed object.
    Translations are deactivated so that strings are stored untranslated.
    Translation happens later on LogEntry access.
    u   addedu   fieldsu   changedu   nameu   objectu   deletedN(
   R@   t   changed_datat   translation_overrideRV   t   new_objectsR   RR   RS   t   changed_objectst   deleted_objects(	   t   formt   formsetsRY   t   change_messaget   formsett   added_objectt   changed_objectt   changed_fieldst   deleted_object(    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   construct_change_messageû  s2    	!			&(@   t
   __future__R    RÊ   RÎ   t   collectionsR   t   django.contrib.authR   t   django.core.exceptionsR   t	   django.dbR   t   django.db.models.constantsR   t   django.db.models.deletionR   t   django.db.models.sql.constantsR   t   django.forms.utilsR   t   django.urlsR	   R
   t   django.utilsR   R   R   t   django.utils.encodingR   R   R   t   django.utils.htmlR   t   django.utils.textR   t   django.utils.translationR   Rà   R   t	   ExceptionR   R,   R1   R=   RF   RK   RN   Rp   Rf   R”   RV   Rš   R    R›   R%   R±   R³   RÈ   RÆ   RÒ   RÓ   RÝ   RÞ   Rì   (    (    (    s<   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/utils.pyt   <module>   sL   						
	4?		;					