ó
r±E^c           @   sb  d  d l  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 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 l m Z d  d l m Z d  d l m  Z  d  d l! m" Z" d Z# d Z$ d Z% d Z& d Z' d Z( e# e$ e% e' e	 e
 f Z) d e* f d „  ƒ  YZ+ d S(   iÿÿÿÿN(   t   OrderedDict(   t   FieldListFilter(   t   DisallowedModelAdminLookupt   DisallowedModelAdminToField(   t   IS_POPUP_VARt   TO_FIELD_VARt   IncorrectLookupParameters(   t   get_fields_from_patht   lookup_needs_distinctt   prepare_lookup_valuet   quote(   t   FieldDoesNotExistt   ImproperlyConfiguredt   SuspiciousOperation(   t   InvalidPage(   t   models(   t   reverse(   t   six(   t
   force_text(   t	   urlencode(   t   ugettextt   allt   ot   ott   pt   qt   et
   ChangeListc           B   s†   e  Z d  „  Z d d „ Z d „  Z d d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   c         C   s8  | |  _  | j |  _ |  j |  _ | j | ƒ |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ |	 |  _ |
 |  _ | |  _ | j | ƒ |  _ y" t | j j t d ƒ ƒ |  _ Wn t k
 rÔ d |  _ n Xt | j k |  _ t | j k |  _ | j j t ƒ } | r7| j | | ƒ r7t d | ƒ ‚ n  | |  _ t | j j  ƒ  ƒ |  _! t |  j! k rt|  j! t =n  t" |  j! k r�|  j! t" =n  |  j r¥d |  _# n	 | |  _# | j j t$ d ƒ |  _% |  j | ƒ |  _& |  j' | ƒ |  j rýt( d ƒ } n t( d ƒ } | t) |  j j* ƒ |  _+ |  j j, j- |  _. d  S(   Ni    s"   The field %s cannot be referenced.t    s	   Select %ss   Select %s to change(    (/   t   modelt   _metat   optst   lookup_optst   get_querysett   root_querysett   list_displayt   list_display_linkst   list_filtert   date_hierarchyt   search_fieldst   list_select_relatedt   list_per_paget   list_max_show_allt   model_admint   get_preserved_filterst   preserved_filterst   intt   GETt   gett   PAGE_VARt   page_numt
   ValueErrort   ALL_VARt   show_allR   t   is_popupR   t   to_field_allowedR   t   to_fieldt   dictt   itemst   paramst
   ERROR_FLAGt   list_editablet
   SEARCH_VARt   queryt   querysett   get_resultsR   R   t   verbose_namet   titlet   pkt   attnamet
   pk_attname(   t   selft   requestR   R#   R$   R%   R&   R'   R(   R)   R*   R=   R+   R8   RC   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   __init__&   sP    										"				c         C   sI   | s |  j  } n  | j ƒ  } x$ t D] } | | k r% | | =q% q% W| S(   s:   
        Returns all params except IGNORED_PARAMS
        (   R;   t   copyt   IGNORED_PARAMS(   RG   R;   t   lookup_paramst   ignored(    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   get_filters_paramsW   s    c      	   C   s0  |  j  ƒ  } t } xB | j ƒ  D]4 \ } } |  j j | | ƒ s t d | ƒ ‚ q q Wg  } |  j r‘x(|  j D]} t | ƒ r  | | | |  j |  j ƒ } nÈ d  }	 t
 | t t f ƒ rÊ | \ }
 } n | t j }
 } t
 |
 t j ƒ s|
 }	 t |  j |	 ƒ d }
 n  t | ƒ } | |
 | | |  j |  j d |	 ƒ} | t | ƒ k rh| pbt |  j |	 ƒ } n  | rp | j ƒ  rp | j | ƒ qp qp Wn  yb xE | j ƒ  D]7 \ } } t | | ƒ | | <| pÕt |  j | ƒ } q¡W| t | ƒ | | f SWn6 t k
 r+} t j t t | ƒ t j ƒ  d ƒ n Xd  S(   Ns   Filtering by %s not allowediÿÿÿÿt
   field_pathi   (   RN   t   FalseR:   R+   t   lookup_allowedR   R%   t   callableR   t   Nonet
   isinstancet   tuplet   listR   t   createR   t   FieldR   t   lenR   R    t
   has_outputt   appendR	   t   boolR   R   t   reraiseR   t   syst   exc_info(   RG   RH   RL   t   use_distinctt   keyt   valuet   filter_specsR%   t   specRO   t   fieldt   field_list_filter_classt   lookup_params_countR   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   get_filterse   s@    		c         C   sà   | d  k r i  } n  | d  k r* g  } n  |  j j ƒ  } x> | D]6 } x- t | ƒ D] } | j | ƒ rS | | =qS qS Wq@ WxI | j ƒ  D]; \ } } | d  k r¸ | | k rÂ | | =qÂ q‡ | | | <q‡ Wd t t | j ƒ  ƒ ƒ S(   Ns   ?%s(   RS   R;   RJ   RV   t
   startswithR:   R   t   sorted(   RG   t
   new_paramst   removeR   t   rt   kt   v(    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   get_query_stringœ   s    		c         C   s*  |  j  j | |  j |  j ƒ } | j } |  j  j rE |  j j ƒ  } n d  } | |  j k } | |  j k } |  j	 rx | s | r‘ |  j j
 ƒ  } n7 y | j |  j d ƒ j } Wn t k
 rÇ t ‚ n X| |  _ |  j  j |  _ |  j pó t | ƒ |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   Ni   (   R+   t   get_paginatorR@   R)   t   countt   show_full_result_countR"   RS   R*   R5   t   _clonet   pageR2   t   object_listR   R   t   result_countR\   t   show_admin_actionst   full_result_countt   result_listt   can_show_allt
   multi_paget	   paginator(   RG   RH   R}   Rw   Ry   R{   R|   Rz   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyRA   ®   s*    	
					c         C   s@   g  } |  j  j r! |  j  j } n |  j j r< |  j j } n  | S(   N(   R+   t   orderingR    (   RG   R~   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   _get_default_orderingÏ   s    c         C   s�   y |  j  j | ƒ } | j SWnl t k
 r‹ t | ƒ rB | } n9 t |  j | ƒ ri t |  j | ƒ } n t |  j | ƒ } t | d d ƒ SXd S(   sk  
        Returns the proper model field name corresponding to the given
        field_name to use for ordering. field_name may either be the name of a
        proper model field or the name of a method (on the admin or model) or a
        callable with the 'admin_order_field' attribute. Returns None if no
        proper model field name can be matched.
        t   admin_order_fieldN(
   R    t	   get_fieldt   nameR   RR   t   hasattrR+   t   getattrR   RS   (   RG   t
   field_nameRe   t   attr(    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   get_ordering_field×   s    	c         C   sc  |  j  } t |  j j | ƒ p' |  j ƒ  ƒ } t | k rg  } | t j d ƒ } x¸ | D]­ } yŠ | j d ƒ \ } } }	 |  j t	 |	 ƒ }
 |  j
 |
 ƒ } | s¨ wY n  | j d ƒ r× | d k r× | j | d ƒ n | j | | ƒ WqY t t f k
 rqY qY XqY Wn  | j | j j ƒ |  j j j } t | ƒ d d | d | h @s_| j d ƒ n  | S(   s•  
        Returns the list of ordering fields for the change list.
        First we check the get_ordering() method in model admin, then we check
        the object's default ordering. Then, any manually-specified ordering
        from the query string overrides anything. Finally, a deterministic
        order is guaranteed by ensuring the primary key is used as the last
        ordering field.
        t   .t   -i   RD   s   -pk(   R;   RV   R+   t   get_orderingR   t	   ORDER_VARt   splitt
   rpartitionR#   R.   R‡   Ri   R[   t
   IndexErrorR3   t   extendR?   t   order_byR    RD   R‚   t   set(   RG   RH   R@   R;   R~   t   order_paramsR   t   nonet   pfxt   idxR…   t   order_fieldt   pk_name(    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyRŠ   í   s,    		$ c         C   s#  |  j  ƒ  } t ƒ  } t |  j k r£ xø | D]q } | j d ƒ rS | d } d } n d } x@ t |  j ƒ D]/ \ } } |  j | ƒ | k ri | | | <Pqi qi Wq+ Wn| xy |  j t j d ƒ D]a } | j	 d ƒ \ } }	 }
 y t
 |
 ƒ }
 Wn t k
 rþ qº n X|	 d k rd n d | |
 <qº W| S(   sV   
        Returns an OrderedDict of ordering field column numbers and asc/desc
        R‰   i   t   desct   ascRˆ   (   R   R    R‹   R;   Ri   t	   enumerateR#   R‡   RŒ   R�   R.   R3   (   RG   R~   t   ordering_fieldsRe   t
   order_typet   indexR†   R   R“   R”   R•   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   get_ordering_field_columns  s(    	
	
 c   
      C   s/  |  j  | ƒ \ |  _ |  _ } } |  j } x8 |  j D]- } | j | | ƒ } | d  k	 r4 | } q4 q4 Wy | j | �  } Wn8 t t f k
 r” ‚  n t	 k
 r² } t
 | ƒ ‚ n X| j j sÑ |  j | ƒ } n  |  j | | ƒ } | j | Œ  } |  j j | | |  j ƒ \ } }	 | |	 Br'| j ƒ  S| Sd  S(   N(   Rh   Rc   t   has_filtersR"   R@   RS   t   filterR   R   t	   ExceptionR   R?   t   select_relatedt   apply_select_relatedRŠ   R�   R+   t   get_search_resultst   distinct(
   RG   RH   t   remaining_lookup_paramst   filters_use_distinctt   qst   filter_spect   new_qsR   R~   t   search_use_distinct(    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyR!   :  s(    !	!

c         C   s^   |  j  t k r | j ƒ  S|  j  t k rA |  j ƒ  rA | j ƒ  Sn  |  j  rZ | j |  j  Œ  S| S(   N(   R(   t   TrueR¢   RP   t!   has_related_field_in_list_display(   RG   R¨   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyR£   g  s    
	c         C   ss   xl |  j  D]a } y |  j j | ƒ } Wn t k
 r9 q
 Xt | j t j ƒ r
 | | j ƒ  k rg q
 n  t	 Sq
 Wt
 S(   N(   R#   R    R�   R   RT   t   remote_fieldR   t   ManyToOneRelt   get_attnameR¬   RP   (   RG   R…   Re   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyR­   s  s    c         C   sP   t  | |  j ƒ } t d |  j j |  j j f d t | ƒ f d |  j j j	 ƒS(   Ns   admin:%s_%s_changet   argst   current_app(
   R„   RF   R   R   t	   app_labelt
   model_nameR
   R+   t
   admin_siteR‚   (   RG   t   resultRD   (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   url_for_result�  s
    N(   t   __name__t
   __module__RI   RS   RN   Rh   Rp   RA   R   R‡   RŠ   Rž   R!   R£   R­   R·   (    (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyR   %   s   	1	7	!			,	!	-		(,   R^   t   collectionsR    t   django.contrib.adminR   t   django.contrib.admin.exceptionsR   R   t   django.contrib.admin.optionsR   R   R   t   django.contrib.admin.utilsR   R   R	   R
   t   django.core.exceptionsR   R   R   t   django.core.paginatorR   t	   django.dbR   t   django.urlsR   t   django.utilsR   t   django.utils.encodingR   t   django.utils.httpR   t   django.utils.translationR   R4   R‹   t   ORDER_TYPE_VARR1   R>   R<   RK   t   objectR   (    (    (    sA   /tmp/pip-unpacked-wheel-4tV8Ix/django/contrib/admin/views/main.pyt   <module>   s*   "