Example #1
0
    def handle(self, *app_labels, **options):

        format = options.get('format', 'json')
        indent = options.get('indent', None)
        exclude = options.get('exclude', [])
        show_traceback = options.get('traceback', False)

        excluded_apps = [apps.get_app(app_label) for app_label in exclude]

        if len(app_labels) == 0:
            app_list = [
                app for app in apps.get_apps() if app not in excluded_apps
            ]
        else:
            app_list = [apps.get_app(app_label) for app_label in app_labels]

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        objects = []
        collected = set()
        for app in app_list:  # Yey for ghetto recusion
            objects, collected = _relational_dumpdata(app, collected)
        # ****End New stuff
        try:
            return serializers.serialize(format, objects, indent=indent)
        except Exception as e:
            if show_traceback:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
def get_installed_apps():
    if django.VERSION >= (1, 7):
        from django.apps import apps
        return apps.get_apps()
    else:
        from django.db import models
        return models.get_apps()
Example #3
0
 def generate_app_graphs(self):
     print(self.get_header("Generate App Model Graphs"))
     app_names = ""
     for app in apps.get_apps():
         if str(app.__package__).startswith('mmg.jobtrak'):
             filename = self.MODELGRAPH_DIR + str(app.__package__).replace(
                 '.', '-') + '.png'
             app_names += str(app.__package__).replace('mmg.jobtrak.',
                                                       '') + " "
             cmd = [
                 'python ./manage.py graph_models ' +
                 str(app.__package__).replace('mmg.jobtrak.', '') +
                 ' -g -o "' + filename + '"'
             ]
             print("--> Generating " + filename + "...")
             try:
                 retcode = call(cmd, shell=True)
                 if retcode < 0:
                     print("An error happened. Error Code:", -retcode)
             except OSError as e:
                 print("Execution failed:", e)
     filename = self.MODELGRAPH_DIR + 'all.png'
     cmd = [
         'python ./manage.py graph_models ' + app_names + '-g -o "' +
         filename + '"'
     ]
     print("--> Generating " + filename + "...")
     try:
         retcode = call(cmd, shell=True)
         if retcode < 0:
             print("An error happened. Error Code:", -retcode)
     except OSError as e:
         print("Execution failed:", e)
Example #4
0
 def print_models(self):
     for app in apps.get_apps():
         if str(app.__package__).startswith('mmg.jobtrak'):
             print(" ".join(["App:", app.__package__]))
             for model in apps.get_models(app):
                 print(" ".join(["\tModel:", model._meta.object_name]))
                 for field in model._meta.fields:
                     print(" ".join(["\t\tField:", field.name, field.attname, field.get_internal_type()]))
    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 #6
0
def get_installed_apps():
    """
    Return list of all installed apps
    """

    if django.VERSION >= (1, 7):
        from django.apps import apps
        return apps.get_apps()
    else:
        from django.db import models
        return models.get_apps()
Example #7
0
def edit(request):
    app_name = request.GET.get('appname')
    if app_name:
        return render_to_response('ide.html', {'app_name':app_name})
    else:
        a=[]
        #for app in settings.INSTALLED_APPS:
        for mod in apps.get_apps():
            #mod = __import__(app)
            a += [mod.__name__ [:-7]]  # strip off '.models'
        return render_to_response('ide-index.html', {'apps':list(set(a))})
def get_installed_apps():
    """
    Return list of all installed apps
    """

    if django.VERSION >= (1, 7):
        from django.apps import apps
        return apps.get_apps()
    else:
        from django.db import models
        return models.get_apps()
Example #9
0
    def import_objects(self, verbose):
        import importlib
        from django.apps import apps
        from django.utils.termcolors import colorize

        imported_objects = {}
        imports = SHELL_IMPORTS.copy()

        for app in apps.get_apps():
            if not app.__file__.startswith(settings.BASE_DIR):
                continue

            app_models = apps.get_models(app)
            if not app_models:
                continue

            modules = {}
            for model in app_models:
                data = modules.setdefault(model.__module__, [])
                data.append(model.__name__)

            for module_name, models in modules.items():
                imports.append((module_name, models))

        if verbose:
            self.stdout.write('Autoload modules:\n')

        for module_name, parts in imports:
            module = importlib.import_module(module_name)
            if not parts:
                imported_objects[module_name] = module

                if verbose:
                    msg = '  import %s\n' % module_name
                    self.stdout.write(colorize(msg, fg='green'))
            else:
                for part in parts:
                    imported_objects[part] = getattr(module, part)

                if verbose:
                    msg = '  from %s import %s\n' % (module_name,
                                                     ', '.join(parts))
                    self.stdout.write(colorize(msg, fg='green'))

        return imported_objects
Example #10
0
    def get(self, request, *args, **kwargs):
        """
            Connect to WS and send initial data from apps models get_initial_flow method
        """

        user = request.user
        if not (user.is_authenticated()):
            raise Http404

        flow = []
        ctx = {}
        for app in apps.get_apps():
            if app.__name__.startswith('apps'):
                if hasattr(app, 'get_initial_flow'):
                    for type, data in app.get_initial_flow(request, ctx):
                        flow.append({'type': type, 'data': data})

        return JsonResponse(flow, safe=False)
Example #11
0
def remove_orphaned_images():
    if settings.MEDIA_ROOT == '':
        log.info("MEDIA_ROOT is not set, nothing to do")
        return

    # Get a list of all files under MEDIA_ROOT
    media = []
    for root, dirs, files in os.walk(settings.MEDIA_ROOT):
        for f in files:
            if ('geoserver_icons' not in root) and ('resized' not in root):
                media.append(os.path.abspath(os.path.join(root, f)))
    # Get list of all fields (value) for each model (key)
    # that is a FileField or subclass of a FileField
    model_dict = defaultdict(list)
    for app in cache.get_apps():
        model_list = cache.get_models(app)
        for model in model_list:
            for field in model._meta.fields:
                if issubclass(field.__class__, models.FileField):
                    model_dict[model].append(field)

    # Get a list of all files referenced in the database
    referenced = []
    for model in model_dict:
        all = model.objects.all().iterator()
        for object in all:
            for field in model_dict[model]:
                target_file = getattr(object, field.name)
                if target_file:
                    referenced.append(os.path.abspath(target_file.path))

    # Print each file in MEDIA_ROOT that is not referenced in the database
    c = 0
    for m in media:
        if m not in referenced:
            log.info('Removing image %s' % m)
            os.remove(m)
            c = c + 1
    info = 'Removed %s images, from a total of %s (referenced %s)' % (
        c, len(media), len(referenced))
    log.info(info)
    return info
Example #12
0
    def get_resetable_apps(app_labels=()):
        """ Список приложений, чьи миграции нужно сбросить """
        local_apps = {}
        for app in apps.get_apps():
            app_path = apps._get_app_path(app)
            if app_path.startswith(settings.BASE_DIR):
                app_name = app.__name__.rsplit('.', 1)[0]
                local_apps[app_name] = app_path

        if app_labels:
            result_apps = {}
            for app_label in app_labels:
                if app_label in local_apps:
                    result_apps[app_label] = local_apps[app_label]
                else:
                    raise CommandError('application %s not found' % app_label)
            else:
                return result_apps
        else:
            return local_apps
    def handle(self, *args, **kwargs):
        self.style = color_style()
        installed_apps = dict((a.__name__.rsplit('.', 1)[0], a) for a in apps.get_apps())

        # Make sure we always have args
        if not args:
            args = [False]
        app = installed_apps.get(args[0])
        if not app:
            print(self.style.WARN('This command requires an existing app name as argument'))
            print(self.style.WARN('Available apps:'))
            for app in sorted(installed_apps):
                print(self.style.WARN('    %s' % app))
            sys.exit(1)

        model_res = []
        for arg in args[1:]:
            model_res.append(re.compile(arg, re.IGNORECASE))

        self.handle_app(app, model_res, **kwargs)
Example #14
0
 def generate_model_docs(self):
     WIKI_DIR = os.path.dirname(os.path.dirname(settings.BASE_DIR)) + "/JobTrak.wiki/"
     for app in apps.get_apps():
         if str(app.__package__).startswith('mmg.jobtrak'):
             print("".join(["--> Processing ",str(app.__package__),"..."]))
             a_filename =''.join([
                 "App:-",
                 app.__package__.replace('.','-').replace('mmg-jobtrak-',''),
                 ".md"
             ])
             a_content = "### App: " + app.__package__.replace('mmg.jobtrak.','') + "\n"
             a_content += "**Package**: " + str(app.__package__) + "\n\n"
             if len(apps.get_models(app)) > 0:
                 a_content += "| Model |\n| ----- |\n"
                 for model in apps.get_models(app):
                     m_filename=''.join([
                         "Model:-",
                         str(app.__package__).replace(".","-").replace('mmg-jobtrak-',''),
                         "-",
                         model._meta.object_name,
                         ".md"
                     ])
                     a_content += "| [[" + model._meta.object_name + "|" + m_filename + "]] |\n"
                     m_content = "### Model: " + app.__package__.replace('mmg.jobtrak.','') + "\n"
                     m_content += "**Package**: " + app.__package__ + "\n"
                     m_content += "[[Back to App|" + a_filename + "]]\n\n"
                     m_content += self.generate_model_table_md(model)
                     m_content += "\n\n" + self.get_rev_date()
                     print(" ".join(["    - Writing file:",m_filename]))
                     f = open(WIKI_DIR + m_filename, 'w')
                     f.write(m_content)
                     f.close()
             else: # No models.
                 a_content += "TODO: There are presently no models in this app."
             a_content += "\n\n" + self.get_rev_date()
             print(" ".join(["    - Writing file:", a_filename]))
             f = open(WIKI_DIR + a_filename, 'w')
             f.write(a_content)
             f.close()
         else:
             print("".join(["--> Skipping ",str(app.__package__),"..."]))
Example #15
0
    def get(self, request, *args, **kwargs):
        """
            Connect to WS and send initial data from apps models get_initial_flow method
        """

        user = request.user
        if not (user.is_authenticated()):
            raise Http404

        flow = []
        ctx = {}
        for app in apps.get_apps():
            if app.__name__.startswith('apps'):
                if hasattr(app, 'get_initial_flow'):
                    for type, data in app.get_initial_flow(request, ctx):
                        flow.append({
                            'type': type,
                            'data': data
                        })

        return JsonResponse(flow, safe=False)
Example #16
0
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
        from django.apps import apps
        from django.test.runner import reorder_suite

        suite = self.test_suite()

        if test_labels:
            apps_to_test = [apps.get_app(label) for label in test_labels]
        else:
            apps_to_test = apps.get_apps()

        # always get all features for given apps (for convenience)
        for app in apps_to_test:
            # Check to see if a separate 'features' module exists,
            # parallel to the models module
            features_dir = get_features(app)
            if features_dir is not None:
                # build a test suite for this directory
                suite.addTest(self.make_bdd_test_suite(features_dir))

        return reorder_suite(suite, self.reorder_by)
Example #17
0
    def handle(self, *args, **kwargs):
        self.style = color_style()
        installed_apps = dict(
            (a.__name__.rsplit('.', 1)[0], a) for a in apps.get_apps())

        # Make sure we always have args
        if not args:
            args = [False]
        app = installed_apps.get(args[0])
        if not app:
            print(
                self.style.WARN(
                    'This command requires an existing app name as argument'))
            print(self.style.WARN('Available apps:'))
            for app in sorted(installed_apps):
                print(self.style.WARN('    %s' % app))
            sys.exit(1)

        model_res = []
        for arg in args[1:]:
            model_res.append(re.compile(arg, re.IGNORECASE))

        self.handle_app(app, model_res, **kwargs)
Example #18
0
 def get_apps():
     return (app for app in apps.get_apps()
             if app.__file__.startswith(settings.BASE_DIR))
Example #19
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
        transaction.set_autocommit(False)
        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 apps.get_models(app, include_auto_created=True)
                if router.allow_syncdb(db, m)])
            for app in apps.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 = OrderedDict(
            (app_name, list(filter(model_installed, model_list)))
            for app_name, model_list in all_models
        )

        # Create the tables for each model
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        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))

        transaction.commit(using=db)

        # Send the post_syncdb signal, so individual apps can do whatever they need
        # to do at this point.
        emit_post_migrate_signal(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:
                            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()
                            transaction.rollback(using=db)
                        else:
                            transaction.commit(using=db)
                    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:
                            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))
                            transaction.rollback(using=db)
                        else:
                            transaction.commit(using=db)

        # 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 #20
0
 def generate_app_and_model_docs(self):
     print(self.get_header("Generate App and Model Wiki Docs in Markdown"))
     p_filename = 'Design:-Models'
     p_content = "### Project Models\n\n"
     p_content += "\n| App | Models |\n| :----- | :----- |\n"
     for app in apps.get_apps():
         if str(app.__package__).startswith('mmg.jobtrak'):
             print("".join(["--> Processing ",
                            str(app.__package__), "..."]))
             a_filename = ''.join([
                 "App:-",
                 app.__package__.replace('.',
                                         '-').replace('mmg-jobtrak-', '')
             ])
             a_content = "### App: " + app.__package__.replace(
                 'mmg.jobtrak.', '') + "\n"
             a_content += "**Package**: " + str(app.__package__) + "\n\n"
             p_content += "| [[" + str(
                 app.__package__) + "|" + a_filename + "]] | "
             if len(apps.get_models(app)) > 0:
                 a_content += "| Model |\n| ----- |\n"
                 for model in apps.get_models(app):
                     m_filename = ''.join([
                         "Model:-",
                         str(app.__package__).replace(".", "-").replace(
                             'mmg-jobtrak-', ''), "-",
                         model._meta.object_name
                     ])
                     p_content += "[[" + model._meta.object_name + "|" + m_filename + "]] "
                     a_content += "| [[" + model._meta.object_name + "|" + m_filename + "]] |\n"
                     m_content = "### Model: " + model._meta.object_name + "\n"
                     m_content += "**Package**: " + str(
                         app.__package__) + "\n\n"
                     m_content += "[[Back to " + app.__package__.replace(
                         'mmg.jobtrak.', '')
                     m_content += "|" + a_filename + "]]\n\n"
                     m_content += self.generate_model_table_md(model)
                     m_content += "\n\n" + self.get_rev_date()
                     print(" ".join(
                         ["    - Writing file:", m_filename + ".md"]))
                     f = open(self.WIKI_DIR + m_filename + ".md", 'w')
                     f.write(m_content)
                     f.close()
                 p_content += " |\n"
                 a_content += "\n![" + str(
                     app.__package__
                 ) + "](https://raw.githubusercontent.com/MarconiMediaGroup/JobTrak/master/doc/model_maps/"
                 a_content += str(app.__package__).replace('.',
                                                           '-') + ".png)\n"
             else:  # No models.
                 p_content += " (No models in this app yet) |\n"
                 a_content += "TODO: There are presently no models in this app."
             a_content += "\n\n" + self.get_rev_date()
             print(" ".join(["    - Writing file:", a_filename]))
             f = open(self.WIKI_DIR + a_filename + ".md", 'w')
             f.write(a_content)
             f.close()
     p_content += "![](https://raw.githubusercontent.com/MarconiMediaGroup/JobTrak/master/doc/model_maps/all.png)"
     p_content += "\n\n" + self.get_rev_date()
     f = open(self.WIKI_DIR + p_filename + ".md", 'w')
     f.write(p_content)
     f.close()
Example #21
0
def get_apps():
    """
    Returns a list of all installed modules that contain models.
    """
    return apps.get_apps()
Example #22
0
 def handle(self, *args, **options):
     for app in apps.get_apps():
         couchdbkit_handler.copy_designs(app, temp='tmp', verbosity=2)
Example #23
0
 def handle(self, *args, **options):
     for app in apps.get_apps():
         couchdbkit_handler.sync(app, verbosity=2, temp='tmp')
Example #24
0
def get_apps():
    """
    Returns a list of all installed modules that contain models.
    """
    return apps.get_apps()
