Django

Code

Changeset 1716

Show
Ignore:
Timestamp:
12/17/05 09:39:22 (3 years ago)
Author:
rjwittams
Message:

magic-removal:Management fixes. Everything works on app labels, and constraints generation is correct for mutually referential models.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/contrib/admin/views/main.py

    r1700 r1716  
    4545ADMIN_PREFIX = "/admin/" 
    4646 
    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._meta 
    54     if not opts.admin: 
    55         raise Http404 # This object is valid but has no admin interface. 
    56     return mod, opts 
     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._meta 
     54#    if not opts.admin: 
     55#        raise Http404 # This object is valid but has no admin interface. 
     56#    return mod, opts 
    5757 
    5858def matches_app(mod, comps): 
     
    8686 
    8787def get_app_label(mod): 
     88    #HACK 
    8889    modcomps = mod.__name__.split('.') 
    8990    return modcomps[-2] 
  • django/branches/magic-removal/django/core/management.py

    r1712 r1716  
    6464get_rel_data_type = lambda f: (f.get_internal_type() == 'AutoField') and 'IntegerField' or f.get_internal_type() 
    6565 
    66 def get_sql_create(mod): 
    67     "Returns a list of the CREATE TABLE SQL statements for the given module." 
     66def get_sql_create(app): 
     67    "Returns a list of the CREATE TABLE SQL statements for the given app." 
    6868    from django.db import backend, get_creation_module, models 
    6969 
    7070    data_types = get_creation_module().DATA_TYPES 
    7171    final_output = [] 
    72     opts_output = set() 
     72    models_output = set() 
    7373    pending_references = {} 
    7474 
    75     for klass in mod._MODELS: 
     75    app_models = models.get_models(app) 
     76 
     77    for klass in app_models: 
    7678        opts = klass._meta 
    7779        table_output = [] 
     
    9395                    field_output.append('PRIMARY KEY') 
    9496                if f.rel: 
    95                      if f.rel.to in opts_output: 
     97                     if f.rel.to in models_output: 
    9698                         field_output.append('REFERENCES %s (%s)' % \ 
    9799                             (backend.quote_name(f.rel.to._meta.db_table), 
     
    100102                         # We haven't yet created the table to which this field 
    101103                         # 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)) 
    103105                table_output.append(' '.join(field_output)) 
    104106        if opts.order_with_respect_to: 
     
    116118        # Take care of any ALTER TABLE statements to add constraints 
    117119        # 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 
    120123                r_table = rel_opts.db_table 
    121124                r_col = f.column 
     
    126129                    backend.quote_name("%s_referencing_%s_%s" % (r_col, table, col)), 
    127130                    backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))) 
    128             del pending_references[opts] 
     131            del pending_references[klass] 
    129132 
    130133        # Keep track of the fact that we've created the table for this model. 
    131         opts_output.add(opts) 
     134        models_output.add(klass) 
    132135 
    133136    # Create the many-to-many join tables. 
    134     for klass in mod._MODELS
     137    for klass in app_models
    135138        opts = klass._meta 
    136139        for f in opts.many_to_many: 
     
    153156            final_output.append('\n'.join(table_output)) 
    154157    return final_output 
    155 get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given model module name(s)." 
     158get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app name(s)." 
    156159get_sql_create.args = APP_ARGS 
    157160 
    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 
     161def 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 
    161164 
    162165    try: 
     
    184187 
    185188    references_to_delete = {} 
    186     for klass in mod._MODELS: 
     189    app_models = models.get_models(app) 
     190    for klass in app_models: 
    187191        try: 
    188192            if cursor is not None: 
     
    196200            for f in opts.fields: 
    197201                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: 
    205207         try: 
    206208             if cursor is not None: 
     
    212214         else: 
    213215             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 
    216219                     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 
    220222                     output.append('ALTER TABLE %s DROP CONSTRAINT %s;' % \ 
    221223                        (backend.quote_name(table), 
    222224                         backend.quote_name("%s_referencing_%s_%s" % (col, r_table, r_col)))) 
     225                 del references_to_delete[klass] 
    223226 
    224227    # Output DROP TABLE statements for many-to-many tables. 
    225     for klass in mod._MODELS
     228    for klass in app_models
    226229        opts = klass._meta 
    227230        for f in opts.many_to_many: 
     
    234237                output.append("DROP TABLE %s;" % backend.quote_name(f.get_m2m_db_table(opts))) 
    235238 
    236     app_label = mod._MODELS[0]._meta.app_label 
     239    app_label = app_models[0]._meta.app_label 
    237240 
    238241    # Delete from packages, auth_permissions, content_types. 
     
    260263 
    261264    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 module name(s)." 
     265get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app name(s)." 
    263266get_sql_delete.args = APP_ARGS 
    264267 
    265 def get_sql_reset(mod): 
     268def get_sql_reset(app): 
    266269    "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 module name(s)." 
     270    return get_sql_delete(app) + get_sql_all(app
     271get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." 
    269272get_sql_reset.args = APP_ARGS 
    270273 
    271 def get_sql_initial_data(mod): 
    272     "Returns a list of the initial INSERT SQL statements for the given module." 
     274def get_sql_initial_data(app): 
     275    "Returns a list of the initial INSERT SQL statements for the given app." 
    273276    from django.conf.settings import DATABASE_ENGINE 
     277    from django.db.models import get_models 
    274278    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 
    276282    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: 
    279286        opts = klass._meta 
    280287 
    281288        # Add custom SQL, if it's available. 
     289        # FIXME: THis probably needs changing 
    282290        sql_files = [os.path.join(app_dir, opts.module_name + '.' + DATABASE_ENGINE +  '.sql'), 
    283291                     os.path.join(app_dir, opts.module_name + '.sql')] 
     
    294302            output.append(_get_permission_insert(name, codename, opts)) 
    295303    return output 
    296 get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given model module name(s)." 
     304get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)." 
    297305get_sql_initial_data.args = APP_ARGS 
    298306 
    299 def get_sql_sequence_reset(mod): 
    300     "Returns a list of the SQL statements to reset PostgreSQL sequences for the given module." 
     307 
     308def get_sql_sequence_reset(app): 
     309    "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app." 
    301310    from django.db import backend, models 
    302311    output = [] 
    303     for klass in mod._MODELS
     312    for klass in models.get_models(app)
    304313        for f in klass._meta.fields: 
    305314            if isinstance(f, models.AutoField): 
     
    311320                (f.get_m2m_db_table(klass._meta), backend.quote_name('id'), f.get_m2m_db_table(klass._meta))) 
    312321    return output 
    313 get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given model module name(s)." 
     322get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)." 
    314323get_sql_sequence_reset.args = APP_ARGS 
    315324 
    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 
     325def 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 
    319328    output = [] 
    320     for klass in mod._MODELS: 
     329     
     330    for klass in models.get_models(app): 
    321331        for f in klass._meta.fields: 
    322332            if f.db_index: 
     
    329339get_sql_indexes.args = APP_ARGS 
    330340 
    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
     341def 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
    334344get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given model module name(s)." 
    335345get_sql_all.args = APP_ARGS 
     
    344354    return cursor.rowcount < 1 
    345355 
    346 def database_check(mod): 
     356def database_check(app): 
    347357    "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 
    349359    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 
    351362 
    352363    # Check that the package exists in the database. 
     
    360371    perms_seen = {} 
    361372    contenttypes_seen = {} 
    362     for klass in mod._MODELS
     373    for klass in app_models
    363374        opts = klass._meta 
    364375        perms = _get_all_permissions(opts) 
     
    409420database_check.args = APP_ARGS 
    410421 
    411 def get_admin_index(mod): 
    412     "Returns admin-index template snippet (in list form) for the given module." 
     422def get_admin_index(app): 
     423    "Returns admin-index template snippet (in list form) for the given app." 
    413424    from django.utils.text import capfirst 
     425    from django.db.models import get_models 
    414426    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 
    416429    output.append('{%% if perms.%s %%}' % app_label) 
    417430    output.append('<div class="module"><h2>%s</h2><table>' % app_label.title()) 
     
    428441    output.append('{% endif %}') 
    429442    return output 
    430 get_admin_index.help_doc = "Prints the admin-index template snippet for the given model module name(s)." 
     443get_admin_index.help_doc = "Prints the admin-index template snippet for the given app name(s)." 
    431444get_admin_index.args = APP_ARGS 
    432445 
     
    455468init.args = '' 
    456469 
    457 def install(mod): 
     470def install(app): 
    458471    "Executes the equivalent of 'get_sql_all' in the current database." 
    459472    from django.db import connection 
    460473    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     
    463477    # First, try validating the models. 
    464478    s = StringIO() 
     
    469483        sys.stderr.write(s.read()) 
    470484        sys.exit(1) 
    471     sql_list = get_sql_all(mod
     485    sql_list = get_sql_all(app
    472486 
    473487    try: 
     
    482496Hint: Look at the output of 'django-admin.py sqlall %s'. That's the SQL this command wasn't able to run. 
    483497The full error: %s\n""" % \ 
    484             (mod_name, mod_name, e)) 
     498            (app_name, app_label, e)) 
    485499        connection.rollback() 
    486500        sys.exit(1) 
    487501    connection.commit() 
    488 install.help_doc = "Executes ``sqlall`` for the given model module name(s) in the current database." 
     502install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database." 
    489503install.args = APP_ARGS 
    490504 
    491 def installperms(mod): 
    492     "Installs any permissions for the given model, if needed." 
     505def installperms(app): 
     506    "Installs any permissions for the given app, if needed." 
    493507    from django.models.auth import Permission 
    494508    from django.models.core import Package 
     509    from django.db.models import get_models 
    495510    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: 
    498515        opts = klass._meta 
    499516        for codename, name in _get_all_permissions(opts): 
  • django/branches/magic-removal/django/db/models/__init__.py

    r1715 r1716  
    1515from django.db.models.fields.related import * 
    1616 
    17 from django.core.exceptions import ObjectDoesNotExist 
     17from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 
    1818from django.db.models.exceptions import FieldDoesNotExist, BadKeywordArguments 
    1919from django.db.models.signals import Signals 
     20 
    2021 
    2122# Admin stages. 
     
    2627#    return __import__('%s.%s.%s' % (MODEL_PREFIX, app_label, module_name), '', '', ['']) 
    2728 
    28 #def get_app(app_label): 
    29 #    return __import__('%s.%s' % (MODEL_PREFIX, app_label), '', '', ['']) 
     29def get_models(app): 
     30    models = [] 
     31    get_models_helper(app, models) 
     32    return models 
     33 
     34def 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 
     42def 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 
    3051 
    3152class LazyDate: