
 XB                 @   sI  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$ d d   Z% d d   Z& d d   Z' d d   Z( d d   Z) d d   Z* d d   Z+ Gd d    d  e  Z, d! d"   Z- d d# d$  Z. d d% d&  Z/ d' d(   Z0 d d) d* d+  Z1 d, d-   Z2 d. d/   Z3 d) d0 d1  Z4 Gd2 d3   d3 e5  Z6 d4 d5   Z7 d6 d7   Z8 d8 d9   Z9 d: d;   Z: d S)<    )unicode_literalsN)defaultdict)get_permission_codename)FieldDoesNotExist)models)
LOOKUP_SEP)	Collector)QUERY_TERMS)pretty_name)NoReverseMatchreverse)formatssixtimezone)	force_str
force_text
smart_text)format_html)capfirst)	ungettextc             C   s   | j  d  } | d	 t k r2 | d d
  } n  xe | D]] } |  j |  } t | d  r9 | j   } | d j }  t d d   | D  r d Sq9 q9 Wd S)zU
    Returns True if 'distinct()' should be used to query the given lookup path.
    __   Nget_path_infoc             s   s   |  ] } | j  Vq d  S)N)Zm2m).0path r   H/home/ubuntu/projects/ifolica/build/django/django/contrib/admin/utils.py	<genexpr>%   s    z(lookup_needs_distinct.<locals>.<genexpr>TFr   r   )splitr	   	get_fieldhasattrr   to_optsany)optsZlookup_pathZlookup_fields
field_namefieldZ	path_infor   r   r   lookup_needs_distinct   s    r'   c             C   sX   |  j  d  r! | j d  } n  |  j  d  rT | j   d	 k rK d } qT d } n  | S)
zK
    Returns a lookup value prepared to be used in queryset filtering.
    Z__in,Z__isnull false0FT)r)   zfalser+   )endswithr   lower)keyvaluer   r   r   prepare_lookup_value+   s    		r0   c             C   sy   t  |  t j  s |  St |   } xG t t |   D]3 } | | } | d k r5 d t |  | | <q5 q5 Wd j |  S)a"  
    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.
    z:/_#?;@&=+$,"[]<>%
\z_%02Xr)   )
isinstancer   string_typeslistrangelenordjoin)sresicr   r   r   quote;   s    
r<   c             C   s   t  } t } |  j d  } | d g } | j } | d =x | D] } | d d  r y5 | | | | d d  d   | d d   Wq t k
 r | d |  Yq Xq? | d |  q? Wd j |  S)zI
    Undo the effects of quote(). Based heavily on urllib.unquote().
    _r   r      N   r)   )chrintr   append
ValueErrorr7   )r8   ZmychrZmyatoir3   r9   Zmyappenditemr   r   r   unquoteL   s    	5rE   c             C   sM   g  } x@ |  D]8 } t  | t t f  r8 | j |  q | j |  q W| S)zNReturns a list which is a single level of flattening of the
    original list.)r1   r3   tupleextendrB   )fieldsZflatr&   r   r   r   flattena   s    rI   c             C   s8   g  } x+ |  D]# \ } } | j  t | d   q W| S)z@Returns a list of field names from an admin fieldsets structure.rH   )rG   rI   )Z	fieldsetsfield_namesnamer$   r   r   r   flatten_fieldsetsm   s
    rL   c       	         s   t  d |  } | j |   t        f d d    | j   }  f d d   | j D } d d   | j j   D } | |  | f S)a  
    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.
    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 | SYn Xd | j t d |  f }  j |  s  j | j  n  t d t | j  | |   S| Sd  S)Nz%s: %sz%s:%s_%s_changez%s.%sdeletez{}: <a href="{}">{}</a>)	__class__	_registry_metar   verbose_namer   r   rK   	app_label