Example #25
0
    def handle(self, *app_labels, **options):
        from django.apps import apps

        indent = options.get('indent', None)
        using = options.get('database', DEFAULT_DB_ALIAS)
        connection = connections[using]
        excludes = options.get('exclude',[])
        show_traceback = options.get('traceback', False)
        use_natural_foreign_keys = options.get('use_natural_foreign_keys', False)
        use_base_manager = options.get('use_base_manager', False)

        excluded_apps = set()
        excluded_models = set()
        for exclude in excludes:
            if '.' in exclude:
                app_label, model_name = exclude.split('.', 1)
                model_obj = apps.get_model(app_label, model_name)
                if not model_obj:
                    raise CommandError('Unknown model in excludes: %s' % exclude)
                excluded_models.add(model_obj)
            else:
                try:
                    app_obj = apps.get_app(exclude)
                    excluded_apps.add(app_obj)
                except ImproperlyConfigured:
                    raise CommandError('Unknown app in excludes: %s' % exclude)

        if len(app_labels) == 0:
            app_list = OrderedDict((app, None) for app in apps.get_apps() if app not in excluded_apps)
        else:
            app_list = OrderedDict()
            for label in app_labels:
                try:
                    app_label, model_label = label.split('.')
                    try:
                        app = apps.get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)
                    if app in excluded_apps:
                        continue
                    model = apps.get_model(app_label, model_label)
                    if model is None:
                        raise CommandError("Unknown model: %s.%s" % (app_label, model_label))

                    if app in list(app_list.keys()):
                        if app_list[app] and model not in app_list[app]:
                            app_list[app].append(model)
                    else:
                        app_list[app] = [model]
                except ValueError:
                    # This is just an app - no model qualifier
                    app_label = label
                    try:
                        app = apps.get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)
                    if app in excluded_apps:
                        continue
                    app_list[app] = None

        # Now collate the objects to be serialized.
        objects = []
        for model in sort_dependencies(list(app_list.items())):
            if model in excluded_models:
                continue
            if not model._meta.proxy and router.allow_migrate(using, model):
                if use_base_manager:
                    objects.extend(model._base_manager.using(using).all())
                else:
                    objects.extend(model._default_manager.using(using).all())

        try:
            serializer = XMLExportSerializer()
            return serializer.serialize(objects, indent=indent, use_natural_foreign_keys=use_natural_foreign_keys)
        except Exception as e:
            if show_traceback:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
