
 XbL                 @   ss  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 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 d  d l  m! Z! d  d l" m# Z# d  d l$ m% Z% g  Z& Gd d   d e'  Z( Gd d   d e'  Z) Gd d   d e*  Z+ e+   Z, d S)    )update_wrapper)apps)settings)
ModelAdminactions)REDIRECT_FIELD_NAME)ImproperlyConfiguredPermissionDenied)	ModelBase)Http404HttpResponseRedirect)TemplateResponse)NoReverseMatchreverse)six)capfirst)ugettextugettext_lazy)never_cache)csrf_protect)JavaScriptCatalogc               @   s   e  Z d  Z d S)AlreadyRegisteredN)__name__
__module____qualname__ r   r   H/home/ubuntu/projects/ifolica/build/django/django/contrib/admin/sites.pyr      s   r   c               @   s   e  Z d  Z d S)NotRegisteredN)r   r   r   r   r   r   r   r      s   r   c               @   s  e  Z d  Z d Z e d  Z e d  Z e d  Z d Z d Z	 d Z
 d Z d Z d Z d Z d Z d Z d d	 d
  Z d d d  Z d d   Z d d   Z d d d  Z d d   Z d d   Z e d d    Z e d d    Z e j d d    Z d d   Z d d d   Z d! d"   Z e d# d$    Z d% d&   Z  d d' d(  Z! d d) d*  Z" d+ d,   Z# e$ d d- d.   Z% e$ d d/ d0   Z& d d1 d2  Z' d3 d4   Z( e$ d d5 d6   Z) d d7 d8  Z* d S)9	AdminSiteal  
    An AdminSite object encapsulates an instance of the Django admin application, ready
    to be hooked in to your URLconf. Models are registered with the AdminSite using the
    register() method, and the get_urls() method can then be used to access Django view
    functions that present a full admin interface for the collection of registered
    models.
    zDjango site adminzDjango administrationzSite administration/-Nadminc             C   s;   i  |  _  | |  _ i t j d 6|  _ |  j j   |  _ d  S)Ndelete_selected)	_registrynamer   r"   _actionscopy_global_actions)selfr$   r   r   r   __init__=   s    		zAdminSite.__init__c             K   s  | s t  } n  t | t  r* | g } n  x | D] } | j j rY t d | j   n  | |  j k r~ t d | j   n  | j j	 s1 | r t | d <t
 d | j | f |  } n  | | |   } | t  k	 r t j r t j | j    n  | |  j | <q1 q1 Wd S)a  
        Registers the given model(s) with the given admin class.

        The model(s) should be Model classes, not instances.

        If an admin class isn't given, it will use ModelAdmin (the default
        admin options). If keyword arguments are given -- e.g., list_display --
        they'll be applied as options to the admin class.

        If a model is already registered, this will raise AlreadyRegistered.

        If a model is abstract, this will raise ImproperlyConfigured.
        z@The model %s is abstract, so it cannot be registered with admin.z"The model %s is already registeredr   z%sAdminN)r   
isinstancer
   _metaZabstractr   r   r#   r   Zswappedtyper   DEBUGsystem_check_errorsextendcheck)r(   model_or_iterableZadmin_classoptionsmodelZ	admin_objr   r   r   registerC   s$    	
zAdminSite.registerc             C   s_   t  | t  r | g } n  x= | D]5 } | |  j k rM t d | j   n  |  j | =q" Wd S)z~
        Unregisters the given model(s).

        If a model isn't already registered, this will raise NotRegistered.
        zThe model %s is not registeredN)r*   r
   r#   r   r   )r(   r1   r3   r   r   r   
unregisterr   s    zAdminSite.unregisterc             C   s   | |  j  k S)zM
        Check if a model class is registered with this `AdminSite`.
        )r#   )r(   r3   r   r   r   is_registered   s    zAdminSite.is_registeredc             C   s-   | p | j  } | |  j | <| |  j | <d S)z>
        Register an action to be available globally.
        N)r   r%   r'   )r(   actionr$   r   r   r   
