Skip to content

Commit

Permalink
Aggregates
Browse files Browse the repository at this point in the history
team: performance average
team: rank candidates
performance: average total score
  • Loading branch information
sam-mi committed Jan 10, 2019
1 parent c9f9297 commit 33b78eb
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions the_voice/performances/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import get_user_model
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db.models import QuerySet, DateTimeField, IntegerField
from django.db.models import QuerySet, DateTimeField, IntegerField, Avg
from django_common.models.abstract import AbstractTimeStampedStatusModel, AbstractTimestampedModel
from model_utils import Choices

Expand Down Expand Up @@ -42,10 +42,21 @@ def rank_candidates(self) -> QuerySet:
:return:
"""
candidates = self.candidates.filter(
user_type=USER_TYPE_CHOICES.candidate
).annotate(avg_score=Avg('performances__scores__score')).order_by('avg_score')
return candidates

# class Meta:


@property
def performance_average(self) -> float:
"""
Return a running average of scores
:return:
"""
average = Performance.objects.filter(
performer__in=self.candidates.all()
).aggregate(average_score=Avg('scores__score'))
return average['average_score']


class Song(AbstractTimestampedModel):
Expand Down Expand Up @@ -144,6 +155,15 @@ def is_completed(self):
return True
return False

def total_score(self):
"""
The average score from all of the cores by mentors
:return:
"""
total = self.scores.aggregate(total=Avg('score'))
return total['total']


def __str__(self):
return f'{self.song} by {self.performer.get_full_name()}'

Expand Down

0 comments on commit 33b78eb

Please sign in to comment.