Exemple #1
0
    def handle_noargs(self, **options):
        index_solr = options.get('index_solr')
        index_batch_size = options.get('index_batch_size')
        schema = options.get('solr_schema')
        schema_path = options.get('schema_path')
        flush_solr =options.get('flush_solr')
        solr_fields =options.get('solr_fields')
        start_solr = options.get('start_solr')
        
        from solango import conf
        from solango import autodiscover
        
        autodiscover()
        
        #### SOLR
        SOLR_SCHEMA_PATH = getattr(conf, 'SOLR_SCHEMA_PATH', None)
        SOLR_DATA_DIR = getattr(conf, 'SOLR_DATA_DIR', None)
        SOLR_ROOT = getattr(conf, 'SOLR_ROOT', None)
        
        if solr_fields:
            from solango.utils import create_schema_xml
            create_schema_xml(True)
        
        if schema:
            #Get the Path
            path = None
            if schema_path:
                path = schema_path
            elif SOLR_SCHEMA_PATH:
                path = SOLR_SCHEMA_PATH
            else:
                raise CommandError("Need to specify either a SOLR_SCHEMA_PATH in settings.py or use --path")
            #Make sure the path exists
            if not os.path.exists(path):
                raise CommandError("Path does not exist: %s" % path)
            
            if not os.path.isfile(path):
                path = os.path.join(path, 'schema.xml')
            
            from solango.utils import create_schema_xml
            f = open(path, 'w')
            f.write(create_schema_xml())
            f.close()
            print """
Successfully created schema.xml in/at: %s

******************************************************************************
* Warning : You must restart Solr in order for these changes to take affect. *
******************************************************************************
                """ % path
        
        if flush_solr:
            from solango import Index
            index = Index()
            if index.ping():
                raise CommandError("Flush has a tendency to fail if Solr is running. Please shut it down first.")
            
            if SOLR_DATA_DIR:
                if not os.path.exists(SOLR_DATA_DIR):
                    raise CommandError("Solr Data Directory has already been deleted or doesn't exist: %s" % SOLR_DATA_DIR)
                else:
                    answer = raw_input('Do you wish to delete %s: [y/N] ' % SOLR_DATA_DIR)
                    if answer == 'y' or answer == 'Y':
                        shutil.rmtree(SOLR_DATA_DIR)
                        print 'Removed %s' % SOLR_DATA_DIR
                    else:
                        print 'Did not remove %s' % SOLR_DATA_DIR
            else:
                raise CommandError("Path does not exist: %s" % path)
        
        if index_solr:
            from solango import Index
            index = Index()
            if not index.ping():
                raise CommandError("Solr connection is not available")
            
            from solango.utils import reindex
            if not index_batch_size:
                index_batch_size = getattr(conf,"SOLR_BATCH_INDEX_SIZE")

            try:
                # Throws value errors.
                index_batch_size = int(index_batch_size)
                print "Starting to reindex Solr"
                reindex(batch_size=index_batch_size, document_key=options.get('document_key'))
                print "Finished the reindex of Solr"
            except ValueError, e:
                raise CommandError("ERROR: Invalid --batch-size agrument ( %s ). exception: %s" % (str(index_batch_size), str(e)))