def handle(self, *args, **options):
        if django.VERSION >= (1, 7, 0):
            raise RuntimeError('This command is only meant to be used for 1.6'
                               ' and older version of django. For 1.7, use'
                               ' `migrate_schemas` instead.')

        super(Command, self).handle(*args, **options)

        if "south" in settings.INSTALLED_APPS:
            self.options["migrate"] = False

        # save original settings
        for model in get_models(include_auto_created=True):
            setattr(model._meta, 'was_managed', model._meta.managed)

        ContentType.objects.clear_cache()

        if self.sync_public:
            self.sync_public_apps()
        if self.sync_tenant:
            self.sync_tenant_apps(self.schema_name)

        # restore settings
        for model in get_models(include_auto_created=True):
            model._meta.managed = model._meta.was_managed
def config(dataset):
    print 'graph_title ShotServer -', dataset.title()
    print 'graph_args --base 1000'
    print 'graph_category shotserver'
    if dataset == 'rates':
        print 'graph_period minute'
        print 'graph_vlabel /${graph_period}'
    elif dataset == 'counts':
        print 'graph_vlabel Totals'
    order = [model._meta.db_table
             for model in models.get_models()
             if dataset != 'rates' or has_id(model)]
    order.sort()
    print 'graph_order', ' '.join(order)
    for model in models.get_models():
        if dataset == 'rates' and not has_id(model):
            continue
        key = model._meta.db_table
        print key + '.label', model._meta.db_table
        print key + '.type', DATASET_TYPES[dataset]
        print key + '.min 0'
        # print 'max 5000'
        print key + '.draw LINE2'
        print key + '.info',
        if dataset == 'rates':
            print u'New %s per ${graph_period}' % (
                model._meta.verbose_name_plural)
        elif dataset == 'counts':
            print u'Total %s' % model._meta.verbose_name_plural
def sql_fix_table(app, drop_columns, style):
    if settings.DATABASE_ENGINE == 'dummy':
        # This must be the "dummy" database backend, which means the user
        # hasn't set DATABASE_ENGINE.
        raise CommandError("Django doesn't know which syntax to use for your SQL statements,\n" +
            "because you haven't specified the DATABASE_ENGINE setting.\n" +
            "Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'.")
    if settings.DATABASE_ENGINE != 'mysql':
        raise CommandError("This has only been tested with MySQL and probably doesn't work for others")

    all_models = models.get_models()
    if get_version() < '1.2':
        app_models = models.get_models(app)
    else:
        app_models = models.get_models(app, include_auto_created=True)
    pending_references = {}
    final_output = []

    # Fix up tables
    for model in app_models:
        # Handle opts.unique_together (removals) TODO.
        sql, references = sql_alter_table(connection, model, style, all_models, drop_columns)
        # Handle opts.unique_together (additions) TODO.
        # Handle pending references
        for refto, refs in references.items():
            pending_references.setdefault(refto, []).extend(refs)
            if refto in all_models:
                sql.extend(connection.creation.sql_for_pending_references(refto, style, pending_references))
        sql.extend(sql_new_many_to_many(connection, model, style, connection.introspection.table_names()))

        final_output.extend(sql) 
    return final_output
Example #4
0
 def handle(self, *args, **options):
     if not args:
         apps = [get_app(model._meta.app_label) for model in get_models()]
     else:
         apps = [get_app(arg) for arg in args]
     for app in apps:
         create_permissions(app, get_models(), options.get('verbosity', 0))
Example #5
0
def get_models_to_populate(app_labels):
    """ Gets a list of models for the given app labels, with some exceptions. 
    """

    # These models are not to be populated, e.g. because they can be generated automatically
    EXCLUDED_MODELS = ('contenttypes.ContentType', )

    models = []

    # If no app labels are given, return all but excluded
    if not app_labels:
        for app in get_apps():
            models.extend([ m for m in get_models(app) if full_model_name(m) not in EXCLUDED_MODELS ])

    # Get all relevant apps
    for app_label in app_labels:
        # If a specific model is mentioned, get only that model, even if it might be excluded
        if "." in app_label:
            app_label, model_name = app_label.split(".", 1)
            model = get_model(app_label, model_name)
            if model is None:
                raise CommandError("Unknown model: %s" % '.'.join((app_label, model_name)))
            models.append(model)
        # Get all models for a given app, except excluded
        else:
            try:
                app = get_app(app_label)
            except ImproperlyConfigured:
                raise CommandError("Unknown application: %s" % app_label)
            models.extend([ m for m in get_models(app) if full_model_name(m) not in EXCLUDED_MODELS ])

    return models
Example #6
0
def counts(reqest):
    app = get_app('diagnose_app')
    models =get_models(app, include_auto_created=True)
    models_name= [model._meta.db_table for model in get_models(app, include_auto_created=True)]
	
    #err= names.SH_PNA_ERROR 
    return render_to_response('counts.html',{'models_name':models_name})
Example #7
0
def init_plugins():
    from django.db.models import get_apps, get_models
    for app in get_apps():
        try:
            get_models(app)
        except:
            continue
Example #8
0
def get_app_models(app_labels=None):
    if app_labels is None:
        try:
            # django >= 1.7, to support AppConfig
            from django.apps import apps
            return apps.get_models(include_auto_created=True)
        except ImportError:
            from django.db import models
            return models.get_models(include_auto_created=True)

    if not isinstance(app_labels, (list, tuple, set)):
        app_labels = [app_labels]

    app_models = []
    try:
        # django >= 1.7, to support AppConfig
        from django.apps import apps

        for app_label in app_labels:
            app_config = apps.get_app_config(app_label)
            app_models.extend(app_config.get_models(include_auto_created=True))
    except ImportError:
        from django.db import models

        try:
            app_list = [models.get_app(app_label) for app_label in app_labels]
        except (models.ImproperlyConfigured, ImportError) as e:
            raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e)

        for app in app_list:
            app_models.extend(models.get_models(app, include_auto_created=True))

    return app_models
def process_meta_data():

    xl = pd.ExcelFile('migration_data/initial_data.xlsx')
    all_sheets = xl.sheet_names

    rhizome_app = get_app('rhizome')
    auth_app = get_app('auth')

    models_to_process = {}

    all_models = get_models(rhizome_app) + get_models(auth_app)

    for model in all_models:
        ## iterate through the models in the rhizome app and create a lookup
        ## for {'sheet_name': Model} .. for instance -> {'indicator': Indicator}

        if model._meta.db_table in all_sheets:
            models_to_process[model._meta.db_table] = model

    for sheet in all_sheets:
        ## if the sheet has a cooresponding model, create a data frame out of
        ## the sheet anf bulk insert the data using the model_df_to_data fn

        try:
            model = models_to_process[sheet]
            print 'processing sheet ' + sheet
            model_df = xl.parse(sheet)
            model_ids = model_df_to_data(model_df,model)
        except KeyError:
            pass

    ## once the locations are all created we need to ##
    ## cache them in the locaiton_tree table ##
    ltc = LocationTreeCache()
    ltc.main()
Example #10
0
    def handle(self, *app_labels, **options):
        from django import VERSION
        if VERSION[:2] < (1, 0):
            raise CommandError("SQLDiff only support Django 1.0 or higher!")

        from django.db import models
        from django.conf import settings

        if settings.DATABASE_ENGINE == 'dummy':
            # This must be the "dummy" database backend, which means the user
            # hasn't set DATABASE_ENGINE.
            raise CommandError("Django doesn't know which syntax to use for your SQL statements,\n" +
                "because you haven't specified the DATABASE_ENGINE setting.\n" +
                "Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'.")

        if options.get('all_applications', False):
            app_models = models.get_models()
        else:
            if not app_labels:
                raise CommandError('Enter at least one appname.')
            try:
                app_list = [models.get_app(app_label) for app_label in app_labels]
            except (models.ImproperlyConfigured, ImportError), e:
                raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e)

            app_models = []
            for app in app_list:
                app_models.extend(models.get_models(app))
Example #11
0
    def handle(self, *args, **options):
        fields_data = []
        if len(args) != 0:
            for arg in args:
                field_spec = arg.split('.')

                if len(field_spec) == 1:
                    app = get_app(field_spec[0])
                    models = get_models(app)
                    for model in models:
                        fields_data += all_fsm_fields_data(model)
                elif len(field_spec) == 2:
                    model = get_model(field_spec[0], field_spec[1])
                    fields_data += all_fsm_fields_data(model)
                elif len(field_spec) == 3:
                    model = get_model(field_spec[0], field_spec[1])
                    fields_data.append((model._meta.get_field_by_name(field_spec[2])[0], model))
        else:
            for app in get_apps():
                for model in get_models(app):
                    fields_data += all_fsm_fields_data(model)

        dotdata = generate_dot(fields_data)

        if options['outputfile']:
            self.render_output(dotdata, **options)
        else:
            print(dotdata)
Example #12
0
 def handle(self,*app_labels,**options):
     """
     Main execution point
     """
     if not settings.DEBUG:
         confirm=raw_input(confirm_message)
         if confirm != 'yes': return
     models=[]
     model_labels=[]
     apps=[]
     if app_labels and len(app_labels)>0: apps.extend(app_labels)
     if options.get("apps",False): apps.extend(options.get("apps"))
     if options.get("models",False): model_labels.extend(options.get("models"))
     for a in apps:
         app_label=a
         app=get_app(a)
         if len(model_labels)>0:
             app_models=get_models(app)
             for app_model in app_models:
                 meta=app_model._meta
                 for model_label in model_labels:
                     if meta.object_name==model_label and meta.app_label==app_label:
                         models.append(app_model)
         elif hasattr(app,"DillaController"):
             if hasattr(app.DillaController,"models"):
                 for model in app.DillaController.models:
                     if hasattr(app,model): models.append(getattr(app,model))
                     else: print "Model " + model + " not found."
         else:
             models.extend(get_models(app))
     instances_by_model={}
     for model in models:
         dilla=None
         if not instances_by_model.get(model,None):instances_by_model[model]=[]
         if hasattr(model,'Dilla'):dilla=model.Dilla
         if dilla and getattr(dilla,'skip_model',False):continue
         for i in range(int(options['iterations'])):
             instance=model()
             for field in model._meta.fields:
                 if not field.auto_created:
                     if not field.blank or options['no_doubt'] or hasattr(field,"auto_now") or hasattr(field,"auto_now_add"):
                         self.fill(field=field,obj=instance,dilla=dilla)
                     elif field.blank:
                         self._decide(self.fill,field=field,obj=instance,dilla=dilla)
             
             try:
                 instance.save()
                 #if field has unique, this error will be thrown, in the case of dilla, we don't care
             except MySQLdb.IntegrityError:
                 instance=None
                 continue
             instances_by_model[model].append(instance)
         print "SQL Debug :\n-----------------"
         print connection.queries
     for model in models: #go back through each model, and alter each instance's many to many fields
         if len(model._meta.many_to_many) <= 0: continue
         instances_list=instances_by_model[model]
         dilla=getattr(model,'Dilla',False)
         self.many_to_manys(model,instances_list,dilla)
Example #13
0
def _setup_django():
    import django
    if hasattr(django, 'setup'):
        django.setup()
    else:
        # Emulate Django 1.7 django.setup() with get_models
        from django.db.models import get_models
        get_models()
    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Don't use "from appname.models import ModelName". 
        # Use orm.ModelName to refer to models in this application,
        # and orm['appname.ModelName'] for models in other applications.

        update_contenttypes(get_app('activities'), get_models())
        create_permissions(get_app('activities'), get_models(), 0)
