in kitsune/search/base.py [0:0]
def prepare(cls, instance, parent_id=None, **kwargs):
"""Prepare an object given a model instance.
parent_id: Supplying a parent_id will ignore any fields which aren't a
SumoLocaleAware field, and set the meta.id value to the parent_id one.
"""
obj = cls()
doc_mapping = obj._doc_type.mapping
fields = [f for f in doc_mapping]
fields.remove("indexed_on")
# Loop through the fields of each object and set the right values
# check if the instance is suitable for indexing
# the prepare method of each Document Type can mark an object
# not suitable for indexing based on criteria defined on each said method
if not hasattr(instance, "es_discard_doc"):
for f in fields:
# This will allow child classes to have their own methods
# in the form of prepare_field
prepare_method = getattr(obj, f"prepare_{f}", None)
value = obj.get_field_value(f, instance, prepare_method)
# Assign values to each field.
field_type = doc_mapping.resolve_field(f)
if isinstance(field_type, field.Object) and not (
isinstance(value, InnerDoc)
or (isinstance(value, list) and isinstance((value or [None])[0], InnerDoc))
):
# if the field is an Object but the value isn't an InnerDoc
# or a list containing an InnerDoc then we're dealing with locales
locale = obj.prepare_locale(instance)
# Check specifically against None, False is a valid value
if locale and (value is not None):
obj[f] = {locale: value}
else:
if (
isinstance(field_type, field.Date)
and isinstance(value, datetime)
and timezone.is_naive(value)
):
value = timezone.make_aware(value).astimezone(timezone.utc)
if not parent_id:
setattr(obj, f, value)
else:
obj.es_discard_doc = "unindex_me"
obj.indexed_on = datetime.now(timezone.utc)
obj.meta.id = instance.pk
if parent_id:
obj.meta.id = parent_id
return obj