Пример #1
0
    def handle_app(self, app, **options):
        # Cause the default site to load.
        from haystack import handle_registrations
        handle_registrations()
        
        from django.db.models import get_models
        from haystack import site
        from haystack.exceptions import NotRegistered

        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

            extra_lookup_kwargs = {}
            updated_field = index.get_updated_field()
            
            if self.age:
                if updated_field:
                    extra_lookup_kwargs['%s__gte' % updated_field] = datetime.datetime.now() - datetime.timedelta(hours=self.age)
                else:
                    if self.verbosity >= 2:
                        print "No updated date field found for '%s' - not restricting by age." % model.__name__
            
            # DRL_TODO: .select_related() seems like a good idea here but
            #           can cause empty QuerySets. Why?
            qs = index.get_query_set().filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
            total = qs.count()

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

            for start in range(0, total, self.batchsize):
                end = min(start + self.batchsize, total)
                
                if self.verbosity >= 2:
                    print "  indexing %s - %d of %d." % (start+1, end, total)
                
                # Get a clone of the QuerySet so that the cache doesn't bloat up
                # in memory. Useful when reindexing large amounts of data.
                small_cache_qs = qs.all()
                index.backend.update(index, small_cache_qs[start:end])
                
                # Clear out the DB connections queries because it bloats up RAM.
                reset_queries()
Пример #2
0
 def handle_app(self, app, **options):
     # Cause the default site to load.
     from haystack import handle_registrations
     handle_registrations()
     
     from django.db.models import get_models
     from haystack.exceptions import NotRegistered
     
     if self.site:
         path_bits = self.site.split('.')
         module_name = '.'.join(path_bits[:-1])
         site_name = path_bits[-1]
         
         try:
             module = __import__(module_name, {}, {}, [''])
             site = getattr(module, site_name)
         except (ImportError, NameError):
             # Fall back to the main site.
             from haystack import site
     else:
         from haystack import site
     
     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
             
         extra_lookup_kwargs = {}
         updated_field = index.get_updated_field()
         
         if self.age:
             if updated_field:
                 extra_lookup_kwargs['%s__gte' % updated_field] = datetime.datetime.now() - datetime.timedelta(hours=self.age)
             else:
                 if self.verbosity >= 2:
                     print "No updated date field found for '%s' - not restricting by age." % model.__name__
         
         # `.select_related()` seems like a good idea here but can fail on
         # nullable `ForeignKey` as well as what seems like other cases.
         qs = index.get_queryset().filter(**extra_lookup_kwargs).order_by(model._meta.pk.name)
         total = qs.count()
         
         if self.verbosity >= 1:
             print "Indexing %d %s." % (total, smart_str(model._meta.verbose_name_plural))
         
         for start in range(0, total, self.batchsize):
             end = min(start + self.batchsize, total)
             
             if self.verbosity >= 2:
                 print "  indexing %s - %d of %d." % (start+1, end, total)
             
             # Get a clone of the QuerySet so that the cache doesn't bloat up
             # in memory. Useful when reindexing large amounts of data.
             small_cache_qs = qs.all()
             index.backend.update(index, small_cache_qs[start:end])
             
             # Clear out the DB connections queries because it bloats up RAM.
             reset_queries()