latest award && test fix
This commit is contained in:
parent
4388fb7346
commit
98b0a9af5d
|
|
@ -16,6 +16,8 @@ from phonenumber_field.modelfields import PhoneNumberField
|
|||
from collection.models import Collection
|
||||
from main.models import MetaDataContent
|
||||
from location.models import Address
|
||||
from collection.models import Collection
|
||||
from main.models import Award
|
||||
from review.models import Review
|
||||
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
|
||||
TranslatedFieldsMixin, BaseAttributes)
|
||||
|
|
@ -281,7 +283,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')
|
||||
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,16 +316,8 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
|
|||
|
||||
@property
|
||||
def awards(self):
|
||||
latest_employee_awards = Employee.objects.filter(
|
||||
establishments=self
|
||||
).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]]
|
||||
return [Award.objects.filter(Q(establishment=self) | Q(employees__establishments=self)).latest(
|
||||
field_name='vintage_year')]
|
||||
|
||||
# todo: make via prefetch
|
||||
# @property
|
||||
|
|
@ -422,8 +416,8 @@ class Employee(BaseAttributes):
|
|||
verbose_name=_('User'))
|
||||
name = models.CharField(max_length=255, verbose_name=_('Last name'))
|
||||
establishments = models.ManyToManyField(Establishment, related_name='employees',
|
||||
through=EstablishmentEmployee)
|
||||
awards = generic.GenericRelation(to='main.Award')
|
||||
through=EstablishmentEmployee,)
|
||||
awards = generic.GenericRelation(to='main.Award', related_query_name='employees')
|
||||
tags = generic.GenericRelation(to='main.MetaDataContent')
|
||||
|
||||
class Meta:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user