Example #15
0
 def search(self, *args, **kwargs):
     """
     Proxy to queryset's search method for the manager's model and
     any models that subclass from this manager's model if the
     model is abstract.
     """
     if not settings.SEARCH_MODEL_CHOICES:
         # No choices defined - build a list of leaf models (those
         # without subclasses) that inherit from Displayable.
         models = [m for m in get_models() if issubclass(m, self.model)]
         parents = reduce(ior, [m._meta.get_parent_list() for m in models])
         models = [m for m in models if m not in parents]
     elif getattr(self.model._meta, "abstract", False):
         # When we're combining model subclasses for an abstract
         # model (eg Displayable), we only want to use models that
         # are represented by the ``SEARCH_MODEL_CHOICES`` setting.
         # Now this setting won't contain an exact list of models
         # we should use, since it can define superclass models such
         # as ``Page``, so we check the parent class list of each
         # model when determining whether a model falls within the
         # ``SEARCH_MODEL_CHOICES`` setting.
         search_choices = set([get_model(*name.split(".", 1)) for name in
                               settings.SEARCH_MODEL_CHOICES])
         models = set()
         parents = set()
         for model in get_models():
             # Model is actually a subclasses of what we're
             # searching (eg Displayabale)
             is_subclass = issubclass(model, self.model)
             # Model satisfies the search choices list - either
             # there are no search choices, model is directly in
             # search choices, or its parent is.
             this_parents = set(model._meta.get_parent_list())
             in_choices = not search_choices or model in search_choices
             in_choices = in_choices or this_parents & search_choices
             if is_subclass and (in_choices or not search_choices):
                 # Add to models we'll seach. Also maintain a parent
                 # set, used below for further refinement of models
                 # list to search.
                 models.add(model)
                 parents.update(this_parents)
         # Strip out any models that are superclasses of models,
         # specifically the Page model which will generally be the
         # superclass for all custom content types, since if we
         # query the Page model as well, we will get duplicate
         # results.
         models -= parents
     else:
         models = [self.model]
     all_results = []
     user = kwargs.pop("for_user", None)
     for model in models:
         try:
             queryset = model.objects.published(for_user=user)
         except AttributeError:
             queryset = model.objects.get_query_set()
         all_results.extend(queryset.search(*args, **kwargs))
     return sorted(all_results, key=lambda r: r.result_count, reverse=True)
Example #16
0
 def get_all_models_in_app_from_table_name(self, table_name):
     #print 'table_name', table_name
     from django.db import models
     for app in models.get_apps():
         app_name = app.__name__.split('.')[-2]
         if table_name.startswith(app_name):
             for model in models.get_models(app):
                 if model._meta.db_table == table_name:
                     return set(models.get_models(app))
     return set()
    def handle(self, *app_labels, **options):

        # Activate project's default language
        translation.activate(settings.LANGUAGE_CODE)

        comment = options["comment"]
        batch_size = options["batch_size"]
        database = options.get('database')

        verbosity = int(options.get("verbosity", 1))
        app_list = OrderedDict()
        # if no apps given, use all installed.
        if len(app_labels) == 0:
            for app in get_apps():
                if not app in app_list:
                    app_list[app] = []
                for model_class in get_models(app):
                    if not model_class in app_list[app]:
                        app_list[app].append(model_class)
        else:
            for label in app_labels:
                try:
                    app_label, model_label = label.split(".")
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)

                    model_class = get_model(app_label, model_label)
                    if model_class is None:
                        raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
                    if app in app_list:
                        if app_list[app] and model_class not in app_list[app]:
                            app_list[app].append(model_class)
                    else:
                        app_list[app] = [model_class]
                except ValueError:
                    # This is just an app - no model qualifier.
                    app_label = label
                    try:
                        app = get_app(app_label)
                        if not app in app_list:
                            app_list[app] = []
                        for model_class in get_models(app):
                            if not model_class in app_list[app]:
                                app_list[app].append(model_class)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)
        # Create revisions.
        for app, model_classes in app_list.items():
            for model_class in model_classes:
                self.create_initial_revisions(app, model_class, comment, batch_size, verbosity, database=database)

        # Go back to default language
        translation.deactivate()
Example #18
0
    def handle(self, *app_labels, **options):
        from django import VERSION
        if VERSION[:2] < (1, 0):
            raise CommandError("SQLDiff only support Django 1.0 or higher!")

        from django.db import models
        from django.conf import settings

        engine = None
        if hasattr(settings, 'DATABASES'):
            engine = settings.DATABASES['default']['ENGINE']
        else:
            engine = settings.DATABASE_ENGINE

        if engine == 'dummy':
            # This must be the "dummy" database backend, which means the user
            # hasn't set DATABASE_ENGINE.
            raise CommandError("""Django doesn't know which syntax to use for your SQL statements,
because you haven't specified the DATABASE_ENGINE setting.
Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'.""")

        if options.get('all_applications', False):
            app_models = models.get_models()
        else:
            if not app_labels:
                raise CommandError('Enter at least one appname.')
            try:
                app_list = [models.get_app(app_label)
                            for app_label in app_labels]
            except (models.ImproperlyConfigured, ImportError) as e:
                raise CommandError(
                    "%s. Are you sure your INSTALLED_APPS setting is correct?" %
                    e)

            app_models = []
            for app in app_list:
                app_models.extend(models.get_models(app))

        # remove all models that are not managed by Django
        #app_models = [model for model in app_models if getattr(model._meta, 'managed', True)]

        if not app_models:
            raise CommandError('Unable to execute sqldiff no models founds.')

        if not engine:
            engine = connection.__module__.split('.')[-2]

        if '.' in engine:
            engine = engine.split('.')[-1]

        cls = DATABASE_SQLDIFF_CLASSES.get(engine, GenericSQLDiff)
        sqldiff_instance = cls(app_models, options)
        sqldiff_instance.find_differences()
        sqldiff_instance.print_diff(self.style)
        return
Example #19
0
File: urls.py Project: 755/sentry
def init_all_applications():
    """
    Forces import of all applications to ensure code is registered.
    """
    from django.db.models import get_apps, get_models

    for app in get_apps():
        try:
            get_models(app)
        except Exception:
            continue
Example #20
0
    def handle_app(self, app, **options):
        from django.db.models import get_models

        lines = []
        if options["model"]:
            model = [m for m in get_models(app) if m.__name__ == options["model"][0]][0]
            return "[%s]" % options["model"][0] + "\n\t" + "\n\t".join([str(o) for o in model.objects.all()])
        for model in get_models(app):
            obj = model.objects.all()
            lines.append("[%s]" % model.__name__ + "\n\t" + "\n\t".join([str(o) for o in obj]))
        return "\n".join(lines)
Example #21
0
    def handle(self, *args, **options):
        if not args:
            apps = []
            for model in get_models():
                try:
                    apps.append(get_app(model._meta.app_label))
                except ImproperlyConfigured:
                    pass
        else:
            apps = []
            for arg in args:
                apps.append(get_app(arg))
        
        table_dict = {}
        cursor = connection.cursor()
        
        for app in apps:
            for model in get_models(app):
                key = '%s.%s' %(model._meta.app_label, model.__name__)
                if table_dict.has_key(key):
                    continue
                tbl = model._meta.db_table
                sql = self.STATEMENT % tbl
                cursor.execute(sql)
                row = cursor.fetchone()
                dbtbl = DBTable(model, row)
                table_dict[key] = dbtbl

        tables = table_dict.values()
        tables.sort()
        max = options['max']
        print '\n', '-'*75
        print 'Table Sizes:'
        print '-'*75
        for i, table in enumerate(tables):
            if max and i >= max:
                break
            print str(table)

        db_dict = {}
        for name, dbsettings in settings.DATABASES.items():
            sql = self.DB_STATEMENT % dbsettings['NAME']
            cursor.execute(sql)
            row = cursor.fetchone()
            dbtbl = DBTable(name, row)
            db_dict[name]  = dbtbl

        databases = db_dict.values()
        databases.sort()
        print '\n', '-'*75
        print 'Database Sizes:'
        print '-'*75
        for db in databases:
            print str(db)
    def handle(self, *args, **options):

        if options['stdout']:
            for model in get_models():
                stdout.write("%s.%s %d\n" %
                             (model.__module__, model.__name__,
                              model.objects.count()))
        if options['stderr']:
            for model in get_models():
                stderr.write("Error: %s.%s %d\n" %
                             (model.__module__, model.__name__,
                              model.objects.count()))
Example #23
0
File: core.py Project: kataev/bkz
 def admin(self):
     name = self.args[1]
     app = get_app(name)
     print "# -*- coding: utf-8 -*-"
     print "from django.contrib import admin"
     print "from {}.models import *\n\n".format(name)
     for model in get_models(app):
         print 'class {}Admin(admin.ModelAdmin):'.format(model.__name__)
         print "    pass\n"
     print ''
     for model in get_models(app):
         print 'admin.site.register({},{}Admin)'.format(model.__name__, model.__name__)
    def _set_managed_apps(self, included_apps):
        """ sets which apps are managed by syncdb """
        for model in get_models(include_auto_created=True):
            model._meta.managed = False

        verbosity = int(self.options.get('verbosity'))
        for app_model in get_apps():
            app_name = app_model.__name__.replace('.models', '')
            if hasattr(app_model, 'models') and app_name in included_apps:
                for model in get_models(app_model, include_auto_created=True):
                    model._meta.managed = True and model._meta.was_managed
                    if model._meta.managed and verbosity >= 3:
                        print self.style.NOTICE("=== Include Model: %s: %s" % (app_name, model.__name__))
Example #25
0
 def handle(self, *args, **options):
     print 'Sincronizando permisos...'
     if not args:
         apps = []
         for model in get_models():
             apps.append(get_app(model._meta.app_label))
     else:
         apps = []
         for arg in args:
             apps.append(get_app(arg))
     for app in apps:
         create_permissions(app, get_models(), options.get('verbosity', 0))
     print 'Permisos sincronizados'
Example #26
0
    def handle(self, *args, **options):
        if not args:
            apps = []
            for model in get_models():
                apps.append(get_app(model._meta.app_label))
        else:
            apps = []
            for arg in args:
                apps.append(get_app(arg))

        for app in apps:
            update_contenttypes(app, None, options.get('verbosity', 2), interactive=True)
            create_permissions(app, get_models(), options.get('verbosity', 0))
def _setup_django():
    if 'django' not in sys.modules:
        return

    import django

    if hasattr(django, 'setup'):
        django.setup()
    else:
        # Emulate Django 1.7 django.setup() with get_models
        from django.db.models import get_models

        get_models()
    def _set_managed_apps(self, included_apps):
        for model in get_models(include_auto_created=True):
            model._meta.managed = False

        verbosity = int(self.options.get('verbosity'))
        for app_model in get_apps():
            app_name = app_model.__name__.replace('.models', '')
            if hasattr(app_model, 'models') and app_name in included_apps:
                for model in get_models(app_model, include_auto_created=True):
                    #Doublecheck (get_models is not reliable)
                    model._meta.managed = True and model.__module__ == app_model.__name__ and model._meta.was_managed
                    if model._meta.managed and verbosity >= 3:
                        print self.style.NOTICE("=== Include Model: %s: %s" % (app_name, model.__name__))
Example #29
0
    def handle(self, *app_labels, **options):
        from django.db import models
        from django.conf import settings

        engine = None
        if hasattr(settings, "DATABASES"):
            engine = settings.DATABASES["default"]["ENGINE"]
        else:
            engine = settings.DATABASE_ENGINE

        if engine == "dummy":
            # This must be the "dummy" database backend, which means the user
            # hasn't set DATABASE_ENGINE.
            raise CommandError(
                """Django doesn't know which syntax to use for your SQL statements,
because you haven't specified the DATABASE_ENGINE setting.
Edit your settings file and change DATABASE_ENGINE to something like 'postgresql' or 'mysql'."""
            )

        if options.get("all_applications", False):
            app_models = models.get_models(include_auto_created=True)
        else:
            if not app_labels:
                raise CommandError("Enter at least one appname.")
            try:
                app_list = [models.get_app(app_label) for app_label in app_labels]
            except (models.ImproperlyConfigured, ImportError) as e:
                raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e)

            app_models = []
            for app in app_list:
                app_models.extend(models.get_models(app, include_auto_created=True))

        ## remove all models that are not managed by Django
        # app_models = [model for model in app_models if getattr(model._meta, 'managed', True)]

        if not app_models:
            raise CommandError("Unable to execute sqldiff no models founds.")

        if not engine:
            engine = connection.__module__.split(".")[-2]

        if "." in engine:
            engine = engine.split(".")[-1]

        cls = DATABASE_SQLDIFF_CLASSES.get(engine, GenericSQLDiff)
        sqldiff_instance = cls(app_models, options)
        sqldiff_instance.find_differences()
        if not sqldiff_instance.has_differences:
            self.exit_code = 0
        sqldiff_instance.print_diff(self.style)
