Python django.apps.apps.get_containing_app_config() Examples

The following are 10 code examples of django.apps.apps.get_containing_app_config(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.apps.apps , or try the search function .
Example #1
Source File: util.py    From donation-tracker with Apache License 2.0 5 votes vote down vote up
def app(self):
        return apps.get_containing_app_config(type(self).__module__).name 
Example #2
Source File: fields.py    From django-more with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def type_def(self, type_def):
        if isinstance(type_def, str):
            type_def = import_string(type_def)

        if not issubclass(type_def, self.type_def_subclass):
            raise TypeError('type_def must be a subclass of {}'.format(self.type_def_subclass))

        # Import meta options from type_def
        with suppress(AttributeError):
            self.type_name = type_def.Meta.db_type
        with suppress(AttributeError):
            self.type_app_label = type_def.Meta.app_label

        if not self.type_app_label:
            # Determine app_label of enum being used
            app_config = apps.get_containing_app_config(type_def.__module__)
            if app_config is None:
                raise RuntimeError(
                    "type_def class doesn't declare an explicit app_label, and isn't"
                    " in an application in INSTALLED_APPS")
            self.type_app_label = app_config.label

        # Generate type_name if not already set
        if not self.type_name:
            self.type_name = '{app_label}_{type_subclass}_{type_name}'.format(
                app_label=self.type_app_label,
                type_subclass=self.type_def_subclass.__name__.lower(),
                type_name=type_def.__qualname__.lower())

        self._type_def = type_def 
Example #3
Source File: utils.py    From karrot-backend with GNU Affero General Public License v3.0 5 votes vote down vote up
def app(self):
        return apps.get_containing_app_config(type(self).__module__).name 
Example #4
Source File: test_classes.py    From zulip with Apache License 2.0 5 votes vote down vote up
def app(self) -> str:
        return apps.get_containing_app_config(type(self).__module__).name 
Example #5
Source File: helper.py    From byro with Apache License 2.0 5 votes vote down vote up
def app(self):
        return apps.get_containing_app_config(type(self).__module__).name 
Example #6
Source File: test_migrations.py    From cadasta-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
def app(self):
        return apps.get_containing_app_config(type(self).__module__).name 
Example #7
Source File: views.py    From django-admin-reports with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, report):
        self._report = report
        module = self._report.__class__.__module__
        app_config = apps.get_containing_app_config(module)
        self._app_label = app_config.label
        self._object_name = self._report.__class__.__name__ 
Example #8
Source File: sites.py    From django-admin-reports with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_urls(self):
        urlpatterns = []

        for report in self._registry:
            app_name = apps.get_containing_app_config(report.__module__).name
            urlpatterns.append(
                url(r"^{0}/{1}/$".format(app_name.replace(".", "_"), report.__name__.lower()),
                    admin_site.admin_view(ReportView.as_view(report_class=report)),
                    name=camel_re.sub(r'\1_\2', report.__name__).lower()
                ))
        return urlpatterns 
Example #9
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_containing_app_config_apps_not_ready(self):
        """
        apps.get_containing_app_config() should raise an exception if
        apps.apps_ready isn't True.
        """
        apps.apps_ready = False
        try:
            with self.assertRaisesMessage(AppRegistryNotReady, "Apps aren't loaded yet"):
                apps.get_containing_app_config('foo')
        finally:
            apps.apps_ready = True 
Example #10
Source File: __init__.py    From django-cassandra-engine with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def _add_django_meta_and_register_model(cls, klass, attrs, name):
        # Create the class.
        module = attrs.get('__module__')
        if not module:
            return klass

        new_class = klass
        attr_meta = attrs.pop('Meta', None)
        abstract = getattr(attr_meta, 'abstract', False)
        if not attr_meta:
            meta = getattr(new_class, 'Meta', None)
        else:
            meta = attr_meta

        if meta:
            meta.managed = False

        app_label = None

        # Look for an application configuration to attach the model to.
        app_config = apps.get_containing_app_config(module)

        if getattr(meta, 'app_label', None) is None:
            if app_config is None:
                if not abstract:
                    raise RuntimeError(
                        "Model class %s.%s doesn't declare an explicit "
                        "app_label and isn't in an application in "
                        "INSTALLED_APPS." % (module, name)
                    )

            else:
                app_label = app_config.label

        # Add _meta/Options attribute to the model
        new_class.add_to_class(
            '_meta', DjangoCassandraOptions(meta, app_label, cls=new_class))
        # Add manager to the model
        for manager_attr in _django_manager_attr_names:
            new_class.add_to_class(manager_attr, new_class.objects)
        # Register the model
        new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
        return new_class