Esempio n. 1
0
def register_app(app_label, app):
    """Register a new app in the registry.

    This must be balanced with a :py:func:`unregister_app` call.

    Args:
        app_label (str):
            The label of the app.

        app (module):
            The app module.
    """
    if apps:
        # Django >= 1.7
        app_config = AppConfig(app.__name__, app)
        app_config.label = app_label
        app_config.models_module = app

        apps.set_installed_apps(settings.INSTALLED_APPS + [app_config])
    else:
        # Django < 1.7
        cache.app_store[app] = len(cache.app_store)

        if hasattr(cache, 'app_labels'):
            cache.app_labels[app_label] = app
Esempio n. 2
0
def setup():
    DB_FILE = NAME + '.db'
    with open(DB_FILE, 'w'):
        pass  # wipe the database
    settings.configure(DEBUG=True,
                       DATABASES={
                           DEFAULT_DB_ALIAS: {
                               'ENGINE': 'django.db.backends.sqlite3',
                               'NAME': DB_FILE
                           }
                       },
                       LOGGING={
                           'version': 1,
                           'disable_existing_loggers': False,
                           'formatters': {
                               'debug': {
                                   'format': '%(asctime)s[%(levelname)s]'
                                   '%(name)s.%(funcName)s(): %(message)s',
                                   'datefmt': '%Y-%m-%d %H:%M:%S'
                               }
                           },
                           'handlers': {
                               'console': {
                                   'level': 'DEBUG',
                                   'class': 'logging.StreamHandler',
                                   'formatter': 'debug'
                               }
                           },
                           'root': {
                               'handlers': ['console'],
                               'level': 'WARN'
                           },
                           'loggers': {
                               "django.db": {
                                   "level": "WARN"
                               }
                           }
                       })
    app_config = AppConfig(NAME, sys.modules['__main__'])
    apps.populate([app_config])
    django.setup()
    original_new_func = ModelBase.__new__

    @staticmethod
    def patched_new(cls, name, bases, attrs):
        if 'Meta' not in attrs:

            class Meta:
                app_label = NAME

            attrs['Meta'] = Meta
        return original_new_func(cls, name, bases, attrs)

    ModelBase.__new__ = patched_new