Example #30
0
    def handle_noargs(self, **options):

        call_command('syncdb', database=self.DB, interactive=False,
                migrate_all=True)

        # reset data in needed models
        for reset_model in self.reset_models:
            model = get_model(*reset_model.split('.'))
            model.objects.using(self.DB).delete()

        ignore_models = []
        ignore_apps = []

        self.verbosity = int(options.get('verbosity', 1))

        for label in self.ignore_models:
            to_ignore = label.split('.')

            if len(to_ignore) == 1:
                ignore_apps.append(label)
            else:
                ignore_models.append(to_ignore)

        only_latest = [x.split('.') for x in self.only_latest]

        app_list = get_apps()
        untracked_m2m = []

        all_models = get_models()

        for app in app_list:
            for model in get_models(app):
                app_label = model._meta.app_label
                module_name = model._meta.module_name

                name_pair = [app_label, module_name]

                if not (app_label in ignore_apps or name_pair in ignore_models):

                    # m2m fields without through table won't be tracked here, so we
                    # need to collect it and handle it later
                    for field in model._meta.many_to_many:
                        try:
                            through_model = getattr(model, field.name).through
                            if through_model not in all_models:
                                untracked_m2m.append(through_model)
                        # ReverseGenericRelatedObjectsDescriptor have no
                        # through attribute, ignore
                        except AttributeError,e :
                            pass
                    self.sync_model(model, name_pair in only_latest)
Example #31
0
 def check_meta(self, table_name):
     return table_name in [m._meta.db_table for m in models.get_models()
                           ]  #caching provided by Django
 def get_all_apps():
     apps = set()
     for model in get_models():
         apps.add(get_app(model._meta.app_label))
     return apps
