Changeset 1716
- Timestamp:
- 12/17/05 09:39:22 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/contrib/admin/views/main.py
r1700 r1716 45 45 ADMIN_PREFIX = "/admin/" 46 46 47 def _get_mod_opts(app_label, module_name):48 "Helper function that returns a tuple of (module, opts), raising Http404 if necessary."49 try:50 mod = models.get_app(app_label)51 except ImportError:52 raise Http404 # Invalid app or module name. Maybe it's not in INSTALLED_APPS.53 opts = mod.Klass._meta54 if not opts.admin:55 raise Http404 # This object is valid but has no admin interface.56 return mod, opts47 #def _get_mod_opts(app_label, module_name): 48 # "Helper function that returns a tuple of (module, opts), raising Http404 if necessary." 49 # try: 50 # mod = models.get_app(app_label) 51 # except ImportError: 52 # raise Http404 # Invalid app or module name. Maybe it's not in INSTALLED_APPS. 53 # opts = mod.Klass._meta 54 # if not opts.admin: 55 # raise Http404 # This object is valid but has no admin interface. 56 # return mod, opts 57 57 58 58 def matches_app(mod, comps): … … 86 86 87 87 def get_app_label(mod): 88 #HACK 88 89 modcomps = mod.__name__.split('.') 89 90 return modcomps[-2] django/branches/magic-removal/django/core/management.py
r1712 r1716 64 64 get_rel_data_type = lambda f: (f.get_internal_type() == 'AutoField') and 'IntegerField' or f.get_internal_type() 65 65 66 def get_sql_create( mod):67 "Returns a list of the CREATE TABLE SQL statements for the given module."66 def get_sql_create(app): 67 "Returns a list of the CREATE TABLE SQL statements for the given app." 68 68 from django.db import backend, get_creation_module, models 69 69 70 70 data_types = get_creation_module().DATA_TYPES 71 71 final_output = [] 72 opts_output = set()72 models_output = set() 73 73 pending_references = {} 74 74 75 for klass in mod._MODELS: 75 app_models = models.get_models(app) 76 77 for klass in app_models: 76 78 opts = klass._meta 77 79 table_output = [] … … 93 95 field_output.append('PRIMARY KEY') 94 96 if f.rel: 95 if f.rel.to in opts_output:97 if f.rel.to in models_output: 96 98 field_output.append('REFERENCES %s (%s)' % \ 97 99 (backend.quote_name(f.rel.to._meta.db_table), … … 100 102 # We haven't yet created the table to which this field 101 103 # is related, so save it for later. 102 pr = pending_references.setdefault(f.rel.to ._meta, []).append((opts, f))104 pr = pending_references.setdefault(f.rel.to, []).append((klass, f)) 103 105 table_output.append(' '.join(field_output)) 104 106 if opts.order_with_respect_to: … … 116 118 # Take care of any ALTER TABLE statements to add constraints 117 119 # after the fact. 118 if opts in pending_references: 119 for rel_opts, f in pending_references[opts]: 120 if klass in pending_references: 121 for rel_class, f in pending_references[klass]: 122 rel_opts = rel_class._meta 120 123 r_table = rel_opts.db_table 121 124 r_col = f.column … … 126 129 backend.quote_name("%s_referencing_%s_%s" % (r_col, table, col)), 127 130 backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))) 128 del pending_references[ opts]131 del pending_references[klass] 129 132 130 133 # Keep track of the fact that we've created the table for this model. 131 opts_output.add(opts)134 models_output.add(klass) 132 135 133 136 # Create the many-to-many join tables. 134 for klass in mod._MODELS:137 for klass in app_models: 135 138 opts = klass._meta 136 139 for f in opts.many_to_many: … … 153 156 final_output.append('\n'.join(table_output)) 154 157 return final_output 155 get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given model modulename(s)."158 get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)." 156 159 get_sql_create.args = APP_ARGS 157 160 158 def get_sql_delete( mod):159 "Returns a list of the DROP TABLE SQL statements for the given module."160 from django.db import backend, connection 161 def get_sql_delete(app): 162 "Returns a list of the DROP TABLE SQL statements for the given app." 163 from django.db import backend, connection, models 161 164 162 165 try: … … 184 187 185 188 references_to_delete = {} 186 for klass in mod._MODELS: 189 app_models = models.get_models(app) 190 for klass in app_models: 187 191 try: 188 192 if cursor is not None: … … 196 200 for f in opts.fields: 197 201 if f.rel and f.rel.to not in to_delete: 198 refs = references_to_delete.get(f.rel.to, []) 199 refs.append( (opts, f) ) 200 references_to_delete[f.rel.to] = refs 201 202 to_delete.add(opts) 203 204 for klass in mod._MODELS: 202 references_to_delete.setdefault(f.rel.to, []).append( (klass, f) ) 203 204 to_delete.add(klass) 205 206 for klass in app_models: 205 207 try: 206 208 if cursor is not None: … … 212 214 else: 213 215 output.append("DROP TABLE %s;" % backend.quote_name(klass._meta.db_table)) 214 if references_to_delete.has_key(klass._meta): 215 for opts, f in references_to_delete[klass._meta]: 216 if references_to_delete.has_key(klass): 217 for rel_class, f in references_to_delete[klass]: 218 table = rel_class._meta.db_table 216 219 col = f.column 217 table = opts.db_table 218 r_table = f.rel.to._meta.db_table 219 r_col = f.rel.to.get_field(f.rel.field_name).column 220 r_table = klass._meta.db_table 221 r_col = klass._meta.get_field(f.rel.field_name).column 220 222 output.append('ALTER TABLE %s DROP CONSTRAINT %s;' % \ 221 223 (backend.quote_name(table), 222 224 backend.quote_name("%s_referencing_%s_%s" % (col, r_table, r_col)))) 225 del references_to_delete[klass] 223 226 224 227 # Output DROP TABLE statements for many-to-many tables. 225 for klass in mod._MODELS:228 for klass in app_models: 226 229 opts = klass._meta 227 230 for f in opts.many_to_many: … … 234 237 output.append("DROP TABLE %s;" % backend.quote_name(f.get_m2m_db_table(opts))) 235 238 236 app_label = mod._MODELS[0]._meta.app_label239 app_label = app_models[0]._meta.app_label 237 240 238 241 # Delete from packages, auth_permissions, content_types. … … 260 263 261 264 return output[::-1] # Reverse it, to deal with table dependencies. 262 get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given model modulename(s)."265 get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app name(s)." 263 266 get_sql_delete.args = APP_ARGS 264 267 265 def get_sql_reset( mod):268 def get_sql_reset(app): 266 269 "Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module." 267 return get_sql_delete( mod) + get_sql_all(mod)268 get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given model modulename(s)."270 return get_sql_delete(app) + get_sql_all(app) 271 get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." 269 272 get_sql_reset.args = APP_ARGS 270 273 271 def get_sql_initial_data( mod):272 "Returns a list of the initial INSERT SQL statements for the given module."274 def get_sql_initial_data(app): 275 "Returns a list of the initial INSERT SQL statements for the given app." 273 276 from django.conf.settings import DATABASE_ENGINE 277 from django.db.models import get_models 274 278 output = [] 275 app_label = mod._MODELS[0]._meta.app_label 279 280 app_models = get_models(app) 281 app_label = app_models[0]._meta.app_label 276 282 output.append(_get_packages_insert(app_label)) 277 app_dir = os.path.normpath(os.path.join(os.path.dirname(mod.__file__), '..', 'sql')) 278 for klass in mod._MODELS: 283 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 284 285 for klass in app_models: 279 286 opts = klass._meta 280 287 281 288 # Add custom SQL, if it's available. 289 # FIXME: THis probably needs changing 282 290 sql_files = [os.path.join(app_dir, opts.module_name + '.' + DATABASE_ENGINE + '.sql'), 283 291 os.path.join(app_dir, opts.module_name + '.sql')] … … 294 302 output.append(_get_permission_insert(name, codename, opts)) 295 303 return output 296 get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given model modulename(s)."304 get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)." 297 305 get_sql_initial_data.args = APP_ARGS 298 306 299 def get_sql_sequence_reset(mod): 300 "Returns a list of the SQL statements to reset PostgreSQL sequences for the given module." 307 308 def get_sql_sequence_reset(app): 309 "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app." 301 310 from django.db import backend, models 302 311 output = [] 303 for klass in mod ._MODELS:312 for klass in models.get_models(app): 304 313 for f in klass._meta.fields: 305 314 if isinstance(f, models.AutoField): … … 311 320 (f.get_m2m_db_table(klass._meta), backend.quote_name('id'), f.get_m2m_db_table(klass._meta))) 312 321 return output 313 get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given model modulename(s)."322 get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)." 314 323 get_sql_sequence_reset.args = APP_ARGS 315 324 316 def get_sql_indexes( mod):317 "Returns a list of the CREATE INDEX SQL statements for the given module."318 from django.db import backend 325 def get_sql_indexes(app): 326 "Returns a list of the CREATE INDEX SQL statements for the given app." 327 from django.db import backend, models 319 328 output = [] 320 for klass in mod._MODELS: 329 330 for klass in models.get_models(app): 321 331 for f in klass._meta.fields: 322 332 if f.db_index: … … 329 339 get_sql_indexes.args = APP_ARGS 330 340 331 def get_sql_all( mod):332 "Returns a list of CREATE TABLE SQL and initial-data insert for the given module."333 return get_sql_create( mod) + get_sql_initial_data(mod)341 def get_sql_all(app): 342 "Returns a list of CREATE TABLE SQL and initial-data insert for the given app." 343 return get_sql_create(app) + get_sql_initial_data(app) 334 344 get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given model module name(s)." 335 345 get_sql_all.args = APP_ARGS … … 344 354 return cursor.rowcount < 1 345 355 346 def database_check( mod):356 def database_check(app): 347 357 "Checks that everything is properly installed in the database for the given module." 348 from django.db import backend, connection 358 from django.db import backend, connection, models 349 359 cursor = connection.cursor() 350 app_label = mod._MODELS[0]._meta.app_label 360 app_modules = models.get_models(app) 361 app_label = app_modules[0]._meta.app_label 351 362 352 363 # Check that the package exists in the database. … … 360 371 perms_seen = {} 361 372 contenttypes_seen = {} 362 for klass in mod._MODELS:373 for klass in app_models: 363 374 opts = klass._meta 364 375 perms = _get_all_permissions(opts) … … 409 420 database_check.args = APP_ARGS 410 421 411 def get_admin_index( mod):412 "Returns admin-index template snippet (in list form) for the given module."422 def get_admin_index(app): 423 "Returns admin-index template snippet (in list form) for the given app." 413 424 from django.utils.text import capfirst 425 from django.db.models import get_models 414 426 output = [] 415 app_label = mod._MODELS[0]._meta.app_label 427 app_models = get_models(app) 428 app_label = app_models[0]._meta.app_label 416 429 output.append('{%% if perms.%s %%}' % app_label) 417 430 output.append('<div class="module"><h2>%s</h2><table>' % app_label.title()) … … 428 441 output.append('{% endif %}') 429 442 return output 430 get_admin_index.help_doc = "Prints the admin-index template snippet for the given model modulename(s)."443 get_admin_index.help_doc = "Prints the admin-index template snippet for the given app name(s)." 431 444 get_admin_index.args = APP_ARGS 432 445 … … 455 468 init.args = '' 456 469 457 def install( mod):470 def install(app): 458 471 "Executes the equivalent of 'get_sql_all' in the current database." 459 472 from django.db import connection 460 473 from cStringIO import StringIO 461 mod_name = mod.__name__[mod.__name__.rindex('.')+1:] 462 474 app_name = app.__name__[app.__name__.rindex('.')+1:] 475 app_label = app_name.split('.')[-1] 476 463 477 # First, try validating the models. 464 478 s = StringIO() … … 469 483 sys.stderr.write(s.read()) 470 484 sys.exit(1) 471 sql_list = get_sql_all( mod)485 sql_list = get_sql_all(app) 472 486 473 487 try: … … 482 496 Hint: Look at the output of 'django-admin.py sqlall %s'. That's the SQL this command wasn't able to run. 483 497 The full error: %s\n""" % \ 484 ( mod_name, mod_name, e))498 (app_name, app_label, e)) 485 499 connection.rollback() 486 500 sys.exit(1) 487 501 connection.commit() 488 install.help_doc = "Executes ``sqlall`` for the given model module name(s) in the current database."502 install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database." 489 503 install.args = APP_ARGS 490 504 491 def installperms( mod):492 "Installs any permissions for the given model, if needed."505 def installperms(app): 506 "Installs any permissions for the given app, if needed." 493 507 from django.models.auth import Permission 494 508 from django.models.core import Package 509 from django.db.models import get_models 495 510 num_added = 0 496 package = Package.objects.get_object(pk=mod._MODELS[0]._meta.app_label) 497 for klass in mod._MODELS: 511 app_models = get_models(app) 512 app_label = app_models[0]._meta.app_label 513 package = Package.objects.get_object(pk=app_label) 514 for klass in app_models: 498 515 opts = klass._meta 499 516 for codename, name in _get_all_permissions(opts): django/branches/magic-removal/django/db/models/__init__.py
r1715 r1716 15 15 from django.db.models.fields.related import * 16 16 17 from django.core.exceptions import ObjectDoesNotExist 17 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 18 18 from django.db.models.exceptions import FieldDoesNotExist, BadKeywordArguments 19 19 from django.db.models.signals import Signals 20 20 21 21 22 # Admin stages. … … 26 27 # return __import__('%s.%s.%s' % (MODEL_PREFIX, app_label, module_name), '', '', ['']) 27 28 28 #def get_app(app_label): 29 # return __import__('%s.%s' % (MODEL_PREFIX, app_label), '', '', ['']) 29 def get_models(app): 30 models = [] 31 get_models_helper(app, models) 32 return models 33 34 def get_models_helper(mod, seen_models): 35 if hasattr(mod, '_MODELS'): 36 seen_models.extend(mod._MODELS) 37 if hasattr(mod, '__all__'): 38 for name in mod.__all__: 39 sub_mod = __import__("%s.%s" % (mod.__name__, name), '','',['']) 40 get_models_helper(sub_mod, seen_models) 41 42 def get_app(app_label): 43 44 for app_name in settings.INSTALLED_APPS: 45 comps = app_name.split('.') 46 if app_label == comps[-1]: 47 app_models = __import__('%s.models' % app_name , '','',['']) 48 return app_models 49 50 raise ImproperlyConfigured, "App with label %s could not be found" % app_name 30 51 31 52 class LazyDate:
