localized cities #7

This commit is contained in:
Kuroshini 2020-01-28 12:35:23 +03:00
parent c0f3aca532
commit 029cd36b98
4 changed files with 19 additions and 7 deletions

View File

@ -1,20 +1,20 @@
"""Location app models.""" """Location app models."""
from functools import reduce
from json import dumps
from typing import List
from django.conf import settings from django.conf import settings
from django.contrib.gis.db import models from django.contrib.gis.db import models
from django.contrib.postgres.fields import ArrayField
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.db.transaction import on_commit from django.db.transaction import on_commit
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from functools import reduce
from typing import List
from django.contrib.postgres.fields.jsonb import KeyTextTransform
from django.contrib.postgres.fields import ArrayField
from translation.models import Language from translation.models import Language
from utils.models import (ProjectBaseMixin, SVGImageMixin, TJSONField, from utils.models import (ProjectBaseMixin, SVGImageMixin, TJSONField,
TranslatedFieldsMixin, get_current_locale, TranslatedFieldsMixin, get_current_locale,
IntermediateGalleryModelMixin, GalleryMixin) IntermediateGalleryModelMixin)
class CountryQuerySet(models.QuerySet): class CountryQuerySet(models.QuerySet):
@ -180,6 +180,11 @@ class City(models.Model, TranslatedFieldsMixin):
def __str__(self): def __str__(self):
return self.name return self.name
@property
def name_dumped(self):
"""Used for indexing as string"""
return dumps(self.name)
@property @property
def image_object(self): def image_object(self):
"""Return image object.""" """Return image object."""

View File

@ -175,6 +175,7 @@ class EstablishmentDocument(Document):
'city': fields.ObjectField( 'city': fields.ObjectField(
properties={ properties={
'id': fields.IntegerField(), 'id': fields.IntegerField(),
'name': fields.KeywordField(attr='name_dumped'),
'name_translated': fields.KeywordField(), 'name_translated': fields.KeywordField(),
'is_island': fields.BooleanField(), 'is_island': fields.BooleanField(),
'country': fields.ObjectField( 'country': fields.ObjectField(

View File

@ -44,6 +44,7 @@ class ProductDocument(Document):
attr='address.city', attr='address.city',
properties={ properties={
'id': fields.IntegerField(), 'id': fields.IntegerField(),
'name': fields.KeywordField(attr='name_dumped'),
'name_translated': fields.KeywordField(), 'name_translated': fields.KeywordField(),
'code': fields.KeywordField(), 'code': fields.KeywordField(),
'country': fields.ObjectField( 'country': fields.ObjectField(

View File

@ -125,7 +125,12 @@ class CityDocumentShortSerializer(serializers.Serializer):
id = serializers.IntegerField() id = serializers.IntegerField()
code = serializers.CharField(allow_null=True) code = serializers.CharField(allow_null=True)
# todo: index and use name dict field # todo: index and use name dict field
name_translated = serializers.CharField() name_translated = serializers.SerializerMethodField()
@staticmethod
def get_name_translated(obj):
get_translated_value(loads(obj.name))
class CountryDocumentSerializer(serializers.Serializer): class CountryDocumentSerializer(serializers.Serializer):