Example #33
0
def get_validation_errors(outfile, app=None):
    """
    Validates all models that are part of the specified app. If no app name is provided,
    validates all models of all installed apps. Writes errors, if any, to outfile.
    Returns number of errors.
    """
    from django.db import models, connection
    from django.db.models.loading import get_app_errors
    from django.db.models.deletion import SET_NULL, SET_DEFAULT

    e = ModelErrorCollection(outfile)

    for (app_name, error) in get_app_errors().items():
        e.add(app_name, error)

    for cls in models.get_models(app, include_swapped=True):
        opts = cls._meta

        # Check swappable attribute.
        if opts.swapped:
            try:
                app_label, model_name = opts.swapped.split('.')
            except ValueError:
                e.add(
                    opts, "%s is not of the form 'app_label.app_name'." %
                    opts.swappable)
                continue
            if not models.get_model(app_label, model_name):
                e.add(
                    opts,
                    "Model has been swapped out for '%s' which has not been installed or is abstract."
                    % opts.swapped)
            # No need to perform any other validation checks on a swapped model.
            continue

        # If this is the current User model, check known validation problems with User models
        if settings.AUTH_USER_MODEL == '%s.%s' % (opts.app_label,
                                                  opts.object_name):
            # Check that REQUIRED_FIELDS is a list
            if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):
                e.add(opts, 'The REQUIRED_FIELDS must be a list or tuple.')

            # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS.
            if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
                e.add(
                    opts,
                    'The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.'
                )

            # Check that the username field is unique
            if not opts.get_field(cls.USERNAME_FIELD).unique:
                e.add(
                    opts,
                    'The USERNAME_FIELD must be unique. Add unique=True to the field parameters.'
                )

        # Model isn't swapped; do field-specific validation.
        for f in opts.local_fields:
            if f.name == 'id' and not f.primary_key and opts.pk.name == 'id':
                e.add(
                    opts,
                    '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.'
                    % f.name)
            if f.name.endswith('_'):
                e.add(
                    opts,
                    '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.'
                    % f.name)
            if (f.primary_key and f.null and
                    not connection.features.interprets_empty_strings_as_nulls):
                # We cannot reliably check this for backends like Oracle which
                # consider NULL and '' to be equal (and thus set up
                # character-based fields a little differently).
                e.add(
                    opts,
                    '"%s": Primary key fields cannot have null=True.' % f.name)
            if isinstance(f, models.CharField):
                try:
                    max_length = int(f.max_length)
                    if max_length <= 0:
                        e.add(
                            opts,
                            '"%s": CharFields require a "max_length" attribute that is a positive integer.'
                            % f.name)
                except (ValueError, TypeError):
                    e.add(
                        opts,
                        '"%s": CharFields require a "max_length" attribute that is a positive integer.'
                        % f.name)
            if isinstance(f, models.DecimalField):
                decimalp_ok, mdigits_ok = False, False
                decimalp_msg = '"%s": DecimalFields require a "decimal_places" attribute that is a non-negative integer.'
                try:
                    decimal_places = int(f.decimal_places)
                    if decimal_places < 0:
                        e.add(opts, decimalp_msg % f.name)
                    else:
                        decimalp_ok = True
                except (ValueError, TypeError):
                    e.add(opts, decimalp_msg % f.name)
                mdigits_msg = '"%s": DecimalFields require a "max_digits" attribute that is a positive integer.'
                try:
                    max_digits = int(f.max_digits)
                    if max_digits <= 0:
                        e.add(opts, mdigits_msg % f.name)
                    else:
                        mdigits_ok = True
                except (ValueError, TypeError):
                    e.add(opts, mdigits_msg % f.name)
                invalid_values_msg = '"%s": DecimalFields require a "max_digits" attribute value that is greater than or equal to the value of the "decimal_places" attribute.'
                if decimalp_ok and mdigits_ok:
                    if decimal_places > max_digits:
                        e.add(opts, invalid_values_msg % f.name)
            if isinstance(f, models.FileField) and not f.upload_to:
                e.add(
                    opts,
                    '"%s": FileFields require an "upload_to" attribute.' %
                    f.name)
            if isinstance(f, models.ImageField):
                try:
                    from django.utils.image import Image
                except ImportError:
                    e.add(
                        opts,
                        '"%s": To use ImageFields, you need to install Pillow. Get it at https://pypi.python.org/pypi/Pillow.'
                        % f.name)
            if isinstance(f, models.BooleanField) and getattr(
                    f, 'null', False):
                e.add(
                    opts,
                    '"%s": BooleanFields do not accept null values. Use a NullBooleanField instead.'
                    % f.name)
            if isinstance(f, models.FilePathField) and not (f.allow_files or
                                                            f.allow_folders):
                e.add(
                    opts,
                    '"%s": FilePathFields must have either allow_files or allow_folders set to True.'
                    % f.name)
            if isinstance(f, models.GenericIPAddressField) and not getattr(
                    f, 'null', False) and getattr(f, 'blank', False):
                e.add(
                    opts,
                    '"%s": GenericIPAddressField can not accept blank values if null values are not allowed, as blank values are stored as null.'
                    % f.name)
            if f.choices:
                if isinstance(f.choices,
                              six.string_types) or not is_iterable(f.choices):
                    e.add(
                        opts,
                        '"%s": "choices" should be iterable (e.g., a tuple or list).'
                        % f.name)
                else:
                    for c in f.choices:
                        if isinstance(c, six.string_types
                                      ) or not is_iterable(c) or len(c) != 2:
                            e.add(
                                opts,
                                '"%s": "choices" should be a sequence of two-item iterables (e.g. list of 2 item tuples).'
                                % f.name)
            if f.db_index not in (None, True, False):
                e.add(
                    opts,
                    '"%s": "db_index" should be either None, True or False.' %
                    f.name)

            # Perform any backend-specific field validation.
            connection.validation.validate_field(e, opts, f)

            # Check if the on_delete behavior is sane
            if f.rel and hasattr(f.rel, 'on_delete'):
                if f.rel.on_delete == SET_NULL and not f.null:
                    e.add(
                        opts,
                        "'%s' specifies on_delete=SET_NULL, but cannot be null."
                        % f.name)
                elif f.rel.on_delete == SET_DEFAULT and not f.has_default():
                    e.add(
                        opts,
                        "'%s' specifies on_delete=SET_DEFAULT, but has no default value."
                        % f.name)

            # Check to see if the related field will clash with any existing
            # fields, m2m fields, m2m related objects or related objects
            if f.rel:
                if f.rel.to not in models.get_models():
                    # If the related model is swapped, provide a hint;
                    # otherwise, the model just hasn't been installed.
                    if not isinstance(
                            f.rel.to,
                            six.string_types) and f.rel.to._meta.swapped:
                        e.add(
                            opts,
                            "'%s' defines a relation with the model '%s.%s', which has been swapped out. Update the relation to point at settings.%s."
                            % (f.name, f.rel.to._meta.app_label,
                               f.rel.to._meta.object_name,
                               f.rel.to._meta.swappable))
                    else:
                        e.add(
                            opts,
                            "'%s' has a relation with model %s, which has either not been installed or is abstract."
                            % (f.name, f.rel.to))
                # it is a string and we could not find the model it refers to
                # so skip the next section
                if isinstance(f.rel.to, six.string_types):
                    continue

                # Make sure the related field specified by a ForeignKey is unique
                if f.requires_unique_target:
                    if len(f.foreign_related_fields) > 1:
                        has_unique_field = False
                        for rel_field in f.foreign_related_fields:
                            has_unique_field = has_unique_field or rel_field.unique
                        if not has_unique_field:
                            e.add(
                                opts,
                                "Field combination '%s' under model '%s' must have a unique=True constraint"
                                % (','.join([
                                    rel_field.name
                                    for rel_field in f.foreign_related_fields
                                ]), f.rel.to.__name__))
                    else:
                        if not f.foreign_related_fields[0].unique:
                            e.add(
                                opts,
                                "Field '%s' under model '%s' must have a unique=True constraint."
                                % (f.foreign_related_fields[0].name,
                                   f.rel.to.__name__))

                rel_opts = f.rel.to._meta
                rel_name = f.related.get_accessor_name()
                rel_query_name = f.related_query_name()
                if not f.rel.is_hidden():
                    for r in rel_opts.fields:
                        if r.name == rel_name:
                            e.add(
                                opts,
                                "Accessor for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                %
                                (f.name, rel_opts.object_name, r.name, f.name))
                        if r.name == rel_query_name:
                            e.add(
                                opts,
                                "Reverse query name for field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                %
                                (f.name, rel_opts.object_name, r.name, f.name))
                    for r in rel_opts.local_many_to_many:
                        if r.name == rel_name:
                            e.add(
                                opts,
                                "Accessor for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                %
                                (f.name, rel_opts.object_name, r.name, f.name))
                        if r.name == rel_query_name:
                            e.add(
                                opts,
                                "Reverse query name for field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                %
                                (f.name, rel_opts.object_name, r.name, f.name))
                    for r in rel_opts.get_all_related_many_to_many_objects():
                        if r.get_accessor_name() == rel_name:
                            e.add(
                                opts,
                                "Accessor for field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                % (f.name, r.model._meta.object_name,
                                   r.field.name, f.name))
                        if r.get_accessor_name() == rel_query_name:
                            e.add(
                                opts,
                                "Reverse query name for field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                % (f.name, r.model._meta.object_name,
                                   r.field.name, f.name))
                    for r in rel_opts.get_all_related_objects():
                        if r.field is not f:
                            if r.get_accessor_name() == rel_name:
                                e.add(
                                    opts,
                                    "Accessor for field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                    % (f.name, r.model._meta.object_name,
                                       r.field.name, f.name))
                            if r.get_accessor_name() == rel_query_name:
                                e.add(
                                    opts,
                                    "Reverse query name for field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                    % (f.name, r.model._meta.object_name,
                                       r.field.name, f.name))

        seen_intermediary_signatures = []
        for i, f in enumerate(opts.local_many_to_many):
            # Check to see if the related m2m field will clash with any
            # existing fields, m2m fields, m2m related objects or related
            # objects
            if f.rel.to not in models.get_models():
                # If the related model is swapped, provide a hint;
                # otherwise, the model just hasn't been installed.
                if not isinstance(f.rel.to,
                                  six.string_types) and f.rel.to._meta.swapped:
                    e.add(
                        opts,
                        "'%s' defines a relation with the model '%s.%s', which has been swapped out. Update the relation to point at settings.%s."
                        %
                        (f.name, f.rel.to._meta.app_label,
                         f.rel.to._meta.object_name, f.rel.to._meta.swappable))
                else:
                    e.add(
                        opts,
                        "'%s' has an m2m relation with model %s, which has either not been installed or is abstract."
                        % (f.name, f.rel.to))

                # it is a string and we could not find the model it refers to
                # so skip the next section
                if isinstance(f.rel.to, six.string_types):
                    continue

            # Check that the field is not set to unique.  ManyToManyFields do not support unique.
            if f.unique:
                e.add(
                    opts,
                    "ManyToManyFields cannot be unique.  Remove the unique argument on '%s'."
                    % f.name)

            if f.rel.through is not None and not isinstance(
                    f.rel.through, six.string_types):
                from_model, to_model = cls, f.rel.to
                if from_model == to_model and f.rel.symmetrical and not f.rel.through._meta.auto_created:
                    e.add(
                        opts,
                        "Many-to-many fields with intermediate tables cannot be symmetrical."
                    )
                seen_from, seen_to, seen_self = False, False, 0
                for inter_field in f.rel.through._meta.fields:
                    rel_to = getattr(inter_field.rel, 'to', None)
                    if from_model == to_model:  # relation to self
                        if rel_to == from_model:
                            seen_self += 1
                        if seen_self > 2:
                            e.add(
                                opts, "Intermediary model %s has more than "
                                "two foreign keys to %s, which is ambiguous "
                                "and is not permitted." %
                                (f.rel.through._meta.object_name,
                                 from_model._meta.object_name))
                    else:
                        if rel_to == from_model:
                            if seen_from:
                                e.add(
                                    opts, "Intermediary model %s has more "
                                    "than one foreign key to %s, which is "
                                    "ambiguous and is not permitted." %
                                    (f.rel.through._meta.object_name,
                                     from_model._meta.object_name))
                            else:
                                seen_from = True
                        elif rel_to == to_model:
                            if seen_to:
                                e.add(
                                    opts, "Intermediary model %s has more "
                                    "than one foreign key to %s, which is "
                                    "ambiguous and is not permitted." %
                                    (f.rel.through._meta.object_name,
                                     rel_to._meta.object_name))
                            else:
                                seen_to = True
                if f.rel.through not in models.get_models(
                        include_auto_created=True):
                    e.add(
                        opts, "'%s' specifies an m2m relation through model "
                        "%s, which has not been installed." %
                        (f.name, f.rel.through))
                signature = (f.rel.to, cls, f.rel.through)
                if signature in seen_intermediary_signatures:
                    e.add(
                        opts, "The model %s has two manually-defined m2m "
                        "relations through the model %s, which is not "
                        "permitted. Please consider using an extra field on "
                        "your intermediary model instead." %
                        (cls._meta.object_name,
                         f.rel.through._meta.object_name))
                else:
                    seen_intermediary_signatures.append(signature)
                if not f.rel.through._meta.auto_created:
                    seen_related_fk, seen_this_fk = False, False
                    for field in f.rel.through._meta.fields:
                        if field.rel:
                            if not seen_related_fk and field.rel.to == f.rel.to:
                                seen_related_fk = True
                            elif field.rel.to == cls:
                                seen_this_fk = True
                    if not seen_related_fk or not seen_this_fk:
                        e.add(
                            opts, "'%s' is a manually-defined m2m relation "
                            "through model %s, which does not have foreign keys "
                            "to %s and %s" %
                            (f.name, f.rel.through._meta.object_name,
                             f.rel.to._meta.object_name,
                             cls._meta.object_name))
            elif isinstance(f.rel.through, six.string_types):
                e.add(
                    opts, "'%s' specifies an m2m relation through model %s, "
                    "which has not been installed" % (f.name, f.rel.through))

            rel_opts = f.rel.to._meta
            rel_name = f.related.get_accessor_name()
            rel_query_name = f.related_query_name()
            # If rel_name is none, there is no reverse accessor (this only
            # occurs for symmetrical m2m relations to self). If this is the
            # case, there are no clashes to check for this field, as there are
            # no reverse descriptors for this field.
            if rel_name is not None:
                for r in rel_opts.fields:
                    if r.name == rel_name:
                        e.add(
                            opts,
                            "Accessor for m2m field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, rel_opts.object_name, r.name, f.name))
                    if r.name == rel_query_name:
                        e.add(
                            opts,
                            "Reverse query name for m2m field '%s' clashes with field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, rel_opts.object_name, r.name, f.name))
                for r in rel_opts.local_many_to_many:
                    if r.name == rel_name:
                        e.add(
                            opts,
                            "Accessor for m2m field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, rel_opts.object_name, r.name, f.name))
                    if r.name == rel_query_name:
                        e.add(
                            opts,
                            "Reverse query name for m2m field '%s' clashes with m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, rel_opts.object_name, r.name, f.name))
                for r in rel_opts.get_all_related_many_to_many_objects():
                    if r.field is not f:
                        if r.get_accessor_name() == rel_name:
                            e.add(
                                opts,
                                "Accessor for m2m field '%s' clashes with accessor for m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                % (f.name, r.model._meta.object_name,
                                   r.field.name, f.name))
                        if r.get_accessor_name() == rel_query_name:
                            e.add(
                                opts,
                                "Reverse query name for m2m field '%s' clashes with accessor for m2m field '%s.%s'. Add a related_name argument to the definition for '%s'."
                                % (f.name, r.model._meta.object_name,
                                   r.field.name, f.name))
                for r in rel_opts.get_all_related_objects():
                    if r.get_accessor_name() == rel_name:
                        e.add(
                            opts,
                            "Accessor for m2m field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, r.model._meta.object_name, r.field.name,
                               f.name))
                    if r.get_accessor_name() == rel_query_name:
                        e.add(
                            opts,
                            "Reverse query name for m2m field '%s' clashes with accessor for field '%s.%s'. Add a related_name argument to the definition for '%s'."
                            % (f.name, r.model._meta.object_name, r.field.name,
                               f.name))

        # Check ordering attribute.
        if opts.ordering:
            for field_name in opts.ordering:
                if field_name == '?':
                    continue
                if field_name.startswith('-'):
                    field_name = field_name[1:]
                if opts.order_with_respect_to and field_name == '_order':
                    continue
                # Skip ordering in the format field1__field2 (FIXME: checking
                # this format would be nice, but it's a little fiddly).
                if '__' in field_name:
                    continue
                # Skip ordering on pk. This is always a valid order_by field
                # but is an alias and therefore won't be found by opts.get_field.
                if field_name == 'pk':
                    continue
                try:
                    opts.get_field(field_name, many_to_many=False)
                except models.FieldDoesNotExist:
                    e.add(
                        opts,
                        '"ordering" refers to "%s", a field that doesn\'t exist.'
                        % field_name)

        # Check unique_together.
        for ut in opts.unique_together:
            validate_local_fields(e, opts, "unique_together", ut)
        if not isinstance(opts.index_together, collections.Sequence):
            e.add(opts, '"index_together" must a sequence')
        else:
            for it in opts.index_together:
                validate_local_fields(e, opts, "index_together", it)

    return len(e.errors)
Example #34
0
    def handle_app(self, app, **options):
        from django.db.models import get_models
        from haystack.exceptions import NotRegistered

        site = get_site(self.site)

        if self.workers > 0:
            import multiprocessing

        for model in get_models(app):
            try:
                index = site.get_index(model)
            except NotRegistered:
                if self.verbosity >= 2:
                    print "Skipping '%s' - no index." % model
                continue

            qs = build_queryset(index,
                                model,
                                age=self.age,
                                verbosity=self.verbosity)
            total = qs.count()

            if self.verbosity >= 1:
                print "Indexing %d %s." % (
                    total, smart_str(model._meta.verbose_name_plural))

            pks_seen = set(
                [smart_str(pk) for pk in qs.values_list('pk', flat=True)])

            if self.workers > 0:
                ghetto_queue = []

            for start in range(0, total, self.batchsize):
                end = min(start + self.batchsize, total)

                if self.workers == 0:
                    do_update(index, qs, start, end, total, self.verbosity)
                else:
                    ghetto_queue.append(('do_update', model, start, end, total,
                                         self.site, self.age, self.verbosity))

            if self.workers > 0:
                pool = multiprocessing.Pool(self.workers)
                pool.map(worker, ghetto_queue)

            if self.remove:
                if self.age or total <= 0:
                    # They're using a reduced set, which may not incorporate
                    # all pks. Rebuild the list with everything.
                    qs = index.index_queryset().values_list('pk', flat=True)
                    pks_seen = set([smart_str(pk) for pk in qs])
                    total = len(pks_seen)

                if self.workers > 0:
                    ghetto_queue = []

                for start in range(0, total, self.batchsize):
                    upper_bound = start + self.batchsize

                    if self.workers == 0:
                        do_remove(index, model, pks_seen, start, upper_bound)
                    else:
                        ghetto_queue.append(
                            ('do_remove', model, pks_seen, start, upper_bound,
                             self.site, self.verbosity))

                if self.workers > 0:
                    pool = multiprocessing.Pool(self.workers)
                    pool.map(worker, ghetto_queue)
Example #35
0
 def process_request(self, request):
     self._tracker = ClassTracker()
     for cls in get_models() + self.classes:
         self._tracker.track_class(cls)
     self._tracker.create_snapshot('before')
     self.record_stats({'before': ProcessMemoryInfo()})
Example #36
0
    def _execute(self):
        from gaeapi.appengine.ext import gql
        if self._cursor:
            raise Error('Already executed.')
        # Make sql local just for traceback
        sql = self._sql
        from django.db import models
        # First, let's see if the class is explicitely given.
        # E.g. Model.gql('xxx') set's _real_cls.
        cls = self._real_cls
        if cls is None:
            for xcls in models.get_models():
                if (xcls.__name__ == self._gql._entity \
                    or xcls._meta.db_table in self._sql) \
                and not xcls.__module__.startswith('django.'):
                    cls = xcls
                    break
        if not cls:
            raise Error('Class not found.')
        q = cls.objects.all()
        q = q.select_related()
        #print '-'*10
        #print "xx", sql, self._args, self._kwds
        ancestor = None
        listprop_filter = []
        for key, value in self._gql.filters().items():
            #print key, value
            kwd, op = key
            if op == '=':
                if cls._meta.get_field(kwd).rel:
                    rel_cls = cls._meta.get_field(kwd).rel.to
                else:
                    rel_cls = None
                for xop, val in value:
                    # FIXME: Handle lists...
                    item = val[0]

                    if isinstance(item, gql.Literal):
                        #print 'Literal', item
                        item = item.Get()
                        #print '-->', item
                    elif isinstance(item, basestring):
                        #print 'Keyword', item
                        item = self._kwds[item]
                        #print '-->', item
                    elif isinstance(item, int):
                        #print 'Positional', item
                        item = self._args[item - 1]
                        #print '-->', item
                    else:
                        raise Error('Unhandled args %s' % item)


#                    if rel_cls:
#                        # FIXME: Handle lists
#                        try:
#                            item = rel_cls.objects.get(id=item)
#                        except rel_cls.DoesNotExist:
#                            continue
                    if isinstance(cls._meta.get_field(kwd), ListProperty):
                        listprop_filter.append((kwd, item))
                        continue
                    if isinstance(kwd, unicode):
                        kwd = kwd.encode('ascii')
                    q = q._filter(**{kwd: item})
            elif op == 'is' and kwd == -1:  # ANCESTOR
                if ancestor:
                    raise Error('Ancestor already defined: %s' % ancestor)
                item = value[0][1][0]
                if isinstance(item, basestring):
                    ancestor = self._kwds[item]
                elif isinstance(item, int):
                    ancestor = self._args[item - 1]
                else:
                    raise Error('Unhandled args %s' % item)
                pattern = '@%s@' % ancestor.key()
                q = q._filter(**{'gae_ancestry__contains': pattern})
            elif op == '>':
                item = self._resolve_arg(value[0][1][0])
                q = q._filter(**{'%s__gt' % kwd: item})
            elif op == '<':
                item = self._resolve_arg(value[0][1][0])
                q = q._filter(**{'%s__lt' % kwd: item})
            elif op == '>=':
                item = self._resolve_arg(value[0][1][0])
                q = q._filter(**{'%s__gte' % kwd: item})
            elif op == '<=':
                item = self._resolve_arg(value[0][1][0])
                q = q._filter(**{'%s__lte' % kwd: item})
            else:
                raise Error('Unhandled operator %s' % op)
        orderings = []
        for field, direction in self._gql.orderings():
            if direction != 1:
                field = '-%s' % field
            orderings.append(field)
        if orderings:
            q = q.order_by(*orderings)
        if listprop_filter:
            q._listprop_filter = listprop_filter
        self._results = q
Example #37
0
def gen_content_type():
    from django.contrib.contenttypes.models import ContentType
    from django.db.models import get_models

    return ContentType.objects.get_for_model(choice(get_models()))
Example #38
0
def get_model_from_db_table(db_table):
    for model in models.get_models(include_auto_created=True,
                                   only_installed=False):
        if model._meta.db_table == db_table:
            return model
Example #39
0
def generate_dot(app_labels, **kwargs):
    cli_options = kwargs.get('cli_options', None)
    disable_fields = kwargs.get('disable_fields', False)
    include_models = parse_file_or_list(kwargs.get('include_models', ""))
    all_applications = kwargs.get('all_applications', False)
    use_subgraph = kwargs.get('group_models', False)
    verbose_names = kwargs.get('verbose_names', False)
    inheritance = kwargs.get('inheritance', True)
    relations_as_fields = kwargs.get("relations_as_fields", True)
    sort_fields = kwargs.get("sort_fields", True)
    language = kwargs.get('language', None)
    if language is not None:
        activate_language(language)
    exclude_columns = parse_file_or_list(kwargs.get('exclude_columns', ""))
    exclude_models = parse_file_or_list(kwargs.get('exclude_models', ""))

    def skip_field(field):
        if exclude_columns:
            if verbose_names and field.verbose_name:
                if field.verbose_name in exclude_columns:
                    return True
            if field.name in exclude_columns:
                return True
        return False

    apps = []
    if all_applications:
        apps = models.get_apps()

    for app_label in app_labels:
        app = models.get_app(app_label)
        if not app in apps:
            apps.append(app)

    graphs = []
    for app in apps:
        graph = Context({
            'name':
            '"%s"' % app.__name__,
            'app_name':
            "%s" % '.'.join(app.__name__.split('.')[:-1]),
            'cluster_app_name':
            "cluster_%s" % app.__name__.replace(".", "_"),
            'models': []
        })

        appmodels = get_models(app)
        abstract_models = []
        for appmodel in appmodels:
            abstract_models = abstract_models + [
                abstract_model
                for abstract_model in appmodel.__bases__ if hasattr(
                    abstract_model, '_meta') and abstract_model._meta.abstract
            ]
        abstract_models = list(set(abstract_models))  # remove duplicates
        appmodels = abstract_models + appmodels

        for appmodel in appmodels:
            appmodel_abstracts = [
                abstract_model.__name__
                for abstract_model in appmodel.__bases__
                if hasattr(abstract_model, '_meta')
                and abstract_model._meta.abstract
            ]

            # collect all attribs of abstract superclasses
            def getBasesAbstractFields(c):
                _abstract_fields = []
                for e in c.__bases__:
                    if hasattr(e, '_meta') and e._meta.abstract:
                        _abstract_fields.extend(e._meta.fields)
                        _abstract_fields.extend(getBasesAbstractFields(e))
                return _abstract_fields

            abstract_fields = getBasesAbstractFields(appmodel)

            model = {
                'app_name': appmodel.__module__.replace(".", "_"),
                'name': appmodel.__name__,
                'abstracts': appmodel_abstracts,
                'fields': [],
                'relations': []
            }

            # consider given model name ?
            def consider(model_name):
                if exclude_models and model_name in exclude_models:
                    return False
                return not include_models or model_name in include_models

            if not consider(appmodel._meta.object_name):
                continue

            if verbose_names and appmodel._meta.verbose_name:
                model['label'] = appmodel._meta.verbose_name
            else:
                model['label'] = model['name']

            # model attributes
            def add_attributes(field):
                if verbose_names and field.verbose_name:
                    label = field.verbose_name
                else:
                    label = field.name

                t = type(field).__name__
                if isinstance(field, (OneToOneField, ForeignKey)):
                    t += " ({0})".format(field.rel.field_name)
                # TODO: ManyToManyField, GenericRelation

                model['fields'].append({
                    'name':
                    field.name,
                    'label':
                    label,
                    'type':
                    t,
                    'blank':
                    field.blank,
                    'abstract':
                    field in abstract_fields,
                    'relation':
                    isinstance(field, RelatedField),
                    'primary_key':
                    field.primary_key,
                })

            attributes = [field for field in appmodel._meta.local_fields]
            if not relations_as_fields:
                # Find all the 'real' attributes. Relations are depicted as graph edges instead of attributes
                attributes = [
                    field for field in attributes
                    if not isinstance(field, RelatedField)
                ]

            # find primary key and print it first, ignoring implicit id if other pk exists
            pk = appmodel._meta.pk
            if not appmodel._meta.abstract and pk in attributes:
                add_attributes(pk)

            for field in attributes:
                if skip_field(field):
                    continue
                if field == pk:
                    continue
                add_attributes(field)

            if sort_fields:
                model['fields'] = sorted(model['fields'],
                                         key=lambda field:
                                         (not field['primary_key'], not field[
                                             'relation'], field['label']))

            # FIXME: actually many_to_many fields aren't saved in this model's db table, so why should we add an attribute-line for them in the resulting graph?
            #if appmodel._meta.many_to_many:
            #    for field in appmodel._meta.many_to_many:
            #        if skip_field(field):
            #            continue
            #        add_attributes(field)

            # relations
            def add_relation(field, extras=""):
                if verbose_names and field.verbose_name:
                    label = field.verbose_name
                else:
                    label = field.name

                # show related field name
                if hasattr(field, 'related_query_name'):
                    label += ' (%s)' % field.related_query_name()

                # handle self-relationships
                if field.rel.to == 'self':
                    target_model = field.model
                else:
                    target_model = field.rel.to

                _rel = {
                    'target_app': target_model.__module__.replace('.', '_'),
                    'target': target_model.__name__,
                    'type': type(field).__name__,
                    'name': field.name,
                    'label': label,
                    'arrows': extras,
                    'needs_node': True
                }
                if _rel not in model['relations'] and consider(_rel['target']):
                    model['relations'].append(_rel)

            for field in appmodel._meta.local_fields:
                if field.attname.endswith(
                        '_ptr_id'
                ):  # excluding field redundant with inheritance relation
                    continue
                if field in abstract_fields:  # excluding fields inherited from abstract classes. they too show as local_fields
                    continue
                if skip_field(field):
                    continue
                if isinstance(field, OneToOneField):
                    add_relation(field,
                                 '[arrowhead=none, arrowtail=none, dir=both]')
                elif isinstance(field, ForeignKey):
                    add_relation(field,
                                 '[arrowhead=none, arrowtail=dot, dir=both]')

            for field in appmodel._meta.local_many_to_many:
                if skip_field(field):
                    continue
                if isinstance(field, ManyToManyField):
                    if (getattr(field, 'creates_table', False)
                            or  # django 1.1.
                        (hasattr(field.rel.through, '_meta') and
                         field.rel.through._meta.auto_created)):  # django 1.2
                        add_relation(
                            field, '[arrowhead=dot arrowtail=dot, dir=both]')
                elif isinstance(field, GenericRelation):
                    add_relation(
                        field,
                        mark_safe(
                            '[style="dotted", arrowhead=normal, arrowtail=normal, dir=both]'
                        ))

            if inheritance:
                # add inheritance arrows
                for parent in appmodel.__bases__:
                    if hasattr(parent, "_meta"):  # parent is a model
                        l = "multi-table"
                        if parent._meta.abstract:
                            l = "abstract"
                        if appmodel._meta.proxy:
                            l = "proxy"
                        l += r"\ninheritance"
                        _rel = {
                            'target_app': parent.__module__.replace(".", "_"),
                            'target': parent.__name__,
                            'type': "inheritance",
                            'name': "inheritance",
                            'label': l,
                            'arrows':
                            '[arrowhead=empty, arrowtail=none, dir=both]',
                            'needs_node': True,
                        }
                        # TODO: seems as if abstract models aren't part of models.getModels, which is why they are printed by this without any attributes.
                        if _rel not in model['relations'] and consider(
                                _rel['target']):
                            model['relations'].append(_rel)

            graph['models'].append(model)
        if graph['models']:
            graphs.append(graph)

    nodes = []
    for graph in graphs:
        nodes.extend([e['name'] for e in graph['models']])

    for graph in graphs:
        for model in graph['models']:
            for relation in model['relations']:
                if relation['target'] in nodes:
                    relation['needs_node'] = False

    now = datetime.datetime.now()
    t = loader.get_template('django_extensions/graph_models/digraph.dot')
    c = Context({
        'created_at': now.strftime("%Y-%m-%d %H:%M"),
        'cli_options': cli_options,
        'disable_fields': disable_fields,
        'use_subgraph': use_subgraph,
        'graphs': graphs,
    })
    dot = t.render(c)

    return dot
Example #40
0
    def sync_apps(self, connection, apps):
        "Runs the old syncdb-style operation on a list of apps."
        cursor = connection.cursor()

        # Get a list of already installed *models* so that references work right.
        tables = connection.introspection.table_names()
        seen_models = connection.introspection.installed_models(tables)
        created_models = set()
        pending_references = {}

        # Build the manifest of apps and models that are to be synchronized
        all_models = [(app.__name__.split('.')[-2], [
            m for m in models.get_models(app, include_auto_created=True)
            if router.allow_migrate(connection.alias, m)
        ]) for app in models.get_apps() if app.__name__.split('.')[-2] in apps]

        def model_installed(model):
            opts = model._meta
            converter = connection.introspection.table_name_converter
            # Note that if a model is unmanaged we short-circuit and never try to install it
            return not ((converter(opts.db_table) in tables) or
                        (opts.auto_created and converter(
                            opts.auto_created._meta.db_table) in tables))

        manifest = OrderedDict(
            (app_name, list(filter(model_installed, model_list)))
            for app_name, model_list in all_models)

        create_models = set(itertools.chain(*manifest.values()))
        emit_pre_migrate_signal(create_models, self.verbosity,
                                self.interactive, connection.alias)

        # Create the tables for each model
        if self.verbosity >= 1:
            self.stdout.write("  Creating tables...\n")
        with transaction.atomic(using=connection.alias, savepoint=False):
            for app_name, model_list in manifest.items():
                for model in model_list:
                    # Create the model's database table, if it doesn't already exist.
                    if self.verbosity >= 3:
                        self.stdout.write("    Processing %s.%s model\n" %
                                          (app_name, model._meta.object_name))
                    sql, references = connection.creation.sql_create_model(
                        model, no_style(), seen_models)
                    seen_models.add(model)
                    created_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 self.verbosity >= 1 and sql:
                        self.stdout.write("    Creating table %s\n" %
                                          model._meta.db_table)
                    for statement in sql:
                        cursor.execute(statement)
                    tables.append(
                        connection.introspection.table_name_converter(
                            model._meta.db_table))

        # We force a commit here, as that was the previous behaviour.
        # If you can prove we don't need this, remove it.
        transaction.set_dirty(using=connection.alias)

        # The connection may have been closed by a syncdb handler.
        cursor = connection.cursor()

        # Install custom SQL for the app (but only if this
        # is a model we've just created)
        if self.verbosity >= 1:
            self.stdout.write("  Installing custom SQL...\n")
        for app_name, model_list in manifest.items():
            for model in model_list:
                if model in created_models:
                    custom_sql = custom_sql_for_model(model, no_style(),
                                                      connection)
                    if custom_sql:
                        if self.verbosity >= 2:
                            self.stdout.write(
                                "    Installing custom SQL for %s.%s model\n" %
                                (app_name, model._meta.object_name))
                        try:
                            with transaction.commit_on_success_unless_managed(
                                    using=connection.alias):
                                for sql in custom_sql:
                                    cursor.execute(sql)
                        except Exception as e:
                            self.stderr.write(
                                "    Failed to install custom SQL for %s.%s model: %s\n"
                                % (app_name, model._meta.object_name, e))
                            if self.show_traceback:
                                traceback.print_exc()
                    else:
                        if self.verbosity >= 3:
                            self.stdout.write(
                                "    No custom SQL for %s.%s model\n" %
                                (app_name, model._meta.object_name))

        if self.verbosity >= 1:
            self.stdout.write("  Installing indexes...\n")

        # Install SQL indices for all newly created models
        for app_name, model_list in manifest.items():
            for model in model_list:
                if model in created_models:
                    index_sql = connection.creation.sql_indexes_for_model(
                        model, no_style())
                    if index_sql:
                        if self.verbosity >= 2:
                            self.stdout.write(
                                "    Installing index for %s.%s model\n" %
                                (app_name, model._meta.object_name))
                        try:
                            with transaction.commit_on_success_unless_managed(
                                    using=connection.alias):
                                for sql in index_sql:
                                    cursor.execute(sql)
                        except Exception as e:
                            self.stderr.write(
                                "    Failed to install index for %s.%s model: %s\n"
                                % (app_name, model._meta.object_name, e))

        # Load initial_data fixtures (unless that has been disabled)
        if self.load_initial_data:
            call_command('loaddata',
                         'initial_data',
                         verbosity=self.verbosity,
                         database=connection.alias,
                         skip_validation=True)

        return created_models
Example #41
0
    def handle_noargs(self, **options):

        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        show_traceback = options.get('traceback')
        load_initial_data = options.get('load_initial_data')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError as exc:
                # This is slightly hackish. We want to ignore ImportErrors
                # if the "management" module itself is missing -- but we don't
                # want to ignore the exception if the management module exists
                # but raises an ImportError for some reason. The only way we
                # can do this is to check the text of the exception. Note that
                # we're a bit broad in how we check the text, because different
                # Python implementations may not use the same text.
                # CPython uses the text "No module named management"
                # PyPy uses "No module named myproject.myapp.management"
                msg = exc.args[0]
                if not msg.startswith('No module named') or 'management' not in msg:
                    raise

        db = options.get('database')
        connection = connections[db]
        cursor = connection.cursor()

        # Get a list of already installed *models* so that references work right.
        tables = connection.introspection.table_names()
        seen_models = connection.introspection.installed_models(tables)
        created_models = set()
        pending_references = {}

        # Build the manifest of apps and models that are to be synchronized
        all_models = [
            (app.__name__.split('.')[-2],
                [m for m in models.get_models(app, include_auto_created=True)
                if router.allow_syncdb(db, m)])
            for app in models.get_apps()
        ]

        def model_installed(model):
            opts = model._meta
            converter = connection.introspection.table_name_converter
            return not ((converter(opts.db_table) in tables) or
                (opts.auto_created and converter(opts.auto_created._meta.db_table) in tables))

        manifest = SortedDict(
            (app_name, list(filter(model_installed, model_list)))
            for app_name, model_list in all_models
        )

        create_models = set([x for x in itertools.chain(*manifest.values())])
        emit_pre_sync_signal(create_models, verbosity, interactive, db)

        # Create the tables for each model
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        with transaction.commit_on_success_unless_managed(using=db):
            for app_name, model_list in manifest.items():
                for model in model_list:
                    # Create the model's database table, if it doesn't already exist.
                    if verbosity >= 3:
                        self.stdout.write("Processing %s.%s model\n" % (app_name, model._meta.object_name))
                    sql, references = connection.creation.sql_create_model(model, self.style, seen_models)
                    seen_models.add(model)
                    created_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, self.style, pending_references))
                    sql.extend(connection.creation.sql_for_pending_references(model, self.style, pending_references))
                    if verbosity >= 1 and sql:
                        self.stdout.write("Creating table %s\n" % model._meta.db_table)
                    for statement in sql:
                        cursor.execute(statement)
                    tables.append(connection.introspection.table_name_converter(model._meta.db_table))

        # Send the post_syncdb signal, so individual apps can do whatever they need
        # to do at this point.
        emit_post_sync_signal(created_models, verbosity, interactive, db)

        # The connection may have been closed by a syncdb handler.
        cursor = connection.cursor()

        # Install custom SQL for the app (but only if this
        # is a model we've just created)
        if verbosity >= 1:
            self.stdout.write("Installing custom SQL ...\n")
        for app_name, model_list in manifest.items():
            for model in model_list:
                if model in created_models:
                    custom_sql = custom_sql_for_model(model, self.style, connection)
                    if custom_sql:
                        if verbosity >= 2:
                            self.stdout.write("Installing custom SQL for %s.%s model\n" % (app_name, model._meta.object_name))
                        try:
                            with transaction.commit_on_success_unless_managed(using=db):
                                for sql in custom_sql:
                                    cursor.execute(sql)
                        except Exception as e:
                            self.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \
                                                (app_name, model._meta.object_name, e))
                            if show_traceback:
                                traceback.print_exc()
                    else:
                        if verbosity >= 3:
                            self.stdout.write("No custom SQL for %s.%s model\n" % (app_name, model._meta.object_name))

        if verbosity >= 1:
            self.stdout.write("Installing indexes ...\n")
        # Install SQL indices for all newly created models
        for app_name, model_list in manifest.items():
            for model in model_list:
                if model in created_models:
                    index_sql = connection.creation.sql_indexes_for_model(model, self.style)
                    if index_sql:
                        if verbosity >= 2:
                            self.stdout.write("Installing index for %s.%s model\n" % (app_name, model._meta.object_name))
                        try:
                            with transaction.commit_on_success_unless_managed(using=db):
                                for sql in index_sql:
                                    cursor.execute(sql)
                        except Exception as e:
                            self.stderr.write("Failed to install index for %s.%s model: %s\n" % \
                                                (app_name, model._meta.object_name, e))

        # Load initial_data fixtures (unless that has been disabled)
        if load_initial_data:
            call_command('loaddata', 'initial_data', verbosity=verbosity,
                         database=db, skip_validation=True)
Example #42
0
    def handle(self, *app_labels, **options):

        # Activate project's default language
        translation.activate(settings.LANGUAGE_CODE)

        comment = options["comment"]
        batch_size = options["batch_size"]
        database = options.get('database')

        verbosity = int(options.get("verbosity", 1))
        app_list = OrderedDict()
        # if no apps given, use all installed.
        if len(app_labels) == 0:
            for app in get_apps():
                if not app in app_list:
                    app_list[app] = []
                for model_class in get_models(app):
                    if not model_class in app_list[app]:
                        app_list[app].append(model_class)
        else:
            for label in app_labels:
                try:
                    app_label, model_label = label.split(".")
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" %
                                           app_label)

                    model_class = get_model(app_label, model_label)
                    if model_class is None:
                        raise CommandError("Unknown model: %s.%s" %
                                           (app_label, model_label))
                    if app in app_list:
                        if app_list[app] and model_class not in app_list[app]:
                            app_list[app].append(model_class)
                    else:
                        app_list[app] = [model_class]
                except ValueError:
                    # This is just an app - no model qualifier.
                    app_label = label
                    try:
                        app = get_app(app_label)
                        if not app in app_list:
                            app_list[app] = []
                        for model_class in get_models(app):
                            if not model_class in app_list[app]:
                                app_list[app].append(model_class)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" %
                                           app_label)
        # Create revisions.
        for app, model_classes in app_list.items():
            for model_class in model_classes:
                self.create_initial_revisions(app,
                                              model_class,
                                              comment,
                                              batch_size,
                                              verbosity,
                                              database=database)

        # Go back to default language
        translation.deactivate()
