Carousel mixin

This commit is contained in:
a.gorbunov 2020-02-07 08:44:13 +00:00
parent 40c730bdd3
commit 0eb6a73e09
4 changed files with 43 additions and 17 deletions

View File

@ -33,7 +33,7 @@ from utils.models import (
BaseAttributes, FavoritesMixin, FileMixin, GalleryMixin, HasTagsMixin,
IntermediateGalleryModelMixin, ProjectBaseMixin, TJSONField, TranslatedFieldsMixin,
TypeDefaultImageMixin, URLImageMixin, default_menu_bool_array, PhoneModelMixin,
AwardsModelMixin)
AwardsModelMixin, CarouselMixin)
# todo: establishment type&subtypes check
@ -547,8 +547,14 @@ class EstablishmentQuerySet(models.QuerySet):
return self.prefetch_related('menu_set', 'menu_set__plates', 'back_office_wine')
class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
TranslatedFieldsMixin, HasTagsMixin, FavoritesMixin, AwardsModelMixin):
class Establishment(GalleryMixin,
ProjectBaseMixin,
URLImageMixin,
TranslatedFieldsMixin,
HasTagsMixin,
FavoritesMixin,
AwardsModelMixin,
CarouselMixin):
"""Establishment model."""
ABANDONED = 0
@ -717,9 +723,13 @@ class Establishment(GalleryMixin, ProjectBaseMixin, URLImageMixin,
raise ValidationError('Establishment type of subtype does not match')
self.establishment_subtypes.add(establishment_subtype)
@property
def last_review(self):
return self.reviews.by_status(Review.READY).last()
@property
def vintage_year(self):
last_review = self.reviews.by_status(Review.READY).last()
last_review = self.last_review
if last_review:
return last_review.vintage

View File

@ -20,6 +20,7 @@ from location.serializers import AddressDetailSerializer, TranslatedField, Addre
from main import models as main_models
from main.models import Currency
from main.serializers import AwardSerializer
from review.serializers import ReviewBaseSerializer
from tag.serializers import TagBaseSerializer
from utils.decorators import with_base_attributes
from utils.methods import string_random
@ -211,6 +212,7 @@ class EstablishmentRUDSerializer(model_serializers.EstablishmentBaseSerializer):
subtypes = model_serializers.EstablishmentSubTypeBaseSerializer(source='establishment_subtypes',
read_only=True, many=True)
type = model_serializers.EstablishmentTypeBaseSerializer(source='establishment_type', read_only=True)
phones = serializers.ListField(
source='contact_phones',
allow_null=True,
@ -219,8 +221,11 @@ class EstablishmentRUDSerializer(model_serializers.EstablishmentBaseSerializer):
required=False,
write_only=True,
)
contact_phones = ContactPhonesSerializer(source='phones', read_only=True, many=True)
last_review = ReviewBaseSerializer()
class Meta(model_serializers.EstablishmentBaseSerializer.Meta):
fields = [
'id',
@ -249,6 +254,7 @@ class EstablishmentRUDSerializer(model_serializers.EstablishmentBaseSerializer):
'tags',
'status',
'status_display',
'last_review',
]
def to_representation(self, instance):

View File

@ -22,7 +22,7 @@ from utils.models import (
BaseAttributes, FavoritesMixin, GalleryMixin, HasTagsMixin, IntermediateGalleryModelMixin,
ProjectBaseMixin,
TJSONField, TranslatedFieldsMixin, TypeDefaultImageMixin,
)
CarouselMixin)
from utils.querysets import TranslationQuerysetMixin
@ -257,8 +257,12 @@ class NewsQuerySet(TranslationQuerysetMixin):
return self.filter(site__country__code=country_code) if not user.is_superuser else self
class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
FavoritesMixin):
class News(GalleryMixin,
BaseAttributes,
TranslatedFieldsMixin,
HasTagsMixin,
FavoritesMixin,
CarouselMixin):
"""News model."""
STR_FIELD_NAME = 'title'
@ -366,16 +370,6 @@ class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
self.duplication_date = timezone.now()
self.save()
@property
def must_of_the_week(self) -> bool:
"""Detects whether current item in carousel"""
kwargs = {
'content_type': ContentType.objects.get_for_model(self),
'object_id': self.pk,
'country': self.country,
}
return Carousel.objects.filter(**kwargs).exists()
@property
def publication_datetime(self):
"""Represents datetime object combined from `publication_date` & `publication_time` fields"""

View File

@ -4,6 +4,7 @@ from os.path import exists
from django.conf import settings
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db import models
from django.contrib.postgres.aggregates import ArrayAgg
from django.contrib.postgres.fields import JSONField
@ -537,3 +538,18 @@ class AwardsModelMixin:
if hasattr(self, 'awards'):
self.awards.remove(award)
class CarouselMixin:
@property
def must_of_the_week(self) -> bool:
"""Detects whether current item in carousel"""
from main.models import Carousel
if hasattr(self, 'pk') and hasattr(self, 'country'):
kwargs = {
'content_type': ContentType.objects.get_for_model(self),
'object_id': self.pk,
'country': self.country,
}
return Carousel.objects.filter(**kwargs).exists()