Clean up SearchIndex

This commit is contained in:
jeremystretch 2022-10-21 08:58:28 -04:00
parent 08193e47a7
commit d713915a85

View File

@ -26,21 +26,19 @@ class SearchIndex:
Attrs:
model: The model class for which this index is used.
category: The label of the group under which this indexer is categorized (for form field display). If none,
the name of the model's app will be used.
fields: An iterable of two-tuples defining the model fields to be indexed and the weight associated with each.
"""
model = None
category = None
fields = ()
@classmethod
def get_category(cls):
"""
Return the title of the search category under which this model is registered.
"""
if hasattr(cls, 'category'):
return cls.category
return cls.model._meta.app_config.verbose_name
@staticmethod
def get_field_type(instance, field_name):
"""
Return the data type of the specified model field.
"""
field_cls = instance._meta.get_field(field_name).__class__
if issubclass(field_cls, (models.FloatField, models.DecimalField)):
return FieldTypes.FLOAT
@ -50,10 +48,26 @@ class SearchIndex:
@staticmethod
def get_field_value(instance, field_name):
"""
Return the value of the specified model field as a string.
"""
return str(getattr(instance, field_name))
@classmethod
def get_category(cls):
return cls.category or cls.model._meta.app_config.verbose_name
@classmethod
def to_cache(cls, instance, custom_fields=None):
"""
Return a list of ObjectFieldValue representing the instance fields to be cached.
Args:
instance: The instance being cached.
custom_fields: An iterable of CustomFields to include when caching the instance. If None, all custom fields
defined for the model will be included. (This can also be provided during bulk caching to avoid looking
up the available custom fields for each instance.)
"""
values = []
# Capture built-in fields