Example #43
0
def merge_model_objects(primary_object, alias_objects=[], keep_old=False):
    """
    Use this function to merge model objects (i.e. Users, Organizations, Polls,
    etc.) and migrate all of the related fields from the alias objects to the
    primary object.

    Usage:
    from django.contrib.auth.models import User
    primary_user = User.objects.get(email='*****@*****.**')
    duplicate_user = User.objects.get(email='*****@*****.**')
    merge_model_objects(primary_user, duplicate_user)
    """
    if not isinstance(alias_objects, list):
        alias_objects = [alias_objects]

    # check that all aliases are the same class as primary one and that
    # they are subclass of model
    primary_class = primary_object.__class__

    if not issubclass(primary_class, Model):
        raise TypeError('Only django.db.models.Model subclasses can be merged')

    for alias_object in alias_objects:
        if not isinstance(alias_object, primary_class):
            raise TypeError('Only models of same class can be merged')

    # Get a list of all GenericForeignKeys in all models
    # TODO: this is a bit of a hack, since the generics framework should provide a similar
    # method to the ForeignKey field for accessing the generic related fields.
    generic_fields = []
    for model in get_models():
        for field_name, field in filter(
                lambda x: isinstance(x[1], GenericForeignKey),
                model.__dict__.items()):
            generic_fields.append(field)

    blank_local_fields = set([
        field.attname for field in primary_object._meta.local_fields
        if getattr(primary_object, field.attname) in [None, '']
    ])

    # Loop through all alias objects and migrate their data to the primary
    # object.
    for alias_object in alias_objects:
        # Migrate all foreign key references from alias object to primary
        # object.
        for related_object in alias_object._meta.get_all_related_objects():
            # The variable name on the alias_object model.
            alias_varname = related_object.get_accessor_name()
            # The variable name on the related model.
            obj_varname = related_object.field.name
            try:
                related_objects = getattr(alias_object, alias_varname)
                for obj in related_objects.all():
                    setattr(obj, obj_varname, primary_object)
                    obj.save()
            except Exception as e:
                logger.warn(e)

        # Migrate all many to many references from alias object to primary
        # object.
        for related_many_object in alias_object._meta.get_all_related_many_to_many_objects(
        ):
            alias_varname = related_many_object.get_accessor_name()
            obj_varname = related_many_object.field.name

            if alias_varname is not None:
                # standard case
                related_many_objects = getattr(alias_object,
                                               alias_varname).all()
            else:
                # special case, symmetrical relation, no reverse accessor
                related_many_objects = getattr(alias_object, obj_varname).all()
            for obj in related_many_objects.all():
                getattr(obj, obj_varname).remove(alias_object)
                getattr(obj, obj_varname).add(primary_object)

        # Migrate all generic foreign key references from alias object to
        # primary object.
        for field in generic_fields:
            filter_kwargs = {}
            filter_kwargs[field.fk_field] = alias_object._get_pk_val()
            filter_kwargs[field.ct_field] = field.get_content_type(
                alias_object)
            for generic_related_object in field.model.objects.filter(
                    **filter_kwargs):
                setattr(generic_related_object, field.name, primary_object)
                generic_related_object.save()

        # Try to fill all missing values in primary object by values of
        # duplicates
        filled_up = set()
        for field_name in blank_local_fields:
            val = getattr(alias_object, field_name)
            if val not in [None, '']:
                setattr(primary_object, field_name, val)
                filled_up.add(field_name)
        blank_local_fields -= filled_up

        if not keep_old:
            alias_object.delete()
    primary_object.save()
    return primary_object
Example #44
0
 def handle(self, *args, **options):
     self.stdout.write('Model name - Count of objects')
     for model in get_models():
         msg = '{} - {}'.format(model.__name__, model.objects.count())
         self.stdout.write(msg)
         self.stderr.write('Error: ' + msg)