Example #26
0
def tables_used_by_fixtures(fixture_labels, using=DEFAULT_DB_ALIAS):
    """Act like Django's stock loaddata command, but, instead of loading data,
    return an iterable of the names of the tables into which data would be
    loaded."""
    # Keep a count of the installed objects and fixtures
    fixture_count = 0
    loaded_object_count = 0
    fixture_object_count = 0
    tables = set()

    class SingleZipReader(zipfile.ZipFile):
        def __init__(self, *args, **kwargs):
            zipfile.ZipFile.__init__(self, *args, **kwargs)
            if settings.DEBUG:
                assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
        def read(self):
            return zipfile.ZipFile.read(self, self.namelist()[0])

    compression_types = {
        None: file,
        'gz': gzip.GzipFile,
        'zip': SingleZipReader
    }
    if has_bz2:
        compression_types['bz2'] = bz2.BZ2File

    app_module_paths = []
    for app in apps.get_apps():
        if hasattr(app, '__path__'):
            # It's a 'models/' subpackage
            for path in app.__path__:
                app_module_paths.append(path)
        else:
            # It's a models.py module
            app_module_paths.append(app.__file__)

    app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
    for fixture_label in fixture_labels:
        parts = fixture_label.split('.')

        if len(parts) > 1 and parts[-1] in compression_types:
            compression_formats = [parts[-1]]
            parts = parts[:-1]
        else:
            compression_formats = compression_types.keys()

        if len(parts) == 1:
            fixture_name = parts[0]
            formats = serializers.get_public_serializer_formats()
        else:
            fixture_name, format = '.'.join(parts[:-1]), parts[-1]
            if format in serializers.get_public_serializer_formats():
                formats = [format]
            else:
                formats = []

        if not formats:
            # stderr.write(style.ERROR("Problem installing fixture '%s': %s is
            # not a known serialization format.\n" % (fixture_name, format)))
            return set()

        if os.path.isabs(fixture_name):
            fixture_dirs = [fixture_name]
        else:
            fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']

        for fixture_dir in fixture_dirs:
            # stdout.write("Checking %s for fixtures...\n" %
            # humanize(fixture_dir))

            label_found = False
            for combo in product([using, None], formats, compression_formats):
                database, format, compression_format = combo
                file_name = '.'.join(
                    p for p in [
                        fixture_name, database, format, compression_format
                    ]
                    if p
                )

                # stdout.write("Trying %s for %s fixture '%s'...\n" % \
                # (humanize(fixture_dir), file_name, fixture_name))
                full_path = os.path.join(fixture_dir, file_name)
                open_method = compression_types[compression_format]
                try:
                    fixture = open_method(full_path, 'r')
                    if label_found:
                        fixture.close()
                        # stderr.write(style.ERROR("Multiple fixtures named
                        # '%s' in %s. Aborting.\n" % (fixture_name,
                        # humanize(fixture_dir))))
                        return set()
                    else:
                        fixture_count += 1
                        objects_in_fixture = 0
                        loaded_objects_in_fixture = 0
                        # stdout.write("Installing %s fixture '%s' from %s.\n"
                        # % (format, fixture_name, humanize(fixture_dir)))
                        try:
                            objects = serializers.deserialize(format, fixture, using=using)
                            for obj in objects:
                                objects_in_fixture += 1
                                if router.allow_syncdb(using, obj.object.__class__):
                                    loaded_objects_in_fixture += 1
                                    tables.add(
                                        obj.object.__class__._meta.db_table)
                            loaded_object_count += loaded_objects_in_fixture
                            fixture_object_count += objects_in_fixture
                            label_found = True
                        except (SystemExit, KeyboardInterrupt):
                            raise
                        except Exception:
                            fixture.close()
                            # stderr.write( style.ERROR("Problem installing
                            # fixture '%s': %s\n" % (full_path, ''.join(tra
                            # ceback.format_exception(sys.exc_type,
                            # sys.exc_value, sys.exc_traceback)))))
                            return set()
                        fixture.close()

                        # If the fixture we loaded contains 0 objects, assume that an
                        # error was encountered during fixture loading.
                        if objects_in_fixture == 0:
                            # stderr.write( style.ERROR("No fixture data found
                            # for '%s'. (File format may be invalid.)\n" %
                            # (fixture_name)))
                            return set()

                except Exception:
                    # stdout.write("No %s fixture '%s' in %s.\n" % \ (format,
                    # fixture_name, humanize(fixture_dir)))
                    pass

    return tables
