Example #1
0
 def _update_obj_index_iter(self, obj):
     """Either updates the given object index, or yields an unsaved search entry."""
     model = obj.__class__
     adapter = self.get_adapter(model)
     content_type = ContentType.objects.get_for_model(model)
     object_id = force_text(obj.pk)
     # Create the search entry data.
     search_entry_data = {
         "engine_slug": self._engine_slug,
         "title": adapter.get_title(obj),
         "description": adapter.get_description(obj),
         "content": adapter.get_content(obj),
         "url": adapter.get_url(obj),
         "meta_encoded": json.dumps(adapter.get_meta(obj)),
     }
     # Try to get the existing search entry.
     object_id_int, search_entries = self._get_entries_for_obj(obj)
     # Attempt to update the search entries.
     update_count = search_entries.update(**search_entry_data)
     if update_count == 0:
         # This is the first time the entry was created.
         search_entry_data.update((
             ("content_type", content_type),
             ("object_id", object_id),
             ("object_id_int", object_id_int),
         ))
         yield SearchEntry(**search_entry_data)
     elif update_count > 1:
         # Oh no! Somehow we've got duplicated search entries!
         search_entries.exclude(id=search_entries[0].id).delete()
Example #2
0
 def _update_obj_index_iter(self, obj):
     """Either updates the given object index, or yields an unsaved search entry."""
     from django.contrib.contenttypes.models import ContentType
     from watson.models import SearchEntry, get_str_pk
     model = obj.__class__
     adapter = self.get_adapter(model)
     content_type = ContentType.objects.get_for_model(model)
     object_id = get_str_pk(obj,
                            connections[router.db_for_write(ContentType)])
     # Create the search entry data.
     search_entry_data = {
         "engine_slug": self._engine_slug,
         "title": adapter.get_title(obj),
         "description": adapter.get_description(obj),
         "content": adapter.get_content(obj),
         "url": adapter.get_url(obj),
         "meta_encoded": adapter.serialize_meta(obj),
     }
     # Try to get the existing search entry.
     object_id_int, search_entries = self._get_entries_for_obj(obj)
     # Attempt to update the search entries.
     update_count = search_entries.update(**search_entry_data)
     if update_count == 0:
         # This is the first time the entry was created.
         search_entry_data.update((
             ("content_type", content_type),
             ("object_id", object_id),
             ("object_id_int", object_id_int),
         ))
         yield SearchEntry(**search_entry_data)
     elif update_count > 1:
         # Oh no! Somehow we've got duplicated search entries!
         search_entries.exclude(id=search_entries[0].id).delete()
 def update_obj_index(self, obj):
     """Updates the search index for the given obj."""
     model = obj.__class__
     adapter = self.get_adapter(model)
     content_type = ContentType.objects.get_for_model(model)
     object_id = unicode(obj.pk)
     # Try to get the existing search entry.
     object_id_int, search_entries = self._get_entries_for_obj(obj)
     try:
         search_entry = search_entries.get()
     except SearchEntry.DoesNotExist:
         search_entry = SearchEntry(
             content_type = content_type,
             object_id = object_id,
             object_id_int = object_id_int,
         )
     except SearchEntry.MultipleObjectsReturned:
         # Oh no! Some non-transactional database has messed up!
         search_entry = search_entries[0]
         search_entries.exclude(id=search_entry.id).delete()
     # Store data.
     search_entry.engine_slug = self._engine_slug
     search_entry.title = adapter.get_title(obj)
     search_entry.description = adapter.get_description(obj)
     search_entry.content = adapter.get_content(obj)
     search_entry.url = adapter.get_url(obj)
     search_entry.meta = adapter.get_meta(obj)
     # Pass on the entry for final processing to the search backend.
     get_backend().save_search_entry(search_entry, obj, adapter)