1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

[soc2010/app-loading] added verbose_name to app instance and modified admin to use it

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13592 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Arthur Koziel 2010-08-15 18:31:48 +00:00
parent 3da601b606
commit a357ed10be
6 changed files with 29 additions and 18 deletions

View File

@ -2,7 +2,7 @@ from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
from django.contrib.admin.options import StackedInline, TabularInline from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site from django.contrib.admin.sites import AdminSite, site
from django.core.apps import cache
def autodiscover(): def autodiscover():
""" """
@ -16,8 +16,9 @@ def autodiscover():
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS: for app in cache.installed_apps:
mod = import_module(app) app_instance = cache.find_app(app)
mod = app_instance.module
# Attempt to import the app's admin module. # Attempt to import the app's admin module.
try: try:
before_import_registry = copy.copy(site._registry) before_import_registry = copy.copy(site._registry)

View File

@ -8,6 +8,7 @@ from django.contrib.admin import helpers
from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_objects, model_ngettext, model_format_dict from django.contrib.admin.util import unquote, flatten_fieldsets, get_deleted_objects, model_ngettext, model_format_dict
from django.contrib import messages from django.contrib import messages
from django.views.decorators.csrf import csrf_protect from django.views.decorators.csrf import csrf_protect
from django.core.apps import cache
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
from django.db import models, transaction from django.db import models, transaction
from django.db.models.fields import BLANK_CHOICE_DASH from django.db.models.fields import BLANK_CHOICE_DASH
@ -845,7 +846,7 @@ class ModelAdmin(BaseModelAdmin):
'inline_admin_formsets': inline_admin_formsets, 'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets), 'errors': helpers.AdminErrorList(form, formsets),
'root_path': self.admin_site.root_path, 'root_path': self.admin_site.root_path,
'app_label': opts.app_label, 'app_label': cache.find_app(opts.app_label).verbose_name,
} }
context.update(extra_context or {}) context.update(extra_context or {})
return self.render_change_form(request, context, form_url=form_url, add=True) return self.render_change_form(request, context, form_url=form_url, add=True)
@ -937,7 +938,7 @@ class ModelAdmin(BaseModelAdmin):
'inline_admin_formsets': inline_admin_formsets, 'inline_admin_formsets': inline_admin_formsets,
'errors': helpers.AdminErrorList(form, formsets), 'errors': helpers.AdminErrorList(form, formsets),
'root_path': self.admin_site.root_path, 'root_path': self.admin_site.root_path,
'app_label': opts.app_label, 'app_label': cache.find_app(opts.app_label).verbose_name,
} }
context.update(extra_context or {}) context.update(extra_context or {})
return self.render_change_form(request, context, change=True, obj=obj) return self.render_change_form(request, context, change=True, obj=obj)
@ -1076,7 +1077,7 @@ class ModelAdmin(BaseModelAdmin):
'media': media, 'media': media,
'has_add_permission': self.has_add_permission(request), 'has_add_permission': self.has_add_permission(request),
'root_path': self.admin_site.root_path, 'root_path': self.admin_site.root_path,
'app_label': app_label, 'app_label': cache.find_app(app_label).verbose_name,
'action_form': action_form, 'action_form': action_form,
'actions_on_top': self.actions_on_top, 'actions_on_top': self.actions_on_top,
'actions_on_bottom': self.actions_on_bottom, 'actions_on_bottom': self.actions_on_bottom,
@ -1129,7 +1130,7 @@ class ModelAdmin(BaseModelAdmin):
"perms_lacking": perms_needed, "perms_lacking": perms_needed,
"opts": opts, "opts": opts,
"root_path": self.admin_site.root_path, "root_path": self.admin_site.root_path,
"app_label": app_label, "app_label": cache.find_app(app_label).verbose_name,
} }
context.update(extra_context or {}) context.update(extra_context or {})
context_instance = template.RequestContext(request, current_app=self.admin_site.name) context_instance = template.RequestContext(request, current_app=self.admin_site.name)
@ -1157,7 +1158,7 @@ class ModelAdmin(BaseModelAdmin):
'module_name': capfirst(force_unicode(opts.verbose_name_plural)), 'module_name': capfirst(force_unicode(opts.verbose_name_plural)),
'object': obj, 'object': obj,
'root_path': self.admin_site.root_path, 'root_path': self.admin_site.root_path,
'app_label': app_label, 'app_label': cache.find_app(app_label).verbose_name,
} }
context.update(extra_context or {}) context.update(extra_context or {})
context_instance = template.RequestContext(request, current_app=self.admin_site.name) context_instance = template.RequestContext(request, current_app=self.admin_site.name)

View File

@ -5,6 +5,7 @@ from django.contrib.admin import actions
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
from django.views.decorators.csrf import csrf_protect from django.views.decorators.csrf import csrf_protect
from django.db.models.base import ModelBase from django.db.models.base import ModelBase
from django.core.apps import cache
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
@ -371,7 +372,7 @@ class AdminSite(object):
app_dict[app_label]['models'].append(model_dict) app_dict[app_label]['models'].append(model_dict)
else: else:
app_dict[app_label] = { app_dict[app_label] = {
'name': app_label.title(), 'name': cache.find_app(app_label).verbose_name,
'app_url': app_label + '/', 'app_url': app_label + '/',
'has_module_perms': has_module_perms, 'has_module_perms': has_module_perms,
'models': [model_dict], 'models': [model_dict],
@ -415,6 +416,7 @@ class AdminSite(object):
user = request.user user = request.user
has_module_perms = user.has_module_perms(app_label) has_module_perms = user.has_module_perms(app_label)
app_dict = {} app_dict = {}
app_instance = cache.find_app(app_label)
for model, model_admin in self._registry.items(): for model, model_admin in self._registry.items():
if app_label == model._meta.app_label: if app_label == model._meta.app_label:
if has_module_perms: if has_module_perms:
@ -435,7 +437,7 @@ class AdminSite(object):
# something to display, add in the necessary meta # something to display, add in the necessary meta
# information. # information.
app_dict = { app_dict = {
'name': app_label.title(), 'name': app_instance.verbose_name,
'app_url': '', 'app_url': '',
'has_module_perms': has_module_perms, 'has_module_perms': has_module_perms,
'models': [model_dict], 'models': [model_dict],
@ -445,7 +447,7 @@ class AdminSite(object):
# Sort the models alphabetically within each app. # Sort the models alphabetically within each app.
app_dict['models'].sort(lambda x, y: cmp(x['name'], y['name'])) app_dict['models'].sort(lambda x, y: cmp(x['name'], y['name']))
context = { context = {
'title': _('%s administration') % capfirst(app_label), 'title': _('%s administration') % app_instance.verbose_name,
'app_list': [app_dict], 'app_list': [app_dict],
'root_path': self.root_path, 'root_path': self.root_path,
} }

View File

@ -3,6 +3,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule from django.utils.module_loading import module_has_submodule
from django.utils.translation import ugettext as _
import imp import imp
import sys import sys
@ -22,9 +23,14 @@ class App(object):
""" """
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
# errors raised when trying to import the app self.verbose_name = _(name.title())
self.verbose_name_plural = _(name.title())
self.errors = [] self.errors = []
self.models = [] self.models = []
self.module = None
def __str__(self):
return self.name
def __repr__(self): def __repr__(self):
return '<App: %s>' % self.name return '<App: %s>' % self.name
@ -127,6 +133,7 @@ class AppCache(object):
else: else:
app_instance_name = app_name app_instance_name = app_name
app_instance = app_class(app_instance_name) app_instance = app_class(app_instance_name)
app_instance.module = app_module
self.app_instances.append(app_instance) self.app_instances.append(app_instance)
self.installed_apps.append(app_name) self.installed_apps.append(app_name)

View File

@ -2,6 +2,7 @@ from optparse import make_option
import sys import sys
from django.conf import settings from django.conf import settings
from django.core.apps import cache
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.core.management.color import no_style from django.core.management.color import no_style
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
@ -30,7 +31,7 @@ class Command(NoArgsCommand):
# Import the 'management' module within each installed app, to register # Import the 'management' module within each installed app, to register
# dispatcher events. # dispatcher events.
for app_name in settings.INSTALLED_APPS: for app_name in cache.installed_apps:
try: try:
import_module('.management', app_name) import_module('.management', app_name)
except ImportError, exc: except ImportError, exc:

View File

@ -8,6 +8,7 @@ import sys
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.apps import cache
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader from django.template.loader import BaseLoader
from django.utils._os import safe_join from django.utils._os import safe_join
@ -16,11 +17,9 @@ from django.utils.importlib import import_module
# At compile time, cache the directories to search. # At compile time, cache the directories to search.
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
app_template_dirs = [] app_template_dirs = []
for app in settings.INSTALLED_APPS: for app in cache.installed_apps:
try: app_instance = cache.find_app(app)
mod = import_module(app) mod = app_instance.module
except ImportError, e:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')
if os.path.isdir(template_dir): if os.path.isdir(template_dir):
app_template_dirs.append(template_dir.decode(fs_encoding)) app_template_dirs.append(template_dir.decode(fs_encoding))