Example #45
0
def generate_dot(app_labels, **kwargs):
    disable_fields = kwargs.get('disable_fields', False)
    include_models = kwargs.get('include_models', [])
    all_applications = kwargs.get('all_applications', False)
    use_subgraph = kwargs.get('group_models', False)

    dot = head_template

    apps = []
    if all_applications:
        apps = models.get_apps()

    for app_label in app_labels:
        app = models.get_app(app_label)
        if not app in apps:
            apps.append(app)

    graphs = []
    for app in apps:
        graph = Context({
            'name': '"%s"' % app.__name__,
            'app_name': "%s" % '.'.join(app.__name__.split('.')[:-1]),
            'cluster_app_name': "cluster_%s" % app.__name__.replace(".", "_"),
            'disable_fields': disable_fields,
            'use_subgraph': use_subgraph,
            'models': []
        })

        for appmodel in get_models(app):
            abstracts = [e.__name__ for e in appmodel.__bases__ if hasattr(e, '_meta') and e._meta.abstract]
            abstract_fields = []
            for e in appmodel.__bases__:
                if hasattr(e, '_meta') and e._meta.abstract:
                    abstract_fields.extend(e._meta.fields)
            model = {
                'app_name': appmodel.__module__.replace(".", "_"),
                'name': appmodel.__name__,
                'abstracts': abstracts,
                'fields': [],
                'relations': []
            }

            # consider given model name ?
            def consider(model_name):
                return not include_models or model_name in include_models

            if not consider(appmodel._meta.object_name):
                continue

            # model attributes
            def add_attributes(field):
                model['fields'].append({
                    'name': field.name,
                    'type': type(field).__name__,
                    'blank': field.blank,
                    'abstract': field in abstract_fields,
                })

            for field in appmodel._meta.fields:
                add_attributes(field)

            if appmodel._meta.many_to_many:
                for field in appmodel._meta.many_to_many:
                    add_attributes(field)

            # relations
            def add_relation(field, extras=""):
                _rel = {
                    'target_app': field.rel.to.__module__.replace('.','_'),
                    'target': field.rel.to.__name__,
                    'type': type(field).__name__,
                    'name': field.name,
                    'arrows': extras,
                    'needs_node': True
                }
                if _rel not in model['relations'] and consider(_rel['target']):
                    model['relations'].append(_rel)

            for field in appmodel._meta.fields:
                if isinstance(field, ForeignKey):
                    add_relation(field)
                elif isinstance(field, OneToOneField):
                    add_relation(field, '[arrowhead=none arrowtail=none]')

            if appmodel._meta.many_to_many:
                for field in appmodel._meta.many_to_many:
                    if isinstance(field, ManyToManyField) and getattr(field, 'creates_table', False):
                        add_relation(field, '[arrowhead=normal arrowtail=normal]')
                    elif isinstance(field, GenericRelation):
                        add_relation(field, mark_safe('[style="dotted"] [arrowhead=normal arrowtail=normal]'))
            graph['models'].append(model)
        graphs.append(graph)

    nodes = []
    for graph in graphs:
        nodes.extend([e['name'] for e in graph['models']])

    for graph in graphs:
        # don't draw duplication nodes because of relations
        for model in graph['models']:
            for relation in model['relations']:
                if relation['target'] in nodes:
                    relation['needs_node'] = False
        # render templates
        t = Template(body_template)
        dot += '\n' + t.render(graph)

    for graph in graphs:
        t = Template(rel_template)
        dot += '\n' + t.render(graph)

    dot += '\n' + tail_template
    return dot
Example #46
0
def sort_dependencies(app_list):
    """Sort a list of app,modellist pairs into a single list of models.

    The single list of models is sorted so that any model with a natural key
    is serialized before a normal model, and any model with a natural key
    dependency has it's dependencies serialized first.
    """
    from django.db.models import get_model, get_models
    # Process the list of models, and get the list of dependencies
    model_dependencies = []
    models = set()
    for app, model_list in app_list:
        if model_list is None:
            model_list = get_models(app)

        for model in model_list:
            models.add(model)
            # Add any explicitly defined dependencies
            if hasattr(model, 'natural_key'):
                deps = getattr(model.natural_key, 'dependencies', [])
                if deps:
                    deps = [get_model(*d.split('.')) for d in deps]
            else:
                deps = []

            # Now add a dependency for any FK or M2M relation with
            # a model that defines a natural key
            for field in model._meta.fields:
                if hasattr(field.rel, 'to'):
                    rel_model = field.rel.to
                    if hasattr(rel_model,
                               'natural_key') and rel_model != model:
                        deps.append(rel_model)
            for field in model._meta.many_to_many:
                rel_model = field.rel.to
                if hasattr(rel_model, 'natural_key') and rel_model != model:
                    deps.append(rel_model)
            model_dependencies.append((model, deps))

    model_dependencies.reverse()
    # Now sort the models to ensure that dependencies are met. This
    # is done by repeatedly iterating over the input list of models.
    # If all the dependencies of a given model are in the final list,
    # that model is promoted to the end of the final list. This process
    # continues until the input list is empty, or we do a full iteration
    # over the input models without promoting a model to the final list.
    # If we do a full iteration without a promotion, that means there are
    # circular dependencies in the list.
    model_list = []
    while model_dependencies:
        skipped = []
        changed = False
        while model_dependencies:
            model, deps = model_dependencies.pop()

            # If all of the models in the dependency list are either already
            # on the final model list, or not on the original serialization list,
            # then we've found another model with all it's dependencies satisfied.
            found = True
            for candidate in ((d not in models or d in model_list)
                              for d in deps):
                if not candidate:
                    found = False
            if found:
                model_list.append(model)
                changed = True
            else:
                skipped.append((model, deps))
        if not changed:
            raise CommandError(
                "Can't resolve dependencies for %s in serialized app list." %
                ', '.join('%s.%s' %
                          (model._meta.app_label, model._meta.object_name)
                          for model, deps in sorted(
                              skipped, key=lambda obj: obj[0].__name__)))
        model_dependencies = skipped

    return model_list
Example #47
0
 def get_models():
     """Get models."""
     return models.get_models(include_auto_created=True)
Example #48
0
def generate_dot(app_labels, **kwargs):
    disable_fields = kwargs.get('disable_fields', False)
    include_models = parse_file_or_list(kwargs.get('include_models', ""))
    all_applications = kwargs.get('all_applications', False)
    use_subgraph = kwargs.get('group_models', False)
    verbose_names = kwargs.get('verbose_names', False)
    inheritance = kwargs.get('inheritance', False)
    language = kwargs.get('language', None)
    if language is not None:
        activate_language(language)
    exclude_columns = parse_file_or_list(kwargs.get('exclude_columns', ""))
    exclude_models = parse_file_or_list(kwargs.get('exclude_models', ""))

    def skip_field(field):
        if exclude_columns:
            if verbose_names and field.verbose_name:
                if field.verbose_name in exclude_columns:
                    return True
            if field.name in exclude_columns:
                return True
        return False

    t = loader.get_template_from_string("""
digraph name {
  fontname = "Helvetica"
  fontsize = 8

  node [
    fontname = "Helvetica"
    fontsize = 8
    shape = "plaintext"
  ]
  edge [
    fontname = "Helvetica"
    fontsize = 8
  ]

""")
    c = Context({})
    dot = t.render(c)

    apps = []
    if all_applications:
        apps = models.get_apps()

    for app_label in app_labels:
        app = models.get_app(app_label)
        if not app in apps:
            apps.append(app)

    graphs = []
    for app in apps:
        graph = Context({
            'name':
            '"%s"' % app.__name__,
            'app_name':
            "%s" % '.'.join(app.__name__.split('.')[:-1]),
            'cluster_app_name':
            "cluster_%s" % app.__name__.replace(".", "_"),
            'disable_fields':
            disable_fields,
            'use_subgraph':
            use_subgraph,
            'models': []
        })

        appmodels = get_models(app)
        abstract_models = []
        for appmodel in appmodels:
            abstract_models = abstract_models + [
                abstract_model
                for abstract_model in appmodel.__bases__ if hasattr(
                    abstract_model, '_meta') and abstract_model._meta.abstract
            ]
        abstract_models = list(set(abstract_models))  # remove duplicates
        appmodels = abstract_models + appmodels

        for appmodel in appmodels:
            appmodel_abstracts = [
                abstract_model.__name__
                for abstract_model in appmodel.__bases__
                if hasattr(abstract_model, '_meta')
                and abstract_model._meta.abstract
            ]

            # collect all attribs of abstract superclasses
            def getBasesAbstractFields(c):
                _abstract_fields = []
                for e in c.__bases__:
                    if hasattr(e, '_meta') and e._meta.abstract:
                        _abstract_fields.extend(e._meta.fields)
                        _abstract_fields.extend(getBasesAbstractFields(e))
                return _abstract_fields

            abstract_fields = getBasesAbstractFields(appmodel)

            model = {
                'app_name': appmodel.__module__.replace(".", "_"),
                'name': appmodel.__name__,
                'abstracts': appmodel_abstracts,
                'fields': [],
                'relations': []
            }

            # consider given model name ?
            def consider(model_name):
                if exclude_models and model_name in exclude_models:
                    return False
                return not include_models or model_name in include_models

            if not consider(appmodel._meta.object_name):
                continue

            if verbose_names and appmodel._meta.verbose_name:
                model['label'] = appmodel._meta.verbose_name
            else:
                model['label'] = model['name']

            # model attributes
            def add_attributes(field):
                if verbose_names and field.verbose_name:
                    label = field.verbose_name
                else:
                    label = field.name

                t = type(field).__name__
                if isinstance(field, (OneToOneField, ForeignKey)):
                    t += " ({0})".format(field.rel.field_name)
                # TODO: ManyToManyField, GenericRelation

                model['fields'].append({
                    'name': field.name,
                    'label': label,
                    'type': t,
                    'blank': field.blank,
                    'abstract': field in abstract_fields,
                })

            # Find all the real attributes. Relations are depicted as graph edges instead of attributes
            attributes = [
                field for field in appmodel._meta.local_fields
                if not isinstance(field, RelatedField)
            ]

            # find primary key and print it first, ignoring implicit id if other pk exists
            pk = appmodel._meta.pk
            if not appmodel._meta.abstract and pk in attributes:
                add_attributes(pk)
            for field in attributes:
                if skip_field(field):
                    continue
                if not field.primary_key:
                    add_attributes(field)

            # FIXME: actually many_to_many fields aren't saved in this model's db table, so why should we add an attribute-line for them in the resulting graph?
            #if appmodel._meta.many_to_many:
            #    for field in appmodel._meta.many_to_many:
            #        if skip_field(field):
            #            continue
            #        add_attributes(field)

            # relations
            def add_relation(field, extras=""):
                if verbose_names and field.verbose_name:
                    label = field.verbose_name
                else:
                    label = field.name

                # show related field name
                if hasattr(field, 'related_query_name'):
                    label += ' (%s)' % field.related_query_name()

                # handle self-relationships
                if field.rel.to == 'self':
                    target_model = field.model
                else:
                    target_model = field.rel.to

                _rel = {
                    'target_app': target_model.__module__.replace('.', '_'),
                    'target': target_model.__name__,
                    'type': type(field).__name__,
                    'name': field.name,
                    'label': label,
                    'arrows': extras,
                    'needs_node': True
                }
                if _rel not in model['relations'] and consider(_rel['target']):
                    model['relations'].append(_rel)

            for field in appmodel._meta.local_fields:
                if field.attname.endswith(
                        '_ptr_id'
                ):  # excluding field redundant with inheritance relation
                    continue
                if field in abstract_fields:  # excluding fields inherited from abstract classes. they too show as local_fields
                    continue
                if skip_field(field):
                    continue
                if isinstance(field, OneToOneField):
                    add_relation(field, '[arrowhead=none, arrowtail=none]')
                elif isinstance(field, ForeignKey):
                    add_relation(field, '[arrowhead=none, arrowtail=dot]')

            for field in appmodel._meta.local_many_to_many:
                if skip_field(field):
                    continue
                if isinstance(field, ManyToManyField):
                    if (getattr(field, 'creates_table', False)
                            or  # django 1.1.
                        (hasattr(field.rel.through, '_meta') and
                         field.rel.through._meta.auto_created)):  # django 1.2
                        add_relation(
                            field, '[arrowhead=dot arrowtail=dot, dir=both]')
                    elif isinstance(field, GenericRelation):
                        add_relation(
                            field,
                            mark_safe(
                                '[style="dotted", arrowhead=normal, arrowtail=normal, dir=both]'
                            ))

            if inheritance:
                # add inheritance arrows
                for parent in appmodel.__bases__:
                    if hasattr(parent, "_meta"):  # parent is a model
                        l = "multi-table"
                        if parent._meta.abstract:
                            l = "abstract"
                        if appmodel._meta.proxy:
                            l = "proxy"
                        l += r"\ninheritance"
                        _rel = {
                            'target_app': parent.__module__.replace(".", "_"),
                            'target': parent.__name__,
                            'type': "inheritance",
                            'name': "inheritance",
                            'label': l,
                            'arrows': '[arrowhead=empty, arrowtail=none]',
                            'needs_node': True
                        }
                        # TODO: seems as if abstract models aren't part of models.getModels, which is why they are printed by this without any attributes.
                        if _rel not in model['relations'] and consider(
                                _rel['target']):
                            model['relations'].append(_rel)

            graph['models'].append(model)
        graphs.append(graph)

    nodes = []
    for graph in graphs:
        nodes.extend([e['name'] for e in graph['models']])

    for graph in graphs:
        # don't draw duplication nodes because of relations
        for model in graph['models']:
            for relation in model['relations']:
                if relation['target'] in nodes:
                    relation['needs_node'] = False
        # render templates
        t = loader.get_template_from_string("""{% if use_subgraph %}
subgraph {{ cluster_app_name }} {
  label=<
        <TABLE BORDER="0" CELLBORDER="0" CELLSPACING="0">
        <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER"
        ><FONT FACE="Helvetica Bold" COLOR="Black" POINT-SIZE="12"
        >{{ app_name }}</FONT></TD></TR>
        </TABLE>
        >
  color=olivedrab4
  style="rounded"
{% endif %}
{% for model in models %}
    {{ model.app_name }}_{{ model.name }} [label=<
    <TABLE BGCOLOR="palegoldenrod" BORDER="0" CELLBORDER="0" CELLSPACING="0">
     <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="olivedrab4"
     ><FONT FACE="Helvetica Bold" COLOR="white"
     >{{ model.label }}{% if model.abstracts %}<BR/>&lt;<FONT FACE="Helvetica Italic">{{ model.abstracts|join:"," }}</FONT>&gt;{% endif %}</FONT></TD></TR>
    {% if not disable_fields %}
        {% for field in model.fields %}
        <TR><TD ALIGN="LEFT" BORDER="0"
        ><FONT {% if field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Helvetica {% if field.abstract %}Italic{% else %}Bold{% endif %}">{{ field.label }}</FONT
></TD>
        <TD ALIGN="LEFT"
        ><FONT {% if field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Helvetica {% if field.abstract %}Italic{% else %}Bold{% endif %}">{{ field.type }}</FONT
></TD></TR>
        {% endfor %}
    {% endif %}
    </TABLE>
    >]
{% endfor %}
{% if use_subgraph %}
}
{% endif %}""")
        dot += '\n' + t.render(graph)

    for graph in graphs:
        t = loader.get_template_from_string("""{% for model in models %}
  {% for relation in model.relations %}
  {% if relation.needs_node %}
  {{ relation.target_app }}_{{ relation.target }} [label=<
      <TABLE BGCOLOR="palegoldenrod" BORDER="0" CELLBORDER="0" CELLSPACING="0">
      <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="olivedrab4"
      ><FONT FACE="Helvetica Bold" COLOR="white"
      >{{ relation.target }}</FONT></TD></TR>
      </TABLE>
      >]
  {% endif %}
  {{ model.app_name }}_{{ model.name }} -> {{ relation.target_app }}_{{ relation.target }}
  [label="{{ relation.label }}"] {{ relation.arrows }};
  {% endfor %}
{% endfor %}""")
        dot += '\n' + t.render(graph)

    t = loader.get_template_from_string("}")
    c = Context({})
    dot += '\n' + t.render(c)
    return dot