model_namer<   Z_get_pk_valr   r   Zhas_permaddr   )objZ	has_adminr$   Zno_edit_linkZ	admin_urlp)
admin_siteperms_neededuserr   r   format_callback   s,    	
		z,get_deleted_objects.<locals>.format_callbackc                s   g  |  ] }   |   q Sr   r   )r   rV   )r[   r   r   
<listcomp>   s   	 z'get_deleted_objects.<locals>.<listcomp>c             S   s+   i  |  ]! \ } } t  |  | j j  q Sr   )r5   rQ   verbose_name_plural)r   modelobjsr   r   r   
<dictcomp>   s   	 z'get_deleted_objects.<locals>.<dictcomp>)NestedObjectscollectsetnested	protected
model_objsitems)	r_   r$   rZ   rX   rM   Z	collectorZ	to_deletere   Zmodel_countr   )rX   r[   rY   rZ   r   get_deleted_objectsw   s    	 rh   c                   s   e  Z d  Z   f d d   Z d d   Z d d   f d d  Z   f d d	   Z d
 d   Z d d d  Z d d   Z	   S)ra   c                sA   t  t |   j | |   i  |  _ t   |  _ t t  |  _ d  S)N)superra   __init__edgesrc   re   r   rf   )selfargskwargs)rO   r   r   rj      s    	zNestedObjects.__init__c             C   s    |  j  j | g   j |  d  S)N)rk   
setdefaultrB   )rl   sourcetargetr   r   r   add_edge   s    zNestedObjects.add_edgeNc                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 } z |  j j | j  WYd  d  } ~ Xn Xd  S)N+classrS   source_attr)r,   rQ   rT   rS   rr   getattrrf   r^   rU   ri   ra   rb   r   ZProtectedErrorre   updateZprotected_objects)rl   r_   rp   ru   rn   rV   related_namee)rO   r   r   rb      s    #zNestedObjects.collectc                s.   t  t |   j | |  } | j | j j  S)N)ri   ra   related_objectsZselect_relatedr&   rK   )rl   relatedr_   qs)rO   r   r   rz      s    zNestedObjects.related_objectsc             C   s   | | k r g  S| j  |  g  } x9 |  j j | f   D]" } | j |  j | | |   q9 W| rw | |  g } n	 | g } | r | j |  n  | S)N)rU   rk   getrG   _nestedrB   )rl   rV   seenr[   childrenchildretr   r   r   r~      s     	zNestedObjects._nestedc             C   sO   t    } g  } x9 |  j j d f   D]" } | j |  j | | |   q% W| S)z4
        Return the graph as a nested list.
        N)rc   rk   r}   rG   r~   )rl   r[   r   rootsrootr   r   r   rd      s
    	 zNestedObjects.nestedc             O   s   d S)z
        We always want to load the objects into memory so that we can display
        them to the user in confirm page.
        Fr   )rl   rm   rn   r   r   r   can_fast_delete   s    zNestedObjects.can_fast_delete)
