def create_initial_revisions(self,
                                 app,
                                 model_class,
                                 comment,
                                 batch_size,
                                 verbosity=2,
                                 **kwargs):
        """Creates the set of initial revisions for the given model."""
        # Import the relevant admin module.
        try:
            import_module("%s.admin" % app.__name__.rsplit(".", 1)[0])
        except ImportError:
            pass
        # Check all models for empty revisions.
        if default_revision_manager.is_registered(model_class):
            created_count = 0
            content_type = ContentType.objects.get_for_model(model_class)
            versioned_pk_queryset = Version.objects.filter(
                content_type=content_type).all()
            live_objs = model_class._default_manager.all()
            if has_int_pk(model_class):
                # We can do this as a fast database join!
                live_objs = live_objs.exclude(
                    pk__in=versioned_pk_queryset.values_list("object_id_int",
                                                             flat=True))
            else:
                # This join has to be done as two separate queries.
                live_objs = live_objs.exclude(pk__in=list(
                    versioned_pk_queryset.values_list("object_id",
                                                      flat=True).iterator()))
            # Save all the versions.
            ids = list(
                live_objs.values_list(model_class._meta.pk.name, flat=True))
            total = len(ids)
            for i in xrange(0, total, batch_size):
                chunked_ids = ids[i:i + batch_size]
                objects = live_objs.in_bulk(chunked_ids)
                for id, obj in objects.iteritems():
                    try:
                        default_revision_manager.save_revision((obj, ),
                                                               comment=comment)
                    except:
                        print "ERROR: Could not save initial version for %s %s." % (
                            model_class.__name__, obj.pk)
                        raise
                    created_count += 1
                reset_queries()
                if verbosity >= 2:
                    print u"Created %s of %s." % (created_count, total)

            # Print out a message, if feeling verbose.
            if verbosity >= 2:
                print u"Created %s initial revision(s) for model %s." % (
                    created_count, smart_unicode(
                        model_class._meta.verbose_name))
        else:
            if verbosity >= 2:
                print u"Model %s is not registered." % (smart_unicode(
                    model_class._meta.verbose_name))
    def _save_initial_revision(self, obj):
        # try to load admin
        try:
            import_module("%s.admin" % obj.__module__.rsplit('.', 1)[0])
        except ImportError:
            pass

        # save revision
        default_revision_manager.save_revision((obj,),
                                               comment='Initial revision')
示例#3
0
    def _save_initial_revision(self, obj):
        # try to load admin
        try:
            import_module("%s.admin" % obj.__module__.rsplit('.', 1)[0])
        except ImportError:
            pass

        # save revision
        default_revision_manager.save_revision((obj, ),
                                               comment='Initial revision')
    def create_initial_revisions(self, app, model_class, comment, batch_size, verbosity=2, database=None, **kwargs):
        """Creates the set of initial revisions for the given model."""
        # Import the relevant admin module.
        try:
            import_module("%s.admin" % app.__name__.rsplit(".", 1)[0])
        except ImportError:
            pass
        # Check all models for empty revisions.
        if default_revision_manager.is_registered(model_class):
            if verbosity >= 2:
                print("Creating initial revision(s) for model %s ..."  % (force_text(model_class._meta.verbose_name)))
            created_count = 0
            content_type = ContentType.objects.db_manager(database).get_for_model(model_class)
            versioned_pk_queryset = Version.objects.using(database).filter(content_type=content_type).all()
            live_objs = model_class._default_manager.using(database).all()
            if has_int_pk(model_class):
                # We can do this as a fast database join!
                live_objs = live_objs.exclude(
                    pk__in = versioned_pk_queryset.values_list("object_id_int", flat=True)
                )
            else:
                # This join has to be done as two separate queries.
                live_objs = live_objs.exclude(
                    pk__in = list(versioned_pk_queryset.values_list("object_id", flat=True).iterator())
                )
            # Save all the versions.
            ids = list(live_objs.values_list(model_class._meta.pk.name, flat=True))
            total = len(ids)
            for i in range(0, total, batch_size):
                chunked_ids = ids[i:i+batch_size]
                objects = live_objs.in_bulk(chunked_ids)
                for id, obj in objects.items():
                    try:
                        default_revision_manager.save_revision((obj,), comment=comment, db=database)
                    except:
                        print("ERROR: Could not save initial version for %s %s." % (model_class.__name__, obj.pk))
                        raise
                    created_count += 1
                reset_queries()
                if verbosity >= 2:
                    print("Created %s of %s." % (created_count, total))

            # Print out a message, if feeling verbose.
            if verbosity >= 2:
                print("Created %s initial revision(s) for model %s." % (created_count, force_text(model_class._meta.verbose_name)))
        else:
            if verbosity >= 2:
                print("Model %s is not registered."  % (force_text(model_class._meta.verbose_name)))
 def create_initial_revisions(self, app, model_class, comment, verbosity=2, **kwargs):
     """Creates the set of initial revisions for the given model."""
     # Import the relevant admin module.
     try:
         import_module("%s.admin" % app.__name__.rsplit(".", 1)[0])
     except ImportError:
         pass
     # Check all models for empty revisions.
     if default_revision_manager.is_registered(model_class):
         created_count = 0
         content_type = ContentType.objects.get_for_model(model_class)
         versioned_pk_queryset = Version.objects.filter(content_type=content_type).all()
         live_objs = model_class._default_manager.all()
         if has_int_pk(model_class):
             # We can do this as a fast database join!
             live_objs = live_objs.exclude(
                 pk__in = versioned_pk_queryset.values_list("object_id_int", flat=True)
             )
         else:
             # This join has to be done as two separate queries.
             live_objs = live_objs.exclude(
                 pk__in = list(versioned_pk_queryset.values_list("object_id", flat=True).iterator())
             )
         # Save all the versions.
         for obj in live_objs:
             try:
                 default_revision_manager.save_revision((obj,), comment=comment)
             except:
                 print "ERROR: Could not save initial version for %s %s." % (model_class.__name__, obj.pk)
                 raise
             created_count += 1
         # Print out a message, if feeling verbose.
         if verbosity >= 2:
             print u"Created %s initial revision(s) for model %s." % (created_count, smart_unicode(model_class._meta.verbose_name))
     else:
         if verbosity >= 2:
             print u"Model %s is not registered."  % (smart_unicode(model_class._meta.verbose_name))