Exemplo n.º 1
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps.
        from djangocg.db.models.loading import get_models
        get_models()

        use_plain = options.get('plain', False)
        interface = options.get('interface', None)

        try:
            if use_plain:
                # Don't bother loading IPython, because the user wants plain Python.
                raise ImportError

            self.run_shell(shell=interface)
        except ImportError:
            import code
            # Set up a dictionary to serve as the environment for the shell, so
            # that tab completion works on objects that are imported at runtime.
            # See ticket 5082.
            imported_objects = {}
            try:  # Try activating rlcompleter, because it's handy.
                import readline
            except ImportError:
                pass
            else:
                # We don't have to wrap the following import in a 'try', because
                # we already know 'readline' was imported successfully.
                import rlcompleter
                readline.set_completer(rlcompleter.Completer(imported_objects).complete)
                readline.parse_and_bind("tab:complete")

            # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
            # conventions and get $PYTHONSTARTUP first then .pythonrc.py.
            if not use_plain:
                for pythonrc in (os.environ.get("PYTHONSTARTUP"),
                                 os.path.expanduser('~/.pythonrc.py')):
                    if pythonrc and os.path.isfile(pythonrc):
                        try:
                            with open(pythonrc) as handle:
                                exec(compile(handle.read(), pythonrc, 'exec'))
                        except NameError:
                            pass
            code.interact(local=imported_objects)
Exemplo n.º 2
0
 def _fill_related_many_to_many_cache(self):
     cache = SortedDict()
     parent_list = self.get_parent_list()
     for parent in self.parents:
         for obj, model in parent._meta.get_all_related_m2m_objects_with_model():
             if obj.field.creation_counter < 0 and obj.model not in parent_list:
                 continue
             if not model:
                 cache[obj] = parent
             else:
                 cache[obj] = model
     for klass in get_models(only_installed=False):
         for f in klass._meta.local_many_to_many:
             if f.rel and not isinstance(f.rel.to, six.string_types) and self == f.rel.to._meta:
                 cache[RelatedObject(f.rel.to, klass, f)] = None
     if app_cache_ready():
         self._related_many_to_many_cache = cache
     return cache
Exemplo n.º 3
0
 def _fill_related_objects_cache(self):
     cache = SortedDict()
     parent_list = self.get_parent_list()
     for parent in self.parents:
         for obj, model in parent._meta.get_all_related_objects_with_model(include_hidden=True):
             if (obj.field.creation_counter < 0 or obj.field.rel.parent_link) and obj.model not in parent_list:
                 continue
             if not model:
                 cache[obj] = parent
             else:
                 cache[obj] = model
     # Collect also objects which are in relation to some proxy child/parent of self.
     proxy_cache = cache.copy()
     for klass in get_models(include_auto_created=True, only_installed=False):
         for f in klass._meta.local_fields:
             if f.rel and not isinstance(f.rel.to, six.string_types):
                 if self == f.rel.to._meta:
                     cache[RelatedObject(f.rel.to, klass, f)] = None
                     proxy_cache[RelatedObject(f.rel.to, klass, f)] = None
                 elif self.concrete_model == f.rel.to._meta.concrete_model:
                     proxy_cache[RelatedObject(f.rel.to, klass, f)] = None
     self._related_objects_cache = cache
     self._related_objects_proxy_cache = proxy_cache
Exemplo n.º 4
0
 def test_get_models_with_not_installed(self):
     self.assertTrue(
         "NotInstalledModel" in [
             m.__name__ for m in get_models(only_installed=False)])
Exemplo n.º 5
0
 def test_get_models_with_app_label_only_returns_installed_models(self):
     self.assertEqual(get_models(self.not_installed_module), [])
Exemplo n.º 6
0
 def test_get_models_only_returns_installed_models(self):
     self.assertFalse(
         "NotInstalledModel" in
         [m.__name__ for m in get_models()])