added image url to es

This commit is contained in:
Anatoly 2019-12-20 18:34:33 +03:00
parent f565e188f1
commit cc7125b468
9 changed files with 51 additions and 39 deletions

View File

@ -11,7 +11,7 @@ from utils.models import (
URLImageMixin, URLImageMixin,
) )
from utils.querysets import RelatedObjectsCountMixin from utils.querysets import RelatedObjectsCountMixin
from utils.models import IntermediateGalleryModelMixin, GalleryModelMixin from utils.models import IntermediateGalleryModelMixin, GalleryMixin
# Mixins # Mixins

View File

@ -27,13 +27,13 @@ from main.models import Award, Currency
from review.models import Review from review.models import Review
from tag.models import Tag from tag.models import Tag
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes, GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes, GalleryMixin,
IntermediateGalleryModelMixin, HasTagsMixin, IntermediateGalleryModelMixin, HasTagsMixin,
FavoritesMixin) FavoritesMixin, TypeDefaultImageMixin)
# todo: establishment type&subtypes check # todo: establishment type&subtypes check
class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin): class EstablishmentType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""Establishment type model.""" """Establishment type model."""
STR_FIELD_NAME = 'name' STR_FIELD_NAME = 'name'
@ -73,7 +73,7 @@ class EstablishmentSubTypeManager(models.Manager):
return obj return obj
class EstablishmentSubType(ProjectBaseMixin, TranslatedFieldsMixin): class EstablishmentSubType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""Establishment type model.""" """Establishment type model."""
# EXAMPLE OF INDEX NAME CHOICES # EXAMPLE OF INDEX NAME CHOICES
@ -431,7 +431,7 @@ class EstablishmentQuerySet(models.QuerySet):
) )
class Establishment(GalleryModelMixin, ProjectBaseMixin, URLImageMixin, class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin): TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin):
"""Establishment model.""" """Establishment model."""

View File

@ -12,7 +12,7 @@ from typing import List
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, GalleryModelMixin) IntermediateGalleryModelMixin, GalleryMixin)
class CountryQuerySet(models.QuerySet): class CountryQuerySet(models.QuerySet):
@ -134,7 +134,7 @@ class CityQuerySet(models.QuerySet):
return self.filter(country__code=code) return self.filter(country__code=code)
class City(GalleryModelMixin): class City(GalleryMixin, models.Model):
"""Region model.""" """Region model."""
name = models.CharField(_('name'), max_length=250) name = models.CharField(_('name'), max_length=250)
code = models.CharField(_('code'), max_length=250) code = models.CharField(_('code'), max_length=250)

View File

@ -14,7 +14,7 @@ from rest_framework.reverse import reverse
from main.models import Carousel from main.models import Carousel
from rating.models import Rating, ViewCount from rating.models import Rating, ViewCount
from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, HasTagsMixin, from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, HasTagsMixin,
ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin, ProjectBaseMixin, GalleryMixin, IntermediateGalleryModelMixin,
FavoritesMixin) FavoritesMixin)
from utils.querysets import TranslationQuerysetMixin from utils.querysets import TranslationQuerysetMixin
from datetime import datetime from datetime import datetime
@ -140,7 +140,7 @@ class NewsQuerySet(TranslationQuerysetMixin):
return self.filter(title__icontains=locale) return self.filter(title__icontains=locale)
class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin, class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
FavoritesMixin): FavoritesMixin):
"""News model.""" """News model."""

View File

@ -14,10 +14,11 @@ from location.models import WineOriginAddressMixin
from review.models import Review from review.models import Review
from utils.models import (BaseAttributes, ProjectBaseMixin, HasTagsMixin, from utils.models import (BaseAttributes, ProjectBaseMixin, HasTagsMixin,
TranslatedFieldsMixin, TJSONField, FavoritesMixin, TranslatedFieldsMixin, TJSONField, FavoritesMixin,
GalleryModelMixin, IntermediateGalleryModelMixin) GalleryMixin, IntermediateGalleryModelMixin,
TypeDefaultImageMixin)
class ProductType(TranslatedFieldsMixin, ProjectBaseMixin): class ProductType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""ProductType model.""" """ProductType model."""
STR_FIELD_NAME = 'name' STR_FIELD_NAME = 'name'
@ -49,7 +50,7 @@ class ProductType(TranslatedFieldsMixin, ProjectBaseMixin):
verbose_name_plural = _('Product types') verbose_name_plural = _('Product types')
class ProductSubType(TranslatedFieldsMixin, ProjectBaseMixin): class ProductSubType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""ProductSubtype model.""" """ProductSubtype model."""
STR_FIELD_NAME = 'name' STR_FIELD_NAME = 'name'
@ -203,7 +204,7 @@ class ProductQuerySet(models.QuerySet):
return self.none() return self.none()
class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes, class Product(GalleryMixin, TranslatedFieldsMixin, BaseAttributes,
HasTagsMixin, FavoritesMixin): HasTagsMixin, FavoritesMixin):
"""Product models.""" """Product models."""

