Esempio n. 1
0
    def _import_objects(self, modules):
        from django.db.models.loading import get_models, get_apps

        loaded_models = get_models()
        # 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 = {}

        for app_mod in get_apps():
            app_models = get_models(app_mod)
            if not app_models:
                continue
            model_labels = ", ".join([model.__name__ for model in app_models])
            print self.style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split(".")[-2], model_labels))
            for model in app_models:
                try:
                    imported_objects[model.__name__] = getattr(
                        __import__(app_mod.__name__, {}, {}, model.__name__), model.__name__
                    )
                except AttributeError, e:
                    print self.style.ERROR_OUTPUT(
                        "Failed to import '%s' from '%s' reason: %s"
                        % (model.__name__, app_mod.__name__.split(".")[-2], str(e))
                    )
                    continue
def run(working_dir):
  sys.path.insert(0, working_dir)
  manage_file = os.getenv('PYCHARM_DJANGO_MANAGE_MODULE')
  if not manage_file:
    manage_file = 'manage'

  def execute_manager(settings_mod, argv = None):
      management.setup_environ(settings_mod)

  management.execute_manager = execute_manager

  def execute_from_command_line(argv=None):
    pass

  management.execute_from_command_line = execute_from_command_line

  fixGetpass()

  try:
      #import settings to prevent circular dependencies later on import django.db
      from django.conf import settings
      apps=settings.INSTALLED_APPS

      # From django.core.management.shell

      # XXX: (Temporary) workaround for ticket #1796: force early loading of all
      # models from installed apps.
      from django.db.models.loading import get_models
      get_models()

  except:
      pass

  run_module(manage_file, None, '__main__', True)
Esempio n. 3
0
    def handle(self, *args, **options):
        """ Handle the Django management command. """

        from django.db.models.loading import get_models
        get_models()

        self.ipython_notebook()
Esempio n. 4
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps. (this is fixed by now, but leaving it here
        # for people using 0.96 or older trunk (pre [5919]) versions.
        from django.db.models.loading import get_models, get_apps
        loaded_models = get_models()

        use_plain = options.get('plain', False)
        use_pythonrc = not options.get('no_pythonrc', True)

        # 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.
        from django.conf import settings
        imported_objects = {'settings': settings}
        for app_mod in get_apps():
            app_models = get_models(app_mod)
            if not app_models:
                continue
            model_labels = ", ".join([model.__name__ for model in app_models])
            print self.style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], model_labels))
            for model in app_models:
                try:
                    imported_objects[model.__name__] = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__)
                except AttributeError, e:
                    print self.style.ERROR_OUTPUT("Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_mod.__name__.split('.')[-2], str(e)))
                    continue
    def handle_noargs(self, **options):
        # workaround taken from django.core.management.commands.shell
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps.
        from django.db.models.loading import get_models
        get_models()

        self.ipython_notebook()
Esempio n. 6
0
def import_objects(options, style):

    class ObjectImportError(Exception):
        pass

    # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    # models from installed apps. (this is fixed by now, but leaving it here
    # for people using 0.96 or older trunk (pre [5919]) versions.

    from django.db.models.loading import get_models, get_apps
    # from django.db.models.loading import get_models, get_apps
    loaded_models = get_models()  # NOQA

    from django.conf import settings
    imported_objects = {'settings': settings}

    dont_load_cli = options.get('dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    for app_mod in get_apps():
        app_models = get_models(app_mod)
        if not app_models:
            continue

        app_name = app_mod.__name__.split('.')[-2]
        if app_name in dont_load:
            continue

        app_aliases = model_aliases.get(app_name, {})
        model_labels = []

        for model in app_models:
            try:
                imported_object = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__)
                model_name = model.__name__

                if "%s.%s" % (app_name, model_name) in dont_load:
                    continue

                alias = app_aliases.get(model_name, model_name)
                imported_objects[alias] = imported_object
                if model_name == alias:
                    model_labels.append(model_name)
                else:
                    model_labels.append("%s (as %s)" % (model_name, alias))

            except AttributeError as e:
                if not quiet_load:
                    print(style.ERROR("Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_name, str(e))))
                continue
        if not quiet_load:
            print(style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], ", ".join(model_labels))))

    return imported_objects
Esempio n. 7
0
def import_objects(options, style, global_model_scope=None):
    # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    # models from installed apps. (this is fixed by now, but leaving it here
    # for people using 0.96 or older trunk (pre [5919]) versions.

    # global_model_scope is used by shell_plus to autoreload models.py modules
    from django.db.models.loading import get_models, get_apps
    loaded_models = get_models()  # NOQA

    from django.conf import settings
    imported_objects = {'settings': settings}

    dont_load_cli = options.get('dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    for app_mod in get_apps():
        app_models = get_models(app_mod)
        if not app_models:
            continue

        app_name = app_mod.__name__.split('.')[-2]
        if app_name in dont_load:
            continue

        app_aliases = model_aliases.get(app_name, {})
        model_labels = []

        for model in app_models:
            # global scope required for autoreloading models functionality
            # May be able to avoid this but have not succeeded so far
            if global_model_scope is not None:
                global_model_scope[app_name].append(model.__name__)

            try:
                imported_object = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__)
                model_name = model.__name__

                if "%s.%s" % (app_name, model_name) in dont_load:
                    continue

                alias = app_aliases.get(model_name, model_name)
                imported_objects[alias] = imported_object
                if model_name == alias:
                    model_labels.append(model_name)
                else:
                    model_labels.append("%s (as %s)" % (model_name, alias))

            except AttributeError, e:
                if not quiet_load:
                    print style.ERROR("Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_name, str(e)))
                continue
        if not quiet_load:
            print style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], ", ".join(model_labels)))
Esempio n. 8
0
def iter_adapters(flavor=None):
    from django.db.models.loading import get_models

    # force models to be loaded
    get_models()
    print "loaded adapters", _adapters
    for adapter in _adapters:
        if flavor is None or adapter.flavor == flavor:
            yield adapter
Esempio n. 9
0
	def find_module(self,fullname,path=None):
		if len( self.apps ) == 0:
			get_models()
		if not self.no_model in self.apps:
			self.apps.add( self.no_model )
		if fullname.startswith('tranquil.models.'):
			app = fullname.replace( 'tranquil.models.', '' )
			if app in self.apps:
				return self
		return None
Esempio n. 10
0
    def test_fixture_load(self):
        for model in get_models(models):
            assert 0 == model.objects.count()

        dj_fixture = DjangoFixture()
        with dj_fixture.data(AuthorData, BookData):
            assert models.Author.objects.get(first_name='Frank').books.count() == 1

        for model in get_models(models):
            assert 0 == model.objects.count()
Esempio n. 11
0
def setup_django_env():
    from django.db.models.loading import get_models
    from django.conf import settings

    print "Importing ..."
    for m in get_models():
        ip.ex("from %s import %s" % (m.__module__, m.__name__))

    # Output alphabetic list of imported modules.
    module_names = [i.__name__ for i in get_models()]
    module_names.sort()

    for name in module_names:
        print name
Esempio n. 12
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps. (this is fixed by now, but leaving it here
        # for people using 0.96 or older trunk (pre [5919]) versions.
        from django.db.models.loading import get_models, get_apps
        loaded_models = get_models()

        use_ipython = options.get('ipython', False)
        use_plain = options.get('plain', False)
        use_pythonrc = not options.get('no_pythonrc', True)

        if options.get("print_sql", False):
            # Code from http://gist.github.com/118990
            from django.db.backends import util
            try:
                import sqlparse
            except ImportError:
                sqlparse = None

            class PrintQueryWrapper(util.CursorDebugWrapper):
                def execute(self, sql, params=()):
                    try:
                        return self.cursor.execute(sql, params)
                    finally:
                        raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
                        if sqlparse:
                            print sqlparse.format(raw_sql, reindent=True)
                        else:
                            print raw_sql
                        print

            util.CursorDebugWrapper = PrintQueryWrapper

        # 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.
        from django.conf import settings
        imported_objects = {'settings': settings}
        for app_mod in get_apps():
            app_models = get_models(app_mod)
            if not app_models:
                continue
            model_labels = ", ".join([model.__name__ for model in app_models])
            print self.style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], model_labels))
            for model in app_models:
                try:
                    imported_objects[model.__name__] = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__)
                except AttributeError, e:
                    print self.style.ERROR("Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_mod.__name__.split('.')[-2], str(e)))
                    continue
Esempio n. 13
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps.
        from django.db.models.loading import get_models
        get_models()

        use_plain = options.get('plain', False)
        no_startup = options.get('no_startup', 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 no_startup:
                for pythonrc in (os.environ.get("PYTHONSTARTUP"), '~/.pythonrc.py'):
                    if not pythonrc:
                        continue
                    pythonrc = os.path.expanduser(pythonrc)
                    if not os.path.isfile(pythonrc):
                        continue
                    try:
                        with open(pythonrc) as handle:
                            exec(compile(handle.read(), pythonrc, 'exec'), imported_objects)
                    except NameError:
                        pass
            code.interact(local=imported_objects)
Esempio n. 14
0
def import_objects(options, style):
    # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    # models from installed apps. (this is fixed by now, but leaving it here
    # for people using 0.96 or older trunk (pre [5919]) versions.
    from django.db.models.loading import get_models, get_apps
    loaded_models = get_models()  # NOQA

    from django.conf import settings
    imported_objects = {'settings': settings}

    dont_load_cli = options.get('dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    import os, sys
    if 'PYTHONSTARTUP' in os.environ:
        try:
            sys.path.append(os.environ['PYTHONSTARTUP'])
            import startup
            content = [element for element in dir(startup)]
            for element in content:
                imported_objects[element] = getattr(startup, element)
        except Exception, ex:
            sys.exit("Could not import startup module content, Error:\n%s" % ex)
Esempio n. 15
0
def resolve_model(model_spec, app_label=None, strict=False):
    """Return the class object of the specified model. `model_spec` is
    usually the global model name (i.e. a string like
    ``'contacts.Person'``).

    If `model_spec` does not refer to a known model, the function
    returns :class:`UnresolvedModel` (unless `strict=True` is
    specified).

    Using this method is better than simply importing the class
    object, because Lino applications can override the model
    implementation.
    
    This function **does not** trigger a loading of Django's model
    cache, so you should not use it at module-level of a
    :xfile:`models.py` module.

    In general we recommend to use ``from lino.api import rt`` and
    ``rt.modules.contacts.Person`` over
    ``resolve_model('contacts.Person')``. Note however that this works
    only in a local scope, not at global module level.

    """
    # ~ models.get_apps() # trigger django.db.models.loading.cache._populate()
    if isinstance(model_spec, basestring):
        if '.' in model_spec:
            app_label, model_name = model_spec.split(".")
        else:
            model_name = model_spec

        if AFTER17:
            from django.apps import apps
            try:
                model = apps.get_model(app_label, model_name)
            except LookupError:
                model = None
        else:
            model = models.get_model(app_label, model_name, seed_cache=False)
        #~ model = models.get_model(app_label,model_name,seed_cache=seed_cache)
    else:
        model = model_spec
    if not isinstance(model, type) or not issubclass(model, models.Model):
        if strict:
            if False:
                from django.db.models import loading
                print(20130219, settings.INSTALLED_APPS)
                print([full_model_name(m) for m in get_models()])
                if len(loading.cache.postponed) > 0:
                    print("POSTPONED:", loading.cache.postponed)

            if isinstance(strict, basestring):
                raise Exception(strict % model_spec)
            raise ImportError(
                "resolve_model(%r,app_label=%r) found %r "
                "(settings %s, INSTALLED_APPS=%s)" % (
                    model_spec, app_label, model,
                    settings.SETTINGS_MODULE, settings.INSTALLED_APPS))
        #~ logger.info("20120628 unresolved %r",model)
        return UnresolvedModel(model_spec, app_label)
    return model
Esempio n. 16
0
def _models_generator():
    """
    Build a hash of model verbose names to models
    """
    for model in get_models():
        yield (unicode(model._meta.verbose_name), model)
        yield (unicode(model._meta.verbose_name_plural), model)
Esempio n. 17
0
    def handle_noargs(self, **options):
        global use_plain

        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps.
        from django.db.models.loading import get_models
        loaded_models = get_models()

        use_plain = options.get('plain', False)
        use_ipython = options.get('ipython', False)
        use_bpython = options.get('bpython', False)
        
        try:
            if use_plain:
                # Don't bother loading bpython, because the user wants plain Python.
                raise ImportError
            elif use_ipython:
                start_ipython_shell()
            elif use_bpython:
                start_bpython_shell()
            else:
                # backward compatible behavior.
                start_bpython_shell()

        except ImportError:
            # fallback to plain shell if we encounter an ImportError
            start_plain_shell()
def filter_model_fields(app_labels=None, model_fields=None):
    """
    Collect a filtered and sorted list of model fields.

    >>> from django.db import models
    >>> fields = filter_model_fields(
    ...     app_labels = ("auth",), model_fields = (models.CharField,)
    ... )
    >>> field2dot_name(fields[0])
    'auth.Group.name'
    >>> [field.get_attname() for field in fields]
    ['name', 'codename', 'name', 'email', 'first_name', 'last_name', 'password', 'username']

    :param app_labels: Filter by app labels, if None: all installed apps
    :param model_fields: List of field classes for filtering
    :return: field list, sorted by dot name representation
    """
    filtered_fields = set()
    for app in get_apps():
        for model in get_models(app):
            for field in model._meta.fields:
                if app_labels is not None:
                    if field.model._meta.app_label not in app_labels:
                        continue

                if not isinstance(field, model_fields):
                    continue

                filtered_fields.add(field)

    return sort_fields(filtered_fields)
Esempio n. 19
0
 def yumlfy(self, applications):
     F = YUMLFormatter()
     model_list = []
     arrow_list = []
     external_model = set()
     for app_module in applications:
         models = get_models(app_module)
         if not models:
             continue
         for m in models:
             string = F.label(m) + F.PIPE
             fields = [f for f in m._meta.fields if not f.auto_created]
             for field in fields:
                 string += F.field(field)
                 if field.rel:
                     arrow_list.append(F.relation(m, field.rel))
                     if get_app(field.rel.to._meta.app_label) not in applications:
                         external_model.add(field.rel.to)
             fields = [f for f in m._meta.many_to_many]
             for field in fields:
                 string += F.field(field)
                 if field.rel.through._meta.auto_created:
                     arrow_list.append(F.relation(m, field.rel))
                 else:
                     arrow_list.append(F.through(m, field.rel))
                 if get_app(field.rel.to._meta.app_label) not in applications:
                     external_model.add(field.rel.to)
             model_list.append(F.wrap(string))
             for parent in m._meta.parents:
                 arrow_list.append(F.inherit(m, parent))
                 if get_app(parent._meta.app_label) not in applications:
                     external_model.add(parent)
     for ext in external_model:
         model_list.append(F.external(ext))
     return model_list + arrow_list
Esempio n. 20
0
def get_permission_choices():
    """
        Rather than creating permissions in the datastore which is incredibly slow (and relational)
        we just use the permission codenames, stored in a ListField.
    """

    global PERMISSIONS_LIST

    if PERMISSIONS_LIST:
        return PERMISSIONS_LIST

    from django.conf import settings

    AUTO_PERMISSIONS = getattr(settings, "AUTOGENERATED_PERMISSIONS", ('add', 'change', 'delete'))

    result = getattr(settings, "MANUAL_PERMISSIONS", [])

    for app in get_apps():
        for model in get_models(app):
            for action in AUTO_PERMISSIONS:
                opts = model._meta
                result.append((get_permission_codename(action, opts), 'Can %s %s' % (action, opts.verbose_name_raw)))

    PERMISSIONS_LIST = sorted(result)
    return PERMISSIONS_LIST
Esempio n. 21
0
def get_import_objects():
    """
    Collect objects to populate the interactive session
    """
    imported_objects = {}
    loaded_model_names = set()
    for app in get_apps():
        app_name = app.__name__.split('.')[-2]
        for model in get_models(app):
            model_name = model.__name__
            if model_name in loaded_model_names:
                model_name = '_'.join((app_name, model_name))
            else:
                loaded_model_names.add(model_name)
            imported_objects[model_name] = model

    for extra_import in settings.EXTRA_SHELLPLUS_IMPORTS:
        if isinstance(extra_import, basestring):
            obj = import_object(extra_import)
            name = extra_import.split('.')[-1]
        else:
            import_path, name = extra_import
            obj = import_object(import_path)
        imported_objects[name] = obj

    return imported_objects
def get_schematics_models(app_name):
    app = get_app(app_name)
    models =  get_models(app)
    all_models = []
    for model in models:
        all_models += SchematicsModel.from_django(model)
    return all_models
Esempio n. 23
0
def auto_patch_all():
    for app in get_apps():
        for model in get_models(app):
            try:
                patch_admin(model, skip_non_revision=True)
            except Exception as err:
                logging.warning("Can't patch admin for model %r: %s" % (model, err))
Esempio n. 24
0
    def handle_noargs(self, **options):
        app_labels = []

        for model in get_models():
            app_labels.append(tuple([model.__name__, "fields = " +  \
                                     str(len(model._meta.fields)) ]))
        return "\n".join(str(v) for v in app_labels)
Esempio n. 25
0
    def handle_noargs(self, **options):

        existing_files = set(files_in_folder(settings.MEDIA_ROOT))

        referenced_fields = set()
        for model in get_models():
            if model._meta.app_label in ('south', 'django'):
                continue
            fname = lambda f: f.name
            is_filefiled = lambda f: isinstance(f, FileField)
            fields = map(fname, filter(is_filefiled, model._meta.fields))
            if not fields:
                continue

            for row in model.objects.values_list(*fields):
                for value in row:
                    referenced_fields.add(value)
        unused_files = (existing_files - referenced_fields)
        unused_files = map(lambda f: os.path.join(settings.MEDIA_ROOT, f),
                           unused_files)
        count = len(list(unused_files))
        if count:
            if confirm(red('Delete %d files?' % count)):
                map(os.unlink, unused_files)
        else:
            print(green("No files to remove :D"))
Esempio n. 26
0
def dump(request):
    response = HttpResponse(mimetype="application/ms-excel")
    response['Content-Disposition'] = 'attachment; filename=PennCycle-Database-Dump-%s.xls' % (str(datetime.datetime.today()))

    if not request.user.is_staff:
        return HttpResponseForbidden()

    wb = xlwt.Workbook()
    excel_date_fmt = 'M/D/YY h:mm'
    datestyle = xlwt.XFStyle()
    datestyle.num_format_str = excel_date_fmt
    plainstyle = xlwt.XFStyle()

    app = get_app('app')
    models = get_models(app)

    for model in models:
        name = model.__name__
        print name
        if name == 'Plan':
            break
        ws = wb.add_sheet(slugify(name))
        xl_export(model, ws, datestyle, plainstyle)

    wb.save(response)
    return response
Esempio n. 27
0
def models_by_base(base, toplevel_only=False):
    """Yields a list of installed models that are subclass of the given
    base class.

    Changed 2015-11-03: The list is sorted alphabetically using
    :func:`full_model_name` because anyway the sort order was
    unpredictable and changed between Django versions.

    """
    found = []
    for m in get_models():
        if issubclass(m, base):
            add = True
            if toplevel_only:
                for i, old in enumerate(found):
                    if issubclass(m, old):
                        add = False
                    elif issubclass(old, m):
                        found[i] = m
                        add = False
            if add:
                found.append(m)

    def f(a, b):
        return cmp(full_model_name(a), full_model_name(b))
    found.sort(f)
    return found
Esempio n. 28
0
def add_new_model(request, model_name):
    if (model_name.lower() == model_name):
        normal_model_name = model_name.capitalize()
    else:
        normal_model_name = model_name

    app_list = get_apps()
    for app in app_list:
        for model in get_models(app):
            if model.__name__ == normal_model_name:
                form = modelform_factory(model)

                if request.method == 'POST':
                    form = form(request.POST)
                    if form.is_valid():
                        try:
                            new_obj = form.save()
                        except forms.ValidationError, error:
                            new_obj = None

                        if new_obj:
                            return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
                                (escape(new_obj._get_pk_val()), escape(new_obj)))

                else:
                    form = form()

                page_context = {'form': form, 'field': normal_model_name}
                return render_to_response('widgets/popup.html', page_context, context_instance=RequestContext(request))
Esempio n. 29
0
    def handle(self, **options):
        imported_objects = {}
        try:
            from django.db.models.loading import get_models
        except ImportError:
            from django.apps import apps
            get_models = apps.get_models
        for m in get_models():
            imported_objects[m.__name__] = m

        try:
            self.ipython(imported_objects)
        except ImportError:
            import code
            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'
                # we already know 'readline' was imported successfully.
                import rlcompleter
                readline.set_completer(
                    rlcompleter.Completer(imported_objects).complete)
                readline.parse_and_bind("tab:complete")
            code.interact(local=imported_objects)
Esempio n. 30
0
def make_namespace():
    ns = {}
    for app in get_apps():
        for model in get_models(app):
            ns[model.__name__] = model
            ns[model._meta.app_label + '_' + model._meta.object_name] = model
    return ns
Esempio n. 31
0
 def test_get_models_with_not_installed(self):
     self.assertTrue("NotInstalledModel" in
                     [m.__name__ for m in get_models(only_installed=False)])
Esempio n. 32
0
 def test_get_models_with_app_label_only_returns_installed_models(self):
     self.assertEqual(get_models(self.not_installed_module), [])
Esempio n. 33
0
def get_model_list():
    """
    Returns a model list with all the data tables in this application
    """
    return get_models(get_app("calaccess_raw"))
Esempio n. 34
0
def index(request):
    context = dict()
    app = get_app('hr')
    context.update({'models': get_models(app)})
    return render(request, "index.html", context)
Esempio n. 35
0
def import_objects(options, style):
    # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    # models from installed apps. (this is fixed by now, but leaving it here
    # for people using 0.96 or older trunk (pre [5919]) versions.
    from django.db.models.loading import get_models, get_apps
    mongoengine = False
    try:
        from mongoengine.base import _document_registry
        mongoengine = True
    except:
        pass

    loaded_models = get_models()  # NOQA

    from django.conf import settings
    imported_objects = {}

    dont_load_cli = options.get(
        'dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    # Perform pre-imports before any other imports
    SHELL_PLUS_PRE_IMPORTS = getattr(settings, '', {})
    if SHELL_PLUS_PRE_IMPORTS:
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus User Imports"))
        imports = import_items(SHELL_PLUS_PRE_IMPORTS,
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    load_models = {}

    if mongoengine:
        for name, mod in six.iteritems(_document_registry):
            name = name.split('.')[-1]
            app_name = mod.__module__.split('.')[-2]
            if app_name in dont_load or ("%s.%s" %
                                         (app_name, name)) in dont_load:
                continue

            load_models.setdefault(mod.__module__, [])
            load_models[mod.__module__].append(name)

    for app_mod in get_apps():
        app_models = get_models(app_mod)
        if not app_models:
            continue

        app_name = app_mod.__name__.split('.')[-2]
        if app_name in dont_load:
            continue

        app_aliases = model_aliases.get(app_name, {})
        for mod in app_models:
            if "%s.%s" % (app_name, mod.__name__) in dont_load:
                continue

            load_models.setdefault(mod.__module__, [])
            load_models[mod.__module__].append(mod.__name__)

    if not quiet_load:
        print(style.SQL_TABLE("# Shell Plus Model Imports"))
    for app_mod, models in sorted(six.iteritems(load_models)):
        app_name = app_mod.split('.')[-2]
        app_aliases = model_aliases.get(app_name, {})
        model_labels = []

        for model_name in sorted(models):
            try:
                imported_object = getattr(
                    __import__(app_mod, {}, {}, [model_name]), model_name)

                if "%s.%s" % (app_name, model_name) in dont_load:
                    continue

                alias = app_aliases.get(model_name, model_name)
                imported_objects[alias] = imported_object
                if model_name == alias:
                    model_labels.append(model_name)
                else:
                    model_labels.append("%s (as %s)" % (model_name, alias))

            except AttributeError as e:
                if options.get("traceback"):
                    traceback.print_exc()
                if not quiet_load:
                    print(
                        style.ERROR(
                            "Failed to import '%s' from '%s' reason: %s" %
                            (model_name, app_mod, str(e))))
                continue

        if not quiet_load:
            print(
                style.SQL_COLTYPE("from %s import %s" %
                                  (app_mod, ", ".join(model_labels))))

    # Imports often used from Django
    if getattr(settings, 'SHELL_PLUS_DJANGO_IMPORTS', True):
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus Django Imports"))
        SHELL_PLUS_DJANGO_IMPORTS = (
            ('django.core.cache', ['cache']),
            ('django.core.urlresolvers', ['reverse']),
            ('django.conf', ['settings']),
            ('django.db', ['transaction']),
            ('django.db.models',
             ['Avg', 'Count', 'F', 'Max', 'Min', 'Sum', 'Q']),
            ('django.utils', ['timezone']),
        )
        imports = import_items(SHELL_PLUS_DJANGO_IMPORTS,
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    # Perform post-imports after any other imports
    SHELL_PLUS_POST_IMPORTS = getattr(settings, 'SHELL_PLUS_POST_IMPORTS', {})
    if SHELL_PLUS_POST_IMPORTS:
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus User Imports"))
        imports = import_items(SHELL_PLUS_POST_IMPORTS,
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    return imported_objects
Esempio n. 36
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps. (this is fixed by now, but leaving it here
        # for people using 0.96 or older trunk (pre [5919]) versions.
        from django.db.models.loading import get_models, get_apps
        loaded_models = get_models()

        use_plain = options.get('plain', False)
        use_pythonrc = not options.get('no_pythonrc', True)

        # 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 = {}
        for app_mod in get_apps():
            app_models = get_models(app_mod)
            if not app_models:
                continue
            model_labels = ", ".join([model.__name__ for model in app_models])
            print(self.style.SQL_COLTYPE("From '%s' autoload: %s" % (app_mod.__name__.split('.')[-2], model_labels)))
            for model in app_models:
                try:
                    imported_objects[model.__name__] = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__)
                except AttributeError as e:
                    print(self.style.ERROR_OUTPUT("Failed to import '%s' from '%s' reason: %s" % (model.__name__, app_mod.__name__.split('.')[-2], str(e))))
                    continue
        try:
            if use_plain:
                # Don't bother loading IPython, because the user wants plain Python.
                raise ImportError
            import IPython
            # Explicitly pass an empty list as arguments, because otherwise IPython
            # would use sys.argv from this script.
            shell = IPython.Shell.IPShell(argv=[], user_ns=imported_objects)
            shell.mainloop()
        except ImportError:
            # Using normal Python shell
            import code
            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 import user.
            if use_pythonrc:
                pythonrc = os.environ.get("PYTHONSTARTUP") 
                if pythonrc and os.path.isfile(pythonrc): 
                    try: 
                        exec(compile(open(pythonrc, "rb").read(), pythonrc, 'exec')) 
                    except NameError: 
                        pass
                # This will import .pythonrc.py as a side-effect
                import user
            code.interact(local=imported_objects)
Esempio n. 37
0
 def get_apps_and_models():
     for app_mod in get_apps():
         app_models = get_models(app_mod)
         yield app_mod, app_models
Esempio n. 38
0
from django.db.models.loading import get_models, get_app
from django.contrib import admin

for model in get_models(get_app('teams')):
    admin.site.register(model)
def get_all_models(app):
    try:
        app_mod = load_app(app)  # First try full path
    except ImportError:
        app_mod = get_app(app)  # Then try just app_label
    return get_models(app_mod)
Esempio n. 40
0
# Set the DJANGO_SETTINGS_MODULE environment variable.   
os.environ['DJANGO_SETTINGS_MODULE'] = "buptCrawler.settings"

from Tools import PriorityQueue,Parser,ParserForBKEmploy

import urllib2,urllib
import sys
import os
import string
import socket
import cookielib
import time
import datetime

from django.db.models.loading import get_models
loaded_models = get_models()
from buptCrawler.crawler.models  import Link as dblink

from stripogram import html2text, html2safehtml


def zh2unicode2(stri): 
    """转换编码到unicode,
    编码方式从utf-8,gbk,iso-8859-1,big5,ascii中选择.""" 
    for c in ('ascii', 'utf-8', 'gb2312', 'gbk', 'gb18030', 'big5'): 
	flag = 0
        try: 
	    decodeString = stri.decode(c)
	    flag = 1
            return decodeString
        except: 
Esempio n. 41
0
    url(r'^brewers/', BrewerListView.as_view(), name='brewers'),
    # staff views
    url(r'^equipment/branch/add', BranchEquipmentAddView.as_view(extra_context={'extra_title': 'Add Branch Tool'}), name='add-branch-tool'),
    # action views - contributions (PROTECTED)
    # url(r'^contribute/$', OfferToolView.as_view(extra_context={'extra_title': 'Offer to Contribute Tool'}), name='contribute-tool'),
    # url(r'^contribute/pending/$', ProtectedListView.as_view(model=EquipmentOffer), name='browse-offers'),
    # url(r'^contribute/pending/(?P<pk>\d+)/$', ProtectedDetailView.as_view(model=EquipmentOffer), name='offer-detail'),
    # url(r'^contribute/pending/(?P<pk>\d+)/accept/$', AcceptToolView.as_view(extra_context={'extra_title': 'Add Pending Contribution'}), name='accept-offer'),
    # url(r'^contribute/pending/(?P<pk>\d+)/delete/$', ProtectedDeleteView.as_view(model=EquipmentOffer), name='delete-offer'),
    # staff views
)

# Add generic views for each model automagically
# These views may be overriden if they exist higher up in the urlpatterns
# Staff only
for model in get_models(get_app('core')):
    included = ['Equipment', 'Commodity']  # Any objects that should be included go here!
    if model.__name__ not in included:
        continue
    name = model.__name__
    lower = name.lower()
    list = "%s/$" % lower
    create = "%s/create/$" % lower
    detail = "%s/(?P<pk>\d+)/$" % lower
    update = "%s/(?P<pk>\d+)/update/$" % lower
    delete = "%s/(?P<pk>\d+)/delete/$" % lower
    urlpatterns += patterns('',
        url(r'^%s' % list, staff_member_required(ProtectedListView.as_view(model=model, extra_context={'extra_title': name + " List"})), name=lower + "_list"),
        url(r'^%s' % detail, staff_member_required(ProtectedDetailView.as_view(model=model, extra_context={'extra_title': name})), name=lower + "_detail"),
        url(r'^%s' % create, staff_member_required(ProtectedCreateView.as_view(model=model, extra_context={'extra_title': "Add " + name})), name=lower + "_create"),
        url(r'^%s' % update, staff_member_required(ProtectedUpdateView.as_view(model=model, extra_context={'extra_title': "Update " + name})), name=lower + "_update"),
Esempio n. 42
0
 def __init__(self):
     for m in get_models():
         setattr(self, m.__name__, m)
Esempio n. 43
0
 def __init__(self):
     from django.db.models.loading import get_models
     for m in get_models():
         setattr(self, m.__name__, m)
Esempio n. 44
0
def test_dev_setup(appnexus_api_requests, cdn_api):
    """
    Test preparing kanary to development
    #. drop current database
    #. run syncdb. Since pytest-django turns off south's syncdb command,
        we have to import it manually. Only this overwritten command,
        can sync apps that does not have migrations
    #. migrate database
    #. Check report adverts count - should be none, since they're being created
        in fill_campaigns only
    #. run fill_campaigns and fill_reports command
    #. check number of report adverts
    #. Try to read data from all models.
        If someone will forget about migrations, that's where we'll hit exceptions
    #. Check whether model differs from last migration
    """
    from south.management.commands.syncdb import Command as SyncCommand
    """Run commands as a fresh dev."""
    management.call_command('drop_kanary', interactive=False)
    sync = SyncCommand()
    sync.execute(verbosity=0, database=settings.DATABASES.keys()[0])
    management.call_command('migrate', interactive=False)
    assert ReportAdvert.objects.count() == 0
    management.call_command('fill_campaigns', interactive=False)
    management.call_command('fill_reports', interactive=False)
    assert ReportAdvert.objects.count() > 0

    app_models = []
    for app_mod in get_apps():
        app_models.extend(get_models(app_mod))

    for model in app_models:
        print('Querying for: ' + model.__name__)
        model.objects.first()

    for app in settings.INSTALLED_APPS:
        if app.split('.')[0] == 'ui':
            app = app.split('.')[-1]
            try:
                migrations = Migrations(app,
                                        force_creation=False,
                                        verbose_creation=False)
            # if there is no migrations directory
            except NoMigrations:
                continue
            # if there are no models
            except ImproperlyConfigured:
                continue

            # if migrations directory is empty
            if not migrations:
                continue

            last_migration = migrations[-1]

            # Two models saved in dictionary, one based migration, second on models.py
            migration_defs = dict(
                (k, v)
                for k, v in last_migration.migration_class().models.items()
                if k.split(".")[0] == migrations.app_label())
            model_defs = dict((k, v) for k, v in freezer.freeze_apps(
                [migrations.app_label()]).items()
                              if k.split(".")[0] == migrations.app_label())
            change_source = AutoChanges(
                migrations=migrations,
                old_defs=migration_defs,
                old_orm=last_migration.orm(),
                new_defs=model_defs,
            )

            assert list(change_source.get_changes()) == []
Esempio n. 45
0
 def get_models(self, app):
     r = loading.get_models(app)
     return r
Esempio n. 46
0
    def handle_noargs(self, **options):
        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
        # models from installed apps. (this is fixed by now, but leaving it here
        # for people using 0.96 or older trunk (pre [5919]) versions.
        from django.db.models.loading import get_models, get_apps
        loaded_models = get_models()

        use_ipython = options.get('ipython', False)
        use_plain = options.get('plain', False)
        use_pythonrc = not options.get('no_pythonrc', True)

        if options.get("print_sql", False):
            # Code from http://gist.github.com/118990
            from django.db.backends import util
            try:
                import sqlparse
            except ImportError:
                sqlparse = None

            class PrintQueryWrapper(util.CursorDebugWrapper):
                def execute(self, sql, params=()):
                    starttime = time.time()
                    try:
                        return self.cursor.execute(sql, params)
                    finally:
                        raw_sql = self.db.ops.last_executed_query(
                            self.cursor, sql, params)
                        execution_time = time.time() - starttime
                        if sqlparse:
                            print sqlparse.format(raw_sql, reindent=True)
                        else:
                            print raw_sql
                        print
                        print 'Execution time: %.6fs' % execution_time
                        print

            util.CursorDebugWrapper = PrintQueryWrapper

        # 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.
        from django.conf import settings
        imported_objects = {'settings': settings}

        dont_load_cli = options.get(
            'dont_load')  # optparse will set this to [] if it doensnt exists
        dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
        dont_load = dont_load_cli + dont_load_conf

        model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

        for app_mod in get_apps():
            app_models = get_models(app_mod)
            if not app_models:
                continue

            app_name = app_mod.__name__.split('.')[-2]
            if app_name in dont_load:
                continue

            app_aliases = model_aliases.get(app_name, {})
            model_labels = []

            for model in app_models:
                try:
                    imported_object = getattr(
                        __import__(app_mod.__name__, {}, {}, model.__name__),
                        model.__name__)
                    model_name = model.__name__

                    if "%s.%s" % (app_name, model_name) in dont_load:
                        continue

                    alias = app_aliases.get(model_name, model_name)
                    imported_objects[alias] = imported_object
                    if model_name == alias:
                        model_labels.append(model_name)
                    else:
                        model_labels.append("%s (as %s)" % (model_name, alias))

                except AttributeError, e:
                    print self.style.ERROR(
                        "Failed to import '%s' from '%s' reason: %s" %
                        (model.__name__, app_name, str(e)))
                    continue
            print self.style.SQL_COLTYPE(
                "From '%s' autoload: %s" %
                (app_mod.__name__.split('.')[-2], ", ".join(model_labels)))
Esempio n. 47
0
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#########################################################################

from django.core.management import setup_environ
import dam.settings as settings
setup_environ(settings)
from django.db.models.loading import get_models
get_models()

from twisted.internet import defer, reactor
from dam.repository.models import Item


def save_metadata(deferred, item, metadata_namespace, metadata_name,
                  metadata_value):
    try:
        item.set_metadata(metadata_namespace, metadata_name, metadata_value)
        deferred.callback('ok')
    except Exception, ex:
        deferred.errback('error: %s' % ex)
    finally:
        return deferred
Esempio n. 48
0
def import_objects(options, style):
    # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    # models from installed apps. (this is fixed by now, but leaving it here
    # for people using 0.96 or older trunk (pre [5919]) versions.
    from django.db.models.loading import get_models, get_apps
    loaded_models = get_models()  # NOQA

    from django.conf import settings
    imported_objects = {'settings': settings}

    dont_load_cli = options.get(
        'dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    # Perform pre-imports before any other imports
    imports = import_items(getattr(settings, 'SHELL_PLUS_PRE_IMPORTS', {}))
    for k, v in imports.items():
        imported_objects[k] = v

    for app_mod in get_apps():
        app_models = get_models(app_mod)
        if not app_models:
            continue

        app_name = app_mod.__name__.split('.')[-2]
        if app_name in dont_load:
            continue

        app_aliases = model_aliases.get(app_name, {})
        model_labels = []

        for model in app_models:
            try:
                imported_object = getattr(
                    __import__(app_mod.__name__, {}, {}, model.__name__),
                    model.__name__)
                model_name = model.__name__

                if "%s.%s" % (app_name, model_name) in dont_load:
                    continue

                alias = app_aliases.get(model_name, model_name)
                imported_objects[alias] = imported_object
                if model_name == alias:
                    model_labels.append(model_name)
                else:
                    model_labels.append("%s (as %s)" % (model_name, alias))

            except AttributeError as e:
                if options.get("traceback"):
                    traceback.print_exc()
                if not quiet_load:
                    print(
                        style.ERROR(
                            "Failed to import '%s' from '%s' reason: %s" %
                            (model.__name__, app_mod.__name__, str(e))))
                continue
        if not quiet_load:
            print(
                style.SQL_COLTYPE("From '%s' autoload: %s" %
                                  (app_mod.__name__.split('.')[-2],
                                   ", ".join(model_labels))))

    # Perform post-imports after any other imports
    imports = import_items(getattr(settings, 'SHELL_PLUS_POST_IMPORTS', {}))
    for k, v in imports.items():
        imported_objects[k] = v

    return imported_objects
Esempio n. 49
0
def import_objects(options, style):
    # Django 1.7 introduced the app registry which must be initialized before we
    # can call get_apps(). Django already does this for us when we are invoked
    # as manage.py command, but we have to do it ourselves if when running as
    # iPython notebook extension, so we call django.setup() if the app registry
    # isn't initialized yet. The try/except can be removed when support for
    # Django 1.6 is dropped.
    try:
        from django.apps import apps
        from django import setup
    except ImportError:
        pass
    else:
        if not apps.ready:
            setup()

    from django.db.models.loading import get_models, get_apps
    mongoengine = False
    try:
        from mongoengine.base import _document_registry
        mongoengine = True
    except:
        pass

    from django.conf import settings
    imported_objects = {}

    dont_load_cli = options.get(
        'dont_load')  # optparse will set this to [] if it doensnt exists
    dont_load_conf = getattr(settings, 'SHELL_PLUS_DONT_LOAD', [])
    dont_load = dont_load_cli + dont_load_conf
    quiet_load = options.get('quiet_load')

    model_aliases = getattr(settings, 'SHELL_PLUS_MODEL_ALIASES', {})

    # Perform pre-imports before any other imports
    SHELL_PLUS_PRE_IMPORTS = getattr(settings, 'SHELL_PLUS_PRE_IMPORTS', {})
    if SHELL_PLUS_PRE_IMPORTS:
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus User Imports"))
        imports = import_items(SHELL_PLUS_PRE_IMPORTS,
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    load_models = {}

    if mongoengine:
        for name, mod in six.iteritems(_document_registry):
            name = name.split('.')[-1]
            app_name = mod.__module__.split('.')[-2]
            if app_name in dont_load or ("%s.%s" %
                                         (app_name, name)) in dont_load:
                continue

            load_models.setdefault(mod.__module__, [])
            load_models[mod.__module__].append(name)

    for app_mod in get_apps():
        app_models = get_models(app_mod)
        if not app_models:
            continue

        app_name = app_mod.__name__.split('.')[-2]
        if app_name in dont_load:
            continue

        app_aliases = model_aliases.get(app_name, {})
        for mod in app_models:
            if "%s.%s" % (app_name, mod.__name__) in dont_load:
                continue

            if mod.__module__:
                # Only add the module to the dict if `__module__` is not empty.
                load_models.setdefault(mod.__module__, [])
                load_models[mod.__module__].append(mod.__name__)

    if not quiet_load:
        print(style.SQL_TABLE("# Shell Plus Model Imports"))
    for app_mod, models in sorted(six.iteritems(load_models)):
        try:
            app_name = app_mod.split('.')[-2]
        except IndexError:
            # Some weird model naming scheme like in Sentry.
            app_name = app_mod
        app_aliases = model_aliases.get(app_name, {})
        model_labels = []

        for model_name in sorted(models):
            try:
                imported_object = getattr(
                    __import__(app_mod, {}, {}, [model_name]), model_name)

                if "%s.%s" % (app_name, model_name) in dont_load:
                    continue

                alias = app_aliases.get(model_name, model_name)
                imported_objects[alias] = imported_object
                if model_name == alias:
                    model_labels.append(model_name)
                else:
                    model_labels.append("%s (as %s)" % (model_name, alias))

            except AttributeError as e:
                if options.get("traceback"):
                    traceback.print_exc()
                if not quiet_load:
                    print(
                        style.ERROR(
                            "Failed to import '%s' from '%s' reason: %s" %
                            (model_name, app_mod, str(e))))
                continue

        if not quiet_load:
            print(
                style.SQL_COLTYPE("from %s import %s" %
                                  (app_mod, ", ".join(model_labels))))

    # Imports often used from Django
    if getattr(settings, 'SHELL_PLUS_DJANGO_IMPORTS', True):
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus Django Imports"))
        SHELL_PLUS_DJANGO_IMPORTS = {
            'django.core.cache': ['cache'],
            'django.core.urlresolvers': ['reverse'],
            'django.conf': ['settings'],
            'django.db': ['transaction'],
            'django.db.models':
            ['Avg', 'Count', 'F', 'Max', 'Min', 'Sum', 'Q'],
            'django.utils': ['timezone'],
        }
        if django.VERSION[:2] >= (1, 7):
            SHELL_PLUS_DJANGO_IMPORTS['django.db.models'].append("Prefetch")
        imports = import_items(SHELL_PLUS_DJANGO_IMPORTS.items(),
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    # Perform post-imports after any other imports
    SHELL_PLUS_POST_IMPORTS = getattr(settings, 'SHELL_PLUS_POST_IMPORTS', {})
    if SHELL_PLUS_POST_IMPORTS:
        if not quiet_load:
            print(style.SQL_TABLE("# Shell Plus User Imports"))
        imports = import_items(SHELL_PLUS_POST_IMPORTS,
                               style,
                               quiet_load=quiet_load)
        for k, v in six.iteritems(imports):
            imported_objects[k] = v

    return imported_objects
Esempio n. 50
0
def ensure_completely_loaded(force=False):
    """
    This method ensures all models are completely loaded

    FeinCMS requires Django to be completely initialized before proceeding,
    because of the extension mechanism and the dynamically created content
    types.

    For more informations, have a look at issue #23 on github:
    http://github.com/feincms/feincms/issues#issue/23
    """

    global COMPLETELY_LOADED
    if COMPLETELY_LOADED and not force:
        return True

    # Ensure meta information concerning related fields is up-to-date.
    # Upon accessing the related fields information from Model._meta,
    # the related fields are cached and never refreshed again (because
    # models and model relations are defined upon import time, if you
    # do not fumble around with models like we do in FeinCMS.)
    #
    # Here we flush the caches rather than actually _filling them so
    # that relations defined after all content types registrations
    # don't miss out.
    from django.db.models import loading
    for model in loading.get_models():
        for cache_name in ('_field_cache', '_field_name_cache', '_m2m_cache',
                           '_related_objects_cache',
                           '_related_many_to_many_cache', '_name_map'):
            try:
                delattr(model._meta, cache_name)
            except AttributeError:
                pass

        # Randomly call some cache filling methods
        # http://goo.gl/XNI2qz
        model._meta._fill_fields_cache()

    # Calls to get_models(...) are cached by the arguments used in the call.
    # This cache is normally cleared in loading.register_models(), but we
    # invalidate the get_models() cache, by calling get_models
    # above before all apps have loaded. (Django's load_app() doesn't clear the
    # get_models cache as it perhaps should). So instead we clear the
    # get_models cache again here. If we don't do this, Django 1.5 chokes on
    # a model validation error (Django 1.4 doesn't exhibit this problem).
    # See Issue #323 on github.
    if hasattr(loading, 'cache'):
        try:
            loading.cache.get_models.cache_clear()  # Django 1.7+
        except AttributeError:
            loading.cache._get_models_cache.clear()  # Django 1.6-

    if hasattr(loading.app_cache_ready, '__call__'):
        if loading.app_cache_ready():
            COMPLETELY_LOADED = True
    else:
        # TODO Django 1.7 offers us better ways of handling this, maybe.
        if loading.app_cache_ready:
            COMPLETELY_LOADED = True
    return True
Esempio n. 51
0
# How do I list all classes of a given type in a python file?
from django.db.models.loading import get_models, get_app
app = get_app('myappname')
models = get_models(app)
Esempio n. 52
0
def assert_empty(mod):
    for model in get_models(mod):
        assert model.objects.count() == 0
Esempio n. 53
0
 def get_models(self):
     return [(m, model_name(m)) for m in get_models()]
Esempio n. 54
0
 def handle(self, **options):
     from django.db.models.loading import get_models
     from bpython import cli
     for model in get_models():
         exec "from %s import %s" % (model.__module__, model.__name__)
     cli.main(args=[], locals_=locals())
Esempio n. 55
0
 def test_get_models_only_returns_installed_models(self):
     self.assertFalse(
         "NotInstalledModel" in [m.__name__ for m in get_models()])
Esempio n. 56
0
#!/usr/bin/env python

import os, sys

project = os.path.dirname(os.path.realpath(__file__))
root = os.path.dirname(project)
sys.path.append(root)

try:
    # activate virtualenv
    envroot = os.path.dirname(root)
    activate_this = os.path.join(envroot, 'env', 'bin', 'activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
except IOError:
    print "virtualenv loading failed"
    pass

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esp.settings")

from django.db.models.loading import get_models
from django.conf import settings as S

# http://sontek.net/blog/detail/tips-and-tricks-for-the-python-interpreter
for m in get_models():
    globals()[m.__name__] = m

from esp.utils.shell_utils import *
Esempio n. 57
0
from django.db.models.loading import get_models
from dse import DSE


# credit: http://greengiraffe.posterous.com/singleton-pattern-in-python
def singleton(cls, model):
    instances = {}

    def getinstance():
        if cls not in instances:
            instances[cls] = cls(model)
        return instances[cls]

    return getinstance


class _Models:
    pass


Models = _Models()

for model in get_models():
    setattr(Models, model._meta.object_name, singleton(DSE, model))
Esempio n. 58
0
 def __init__(self):
     # cache relevant models
     self._app_models = loading.get_models(models)
     super(AutoCurrentUserMiddleware, self).__init__()
Esempio n. 59
0
def all_models():
    for appname in APPNAMES:
        for model in filter(
                is_concrete,
                get_models(get_app(appname), include_auto_created=True)):
            yield model
    def handle(self, *args, **options):
        verbosity = int(options['verbosity'])

        if "postgresql" in settings.DATABASES['default']['ENGINE']:

            updated_field_count = 0
            total_values_updated = 0
            start_dt = datetime.now()
            print "START: %s" % start_dt

            models = get_models()
            for model in models:
                # Disable auto_now for this model so we don't affect these
                # fields with this update
                for field in model._meta.fields:
                    if field.name in [
                            'update_dt', 'date_done', 'action_time',
                            'date_changed'
                    ]:
                        field.auto_now = False

                for field in model._meta.fields:
                    cursor = connection.cursor()
                    cursor.execute(
                        "SELECT relname FROM pg_class WHERE relname = '%s';" %
                        model._meta.db_table)
                    table_exists = cursor.fetchone()
                    if table_exists:
                        cursor = connection.cursor()
                        cursor.execute(
                            "SELECT atttypid FROM pg_attribute WHERE attrelid = '%s'::regclass AND attname = '%s';"
                            % (model._meta.db_table, field.name))

                        field_type = cursor.fetchone()
                        if field_type:
                            field_type = field_type[0]

                        # "timestamp without time zone" = 1114
                        # "timestamp with time zone" = 1184

                        if field_type == 1114 and field.db_type(
                                connection=connection
                        ) == "timestamp with time zone":

                            print "Updating %s.%s data" % (
                                model._meta.db_table, field.name)
                            print "%s\n" % datetime.now()
                            try:
                                objects = model.objects.all()
                                print objects
                                print "%s objects" % objects.count()
                                total_values_updated = total_values_updated + objects.count(
                                )
                            except:
                                objects = []

                            if objects:
                                for obj in objects:
                                    try:
                                        val = getattr(obj, field.name)
                                    except:
                                        val = None

                                    if val:
                                        new_val = self.convert_to_utc(val)
                                        if verbosity >= 2:
                                            print "%s %s ID:%s %s -> %s" % (
                                                model._meta.verbose_name,
                                                field.name, obj.pk, val,
                                                new_val)

                                        setattr(obj, field.name, new_val)
                                        try:
                                            obj.save()
                                        except Exception, e:
                                            print "failed to update %s %s" % (
                                                model._meta.verbose_name,
                                                obj.pk)
                                            print e

                            # Change the field type to be 'timestamp with time zone', 1184
                            cursor = connection.cursor()
                            cursor.execute(
                                "UPDATE pg_attribute SET atttypid = '1184' WHERE attrelid = '%s'::regclass AND attname = '%s';"
                                % (model._meta.db_table, field.name))
                            print "Finished %s.%s data\n" % (
                                model._meta.db_table, field.name)

                            updated_field_count = updated_field_count + 1

                # Turn auto_now back on for this model
                for field in model._meta.fields:
                    if field.name in [
                            'update_dt', 'date_done', 'action_time',
                            'date_changed'
                    ]:
                        field.auto_now = True

            print "FINISH at : %s" % datetime.now()
            print "Started at: %s" % start_dt
            print "Updated %s timestamp fields to utc with timezone support." % updated_field_count
            print "Updated %s timestamp values." % total_values_updated