add_action   s    zAdminSite.add_actionc             C   s   |  j  | =d S)zZ
        Disable a globally-registered action. Raises KeyError for invalid names.
        N)r%   )r(   r$   r   r   r   disable_action   s    zAdminSite.disable_actionc             C   s   |  j  | S)z
        Explicitly get a registered global action whether it's enabled or
        not. Raises KeyError for invalid names.
        )r'   )r(   r$   r   r   r   
get_action   s    zAdminSite.get_actionc             C   s   t  j |  j  S)zM
        Get all the enabled actions as an iterable of (name, func).
        )r   	iteritemsr%   )r(   r   r   r   r      s    zAdminSite.actionsc             C   s   |  j  S)N)_empty_value_display)r(   r   r   r   empty_value_display   s    zAdminSite.empty_value_displayc             C   s   | |  _  d  S)N)r<   )r(   r=   r   r   r   r=      s    c             C   s   | j  j o | j  j S)z}
        Returns True if the given HttpRequest has permission to view
        *at least one* page in the admin site.
        )userZ	is_activeZis_staff)r(   requestr   r   r   has_permission   s    zAdminSite.has_permissionFc                sX      f d d   } | s* t  |  } n  t  d d  sK t |  } n  t |   S)a  
        Decorator to create an admin view attached to this ``AdminSite``. This
        wraps the view and provides permission checking by calling
        ``self.has_permission``.

        You'll want to use this from within ``AdminSite.get_urls()``:

            class MyAdminSite(AdminSite):

                def get_urls(self):
                    from django.conf.urls import url

                    urls = super(MyAdminSite, self).get_urls()
                    urls += [
                        url(r'^my_view/$', self.admin_view(some_view))
                    ]
                    return urls

        By default, admin_views are marked non-cacheable using the
        ``never_cache`` decorator. If the view can be safely cached, set
        cacheable=True.
        c                s     j  |   s~ |  j t d d   j k rL t d d   j } t |  Sd d l m } | |  j   t d d   j  S |  | |  S)Nzadmin:logoutcurrent_appzadmin:indexr   )redirect_to_loginzadmin:login)r@   pathr   r$   r   django.contrib.auth.viewsrB   get_full_path)r?   argskwargs
index_pathrB   )r(   viewr   r   inner   s    
	z#AdminSite.admin_view.<locals>.innerZcsrf_exemptF)r   getattrr   r   )r(   rI   	cacheablerJ   r   )r(   rI   r   
admin_view   s    zAdminSite.admin_viewc       
         s  d d l  m } m } d d l m } d   f d d  } | d |   j  d d	 | d
   j d d | d |   j  d d | d |   j d d d d | d |   j	 d d d d | d |   j
 d d d d | d | | j  d d g } g  } xz   j j   D]i \ } } | | d | j j | j j f | | j   g 7} | j j | k r | j | j j  q q W| rd d j |  d }	 | | |	 |   j  d d g 7} n  | S)Nr   )urlinclude)viewsFc                s.       f d d   }  | _  t |   S)Nc                 s    j      |  |   S)N)rM   )rF   rG   )rL   r(   rI   r   r   wrapper   s    z1AdminSite.get_urls.<locals>.wrap.<locals>.wrapper)Z
admin_siter   )rI   rL   rQ   )r(   )rL   rI   r   wrap   s    	z AdminSite.get_urls.<locals>.wrapz^$r$   indexz^login/$loginz	^logout/$logoutz^password_change/$rL   Tpassword_changez^password_change/done/$password_change_donez	^jsi18n/$Zjsi18nz/^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$Zview_on_sitez^%s/%s/z^(?P<app_label>|z)/$app_list)Zdjango.conf.urlsrN   rO   Zdjango.contrib.contenttypesrP   rS   rT   rU   rV   rW   i18n_javascriptZshortcutr#   itemsr+   	app_label
model_nameurlsappendjoin	app_index)
r(   rN   rO   Zcontenttype_viewsrR   ZurlpatternsZvalid_app_labelsr3   model_adminregexr   )r(   r   get_urls   s.    !!2%zAdminSite.get_urlsc             C   s   |  j    d |  j f S)Nr!   )rd   r$   )r(   r   r   r   r^   	  s    zAdminSite.urlsc             C   sp   | j  d } |  j d k r( | r( | n |  j } i |  j d 6|  j d 6| d 6|  j |  d 6|  j |  d 6S)z
        Returns a dictionary of variables to put in the template context for
        *every* page in the admin site.

        For sites running on a subpath, use the SCRIPT_NAME value if site_url
        hasn't been customized.
        ZSCRIPT_NAMEr   
site_titlesite_headersite_urlr@   Zavailable_apps)ZMETArg   re   rf   r@   get_app_list)r(   r?   script_namerg   r   r   r   each_context  s    $

zAdminSite.each_contextc             C   s   d d l  m } d d l m } t d d |  j } i | d 6| d 6t |  j |  | p^ i   d 6} |  j d	 k	 r |  j | d
 <n  |  j | _	 | | |  S)zY
        Handles the "change password" task -- both form display and validation.
        r   )AdminPasswordChangeForm)rV   zadmin:password_change_donerA   Zpassword_change_formZpost_change_redirectextra_contextNtemplate_name)
django.contrib.admin.formsrk   rD   rV   r   r$   dictrj   password_change_templaterA   )r(   r?   rl   rk   rV   rN   defaultsr   r   r   rV     s    "zAdminSite.password_changec             C   sm   d d l  m } i t |  j |  | p+ i   d 6} |  j d k	 rT |  j | d <n  |  j | _ | | |  S)zF
        Displays the "success" page after a password change.
        r   )rW   rl   Nrm   )rD   rW   ro   rj   password_change_done_templater$   rA   )r(   r?   rl   rW   rq   r   r   r   rW   0  s    "zAdminSite.password_change_donec             C   s   t  j d d g  |  S)zN
        Displays the i18n JavaScript that the Django admin requires.
        packageszdjango.contrib.admin)r   Zas_view)r(   r?   r   r   r   rZ   =  s    zAdminSite.i18n_javascriptc             C   ss   d d l  m } i t |  j |  d d | p1 i  d 6} |  j d k	 rZ |  j | d <n  |  j | _ | | |  S)z
        Logs out the user for the given HttpRequest.

        This should *not* assume the user is already logged in.
        r   )rU   r@   Frl   Nrm   )rD   rU   ro   rj   logout_templater$   rA   )r(   r?   rl   rU   rq   r   r   r   rU   C  s    zAdminSite.logoutc             C   s,  | j  d k r= |  j |  r= t d d |  j } t |  Sd d l m } d d l m } t	 |  j
 |  d t d  d	 | j   d
 | j j   } t | j k r t | j k r t d d |  j | t <n  | j | p i   i | d 6|  j p | d 6|  j pd d 6} |  j | _ | | |  S)zD
        Displays the login form for the given HttpRequest.
        GETzadmin:indexrA   r   )rT   )AdminAuthenticationFormtitlezLog inZapp_pathusernamerl   Zauthentication_formzadmin/login.htmlrm   )methodr@   r   r$   r   rD   rT   rn   rv   ro   rj   _rE   r>   Zget_usernamer   ru   POSTupdate
login_formlogin_templaterA   )r(   r?   rl   rH   rT   rv   contextrq   r   r   r   rT   Y  s(    
zAdminSite.loginc                s  i  }   r1   f d d   |  j  j   D } n	 |  j  } x| j   D]\ } } | j j } | j |  } | s   rG t  qG qG n  | j |  }	 d |	 j   k r qG n  | | j j f }
 i t	 | j j
  d 6| j j d 6|	 d 6} |	 j d  r7y! t d |
 d	 |  j | d
 <Wq7t k
 r3Yq7Xn  |	 j d  ry! t d |
 d	 |  j | d <Wqt k
 r{YqXn  | | k r| | d j |  qG i t j |  j d 6| d 6t d d i | d 6d	 |  j d 6| d 6| g d 6| | <qG W  r| j    S| S)z{
        Builds the app dictionary. Takes an optional label parameters to filter
        models of a specific app.
        c                s1   i  |  ]' \ } } | j  j   k r | |  q Sr   )r+   r\   ).0mZm_a)labelr   r   
<dictcomp>  s   	 	z-AdminSite._build_app_dict.<locals>.<dictcomp>Tr$   object_namepermsZchangezadmin:%s_%s_changelistrA   Z	admin_urladdzadmin:%s_%s_addZadd_urlmodelsr\   zadmin:app_listrG   Zapp_urlhas_module_perms)r#   r[   r+   r\   Zhas_module_permissionr	   Zget_model_permsvaluesr]   r   Zverbose_name_pluralr   getr   r$   r   r_   r   get_app_configverbose_name)r(   r?   r   app_dictr   r3   rb   r\   r   r   infoZ
model_dictr   )r   r   _build_app_dict{  sV    %		
!!zAdminSite._build_app_dictc             C   s\   |  j  |  } t | j   d d d   } x( | D]  } | d j d d d    q4 W| S)zq
        Returns a sorted list of all the installed apps that have been
        registered in this site.
        keyc             S   s   |  d j    S)Nr$   )lower)xr   r   r   <lambda>  s    z(AdminSite.get_app_list.<locals>.<lambda>r   c             S   s   |  d S)Nr$   r   )r   r   r   r   r     s    )r   sortedr   sort)r(   r?   r   rY   appr   r   r   rh     s
    zAdminSite.get_app_listc             C   sk   |  j  |  } t |  j |  d |  j d | } | j | pB i   |  j | _ t | |  j pd d |  S)z
        Displays the main admin index page, which lists all of the installed
        apps that have been registered in this site.
        rw   rY   zadmin/index.html)	rh   ro   rj   index_titler|   r$   rA   r   index_template)r(   r?   rl   rY   r   r   r   r   rS     s    		zAdminSite.indexc             C   s   |  j  | |  } | s' t d   n  | d j d d d    t j |  j } t |  j |  d t d  i | d 6d	 | g d
 | } | j	 | p i   |  j
 | _ t | |  j p d | d g |  S)Nz(The requested admin page does not exist.r   r   c             S   s   |  d S)Nr$   r   )r   r   r   r   r     s    z%AdminSite.app_index.<locals>.<lambda>rw   z%(app)s administrationr   rY   r\   zadmin/%s/app_index.htmlzadmin/app_index.html)r   r   r   r   r   r   ro   rj   rz   r|   r$   rA   r   app_index_template)r(   r?   r\   rl   r   Zapp_namer   r   r   r   ra     s     		zAdminSite.app_index)+r   r   r   __doc__r   re   rf   r   rg   r<   r}   r   r   r~   rt   rp   rr   r)   r4   r5   r6   r8   r9   r:   propertyr   r=   setterr@   rM   rd   r^   rj   rV   rW   rZ   r   rU   rT   r   rh   rS   ra   r   r   r   r   r      sP   /,-!Cr   N)-	functoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.contrib.adminr   r   Zdjango.contrib.authr   Zdjango.core.exceptionsr   r	   Zdjango.db.models.baser
   Zdjango.httpr   r   Zdjango.template.responser   Zdjango.urlsr   r   Zdjango.utilsr   Zdjango.utils.textr   Zdjango.utils.translationr   rz   r   Zdjango.views.decorators.cacher   Zdjango.views.decorators.csrfr   Zdjango.views.i18nr   r.   	Exceptionr   r   objectr   siter   r   r   r   <module>   s*    