Changeset View
Changeset View
Standalone View
Standalone View
search/serializers/base.py
| Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | def _set_common_additional_fields( | ||||
| self, instance_dict: Dict[Any, Any], instance: SearchableModel | self, instance_dict: Dict[Any, Any], instance: SearchableModel | ||||
| ) -> Dict[Any, Any]: | ) -> Dict[Any, Any]: | ||||
| """Adds fields common to all searchable models to the values dict of the instance.""" | """Adds fields common to all searchable models to the values dict of the instance.""" | ||||
| instance_dict['url'] = instance.url | instance_dict['url'] = instance.url | ||||
| instance_dict['timestamp'] = instance.date_created.timestamp() | instance_dict['timestamp'] = instance.date_created.timestamp() | ||||
| if isinstance(instance, Asset): | if isinstance(instance, Asset): | ||||
| instance_dict['thumbnail_url'] = ( | instance_dict['thumbnail_url'] = ( | ||||
| instance.static_asset.preview.url if instance.static_asset.preview else '' | instance.static_asset.thumbnail_url if instance.static_asset.preview else '' | ||||
| ) | ) | ||||
| elif isinstance(instance, Section): | elif isinstance(instance, Section): | ||||
| instance_dict['thumbnail_url'] = instance.chapter.training.thumbnail.url | instance_dict['thumbnail_url'] = instance.chapter.training.thumbnail_url | ||||
| else: | else: | ||||
| instance_dict['thumbnail_url'] = instance.thumbnail.url | instance_dict['thumbnail_url'] = instance.thumbnail_url | ||||
| return instance_dict | return instance_dict | ||||
| def _serialize_data( | def _serialize_data( | ||||
| self, qs_values: 'ValuesQuerySet[SearchableModel, Any]' | self, qs_values: 'ValuesQuerySet[SearchableModel, Any]' | ||||
| ) -> List[Dict[str, Any]]: | ) -> List[Dict[str, Any]]: | ||||
| """Turns values queryset into a list of objects that can be added to a search index. | """Turns values queryset into a list of objects that can be added to a search index. | ||||
| The Index.add_documents method expects a list of objects, not a single object. | The Index.add_documents method expects a list of objects, not a single object. | ||||
| Datetime values have to be serialized with DjangoJSONEncoder. | Datetime values have to be serialized with DjangoJSONEncoder. | ||||
| """ | """ | ||||
| serialized_data: List[Dict[str, Any]] = json.loads( | serialized_data: List[Dict[str, Any]] = json.loads( | ||||
| json.dumps(list(qs_values), cls=DjangoJSONEncoder) | json.dumps(list(qs_values), cls=DjangoJSONEncoder) | ||||
| ) | ) | ||||
| return serialized_data | return serialized_data | ||||