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,
)
from utils.querysets import RelatedObjectsCountMixin
from utils.models import IntermediateGalleryModelMixin, GalleryModelMixin
from utils.models import IntermediateGalleryModelMixin, GalleryMixin
# Mixins

View File

@ -27,13 +27,13 @@ from main.models import Award, Currency
from review.models import Review
from tag.models import Tag
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes, GalleryModelMixin,
TranslatedFieldsMixin, BaseAttributes, GalleryMixin,
IntermediateGalleryModelMixin, HasTagsMixin,
FavoritesMixin)
FavoritesMixin, TypeDefaultImageMixin)
# todo: establishment type&subtypes check
class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin):
class EstablishmentType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""Establishment type model."""
STR_FIELD_NAME = 'name'
@ -73,7 +73,7 @@ class EstablishmentSubTypeManager(models.Manager):
return obj
class EstablishmentSubType(ProjectBaseMixin, TranslatedFieldsMixin):
class EstablishmentSubType(TypeDefaultImageMixin, TranslatedFieldsMixin, ProjectBaseMixin):
"""Establishment type model."""
# 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):
"""Establishment model."""

View File

@ -12,7 +12,7 @@ from typing import List
from translation.models import Language
from utils.models import (ProjectBaseMixin, SVGImageMixin, TJSONField,
TranslatedFieldsMixin, get_current_locale,
IntermediateGalleryModelMixin, GalleryModelMixin)
IntermediateGalleryModelMixin, GalleryMixin)
class CountryQuerySet(models.QuerySet):
@ -134,7 +134,7 @@ class CityQuerySet(models.QuerySet):
return self.filter(country__code=code)
class City(GalleryModelMixin):
class City(GalleryMixin, models.Model):
"""Region model."""
name = models.CharField(_('name'), 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 rating.models import Rating, ViewCount
from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, HasTagsMixin,
ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin,
ProjectBaseMixin, GalleryMixin, IntermediateGalleryModelMixin,
FavoritesMixin)
from utils.querysets import TranslationQuerysetMixin
from datetime import datetime
@ -140,7 +140,7 @@ class NewsQuerySet(TranslationQuerysetMixin):
return self.filter(title__icontains=locale)
class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
FavoritesMixin):
"""News model."""

View File

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

View File

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

View File

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

View File

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

View File

@ -364,16 +364,12 @@ class GMTokenGenerator(PasswordResetTokenGenerator):
return self.get_fields(user, timestamp)
class GalleryModelMixin(models.Model):
class GalleryMixin:
"""Mixin for models that has gallery."""
class Meta:
"""Meta class."""
abstract = True
@property
def crop_gallery(self):
if hasattr(self, 'gallery'):
if hasattr(self, 'gallery') and hasattr(self, '_meta'):
gallery = []
images = self.gallery.all()
crop_parameters = [p for p in settings.SORL_THUMBNAIL_ALIASES
@ -393,22 +389,23 @@ class GalleryModelMixin(models.Model):
@property
def crop_main_image(self):
if hasattr(self, 'main_image') and self.main_image:
image = self.main_image
image_property = {
'id': image.id,
'title': image.title,
'original_url': image.image.url,
'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())]
for crop in crop_parameters:
image_property['auto_crop_images'].update(
{crop: image.get_image_url(crop)}
)
return image_property
if hasattr(self, 'main_image') and hasattr(self, '_meta'):
if self.main_image:
image = self.main_image
image_property = {
'id': image.id,
'title': image.title,
'original_url': image.image.url,
'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())]
for crop in crop_parameters:
image_property['auto_crop_images'].update(
{crop: image.get_image_url(crop)}
)
return image_property
class IntermediateGalleryModelQuerySet(models.QuerySet):
@ -459,4 +456,14 @@ class FavoritesMixin:
return self.favorites.aggregate(arr=ArrayAgg('user_id')).get('arr')
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