Ticket #1332: management.py+get_validation_errors+core_modules_validation_on_install.patch

File management.py+get_validation_errors+core_modules_validation_on_install.patch, 3.0 KB (added by jakamkon@…, 19 years ago)
  • django_src/django/core/meta/__init__.py

     
    117117                _installed_modules_cache.append(mod)
    118118    return _installed_modules_cache
    119119
     120_core_model_modules = []
     121def get_core_model_modules():
     122    " Returns a list of the core models modules."
     123
     124    from django.models import __all__
     125    for cm in __all__:
     126        _core_model_modules.append(__import__('django.models.%s' % cm, '', '',['']))
     127    return _core_model_modules
     128       
     129   
    120130class LazyDate:
    121131    """
    122132    Use in limit_choices_to to compare the field to dates calculated at run time
  • django_src/django/core/management.py

     
    397397def install(mod):
    398398    "Executes the equivalent of 'get_sql_all' in the current database."
    399399    from django.core import db
     400    from django.core.meta import get_core_model_modules
    400401    from cStringIO import StringIO
    401402    mod_name = mod.__name__[mod.__name__.rindex('.')+1:]
    402403
    403404    # First, try validating the models.
    404     s = StringIO()
    405     num_errors = get_validation_errors(s)
    406     if num_errors:
    407         sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % mod_name)
    408         s.seek(0)
    409         sys.stderr.write(s.read())
    410         sys.exit(1)
    411     sql_list = get_sql_all(mod)
     405    module_list = []
     406    module_list.extend(get_core_model_modules())
     407    module_list.append(mod)
     408    for m in module_list:
     409        mn = m.__name__[m.__name__.rindex('.')+1:]
     410        s = StringIO()
     411        num_errors = get_validation_errors(s, selected_mod=m)
     412        if num_errors:
     413            sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % mn)
     414            s.seek(0)
     415            sys.stderr.write(s.read())
     416            sys.exit(1)
     417        sql_list = get_sql_all(m)
    412418
    413419    try:
    414420        cursor = db.db.cursor()
     
    646652        self.errors.append((opts, error))
    647653        self.outfile.write("%s.%s: %s\n" % (opts.app_label, opts.module_name, error))
    648654
    649 def get_validation_errors(outfile):
    650     "Validates all installed models. Writes errors, if any, to outfile. Returns number of errors."
     655def get_validation_errors(outfile, selected_mod=None):
     656    "Validates all installed models or models from 'selected_mod'.Writes errors, if any, to outfile.Returns number of errors."
     657
    651658    import django.models
    652659    from django.core import meta
    653660    e = ModelErrorCollection(outfile)
    654     module_list = meta.get_installed_model_modules()
     661    if not selected_mod:
     662        module_list = meta.get_installed_model_modules()
     663    else:
     664        module_list = selected_mod,       
    655665    for module in module_list:
    656666        for mod in module._MODELS:
    657667            opts = mod._meta
Back to Top