Example #49
0
 def enable_instrumentation(self):
     self._tracker = ClassTracker()
     for cls in get_models() + self.classes:
         self._tracker.track_class(cls)
Example #50
0
"""
import os

from django.db import connection

try:
    # Django >= 1.8
    import django.apps

    get_models = django.apps.apps.get_models
    del django.apps
except ImportError:
    # Django < 1.9
    from django.db.models import get_models

import pytest

import nav.models

# Ensure that all modules are loaded
for file_name in os.listdir(os.path.dirname(nav.models.__file__)):
    if file_name.endswith('.py') and not file_name.startswith('__init__'):
        module_name = file_name.replace('.py', '')
        __import__('nav.models.%s' % module_name)


@pytest.mark.parametrize("model", get_models())
def test_django_model(model):
    connection.close()  # Ensure clean connection
    list(model.objects.all()[:5])
Example #51
0
def get_indexed_models():
    return [
        model for model in models.get_models()
        if issubclass(model, Indexed) and not model._meta.abstract
    ]
Example #52
0
File: sql.py Project: zeroos/django
def sql_indexes(app, style, connection):
    "Returns a list of the CREATE INDEX SQL statements for all models in the given app."
    output = []
    for model in models.get_models(app, include_auto_created=True):
        output.extend(connection.creation.sql_indexes_for_model(model, style))
    return output
Example #53
0
    #print "    Checking", field.column
    sql = "SELECT src.%s,src.%s FROM %s AS src WHERE src.%s RLIKE '[^\\t-~]+'" % (
        model._meta.pk.column, field.column, model._meta.db_table,
        field.column)
    #print sql
    cursor.execute(sql)
    rows = cursor.fetchall()
    if len(rows) > 0:
        print "    NON-ASCII: %s.%s (%d rows)" % (model._meta.db_table,
                                                  field.column, len(rows))
        #for row in rows[0:20]:
        #    print "   ", row
        #print "    Use the following SQL to debug:"
        #print sql


APPS = [
    'announcements', 'idrfc', 'idtracker', 'iesg', 'ietfauth', 'ipr',
    'liaisons', 'proceedings', 'redirects'
]
all_models = []
for app_label in APPS:
    all_models.extend(models.get_models(models.get_app(app_label)))

for model in all_models:
    print "\nChecking %s (table %s)" % (model._meta.object_name,
                                        model._meta.db_table)
    for f in model._meta.fields:
        if isinstance(f, CharField) or isinstance(f, TextField):
            check_non_ascii(model, f)
def _get_site_models():
    """Get all model classes which subclass SiteRelated."""
    return filter(lambda m: issubclass(m, SiteRelated) and not m == Page, models.get_models())
Example #55
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#from django.conf import settings
#from django.utils.importlib import import_module
from django.db import models
from django.utils.translation import ugettext_lazy as _

from opps.core.models import Publishable, BaseBox

try:
    OPPS_APPS = tuple([(u"{0}.{1}".format(app._meta.app_label,
                                          app._meta.object_name),
                        u"{0} - {1}".format(app._meta.app_label,
                                            app._meta.object_name))
                       for app in models.get_models()
                       if 'opps.' in app.__module__])
except ImportError:
    OPPS_APPS = tuple([])


class QuerySet(Publishable):
    name = models.CharField(_(u"Dynamic queryset name"), max_length=140)
    slug = models.SlugField(
        _(u"Slug"),
        db_index=True,
        max_length=150,
        unique=True,
    )

    model = models.CharField(_(u'Model'), max_length=150, choices=OPPS_APPS)
    limit = models.PositiveIntegerField(_(u'Limit'), default=7)
Example #56
0
File: sql.py Project: zeroos/django
def sql_create(app, style, connection):
    "Returns a list of the CREATE TABLE SQL statements for the given app."

    if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy':
        # This must be the "dummy" database backend, which means the user
        # hasn't set ENGINE for the database.
        raise CommandError(
            "Django doesn't know which syntax to use for your SQL statements,\n"
            +
            "because you haven't properly specified the ENGINE setting for the database.\n"
            +
            "see: https://docs.djangoproject.com/en/dev/ref/settings/#databases"
        )

    # Get installed models, so we generate REFERENCES right.
    # We trim models from the current app so that the sqlreset command does not
    # generate invalid SQL (leaving models out of known_models is harmless, so
    # we can be conservative).
    app_models = models.get_models(app, include_auto_created=True)
    final_output = []
    tables = connection.introspection.table_names()
    known_models = set([
        model for model in connection.introspection.installed_models(tables)
        if model not in app_models
    ])
    pending_references = {}

    for model in app_models:
        output, references = connection.creation.sql_create_model(
            model, style, known_models)
        final_output.extend(output)
        for refto, refs in references.items():
            pending_references.setdefault(refto, []).extend(refs)
            if refto in known_models:
                final_output.extend(
                    connection.creation.sql_for_pending_references(
                        refto, style, pending_references))
        final_output.extend(
            connection.creation.sql_for_pending_references(
                model, style, pending_references))
        # Keep track of the fact that we've created the table for this model.
        known_models.add(model)

    # Handle references to tables that are from other apps
    # but don't exist physically.
    not_installed_models = set(pending_references.keys())
    if not_installed_models:
        alter_sql = []
        for model in not_installed_models:
            alter_sql.extend([
                '-- ' + sql
                for sql in connection.creation.sql_for_pending_references(
                    model, style, pending_references)
            ])
        if alter_sql:
            final_output.append(
                '-- The following references should be added but depend on non-existent tables:'
            )
            final_output.extend(alter_sql)

    return final_output
Example #57
0
 def test_all_managers_have_correct_model(self):
     # all tree managers should have the correct model.
     for model in get_models():
         if not issubclass(model, MPTTModel):
             continue
         self.assertEqual(model._tree_manager.model, model)
Example #58
0
 def handle_app(self, app, **options):
     from django.db import connection, models
     return u'\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app))).encode('utf-8')
Example #59
0
def model_detail(request, app_label, model_name):
    if not utils.docutils_is_available:
        return missing_docutils_page(request)

    # Get the model class.
    try:
        app_mod = models.get_app(app_label)
    except ImproperlyConfigured:
        raise Http404(_("App %r not found") % app_label)
    model = None
    for m in models.get_models(app_mod):
        if m._meta.model_name == model_name:
            model = m
            break
    if model is None:
        raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % {'model_name': model_name, 'app_label': app_label})

    opts = model._meta

    # Gather fields/field descriptions.
    fields = []
    for field in opts.fields:
        # ForeignKey is a special case since the field will actually be a
        # descriptor that returns the other object
        if isinstance(field, models.ForeignKey):
            data_type = field.rel.to.__name__
            app_label = field.rel.to._meta.app_label
            verbose = utils.parse_rst((_("the related `%(app_label)s.%(data_type)s` object")  % {'app_label': app_label, 'data_type': data_type}), 'model', _('model:') + data_type)
        else:
            data_type = get_readable_field_data_type(field)
            verbose = field.verbose_name
        fields.append({
            'name': field.name,
            'data_type': data_type,
            'verbose': verbose,
            'help_text': field.help_text,
        })

    # Gather many-to-many fields.
    for field in opts.many_to_many:
        data_type = field.rel.to.__name__
        app_label = field.rel.to._meta.app_label
        verbose = _("related `%(app_label)s.%(object_name)s` objects") % {'app_label': app_label, 'object_name': data_type}
        fields.append({
            'name': "%s.all" % field.name,
            "data_type": 'List',
            'verbose': utils.parse_rst(_("all %s") % verbose , 'model', _('model:') + opts.model_name),
        })
        fields.append({
            'name'      : "%s.count" % field.name,
            'data_type' : 'Integer',
            'verbose'   : utils.parse_rst(_("number of %s") % verbose , 'model', _('model:') + opts.model_name),
        })

    # Gather model methods.
    for func_name, func in model.__dict__.items():
        if (inspect.isfunction(func) and len(inspect.getargspec(func)[0]) == 1):
            try:
                for exclude in MODEL_METHODS_EXCLUDE:
                    if func_name.startswith(exclude):
                        raise StopIteration
            except StopIteration:
                continue
            verbose = func.__doc__
            if verbose:
                verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.model_name)
            fields.append({
                'name': func_name,
                'data_type': get_return_data_type(func_name),
                'verbose': verbose,
            })

    # Gather related objects
    for rel in opts.get_all_related_objects() + opts.get_all_related_many_to_many_objects():
        verbose = _("related `%(app_label)s.%(object_name)s` objects") % {'app_label': rel.opts.app_label, 'object_name': rel.opts.object_name}
        accessor = rel.get_accessor_name()
        fields.append({
            'name'      : "%s.all" % accessor,
            'data_type' : 'List',
            'verbose'   : utils.parse_rst(_("all %s") % verbose , 'model', _('model:') + opts.model_name),
        })
        fields.append({
            'name'      : "%s.count" % accessor,
            'data_type' : 'Integer',
            'verbose'   : utils.parse_rst(_("number of %s") % verbose , 'model', _('model:') + opts.model_name),
        })
    return render_to_response('admin_doc/model_detail.html', {
        'root_path': urlresolvers.reverse('admin:index'),
        'name': '%s.%s' % (opts.app_label, opts.object_name),
        # Translators: %s is an object type name
        'summary': _("Attributes on %s objects") % opts.object_name,
        'description': model.__doc__,
        'fields': fields,
    }, context_instance=RequestContext(request))
Example #60
0
from django.contrib import admin

# Register your models here.

from django.db.models import get_models, get_app

for model in get_models(get_app('member')):
    admin.site.register(model)

for model in get_models(get_app('bitcoin_analyze')):
    admin.site.register(model)