latest award && test fix

This commit is contained in:
Kuroshini 2019-09-26 01:26:52 +03:00
parent 4388fb7346
commit 98b0a9af5d

View File

@ -16,6 +16,8 @@ from phonenumber_field.modelfields import PhoneNumberField
from collection.models import Collection from collection.models import Collection
from main.models import MetaDataContent from main.models import MetaDataContent
from location.models import Address from location.models import Address
from collection.models import Collection
from main.models import Award
from review.models import Review from review.models import Review
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes) TranslatedFieldsMixin, BaseAttributes)
@ -281,7 +283,7 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
slug = models.SlugField(unique=True, max_length=50, null=True, slug = models.SlugField(unique=True, max_length=50, null=True,
verbose_name=_('Establishment slug'), editable=True) verbose_name=_('Establishment slug'), editable=True)
establishment_awards = generic.GenericRelation(to='main.Award') establishment_awards = generic.GenericRelation(to='main.Award', related_query_name='establishment')
tags = generic.GenericRelation(to='main.MetaDataContent') tags = generic.GenericRelation(to='main.MetaDataContent')
reviews = generic.GenericRelation(to='review.Review') reviews = generic.GenericRelation(to='review.Review')
comments = generic.GenericRelation(to='comment.Comment') comments = generic.GenericRelation(to='comment.Comment')
@ -314,16 +316,8 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
@property @property
def awards(self): def awards(self):
latest_employee_awards = Employee.objects.filter( return [Award.objects.filter(Q(establishment=self) | Q(employees__establishments=self)).latest(
establishments=self field_name='vintage_year')]
).annotate(
latest_employee_award=Max('awards__vintage_year')
).filter(
awards__vintage_year=F('awards__vintage_year')
).get().awards
latest_establishment_award = self.establishment_awards.latest(field_name='vintage_year').get()
awards = [award for award in latest_employee_awards + [latest_establishment_award] if award is not None]
return [awards.sort(key=lambda award: award.vintage_year)[0]]
# todo: make via prefetch # todo: make via prefetch
# @property # @property
@ -422,8 +416,8 @@ class Employee(BaseAttributes):
verbose_name=_('User')) verbose_name=_('User'))
name = models.CharField(max_length=255, verbose_name=_('Last name')) name = models.CharField(max_length=255, verbose_name=_('Last name'))
establishments = models.ManyToManyField(Establishment, related_name='employees', establishments = models.ManyToManyField(Establishment, related_name='employees',
through=EstablishmentEmployee) through=EstablishmentEmployee,)
awards = generic.GenericRelation(to='main.Award') awards = generic.GenericRelation(to='main.Award', related_query_name='employees')
tags = generic.GenericRelation(to='main.MetaDataContent') tags = generic.GenericRelation(to='main.MetaDataContent')
class Meta: class Meta: