2008-08-02 13:56:57 +08:00
|
|
|
import re
|
2008-07-19 07:54:34 +08:00
|
|
|
from django import http, template
|
2010-12-02 08:44:35 +08:00
|
|
|
from django.contrib.admin import ModelAdmin, actions
|
|
|
|
from django.contrib.admin.forms import AdminAuthenticationForm, ERROR_MESSAGE
|
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME, authenticate, login
|
2009-10-30 08:17:29 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.db.models.base import ModelBase
|
2008-08-10 07:40:57 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2009-07-17 00:16:13 +08:00
|
|
|
from django.core.urlresolvers import reverse
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.shortcuts import render_to_response
|
2009-01-15 04:22:25 +08:00
|
|
|
from django.utils.functional import update_wrapper
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
from django.utils.text import capfirst
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext as _
|
|
|
|
from django.views.decorators.cache import never_cache
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
LOGIN_FORM_KEY = 'this_is_the_login_form'
|
|
|
|
|
|
|
|
class AlreadyRegistered(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotRegistered(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AdminSite(object):
|
|
|
|
"""
|
|
|
|
An AdminSite object encapsulates an instance of the Django admin application, ready
|
2009-04-01 00:07:07 +08:00
|
|
|
to be hooked in to your URLconf. Models are registered with the AdminSite using the
|
2010-10-14 09:24:20 +08:00
|
|
|
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.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2010-12-02 08:44:35 +08:00
|
|
|
login_form = None
|
2008-07-19 07:54:34 +08:00
|
|
|
index_template = None
|
2008-08-24 00:27:12 +08:00
|
|
|
app_index_template = None
|
2010-01-13 07:34:46 +08:00
|
|
|
login_template = None
|
|
|
|
logout_template = None
|
|
|
|
password_change_template = None
|
|
|
|
password_change_done_template = None
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def __init__(self, name=None, app_name='admin'):
|
2008-07-19 07:54:34 +08:00
|
|
|
self._registry = {} # model_class class -> admin_class instance
|
2009-07-17 00:16:13 +08:00
|
|
|
self.root_path = None
|
2009-01-15 04:22:25 +08:00
|
|
|
if name is None:
|
2009-07-17 00:16:13 +08:00
|
|
|
self.name = 'admin'
|
2009-01-15 04:22:25 +08:00
|
|
|
else:
|
2009-07-17 00:16:13 +08:00
|
|
|
self.name = name
|
|
|
|
self.app_name = app_name
|
2009-04-07 04:23:33 +08:00
|
|
|
self._actions = {'delete_selected': actions.delete_selected}
|
|
|
|
self._global_actions = self._actions.copy()
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def register(self, model_or_iterable, admin_class=None, **options):
|
|
|
|
"""
|
|
|
|
Registers the given model(s) with the given admin class.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
The model(s) should be Model classes, not instances.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
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.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
If a model is already registered, this will raise AlreadyRegistered.
|
|
|
|
"""
|
2009-03-18 04:40:01 +08:00
|
|
|
if not admin_class:
|
|
|
|
admin_class = ModelAdmin
|
|
|
|
|
2008-07-24 02:58:06 +08:00
|
|
|
# Don't import the humongous validation code unless required
|
|
|
|
if admin_class and settings.DEBUG:
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.contrib.admin.validation import validate
|
2008-07-24 02:58:06 +08:00
|
|
|
else:
|
|
|
|
validate = lambda model, adminclass: None
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
if isinstance(model_or_iterable, ModelBase):
|
|
|
|
model_or_iterable = [model_or_iterable]
|
|
|
|
for model in model_or_iterable:
|
|
|
|
if model in self._registry:
|
|
|
|
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-24 02:58:06 +08:00
|
|
|
# If we got **options then dynamically construct a subclass of
|
|
|
|
# admin_class with those **options.
|
|
|
|
if options:
|
|
|
|
# For reasons I don't quite understand, without a __module__
|
|
|
|
# the created class appears to "live" in the wrong place,
|
|
|
|
# which causes issues later on.
|
|
|
|
options['__module__'] = __name__
|
|
|
|
admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-24 02:58:06 +08:00
|
|
|
# Validate (which might be a no-op)
|
|
|
|
validate(admin_class, model)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-24 02:58:06 +08:00
|
|
|
# Instantiate the admin class to save in the registry
|
2008-07-19 07:54:34 +08:00
|
|
|
self._registry[model] = admin_class(model, self)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def unregister(self, model_or_iterable):
|
|
|
|
"""
|
|
|
|
Unregisters the given model(s).
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
If a model isn't already registered, this will raise NotRegistered.
|
|
|
|
"""
|
|
|
|
if isinstance(model_or_iterable, ModelBase):
|
|
|
|
model_or_iterable = [model_or_iterable]
|
|
|
|
for model in model_or_iterable:
|
|
|
|
if model not in self._registry:
|
|
|
|
raise NotRegistered('The model %s is not registered' % model.__name__)
|
|
|
|
del self._registry[model]
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def add_action(self, action, name=None):
|
|
|
|
"""
|
|
|
|
Register an action to be available globally.
|
|
|
|
"""
|
|
|
|
name = name or action.__name__
|
|
|
|
self._actions[name] = action
|
|
|
|
self._global_actions[name] = action
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def disable_action(self, name):
|
|
|
|
"""
|
|
|
|
Disable a globally-registered action. Raises KeyError for invalid names.
|
|
|
|
"""
|
|
|
|
del self._actions[name]
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def get_action(self, name):
|
|
|
|
"""
|
|
|
|
Explicitally get a registered global action wheather it's enabled or
|
|
|
|
not. Raises KeyError for invalid names.
|
|
|
|
"""
|
|
|
|
return self._global_actions[name]
|
2009-07-13 21:46:31 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@property
|
2009-04-07 04:23:33 +08:00
|
|
|
def actions(self):
|
|
|
|
"""
|
|
|
|
Get all the enabled actions as an iterable of (name, func).
|
|
|
|
"""
|
|
|
|
return self._actions.iteritems()
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def has_permission(self, request):
|
|
|
|
"""
|
|
|
|
Returns True if the given HttpRequest has permission to view
|
|
|
|
*at least one* page in the admin site.
|
|
|
|
"""
|
2010-01-11 00:51:13 +08:00
|
|
|
return request.user.is_active and request.user.is_staff
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-10 20:41:42 +08:00
|
|
|
def check_dependencies(self):
|
2008-08-10 07:40:57 +08:00
|
|
|
"""
|
|
|
|
Check that all things needed to run the admin have been correctly installed.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-10 07:40:57 +08:00
|
|
|
The default implementation checks that LogEntry, ContentType and the
|
|
|
|
auth context processor are installed.
|
|
|
|
"""
|
|
|
|
from django.contrib.admin.models import LogEntry
|
2008-08-10 07:56:34 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-10 07:40:57 +08:00
|
|
|
if not LogEntry._meta.installed:
|
2010-02-22 07:40:47 +08:00
|
|
|
raise ImproperlyConfigured("Put 'django.contrib.admin' in your "
|
|
|
|
"INSTALLED_APPS setting in order to use the admin application.")
|
2008-08-10 07:40:57 +08:00
|
|
|
if not ContentType._meta.installed:
|
2010-02-22 07:40:47 +08:00
|
|
|
raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in "
|
|
|
|
"your INSTALLED_APPS setting in order to use the admin application.")
|
|
|
|
if not ('django.contrib.auth.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS or
|
|
|
|
'django.core.context_processors.auth' in settings.TEMPLATE_CONTEXT_PROCESSORS):
|
|
|
|
raise ImproperlyConfigured("Put 'django.contrib.auth.context_processors.auth' "
|
|
|
|
"in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.")
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-07-13 21:46:31 +08:00
|
|
|
def admin_view(self, view, cacheable=False):
|
2008-08-02 13:56:57 +08:00
|
|
|
"""
|
2009-07-13 21:46:31 +08:00
|
|
|
Decorator to create an admin view attached to this ``AdminSite``. This
|
2009-01-15 04:22:25 +08:00
|
|
|
wraps the view and provides permission checking by calling
|
|
|
|
``self.has_permission``.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
You'll want to use this from within ``AdminSite.get_urls()``:
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
class MyAdminSite(AdminSite):
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
def get_urls(self):
|
|
|
|
from django.conf.urls.defaults import patterns, url
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
urls = super(MyAdminSite, self).get_urls()
|
|
|
|
urls += patterns('',
|
2009-04-07 05:11:10 +08:00
|
|
|
url(r'^my_view/$', self.admin_view(some_view))
|
2009-01-15 04:22:25 +08:00
|
|
|
)
|
|
|
|
return urls
|
2009-07-13 21:46:31 +08:00
|
|
|
|
|
|
|
By default, admin_views are marked non-cacheable using the
|
|
|
|
``never_cache`` decorator. If the view can be safely cached, set
|
|
|
|
cacheable=True.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2009-01-15 04:22:25 +08:00
|
|
|
def inner(request, *args, **kwargs):
|
|
|
|
if not self.has_permission(request):
|
|
|
|
return self.login(request)
|
|
|
|
return view(request, *args, **kwargs)
|
2009-07-13 21:46:31 +08:00
|
|
|
if not cacheable:
|
|
|
|
inner = never_cache(inner)
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
# We add csrf_protect here so this function can be used as a utility
|
|
|
|
# function for any view, without having to repeat 'csrf_protect'.
|
2010-02-28 05:08:30 +08:00
|
|
|
if not getattr(view, 'csrf_exempt', False):
|
|
|
|
inner = csrf_protect(inner)
|
2009-01-15 04:22:25 +08:00
|
|
|
return update_wrapper(inner, view)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
def get_urls(self):
|
|
|
|
from django.conf.urls.defaults import patterns, url, include
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-10-24 18:13:24 +08:00
|
|
|
if settings.DEBUG:
|
|
|
|
self.check_dependencies()
|
|
|
|
|
2009-07-13 21:46:31 +08:00
|
|
|
def wrap(view, cacheable=False):
|
2009-01-15 04:22:25 +08:00
|
|
|
def wrapper(*args, **kwargs):
|
2009-07-13 21:46:31 +08:00
|
|
|
return self.admin_view(view, cacheable)(*args, **kwargs)
|
2009-01-15 04:22:25 +08:00
|
|
|
return update_wrapper(wrapper, view)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
# Admin-site-wide views.
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
url(r'^$',
|
|
|
|
wrap(self.index),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='index'),
|
2009-01-15 04:22:25 +08:00
|
|
|
url(r'^logout/$',
|
|
|
|
wrap(self.logout),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='logout'),
|
2009-01-15 04:22:25 +08:00
|
|
|
url(r'^password_change/$',
|
2009-07-13 21:46:31 +08:00
|
|
|
wrap(self.password_change, cacheable=True),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='password_change'),
|
2009-01-15 04:22:25 +08:00
|
|
|
url(r'^password_change/done/$',
|
2009-07-13 21:46:31 +08:00
|
|
|
wrap(self.password_change_done, cacheable=True),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='password_change_done'),
|
2009-01-15 04:22:25 +08:00
|
|
|
url(r'^jsi18n/$',
|
2009-07-13 21:46:31 +08:00
|
|
|
wrap(self.i18n_javascript, cacheable=True),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='jsi18n'),
|
2009-01-15 04:22:25 +08:00
|
|
|
url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$',
|
|
|
|
'django.views.defaults.shortcut'),
|
|
|
|
url(r'^(?P<app_label>\w+)/$',
|
|
|
|
wrap(self.app_index),
|
2009-07-17 00:16:13 +08:00
|
|
|
name='app_list')
|
2009-01-15 04:22:25 +08:00
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2009-01-15 04:22:25 +08:00
|
|
|
# Add in each model's views.
|
|
|
|
for model, model_admin in self._registry.iteritems():
|
|
|
|
urlpatterns += patterns('',
|
|
|
|
url(r'^%s/%s/' % (model._meta.app_label, model._meta.module_name),
|
|
|
|
include(model_admin.urls))
|
|
|
|
)
|
|
|
|
return urlpatterns
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@property
|
2009-01-15 04:22:25 +08:00
|
|
|
def urls(self):
|
2009-07-17 00:16:13 +08:00
|
|
|
return self.get_urls(), self.app_name, self.name
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def password_change(self, request):
|
|
|
|
"""
|
|
|
|
Handles the "change password" task -- both form display and validation.
|
|
|
|
"""
|
|
|
|
from django.contrib.auth.views import password_change
|
2009-07-17 00:16:13 +08:00
|
|
|
if self.root_path is not None:
|
|
|
|
url = '%spassword_change/done/' % self.root_path
|
|
|
|
else:
|
|
|
|
url = reverse('admin:password_change_done', current_app=self.name)
|
2010-01-13 07:34:46 +08:00
|
|
|
defaults = {
|
2010-12-02 08:44:35 +08:00
|
|
|
'current_app': self.name,
|
2010-01-13 07:34:46 +08:00
|
|
|
'post_change_redirect': url
|
|
|
|
}
|
|
|
|
if self.password_change_template is not None:
|
|
|
|
defaults['template_name'] = self.password_change_template
|
|
|
|
return password_change(request, **defaults)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
def password_change_done(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
Displays the "success" page after a password change.
|
|
|
|
"""
|
|
|
|
from django.contrib.auth.views import password_change_done
|
2010-12-02 08:44:35 +08:00
|
|
|
defaults = {
|
|
|
|
'current_app': self.name,
|
|
|
|
'extra_context': extra_context or {},
|
|
|
|
}
|
2010-01-13 07:34:46 +08:00
|
|
|
if self.password_change_done_template is not None:
|
|
|
|
defaults['template_name'] = self.password_change_done_template
|
|
|
|
return password_change_done(request, **defaults)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def i18n_javascript(self, request):
|
|
|
|
"""
|
|
|
|
Displays the i18n JavaScript that the Django admin requires.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
This takes into account the USE_I18N setting. If it's set to False, the
|
|
|
|
generated JavaScript will be leaner and faster.
|
|
|
|
"""
|
|
|
|
if settings.USE_I18N:
|
|
|
|
from django.views.i18n import javascript_catalog
|
|
|
|
else:
|
|
|
|
from django.views.i18n import null_javascript_catalog as javascript_catalog
|
|
|
|
return javascript_catalog(request, packages='django.conf')
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@never_cache
|
|
|
|
def logout(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
Logs out the user for the given HttpRequest.
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
This should *not* assume the user is already logged in.
|
|
|
|
"""
|
|
|
|
from django.contrib.auth.views import logout
|
2010-12-02 08:44:35 +08:00
|
|
|
defaults = {
|
|
|
|
'current_app': self.name,
|
|
|
|
'extra_context': extra_context or {},
|
|
|
|
}
|
2010-01-13 07:34:46 +08:00
|
|
|
if self.logout_template is not None:
|
|
|
|
defaults['template_name'] = self.logout_template
|
|
|
|
return logout(request, **defaults)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@never_cache
|
|
|
|
def login(self, request, extra_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
Displays the login form for the given HttpRequest.
|
|
|
|
"""
|
2010-12-02 08:44:35 +08:00
|
|
|
from django.contrib.auth.views import login
|
|
|
|
context = {
|
|
|
|
'title': _('Log in'),
|
|
|
|
'root_path': self.root_path,
|
|
|
|
'app_path': request.get_full_path(),
|
|
|
|
REDIRECT_FIELD_NAME: request.get_full_path(),
|
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
|
|
|
defaults = {
|
|
|
|
'extra_context': context,
|
|
|
|
'current_app': self.name,
|
|
|
|
'authentication_form': self.login_form or AdminAuthenticationForm,
|
|
|
|
'template_name': self.login_template or 'admin/login.html',
|
|
|
|
}
|
|
|
|
return login(request, **defaults)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
@never_cache
|
2008-07-19 07:54:34 +08:00
|
|
|
def index(self, request, extra_context=None):
|
|
|
|
"""
|
|
|
|
Displays the main admin index page, which lists all of the installed
|
|
|
|
apps that have been registered in this site.
|
|
|
|
"""
|
|
|
|
app_dict = {}
|
|
|
|
user = request.user
|
|
|
|
for model, model_admin in self._registry.items():
|
|
|
|
app_label = model._meta.app_label
|
|
|
|
has_module_perms = user.has_module_perms(app_label)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
if has_module_perms:
|
2009-04-09 03:47:46 +08:00
|
|
|
perms = model_admin.get_model_perms(request)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Check whether user has any perm for this module.
|
|
|
|
# If so, add the module to the model_list.
|
|
|
|
if True in perms.values():
|
|
|
|
model_dict = {
|
|
|
|
'name': capfirst(model._meta.verbose_name_plural),
|
|
|
|
'admin_url': mark_safe('%s/%s/' % (app_label, model.__name__.lower())),
|
|
|
|
'perms': perms,
|
|
|
|
}
|
|
|
|
if app_label in app_dict:
|
|
|
|
app_dict[app_label]['models'].append(model_dict)
|
|
|
|
else:
|
|
|
|
app_dict[app_label] = {
|
|
|
|
'name': app_label.title(),
|
2008-09-16 14:01:47 +08:00
|
|
|
'app_url': app_label + '/',
|
2008-07-19 07:54:34 +08:00
|
|
|
'has_module_perms': has_module_perms,
|
|
|
|
'models': [model_dict],
|
|
|
|
}
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Sort the apps alphabetically.
|
|
|
|
app_list = app_dict.values()
|
2010-08-07 00:31:44 +08:00
|
|
|
app_list.sort(key=lambda x: x['name'])
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# Sort the models alphabetically within each app.
|
|
|
|
for app in app_list:
|
2010-08-07 00:31:44 +08:00
|
|
|
app['models'].sort(key=lambda x: x['name'])
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
context = {
|
|
|
|
'title': _('Site administration'),
|
|
|
|
'app_list': app_list,
|
|
|
|
'root_path': self.root_path,
|
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
2009-07-17 00:16:13 +08:00
|
|
|
context_instance = template.RequestContext(request, current_app=self.name)
|
2008-08-02 13:56:57 +08:00
|
|
|
return render_to_response(self.index_template or 'admin/index.html', context,
|
2009-07-17 00:16:13 +08:00
|
|
|
context_instance=context_instance
|
2008-07-19 07:54:34 +08:00
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-08-25 11:55:47 +08:00
|
|
|
def app_index(self, request, app_label, extra_context=None):
|
2008-08-23 12:00:15 +08:00
|
|
|
user = request.user
|
|
|
|
has_module_perms = user.has_module_perms(app_label)
|
|
|
|
app_dict = {}
|
|
|
|
for model, model_admin in self._registry.items():
|
|
|
|
if app_label == model._meta.app_label:
|
|
|
|
if has_module_perms:
|
2009-04-09 03:47:46 +08:00
|
|
|
perms = model_admin.get_model_perms(request)
|
|
|
|
|
2008-08-23 12:00:15 +08:00
|
|
|
# Check whether user has any perm for this module.
|
|
|
|
# If so, add the module to the model_list.
|
|
|
|
if True in perms.values():
|
|
|
|
model_dict = {
|
|
|
|
'name': capfirst(model._meta.verbose_name_plural),
|
|
|
|
'admin_url': '%s/' % model.__name__.lower(),
|
|
|
|
'perms': perms,
|
|
|
|
}
|
2008-08-27 13:22:25 +08:00
|
|
|
if app_dict:
|
|
|
|
app_dict['models'].append(model_dict),
|
|
|
|
else:
|
|
|
|
# First time around, now that we know there's
|
|
|
|
# something to display, add in the necessary meta
|
|
|
|
# information.
|
|
|
|
app_dict = {
|
|
|
|
'name': app_label.title(),
|
|
|
|
'app_url': '',
|
|
|
|
'has_module_perms': has_module_perms,
|
|
|
|
'models': [model_dict],
|
|
|
|
}
|
|
|
|
if not app_dict:
|
|
|
|
raise http.Http404('The requested admin page does not exist.')
|
2008-08-23 12:00:15 +08:00
|
|
|
# Sort the models alphabetically within each app.
|
2010-08-07 00:31:44 +08:00
|
|
|
app_dict['models'].sort(key=lambda x: x['name'])
|
2008-08-25 11:55:47 +08:00
|
|
|
context = {
|
2008-08-29 04:17:31 +08:00
|
|
|
'title': _('%s administration') % capfirst(app_label),
|
2008-08-27 15:27:09 +08:00
|
|
|
'app_list': [app_dict],
|
|
|
|
'root_path': self.root_path,
|
2008-08-25 11:55:47 +08:00
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
2009-07-17 00:16:13 +08:00
|
|
|
context_instance = template.RequestContext(request, current_app=self.name)
|
2009-04-01 22:13:59 +08:00
|
|
|
return render_to_response(self.app_index_template or ('admin/%s/app_index.html' % app_label,
|
|
|
|
'admin/app_index.html'), context,
|
2009-07-17 00:16:13 +08:00
|
|
|
context_instance=context_instance
|
2008-08-25 11:55:47 +08:00
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# This global object represents the default admin site, for the common case.
|
|
|
|
# You can instantiate AdminSite in your own code to create a custom admin site.
|
|
|
|
site = AdminSite()
|