__name__
__module____qualname__rj   rr   rb   rz   r~   rd   r   r   r   )rO   r   ra      s   
ra   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)z
    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.
    rR   r]   )r1   r   ZModelbaseZ	ModelBaserQ   queryQuerySetr^   r   rR   r]   )rV   r$   r   r   r   model_format_dict   s    r   c             C   ss   t  |  t j j  r< | d k r0 |  j   } n  |  j }  n  t |   } | d | d } } t | | | po d  S)a-  
    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.
    NrR   r]   r   )r1   r   r   r   countr^   r   r   )rV   ndZsingularpluralr   r   r   model_ngettext   s    	r   c             C   s   | j  } y t | |   } Wn t k
 r t |   rM |  } | |  } n | d  k	 r t | |   r |  d k r |  d k r t | |   } | |  } n- t | |   } t |  r |   } n | } d  } Yn Xd  } t | |   } | | | f S)N__str____unicode__)rQ   _get_non_gfk_fieldr   callabler!   rv   )rK   rV   model_adminr$   fattrr/   r   r   r   lookup_field  s*    	r   c             C   sD   |  j  |  } | j r@ | j r+ | j s4 | j r@ t    n  | S)a  
    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`).
    )r    is_relationZmany_to_onerelated_modelZone_to_manyr   )r$   rK   r&   r   r   r   r   ,  s
    	r   Fc             C   s  d } yG t  | j |   } y | j } Wn! t k
 rK | j j j } Yn XWnt k
 r|  d k r t | j j  } t j } nh|  d k r t	 | j j  } t
 } nAt |   r |  } n | d k	 r t | |   r t | |   } nc t | |   rt | |   } nB d |  | j j f } | rG| d | j j f 7} n  t |   t | d  rn| j } n t | t  rt | d  rt | j d  r| j j } nB t |  r| j d k rd	 } qt | j  } n t |   } Yn X| r| | f S| Sd S)
as  
    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.
    Nr   r   zUnable to lookup '%s' on %sz or %sshort_descriptionfgetz<lambda>z--)r   rQ   rR   AttributeErrorr   r   r   r   	text_typer   bytesr   r!   rv   Zobject_namerO   r   r   r1   propertyr   r
   )rK   r^   r   Zreturn_attrr   r&   labelmessager   r   r   label_for_field<  sL    			
r   c             C   sV   d } y t  | j |   } Wn t k
 r0 Yn Xt | d  rL | j } n  t |  S)Nr)   	help_text)r   rQ   r   r!   r   r   )rK   r^   r   r&   r   r   r   help_text_for_fields  s    r   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)Nr   )_boolean_iconflatchoicesz<a href="{}">{}</a>),django.contrib.admin.templatetags.admin_listr   rv   dictr   r}   r1   r   ZBooleanFieldZNullBooleanFieldZDateTimeFieldr   localizer   template_localtimeZ	DateFieldZ	TimeFieldZDecimalFieldnumber_formatZdecimal_placesZIntegerFieldZ
FloatFieldZ	FileFieldr   urldisplay_for_value)r/   r&   empty_value_displayr   r   r   r   display_for_field  s$    $
r   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   |  D  St |   Sd  S)Nr   )r   z, c             s   s   |  ] } t  |  Vq d  S)N)r   )r   vr   r   r   r     s    z$display_for_value.<locals>.<genexpr>)r   r   r1   datetimer   r   r   r   datetimer   integer_typesdecimalZDecimalfloatr   r3   rF   r7   r   )r/   r   booleanr   r   r   r   r     s    
r   c               @   s   e  Z d  Z d S)NotRelationFieldN)r   r   r   r   r   r   r   r     s   r   c             C   s-   t  |  d  r# |  j   d j j St  d  S)Nr   r   r   )r!   r   r"   r^   r   )r&   r   r   r   get_model_from_relation  s    r   c             C   s   g  } |  } | j  t  } x | D] } | j j |  } t |  t |  d k r} y t |  Wq} t k
 ry PYq} Xn  | j r | j o | j	 r | j
   } | j j } n | j j } | j } | j d |  q" W| t j |  f S)z 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.
    r   r   )r   r   rQ   r    r5   r   r   r   Zauto_createdZconcreteZrelated_query_nameZremote_fieldr^   r&   rK   r   insertr7   )r^   r   Zreversed_pathparentpiecespiecer&   rx   r   r   r   reverse_field_path  s"    		r   c             C   sb   | j  t  } g  } xF | D]> } | r; t | d  } n |  } | j | j j |   q W| S)a;   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...>,
    ]
    r   r   )r   r   r   rB   rQ   r    )r^   r   r   rH   r   r   r   r   r   get_fields_from_path  s    	r   c             C   s;   y t  |  d  Wn" t k
 r6 |  d d  }  Yn X|  S)z0 Discard trailing non-relation field if extant. r   Nr   r   )r   r   )rH   r   r   r   remove_trailing_data_field  s
    r   );
__future__r   r   r   collectionsr   Zdjango.contrib.authr   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.models.constantsr   Zdjango.db.models.deletionr   Zdjango.db.models.sql.constantsr	   Zdjango.forms.utilsr
   Zdjango.urlsr   r   Zdjango.utilsr   r   r   Zdjango.utils.encodingr   r   r   Zdjango.utils.htmlr   Zdjango.utils.textr   Zdjango.utils.translationr   r'   r0   r<   rE   rI   rL   rh   ra   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r   r   r   r   r   r   <module>   sJ   
4?7