Пример #1
0
 def get_model_instance(self):
     """
     Returns the model instance that the document refers to.
     """
     if self.data_dict:
         model = get_model_from_key(self.data_dict['model'])
         id = self.data_dict['id'].split(settings.SEARCH_SEPARATOR)[2]
         
         return model.objects.get(pk=id)
     elif self._model:
         return self._model
     return None
Пример #2
0
def reindex():
    """
    Reindexes all of the models registered to solango
    """
    import solango
    from solango.solr import get_model_from_key
    
    for model_key, document in solango.registry.items():
        model = get_model_from_key(model_key)
        for instance in model.objects.all():
            doc = document(instance)
            solango.connection.add(doc)
Пример #3
0
    def get_model_instance(self):
        """
        Returns the model instance that the document refers to.
        """
        if self.data_dict:
            model = get_model_from_key(self.data_dict['model'])
            id = self.data_dict['id'].split(settings.SEARCH_SEPARATOR)[2]
            try:
                model._default_manager.get(pk=id)
            except:
                self._is_deleted = True
		return None

            return model.objects.get(pk=id)
        elif self._model:
            return self._model
        return None
Пример #4
0
    def _index_multiple(self, instances, batch_size=None):
        """
        Indexes multiple items immediately
        """
        import solango
        from solango.solr import get_model_from_key
        
        to_index = []
        to_delete = []
        
        if batch_size is None:
            batch_size = getattr(solango.settings,"SOLR_BATCH_INDEX_SIZE", 10)
        
        for instance in instances:
            doc = solango.get_document(instance)
            if doc.is_deleted():
                to_delete.append(doc)
            else:
                if isinstance(instance, (list, tuple)):
                    model = get_model_from_key(instance[0])
                    instance = model.objects.get(pk=instance[1])
                if doc.is_indexable(instance):
                    to_index.append(doc)
                else:
                    to_delete.append(doc)
            
            if (len(to_index) >= batch_size):
                solango.connection.add(to_index)
                to_index = []
            
            if (len(to_delete) >= batch_size):
                solango.connection.delete(to_delete)
                to_delete = []
                
                
        if (len(to_index) > 0):
            solango.connection.add(to_index)
        
        if (len(to_delete) > 0):
            solango.connection.delete(to_delete)

        # optimize() will also commit()
        solango.connection.optimize()
Пример #5
0
def reindex(batch_size = None):
    """
    Reindexes all of the models registered to solango
    """
    import solango
    from solango.solr import get_model_from_key
    
    instances_to_index = []
    instances_to_delete = []
    
    if batch_size is None:
        batch_size = getattr(settings,"SOLR_BATCH_INDEX_SIZE", 10)

    for model_key, document in solango.registry.items():
        model = get_model_from_key(model_key)
        for instance in model.objects.all():
            doc = document(instance)
            if doc.is_indexable(instance):
                instances_to_index.append(doc)
            else:
                instances_to_delete.append(doc)
                
            if (len(instances_to_index) >= batch_size):
                solango.connection.add(instances_to_index)
                instances_to_index = []
            
            if (len(instances_to_delete) >= batch_size):
                solango.connection.delete(instances_to_delete)
                instances_to_delete = []
            
            
    if (len(instances_to_index) > 0):
        solango.connection.add(instances_to_index)
    
    if (len(instances_to_delete) > 0):
        solango.connection.add(instances_to_delete)

    # optimize() will also commit()
    solango.connection.optimize()
Пример #6
0
    def __init__(self, arg):
        """
        Takes a model, dict, or tuple.
        
        for a model it assumes that you are trying to create a document from the values
        
        for a dict it assumes that you recieved results from solr and you want to make a 
        python object representation of the model    
        
        For a tuple of the form (model_key, instance_id), it creates a document
        from the corresponding model instance. if the instance does not exist, it
        will succeed. This is useful for creating documents to remove from the
        index, after the instance has already been deleted.
            
        """
        
        self.fields = deepcopy(self.base_fields)
        self.pk_field = None
        self._model = None
        self.data_dict = {}
        self.highlight = ""
        self.boost = ""
        self._transformed = False
        self._is_deleted = False

        # If it's a model, set the _model and create a dictionary from the fields
        if isinstance(arg, Model):
            #make it into a dict.
            self._model = arg
            self.data_dict = model_to_dict(arg)
        elif isinstance(arg, dict):
            self.data_dict = arg
        elif isinstance(arg, (tuple, list)):
            if len(arg) != 2:
                raise ValueError('Tuple argument must be of the form (model_key, instance_id)')
            
            model = get_model_from_key(arg[0])
            try:
                self._model = model.objects.get(pk=arg[1])
            except model.DoesNotExist:
                self._is_deleted = True
        else:
            raise ValueError('Argument must be a Model, a dictionary or a tuple')
        
        # Iterate through fields and get value
        for field in self.fields.values():
            #Save value
            if isinstance(field, search_fields.PrimaryKeyField):
                self.pk_field = field
                break
        
        if not self.pk_field:
            raise NoPrimaryKeyFieldException('Search Document needs a Primary Key Field')
        
        if self._model:
            self._transform_field(self.pk_field)
            self.boost = self.get_boost(self._model)
        elif self.data_dict:
            self.clean()
            self._transformed = True
            self.boost = self.get_boost(self.get_model_instance())
        else:
            self.pk_field.value = self.pk_field.make_key(arg[0], arg[1])
Пример #7
0
 def _get_all(self):
     for model_key, document in solango.registry.items():
         model = get_model_from_key(model_key)
         for instance in model.objects.all():
             yield instance
Пример #8
0
def model_choices():
    models = [('','---All---')]
    models.extend([(model_key, get_model_from_key(model_key)._meta.verbose_name_plural)
             for model_key in solango.registry.keys()])
    return models