new establishment attribute

This commit is contained in:
Kuroshini 2019-09-27 16:00:08 +03:00
parent 051ddb617d
commit 9aafe0d69d
2 changed files with 10 additions and 6 deletions

View File

@ -191,6 +191,11 @@ class EstablishmentQuerySet(models.QuerySet):
return self.filter(id__in=subquery_filter_by_distance) \
.order_by('-reviews__published_at')
def the_most_recent_award(self):
""" Returns the most recent award for carousel """
return Award.objects.filter(Q(establishment=self) | Q(employees__establishments=self)).latest(
field_name='vintage_year')
def prefetch_actual_employees(self):
"""Prefetch actual employees."""
return self.prefetch_related(
@ -283,7 +288,7 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
slug = models.SlugField(unique=True, max_length=50, null=True,
verbose_name=_('Establishment slug'), editable=True)
establishment_awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
tags = generic.GenericRelation(to='main.MetaDataContent')
reviews = generic.GenericRelation(to='review.Review')
comments = generic.GenericRelation(to='comment.Comment')
@ -314,11 +319,6 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
country = self.address.city.country
return country.low_price, country.high_price
@property
def awards(self):
return [Award.objects.filter(Q(establishment=self) | Q(employees__establishments=self)).latest(
field_name='vintage_year')]
# todo: make via prefetch
# @property
# def subtypes(self):

View File

@ -141,6 +141,7 @@ class CarouselListSerializer(serializers.ModelSerializer):
image = serializers.URLField(source='image_url')
awards = AwardBaseSerializer(many=True)
vintage_year = serializers.IntegerField()
last_award = serializers.SerializerMethodField()
class Meta:
"""Meta class."""
@ -154,8 +155,11 @@ class CarouselListSerializer(serializers.ModelSerializer):
'public_mark',
'image',
'vintage_year',
'last_award',
]
def get_last_award(self, obj):
return obj.the_most_recent_award()
class PageSerializer(serializers.ModelSerializer):
page_name = serializers.CharField()