Ticket #3591: custom-app-labels.diff
File custom-app-labels.diff, 20.6 KB (added by , 18 years ago) |
---|
-
django/test/client.py
167 167 if response.cookies: 168 168 self.cookies.update(response.cookies) 169 169 170 if 'django.contrib.sessions' in settings.INSTALLED_APPS:170 if 'django.contrib.sessions' in [app.path for app in settings.INSTALLED_APPS]: 171 171 from django.contrib.sessions.middleware import SessionWrapper 172 172 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 173 173 if cookie: -
django/db/models/base.py
8 8 from django.db.models.options import Options, AdminOptions 9 9 from django.db import connection, backend, transaction 10 10 from django.db.models import signals 11 from django.db.models.loading import register_models, get_model 11 from django.db.models.loading import register_models, get_model, get_app_label 12 12 from django.dispatch import dispatcher 13 13 from django.utils.datastructures import SortedDict 14 14 from django.utils.functional import curry … … 42 42 if getattr(new_class._meta, 'app_label', None) is None: 43 43 # Figure out the app_label by looking one level up. 44 44 # For 'django.contrib.sites.models', this would be 'sites'. 45 new_class._meta.app_label = model_module.__name__.split('.')[-2] 45 new_class._meta.app_label = get_app_label(new_class) 46 #new_class._meta.app_label = model_module.__name__.split('.')[-2] 46 47 47 48 # Bail out early if we have already created this class. 48 49 m = get_model(new_class._meta.app_label, name, False) -
django/db/models/options.py
36 36 37 37 def contribute_to_class(self, cls, name): 38 38 cls._meta = self 39 self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS39 self.installed = re.sub('\.models$', '', cls.__module__) in [app.path for app in settings.INSTALLED_APPS] 40 40 # First, construct the default values for these options. 41 41 self.object_name = cls.__name__ 42 42 self.module_name = self.object_name.lower() -
django/db/models/loading.py
1 1 "Utilities for loading models and the modules that contain them." 2 2 3 from django.conf import settings 3 from django.conf import settings, directives 4 4 from django.core.exceptions import ImproperlyConfigured 5 5 import sys 6 6 import os … … 24 24 global _loaded 25 25 if not _loaded: 26 26 _loaded = True 27 for app _namein settings.INSTALLED_APPS:27 for app in settings.INSTALLED_APPS: 28 28 try: 29 load_app(app _name)29 load_app(app.path) 30 30 except Exception, e: 31 31 # Problem importing the app 32 _app_errors[app _name] = e32 _app_errors[app.path] = e 33 33 return _app_list 34 34 35 35 def get_app(app_label, emptyOK=False): 36 36 "Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None." 37 37 get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. 38 for app _namein settings.INSTALLED_APPS:39 if app_label == app _name.split('.')[-1]:40 mod = load_app(app _name)38 for app in settings.INSTALLED_APPS: 39 if app_label == app.app_label: 40 mod = load_app(app.path) 41 41 if mod is None: 42 42 if emptyOK: 43 43 return None … … 61 61 get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. 62 62 return _app_errors 63 63 64 def get_models(app _mod=None):64 def get_models(app=None): 65 65 """ 66 Given a module containing models, returns a list of the models. Otherwise67 returns a list of all installed models.66 Given a app config directive, returns a list of the models in that app. 67 Otherwise returns a list of all installed models. 68 68 """ 69 69 app_list = get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. 70 if app_mod: 71 return _app_models.get(app_mod.__name__.split('.')[-2], {}).values() 70 if app: 71 if isinstance(app, str): 72 print app 73 app = app_for_label(app) 74 print app 75 return _app_models.get(app.app_label, {}).values() 72 76 else: 73 77 model_list = [] 74 for app _mod in app_list:75 model_list.extend(get_models(app _mod))78 for app in settings.INSTALLED_APPS: 79 model_list.extend(get_models(app)) 76 80 return model_list 77 81 78 82 def get_model(app_label, model_name, seed_cache=True): … … 114 118 if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]: 115 119 continue 116 120 model_dict[model_name] = model 121 122 def get_app_label(model): 123 model_module_path = model.__module__ 124 model_module_path_list = model_module_path.split('.') 125 model_module_index = model_module_path_list.index('models') 126 app_module_path_list = model_module_path_list[:model_module_index] 127 app_module_path = '.'.join(app_module_path_list) 128 for app in settings.INSTALLED_APPS: 129 if app_module_path == app.path: 130 return app.app_label 131 # app is most likely not installed, but give it an app label using the old method 132 return model.__module__.split('.')[-2] 133 134 def app_for_path(path): 135 for app in settings.INSTALLED_APPS: 136 if path == app.path: 137 return app 138 # app is most likely not installed, but give it an app label using the old method 139 return directives.app(path.split('.')[-1]) 140 141 def app_for_label(app_label): 142 for app in settings.INSTALLED_APPS: 143 if app_label == app.app_label: 144 return app -
django/conf/__init__.py
8 8 9 9 import os 10 10 import time # Needed for Windows 11 from django.conf import global_settings 11 from django.conf import global_settings, directives 12 from django.utils.datastructures import SortedDict 12 13 13 14 ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" 14 15 … … 94 95 setattr(self, setting, setting_value) 95 96 96 97 # Expand entries in INSTALLED_APPS like "django.contrib.*" to a list 97 # of all those apps. 98 # of all those apps. Also, convert strings into app directives. 98 99 new_installed_apps = [] 99 100 for app in self.INSTALLED_APPS: 100 if app.endswith('.*'): 101 appdir = os.path.dirname(__import__(app[:-2], {}, {}, ['']).__file__) 102 for d in os.listdir(appdir): 103 if d.isalpha() and os.path.isdir(os.path.join(appdir, d)): 104 new_installed_apps.append('%s.%s' % (app[:-2], d)) 101 if isinstance(app, str): 102 if app.endswith('.*'): 103 appdir = os.path.dirname(__import__(app[:-2], {}, {}, ['']).__file__) 104 for d in os.listdir(appdir): 105 if d.isalpha() and os.path.isdir(os.path.join(appdir, d)): 106 new_app = directives.app('%s.%s' % (app[:-2], d)) 107 new_installed_apps.append(new_app) 108 else: 109 new_app = directives.app(app) 110 new_installed_apps.append(new_app) 105 111 else: 106 112 new_installed_apps.append(app) 107 113 self.INSTALLED_APPS = new_installed_apps -
django/conf/directives.py
1 class app(object): 2 """Configuration directive for specifying an app.""" 3 def __init__(self, path, app_label=None, verbose_name=None): 4 self.path = path 5 # if name isn't specified, get the last part of the python dotted path 6 self.app_label = app_label or path.split('.')[-1] 7 self.verbose_name = verbose_name or self.app_label 8 9 class backend(object): 10 """Configuration directive for specifying an authentication backend.""" 11 def __init__(self, path, name=None): 12 self.path = path 13 # if name isn't specified, get the last part of the python dotted path 14 self.name = name or path.split('.')[-1] -
django/core/management.py
56 56 def _get_installed_models(table_list): 57 57 "Gets a set of all models that are installed, given a list of existing tables" 58 58 from django.db import models 59 all_models = [] 60 for app in models.get_apps(): 61 for model in models.get_models(app): 62 all_models.append(model) 59 all_models = models.get_models() 63 60 return set([m for m in all_models if m._meta.db_table in table_list]) 64 61 65 62 def _get_table_list(): … … 363 360 364 361 def get_sql_initial_data(app): 365 362 "Returns a list of the initial INSERT SQL statements for the given app." 366 from django.db .models import get_models363 from django.db import models 367 364 output = [] 368 365 369 app_models = get_models(app) 370 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 366 app_models = models.get_models(app) 367 app_module = models.get_app(app.app_label) 368 app_dir = os.path.normpath(os.path.join(os.path.dirname(app_module.__file__), 'sql')) 371 369 372 370 for model in app_models: 373 371 output.extend(get_sql_initial_data_for_model(model)) … … 450 448 451 449 # Import the 'management' module within each installed app, to register 452 450 # dispatcher events. 453 for app _namein settings.INSTALLED_APPS:451 for app in settings.INSTALLED_APPS: 454 452 try: 455 __import__(app _name+ '.management', {}, {}, [''])453 __import__(app.path + '.management', {}, {}, ['']) 456 454 except ImportError: 457 455 pass 458 456 … … 469 467 created_models = set() 470 468 pending_references = {} 471 469 472 for app in models.get_apps():473 app_name = app. __name__.split('.')[-2]470 for app in settings.INSTALLED_APPS: 471 app_name = app.path 474 472 model_list = models.get_models(app) 475 473 for model in model_list: 476 474 # Create the model's database table, if it doesn't already exist. … … 503 501 504 502 # Send the post_syncdb signal, so individual apps can do whatever they need 505 503 # to do at this point. 506 for app in models.get_apps(): 507 app_name = app.__name__.split('.')[-2] 504 for app in settings.INSTALLED_APPS: 505 app_name = app.path 506 app_module = models.get_app(app.app_label) 508 507 if verbosity >= 2: 509 508 print "Running post-sync handlers for application", app_name 510 dispatcher.send(signal=signals.post_syncdb, sender=app ,509 dispatcher.send(signal=signals.post_syncdb, sender=app_module, 511 510 app=app, created_models=created_models, 512 511 verbosity=verbosity, interactive=interactive) 513 512 … … 530 529 transaction.commit_unless_managed() 531 530 532 531 # Install SQL indicies for all newly created models 533 for app in models.get_apps():534 app_name = app. __name__.split('.')[-2]532 for app in settings.INSTALLED_APPS: 533 app_name = app.path 535 534 for model in models.get_models(app): 536 535 if model in created_models: 537 536 index_sql = get_sql_indexes_for_model(model) … … 1402 1401 action_mapping[action](args[1:]) 1403 1402 else: 1404 1403 from django.db import models 1404 from django.conf import settings 1405 1405 validate(silent_success=True) 1406 1406 try: 1407 mod_list = [models.get_app(app_label) forapp_label in args[1:]]1407 app_list = [app for app in settings.INSTALLED_APPS if app.app_label in args[1:]] 1408 1408 except ImportError, e: 1409 1409 sys.stderr.write(style.ERROR("Error: %s. Are you sure your INSTALLED_APPS setting is correct?\n" % e)) 1410 1410 sys.exit(1) 1411 if not mod_list:1411 if not app_list: 1412 1412 parser.print_usage_and_exit() 1413 1413 if action not in NO_SQL_TRANSACTION: 1414 1414 print style.SQL_KEYWORD("BEGIN;") 1415 for mod in mod_list:1415 for app in app_list: 1416 1416 if action == 'reset': 1417 output = action_mapping[action]( mod, options.interactive)1417 output = action_mapping[action](app, options.interactive) 1418 1418 else: 1419 output = action_mapping[action]( mod)1419 output = action_mapping[action](app) 1420 1420 if output: 1421 1421 print '\n'.join(output) 1422 1422 if action not in NO_SQL_TRANSACTION: -
django/templatetags/__init__.py
1 1 from django.conf import settings 2 2 3 for a in settings.INSTALLED_APPS:3 for app in settings.INSTALLED_APPS: 4 4 try: 5 __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)5 __path__.extend(__import__(app.path + '.templatetags', {}, {}, ['']).__path__) 6 6 except ImportError: 7 7 pass -
django/views/i18n.py
104 104 packages = ['django.conf'] 105 105 if type(packages) in (str, unicode): 106 106 packages = packages.split('+') 107 packages = [p for p in packages if p == 'django.conf' or p in settings.INSTALLED_APPS]107 packages = [p for p in packages if p == 'django.conf' or p in [app.path for app in settings.INSTALLED_APPS]] 108 108 default_locale = to_locale(settings.LANGUAGE_CODE) 109 109 locale = to_locale(get_language()) 110 110 t = {} -
django/contrib/admin/templatetags/adminapplist.py
1 1 from django import template 2 2 from django.db.models import get_models 3 from django.conf import settings 3 4 4 5 register = template.Library() 5 6 … … 13 14 app_list = [] 14 15 user = context['user'] 15 16 16 for app in models.get_apps():17 for app in settings.INSTALLED_APPS: 17 18 # Determine the app_label. 18 19 app_models = get_models(app) 19 20 if not app_models: 20 21 continue 21 app_label = app _models[0]._meta.app_label22 app_label = app.app_label 22 23 23 24 has_module_perms = user.has_module_perms(app_label) 24 25 … … 49 50 model_list = [x for key, x in decorated] 50 51 51 52 app_list.append({ 52 'name': app _label.title(),53 'name': app.verbose_name.title(), 53 54 'has_module_perms': has_module_perms, 54 55 'models': model_list, 55 56 }) -
django/utils/translation/trans_real.py
161 161 if projectpath and os.path.isdir(projectpath): 162 162 res = _merge(projectpath) 163 163 164 for appname in settings.INSTALLED_APPS: 164 for app in settings.INSTALLED_APPS: 165 appname = app.path 165 166 p = appname.rfind('.') 166 167 if p >= 0: 167 app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])168 app_module = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:]) 168 169 else: 169 app = __import__(appname, {}, {}, [])170 app_module = __import__(appname, {}, {}, []) 170 171 171 apppath = os.path.join(os.path.dirname(app .__file__), 'locale')172 apppath = os.path.join(os.path.dirname(app_module.__file__), 'locale') 172 173 173 174 if os.path.isdir(apppath): 174 175 res = _merge(apppath) -
django/template/loaders/app_directories.py
8 8 # At compile time, cache the directories to search. 9 9 app_template_dirs = [] 10 10 for app in settings.INSTALLED_APPS: 11 i = app. rfind('.')11 i = app.path.rfind('.') 12 12 if i == -1: 13 m, a = app , None13 m, a = app.path, None 14 14 else: 15 m, a = app [:i], app[i+1:]15 m, a = app.path[:i], app.path[i+1:] 16 16 try: 17 17 if a is None: 18 18 mod = __import__(m, {}, {}, []) 19 19 else: 20 20 mod = getattr(__import__(m, {}, {}, [a]), a) 21 21 except ImportError, e: 22 raise ImproperlyConfigured, 'ImportError %s: %s' % (app , e.args[0])22 raise ImproperlyConfigured, 'ImportError %s: %s' % (app.path, e.args[0]) 23 23 template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') 24 24 if os.path.isdir(template_dir): 25 25 app_template_dirs.append(template_dir) -
django/template/loaders/eggs.py
18 18 pkg_name = 'templates/' + template_name 19 19 for app in settings.INSTALLED_APPS: 20 20 try: 21 return (resource_string(app , pkg_name), 'egg:%s:%s ' % (app, pkg_name))21 return (resource_string(app.path, pkg_name), 'egg:%s:%s ' % (app.path, pkg_name)) 22 22 except: 23 23 pass 24 24 raise TemplateDoesNotExist, template_name -
tests/runtests.py
2 2 3 3 import os, sys, traceback 4 4 import unittest 5 from django.conf import directives 5 6 6 7 MODEL_TESTS_DIR_NAME = 'modeltests' 7 8 REGRESSION_TESTS_DIR_NAME = 'regressiontests' … … 12 13 REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME) 13 14 14 15 ALWAYS_INSTALLED_APPS = [ 15 'django.contrib.contenttypes',16 'django.contrib.auth',17 'django.contrib.sites',18 'django.contrib.flatpages',19 'django.contrib.redirects',20 'django.contrib.sessions',21 'django.contrib.comments',22 'django.contrib.admin',16 directives.app('django.contrib.contenttypes'), 17 directives.app('django.contrib.auth'), 18 directives.app('django.contrib.sites'), 19 directives.app('django.contrib.flatpages'), 20 directives.app('django.contrib.redirects'), 21 directives.app('django.contrib.sessions'), 22 directives.app('django.contrib.comments'), 23 directives.app('django.contrib.admin'), 23 24 ] 24 25 25 26 def get_test_models(): … … 48 49 49 50 def runTest(self): 50 51 from django.core import management 51 from django.db.models.loading import load_app 52 from django.db.models.loading import load_app, app_for_path 52 53 from cStringIO import StringIO 53 54 54 55 try: 55 56 module = load_app(self.model_label) 57 app = app_for_path(self.model_label) 56 58 except Exception, e: 59 print self.model_label 60 print e 57 61 self.fail('Unable to load invalid model module') 58 62 59 63 s = StringIO() 60 count = management.get_validation_errors(s, module)64 count = management.get_validation_errors(s, app) 61 65 s.seek(0) 62 66 error_log = s.read() 63 67 actual = error_log.split('\n') … … 108 112 if not tests_to_run or model_name in tests_to_run: 109 113 if verbosity >= 1: 110 114 print "Importing model %s" % model_name 115 app = directives.app(model_label) 116 settings.INSTALLED_APPS.append(app) 111 117 mod = load_app(model_label) 112 settings.INSTALLED_APPS.append(model_label)113 118 test_models.append(mod) 114 119 except Exception, e: 115 120 sys.stderr.write("Error while importing %s:" % model_name + ''.join(traceback.format_exception(*sys.exc_info())[1:]))