def load_app(self, app, use_django=False): """ Load a new Django app into the project. app -- the module name string of the app to load. use_django -- if set to True, Django's built-in load_app function will be used. This should only be done before any syncdb calls have been issued, otherwise the models will not have DB tables. """ if not app or app in TestToolsCase._loaded_apps: return TestToolsCase._loaded_apps.append(app) # In case the new app will be overriding templates, make sure it # appears before the current app in INSTALLED_APPS installed_apps = list(settings.INSTALLED_APPS) try: # Find the best matching app name for the current module app_name = sorted( filter( lambda x: self.__module__.startswith(x), settings.INSTALLED_APPS ), lambda x,y: len(y).__cmp__(len(x)) )[0] # Insert new app before the current app in INSTALLED_APPS installed_apps.insert(installed_apps.index(app_name), app) except IndexError: # Current app not found, append new app to the end installed_apps.append(app) settings.INSTALLED_APPS = installed_apps # Load the app if use_django: django_load_app(app) else: apploader_load_app(app) # Check if app has a valid urls module and store it in the class's # _urls property, if it is not set already if not hasattr(self.__class__, '_urls') and not hasattr(self.__class__, 'urls'): url_module = '%s.urls' % app try: __import__(url_module) if hasattr(sys.modules[url_module], 'urlpatterns'): self.__class__._urls = url_module except ImportError: # If the app doesn't have a urls module, no need to load it pass self._refresh_cache()
def load_app(self, app, use_django=False): """ Load a new Django app into the project. app -- the module name string of the app to load. use_django -- if set to True, Django's built-in load_app function will be used. This should only be done before any syncdb calls have been issued, otherwise the models will not have DB tables. """ if not app or app in TestToolsCase._loaded_apps: return TestToolsCase._loaded_apps.append(app) # In case the new app will be overriding templates, make sure it # appears before the current app in INSTALLED_APPS installed_apps = list(settings.INSTALLED_APPS) try: # Find the best matching app name for the current module app_name = sorted( filter(lambda x: self.__module__.startswith(x), settings.INSTALLED_APPS), lambda x, y: len(y).__cmp__(len(x)))[0] # Insert new app before the current app in INSTALLED_APPS installed_apps.insert(installed_apps.index(app_name), app) except IndexError: # Current app not found, append new app to the end installed_apps.append(app) settings.INSTALLED_APPS = installed_apps # Load the app if use_django: django_load_app(app) else: apploader_load_app(app) # Check if app has a valid urls module and store it in the class's # _urls property, if it is not set already if not hasattr(self.__class__, '_urls') and not hasattr( self.__class__, 'urls'): url_module = '%s.urls' % app try: __import__(url_module) if hasattr(sys.modules[url_module], 'urlpatterns'): self.__class__._urls = url_module except ImportError: # If the app doesn't have a urls module, no need to load it pass self._refresh_cache()
def load_app(app_path): testapp = django_load_app(app_path) app_name = testapp.__name__.split('.')[-2] connection = connections[DEFAULT_DB_ALIAS] cursor = connection.cursor() test_models = [m for m in models.get_models(testapp, include_auto_created=True) if router.allow_syncdb(DEFAULT_DB_ALIAS, m)] loaded_models[app_path] = test_models # We assume the models haven't been installed, otherwise there's more to do here # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) pending_references = {} verbosity = 0 # Create the tables for each model for model in test_models: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print "Processing %s.%s model" % (app_name, model._meta.object_name) sql, references = connection.creation.sql_create_model(model, no_style(), seen_models) seen_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend(connection.creation.sql_for_pending_references(refto, no_style(), pending_references)) sql.extend(connection.creation.sql_for_pending_references(model, no_style(), pending_references)) if verbosity >= 1 and sql: print "Creating table %s" % model._meta.db_table for statement in sql: cursor.execute(statement) tables.append(connection.introspection.table_name_converter(model._meta.db_table)) transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS) for model in test_models: index_sql = connection.creation.sql_indexes_for_model(model, no_style()) if index_sql: if verbosity >= 1: print "Installing index for %s.%s model" % (app_name, model._meta.object_name) try: for sql in index_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed(using=DEFAULT_DB_ALIAS) else: transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS)
def load_app(app_path): testapp = django_load_app(app_path) app_name = testapp.__name__.split('.')[-2] connection = connections[DEFAULT_DB_ALIAS] cursor = connection.cursor() test_models = [ m for m in models.get_models(testapp, include_auto_created=True) if router.allow_syncdb(DEFAULT_DB_ALIAS, m) ] loaded_models[app_path] = test_models # We assume the models haven't been installed, otherwise there's more to do here # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) pending_references = {} verbosity = 0 # Create the tables for each model for model in test_models: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print "Processing %s.%s model" % (app_name, model._meta.object_name) sql, references = connection.creation.sql_create_model( model, no_style(), seen_models) seen_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( connection.creation.sql_for_pending_references( refto, no_style(), pending_references)) sql.extend( connection.creation.sql_for_pending_references( model, no_style(), pending_references)) if verbosity >= 1 and sql: print "Creating table %s" % model._meta.db_table for statement in sql: cursor.execute(statement) tables.append( connection.introspection.table_name_converter( model._meta.db_table)) transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS) for model in test_models: index_sql = connection.creation.sql_indexes_for_model( model, no_style()) if index_sql: if verbosity >= 1: print "Installing index for %s.%s model" % ( app_name, model._meta.object_name) try: for sql in index_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed(using=DEFAULT_DB_ALIAS) else: transaction.commit_unless_managed(using=DEFAULT_DB_ALIAS)