translated field into serializer
This commit is contained in:
parent
b326d7b8fd
commit
87efc50d94
|
|
@ -1,6 +1,7 @@
|
||||||
"""News app documents."""
|
"""News app documents."""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django_elasticsearch_dsl import Document, Index, fields
|
from django_elasticsearch_dsl import Document, Index, fields
|
||||||
|
from search_indexes.utils import OBJECT_FIELD_PROPERTIES
|
||||||
from news import models
|
from news import models
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -16,9 +17,9 @@ class NewsDocument(Document):
|
||||||
'id': fields.IntegerField(),
|
'id': fields.IntegerField(),
|
||||||
'name': fields.KeywordField()
|
'name': fields.KeywordField()
|
||||||
})
|
})
|
||||||
title = fields.ObjectField()
|
title = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
|
||||||
subtitle = fields.ObjectField()
|
subtitle = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
|
||||||
description = fields.ObjectField()
|
description = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
|
||||||
country = fields.NestedField(properties={
|
country = fields.NestedField(properties={
|
||||||
'id': fields.IntegerField(),
|
'id': fields.IntegerField(),
|
||||||
'code': fields.KeywordField()
|
'code': fields.KeywordField()
|
||||||
|
|
@ -45,5 +46,3 @@ class NewsDocument(Document):
|
||||||
|
|
||||||
def prepare_description(self, instance):
|
def prepare_description(self, instance):
|
||||||
return instance.description
|
return instance.description
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,15 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
||||||
from search_indexes.documents.news import NewsDocument
|
from search_indexes.documents.news import NewsDocument
|
||||||
from utils.models import get_current_language
|
from search_indexes.utils import get_translated_value
|
||||||
|
|
||||||
|
|
||||||
class NewsDocumentSerializer(DocumentSerializer):
|
class NewsDocumentSerializer(DocumentSerializer):
|
||||||
"""News document serializer."""
|
"""News document serializer."""
|
||||||
|
|
||||||
title_translated = serializers.SerializerMethodField(allow_null=True)
|
title_translated = serializers.SerializerMethodField(allow_null=True)
|
||||||
|
subtitle_translated = serializers.SerializerMethodField(allow_null=True)
|
||||||
|
description_translated = serializers.SerializerMethodField(allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
@ -21,10 +23,18 @@ class NewsDocumentSerializer(DocumentSerializer):
|
||||||
'description',
|
'description',
|
||||||
'web_url',
|
'web_url',
|
||||||
'title_translated',
|
'title_translated',
|
||||||
|
'subtitle_translated',
|
||||||
|
'description_translated',
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_title_translated(self, obj):
|
@staticmethod
|
||||||
title_dict = obj.title
|
def get_title_translated(obj):
|
||||||
if not isinstance(title_dict, dict):
|
return get_translated_value(obj.title)
|
||||||
title_dict = obj.title.to_dict()
|
|
||||||
return title_dict.get(get_current_language())
|
@staticmethod
|
||||||
|
def get_subtitle_translated(obj):
|
||||||
|
return get_translated_value(obj.subtitle)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_description_translated(obj):
|
||||||
|
return get_translated_value(obj.description)
|
||||||
|
|
|
||||||
19
apps/search_indexes/utils.py
Normal file
19
apps/search_indexes/utils.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
"""Search indexes utils."""
|
||||||
|
from django_elasticsearch_dsl import fields
|
||||||
|
from utils.models import get_current_language
|
||||||
|
|
||||||
|
|
||||||
|
# object field properties
|
||||||
|
OBJECT_FIELD_PROPERTIES = {
|
||||||
|
'en-GB': fields.TextField(analyzer='english'),
|
||||||
|
'ru-RU': fields.TextField(analyzer='russian'),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# todo: refactor serializer
|
||||||
|
def get_translated_value(value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
elif not isinstance(value, dict):
|
||||||
|
field_dict = value.to_dict()
|
||||||
|
return field_dict.get(get_current_language())
|
||||||
Loading…
Reference in New Issue
Block a user