View File

@ -5,7 +5,7 @@ from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from utils.models import (BaseAttributes, TranslatedFieldsMixin, from utils.models import (BaseAttributes, TranslatedFieldsMixin,
ProjectBaseMixin, GalleryModelMixin, ProjectBaseMixin, GalleryMixin,
TJSONField, IntermediateGalleryModelMixin) TJSONField, IntermediateGalleryModelMixin)
@ -93,7 +93,7 @@ class Review(BaseAttributes, TranslatedFieldsMixin):
verbose_name_plural = _('Reviews') verbose_name_plural = _('Reviews')
class Inquiries(GalleryModelMixin, ProjectBaseMixin): class Inquiries(GalleryMixin, ProjectBaseMixin):
NONE = 0 NONE = 0
DINER = 1 DINER = 1
LUNCH = 2 LUNCH = 2

View File

@ -23,12 +23,14 @@ class EstablishmentDocument(Document):
'name': fields.ObjectField(attr='name_indexing', 'name': fields.ObjectField(attr='name_indexing',
properties=OBJECT_FIELD_PROPERTIES), properties=OBJECT_FIELD_PROPERTIES),
'index_name': fields.KeywordField(attr='index_name'), 'index_name': fields.KeywordField(attr='index_name'),
'default_image': fields.KeywordField(attr='default_image_url'),
}) })
establishment_subtypes = fields.ObjectField( establishment_subtypes = fields.ObjectField(
properties={ properties={
'id': fields.IntegerField(), 'id': fields.IntegerField(),
'name': fields.ObjectField(attr='name_indexing'), 'name': fields.ObjectField(attr='name_indexing'),
'index_name': fields.KeywordField(attr='index_name'), 'index_name': fields.KeywordField(attr='index_name'),
'default_image': fields.KeywordField(attr='default_image_url'),
}, },
multi=True) multi=True)
works_evening = fields.ListField(fields.IntegerField( works_evening = fields.ListField(fields.IntegerField(

View File

@ -19,6 +19,7 @@ class ProductDocument(Document):
'id': fields.IntegerField(), 'id': fields.IntegerField(),
'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES), 'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES),
'index_name': fields.KeywordField(), 'index_name': fields.KeywordField(),
'default_image': fields.KeywordField(attr='default_image_url'),
}, },
) )
subtypes = fields.ObjectField( subtypes = fields.ObjectField(
@ -26,6 +27,7 @@ class ProductDocument(Document):
'id': fields.IntegerField(), 'id': fields.IntegerField(),
'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES), 'name': fields.ObjectField(attr='name_indexing', properties=OBJECT_FIELD_PROPERTIES),
'index_name': fields.KeywordField(), 'index_name': fields.KeywordField(),
'default_image': fields.KeywordField(attr='default_image_url'),
}, },
multi=True multi=True
) )

View File

@ -364,16 +364,12 @@ class GMTokenGenerator(PasswordResetTokenGenerator):
return self.get_fields(user, timestamp) return self.get_fields(user, timestamp)
class GalleryModelMixin(models.Model): class GalleryMixin:
"""Mixin for models that has gallery.""" """Mixin for models that has gallery."""
class Meta:
"""Meta class."""
abstract = True
@property @property
def crop_gallery(self): def crop_gallery(self):
if hasattr(self, 'gallery'): if hasattr(self, 'gallery') and hasattr(self, '_meta'):
gallery = [] gallery = []
images = self.gallery.all() images = self.gallery.all()
crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES
@ -393,22 +389,23 @@ class GalleryModelMixin(models.Model):
@property @property
def crop_main_image(self): def crop_main_image(self):
if hasattr(self, 'main_image') and self.main_image: if hasattr(self, 'main_image') and hasattr(self, '_meta'):
image = self.main_image if self.main_image:
image_property = { image = self.main_image
'id': image.id, image_property = {
'title': image.title, 'id': image.id,
'original_url': image.image.url, 'title': image.title,
'orientation_display': image.get_orientation_display(), 'original_url': image.image.url,
'auto_crop_images': {}, 'orientation_display': image.get_orientation_display(),
} 'auto_crop_images': {},
crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES }
if p.startswith(self._meta.model_name.lower())] crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES
for crop in crop_parameters: if p.startswith(self._meta.model_name.lower())]
image_property['auto_crop_images'].update( for crop in crop_parameters:
{crop: image.get_image_url(crop)} image_property['auto_crop_images'].update(
) {crop: image.get_image_url(crop)}
return image_property )
return image_property
class IntermediateGalleryModelQuerySet(models.QuerySet): class IntermediateGalleryModelQuerySet(models.QuerySet):
@ -460,3 +457,13 @@ class FavoritesMixin:
timezone.datetime.now().date().isoformat() timezone.datetime.now().date().isoformat()
class TypeDefaultImageMixin:
"""Model mixin for default image."""
@property
def default_image_url(self):
"""Return image url."""
if hasattr(self, 'default_image') and self.default_image:
return self.default_image.image