Example #27
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 = djApps.get_apps()

    for app_label in app_labels:
        app = djApps.get_app(app_label)
        if app not 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 = djApps.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:
                # excluding field redundant with inheritance relation
                if field.attname.endswith("_ptr_id"):
                    continue
                # excluding fields inherited from abstract classes. they too
                # show as local_fields
                if field in abstract_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
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """
    worsk exactly as per normal test
    but only creates the test_db if it doesn't yet exist
    and does not destroy it when done
    tables are flushed and fixtures loaded between tests as per usual
    but if your schema has not changed then this saves significant amounts of time
    and speeds up the test cycle

    Run the unit tests for all the test labels in the provided list.
    Labels must be of the form:
     - app.TestClass.test_method
        Run a single specific test method
     - app.TestClass
        Run all the test methods in a given class
     - app
        Search for doctests and unittests in the named application.

    When looking for tests, the test runner will look in the models and
    tests modules for the application.

    A list of 'extra' tests may also be provided; these tests
    will be added to the test suite.

    Returns the number of tests that failed.
    """
    setup_test_environment()

    settings.DEBUG = False
    suite = unittest.TestSuite()

    if test_labels:
        for label in test_labels:
            if '.' in label:
                suite.addTest(build_test(label))
            else:
                app = apps.get_app(label)
                suite.addTest(build_suite(app))
    else:
        for app in apps.get_apps():
            suite.addTest(build_suite(app))

    for test in extra_tests:
        suite.addTest(test)

    suite = reorder_suite(suite, (TestCase, ))

    old_name = settings.DATABASES['default']['NAME']

    # Everything up to here is from django.test.simple

    from django.db.backends import creation
    from django.db import connection, DatabaseError

    if settings.DATABASES['default']['TEST_NAME']:
        settings.DATABASES['default']['NAME'] = settings.DATABASES['default'][
            'TEST_NAME']
    else:
        settings.DATABASES['default'][
            'NAME'] = creation.TEST_DATABASE_PREFIX + settings.DATABASES[
                'default']['NAME']
    connection.settings_dict["DATABASE_NAME"] = settings.DATABASES['default'][
        'NAME']

    # does test db exist already ?
    try:
        if settings.DATABASES['default']['ENGINE'] == 'sqlite3':
            if not os.path.exists(settings.DATABASES['default']['NAME']):
                raise DatabaseError
        cursor = connection.cursor()
    except Exception:
        # db does not exist
        # juggling !  create_test_db switches the DATABASE_NAME to the TEST_DATABASE_NAME
        settings.DATABASES['default']['NAME'] = old_name
        connection.settings_dict["DATABASE_NAME"] = old_name
        connection.creation.create_test_db(verbosity, autoclobber=True)
    else:
        connection.close()

    settings.DATABASES['default'][
        'SUPPORTS_TRANSACTIONS'] = connections_support_transactions()

    result = unittest.TextTestRunner(verbosity=verbosity).run(suite)

    # Since we don't call destory_test_db, we need to set the db name back.
    settings.DATABASES['default']['NAME'] = old_name
    connection.settings_dict["DATABASE_NAME"] = old_name
    teardown_test_environment()

    return len(result.failures) + len(result.errors)
Example #29
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 = djApps.get_apps()

    for app_label in app_labels:
        app = djApps.get_app(app_label)
        if app not 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 = djApps.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:
                # excluding field redundant with inheritance relation
                if field.attname.endswith('_ptr_id'):
                    continue
                # excluding fields inherited from abstract classes. they too
                # show as local_fields
                if field in abstract_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 #30
0
def run_cmd(args):
    print(' '.join(args))
    res = subprocess.call(args)
    if res != 0:
        print('Failed')
        sys.exit(1)


django.setup()

run_cmd(['python', 'manage.py', 'graph_models', '-aE',
         '-o', 'docs/source/models_svg/overview.svg'])
file_contents += SECTION.format('overview', '-' * len('overview'))

for app in sorted(apps.get_apps(), key=lambda x: x.__name__):
    if not app.__name__.startswith('pdc.'):
        continue

    try:
        _, _, name, _ = app.__name__.split('.')
    except ValueError:
        sys.exit(1)

    run_cmd(['python', 'manage.py', 'graph_models', '-gE', name,
             '-o', 'docs/source/models_svg/{}.svg'.format(name)])
    file_contents += SECTION.format(name, '-' * len(name))

with open('docs/source/model_graphs.rst', 'w') as f:
    f.write(file_contents)
Example #31
0
 def handle(self, *args, **options):
     for app in apps.get_apps():
         couchdbkit_handler.copy_designs(app, temp='tmp', verbosity=2)
Example #32
0
def all_concrete_models():
    return [(app, [model for model in apps.get_models(app) if not model._meta.abstract]) for app in apps.get_apps() if apps.get_models(app)]
def _stashable_models():
    for app in apps.get_apps():
        for model in apps.get_models(app):
            if issubclass(model, SessionStashable):
                yield model
Example #34
0
 def handle_all_apps(self, output=sys.stdout, **options):
     for app in apps.get_apps():
         self.handle_app_config(app, output, **options)
Example #35
0
 def handle(self, *args, **options):
     for app in apps.get_apps():
         couchdbkit_handler.sync(app, verbosity=2)
Example #36
0
def all_concrete_models():
    return [
        (app,
         [model for model in apps.get_models(app) if not model._meta.abstract])
        for app in apps.get_apps() if apps.get_models(app)
    ]