コード例 #1
0
ファイル: registry.py プロジェクト: laranea/solango
    def get_document(self, instance, initial=False):
        
        key = get_instance_key(instance)

        if key not in self.data.keys():
            raise NotRegistered('Instance not registered with Solango')
                
        return self.data[key](instance, initial)
コード例 #2
0
ファイル: fields.py プロジェクト: laranea/solango
 def transform(self, instance):
     """
     Returns a unique identifier string for the specified object.
     
     This avoids duplicate documents
     """
     self.value = self.make_key(get_instance_key(instance), instance.pk)
     
     return unicode(self)
コード例 #3
0
ファイル: registry.py プロジェクト: laranea/solango
 def register(self, search_document=None, model=None, document_index=None,
                      connect_signals=True, document_key=None):
     """ Register Models With Solango """
 
     assert model is not None or search_document is not None, \
          "Register needs a Model or a Search Document"
 
     if document_key is None:
         if model:
             document_key = get_instance_key(model)
         elif search_document and search_document.model_key:
             document_key = search_document.model_key
         else:
             raise AttributeError("Register need a model or search document key")
     
     document = self.get(document_key)
     if document:
         raise AlreadyRegistered('%s has already been registered by search'
                                         % (model or search_document))
     
     #Set Defaults
     if not search_document and model:
         from solango.solr.documents import SearchDocument
         search_document = SearchDocument
     
     if not document_index:
         from solango.solr.indexes.base import Index
         document_index = Index()
     
     search_document.set_index(document_index)
     
     self[document_key] = search_document
     
     #Connect Signals
     if connect_signals and model:
         signals.post_save.connect(document_index.post_save, model)
         signals.post_delete.connect(document_index.post_delete, model)
コード例 #4
0
ファイル: fields.py プロジェクト: laranea/solango
 def transform(self, instance):
     self.value = get_instance_key(instance)
     return unicode(self)
コード例 #5
0
ファイル: documents.py プロジェクト: laranea/solango
    def __init__(self, arg, initial=False):
        """
        Takes a model, form or dict.
        
        For a model or form it assumes that you are trying to create a document
        from the values
        
        For a dict it assumes that you received results from Solr and you want 
        to make a Python object representation of the model     
        """
        
        self.fields = deepcopy(self.base_fields)
        self.pk_field = None
        
        self._instance = None
        self.orginal_dict = {}
        
        self.data_dict = {}
        self.highlight = ""
        self.boost = ""
        self._transformed = False
        
        #Model
        if isinstance(arg, Model):
            self._instance = arg
            self.data_dict = model_to_dict(arg)
        #Form
        elif isinstance(arg, BaseForm):
            if not arg.is_valid():
                raise AttributeError("Form is not valid %s" % 
                                     ["%s:%s" % (key, "".join(value))
                                       for key, value in arg.errors] )
            if not arg.cleaned_data.has_key("id"):
                raise AttributeError("Solango requires that all forms have"+ \
                                " an id field. subclass forms.BaseSolangoForm")
            
            #We need a primary key. This seems to to the trick
            instance = idict(arg.cleaned_data)
            instance.model = get_instance_key(arg)
            
            self._instance = instance
            self.data_dict = arg.cleaned_data
        elif initial:
            self._instance = idict(arg)
            self.data_dict = arg
        #Dictionary
        elif isinstance(arg, dict):
            self.data_dict = arg
        #Error
        else:
            raise ValueError('Argument must be a Model, Form or a Dictionary')
        
        # Find Primary Key Field
        for field in self.fields.values():
            #Save value
            if isinstance(field, search_fields.PrimaryKeyField):
                self.pk_field = field
                if self._instance and not hasattr(self._instance, "pk"):
                    pk = getattr(self._instance,field.name)
                    setattr(self._instance, "pk", pk)
                break
    
        if not self.pk_field:
            raise NoPrimaryKeyFieldException('Search Document needs a Primary Key Field')

        
        if self._instance:
            self._transform_field(self.pk_field)
            self.boost = self.get_boost(self._instance)
        
        elif self.data_dict:
            self.clean()
            self._transformed = True
    
        #Set Up the render Methods.
        for key, template in self.templates:
            setattr(self, "render_%s" % key,
                    curry(self._render_doc_as, template=template))