def get_score_total(self, obj): # return int(Throw.objects.filter(player=obj).annotate(score = F('score_first') + F('score_second')+ F('score_third')+ F('score_fourth')).aggregate(Sum('score'))['score__sum']) try: key = 'player_' + str(obj.id) + '_score_total' score_total = getFromCache(key) if score_total is None: score_total = Throw.objects.filter( player=obj, match__is_validated=True).annotate(score=Case( When(score_first__gt=0, then='score_first'), default=Value('0'), output_field=IntegerField(), ) + Case( When(score_second__gt=0, then='score_second'), default=Value('0'), output_field=IntegerField(), ) + Case( When(score_third__gt=0, then='score_third'), default=Value('0'), output_field=IntegerField(), ) + Case( When(score_fourth__gt=0, then='score_fourth'), default=Value('0'), output_field=IntegerField(), )).aggregate(Sum('score'))['score__sum'] if score_total is None: score_total = 0 setToCache(key, score_total) # return int(Throw.objects.filter(season=self.context.get('season'), player=obj, score__gt=0).aggregate(Sum('score'))['score__sum']) except TypeError: setToCache(key, 0) return 0 self.score_total = score_total return score_total
def get_gteSix_total(self, obj): key = 'player_' + str(obj.id) + '_gteSix_total' gteSix_total = getFromCache(key, self.context.get('season').year) if gteSix_total is None: gteSix_total = Throw.objects.filter( match__is_validated=True, season=self.context.get('season'), player=obj).annotate( count=Count('pk', filter=Q(score_first="6") | Q(score_first="7") | Q(score_first="8") | Q(score_first="9")) + Count('pk', filter=Q(score_second="6") | Q(score_second="7") | Q(score_second="8") | Q(score_second="9")) + Count('pk', filter=Q(score_third="6") | Q(score_third="7") | Q(score_third="8") | Q(score_third="9")) + Count('pk', filter=Q(score_fourth="6") | Q(score_fourth="7") | Q(score_fourth="8") | Q(score_fourth="9"))).aggregate( Sum('count'))['count__sum'] if gteSix_total is None: gteSix_total = 0 setToCache(key, gteSix_total, season_year=self.context.get('season').year) return gteSix_total
def get_throws_total(self, obj): key = 'player_' + str(obj.id) + '_throws_total' throws_total = getFromCache(key, self.context.get('season').year) if throws_total is None: # throws_total = Throw.objects.filter(match__is_validated=True, season=self.context.get('season'), player=obj).count() * 4 throws_total = Throw.objects.filter( match__is_validated=True, season=self.context.get('season'), player=obj).annotate(count=Case( When(score_first="e", then=Value(0)), default=Value(1), output_field=IntegerField(), ) + Case( When(score_second="e", then=Value(0)), default=Value(1), output_field=IntegerField(), ) + Case( When(score_third="e", then=Value(0)), default=Value(1), output_field=IntegerField(), ) + Case( When(score_fourth="e", then=Value(0)), default=Value(1), output_field=IntegerField(), )).aggregate(Sum('count'))['count__sum'] if throws_total is None: throws_total = 0 setToCache(key, throws_total, season_year=self.context.get('season').year) self.throws = throws_total return self.throws
def get_away_team(self, obj): key = 'team_' + str(obj.away_team.id) team = getFromCache(key) if team is None: team = TeamSerializer(obj.away_team).data setToCache(key, team, 3600) return team
def get_rounds_total(self, obj): key = 'player_' + str(obj.id) + '_rounds_total' rounds_total = getFromCache(key) if rounds_total is None: rounds_total = obj.throw_set.filter( match__is_validated=True).count() setToCache(key, rounds_total) return rounds_total
def get_score_per_throw(self, obj): key = 'player_' + str(obj.id) + '_score_per_throw' score_per_throw = getFromCache(key) if score_per_throw is None: try: score_per_throw = round(self.score_total / self.throws, 2) except (ZeroDivisionError, TypeError): score_per_throw = 0 setToCache(key, score_per_throw) return score_per_throw
def get_match_count(self, obj): key = 'player_' + str(obj.id) + '_match_count' match_count = getFromCache(key) if match_count is None: match_count = Match.objects.filter( season=self.context.get('season'), throw__player=obj).distinct().count() setToCache(key, match_count) return match_count
def get_away_score_total(self, obj): key = 'match_' + str(obj.id) + '_away_score_total' away_score_total = getFromCache(key) if away_score_total is None: try: away_score_total = obj.away_first_round_score + obj.away_second_round_score except TypeError: away_score_total = None setToCache(key, away_score_total) return away_score_total
def get_home_score_total(self, obj): key = 'match_' + str(obj.id) + '_home_score_total' home_score_total = getFromCache(key) if home_score_total is None: try: home_score_total = obj.home_first_round_score + obj.home_second_round_score except TypeError: home_score_total = None setToCache(key, home_score_total) return home_score_total
def list(self, request, format=None): season = getSeason(request) key = 'all_players' all_players = getFromCache(key) if all_players is None: self.queryset = self.queryset.filter(playersinteam__season=season) serializer = PlayerListSerializer(self.queryset, many=True, context={'season': season}) all_players = serializer.data setToCache(key, all_players) return Response(all_players)
def get_away_score_total(self, obj): key = 'match_' + str(obj.id) + '_away_score_total' away_score_total = getFromCache(key, self.context.get('season').year) if away_score_total is None: try: away_score_total = obj.away_first_round_score + obj.away_second_round_score except TypeError: away_score_total = None setToCache(key, away_score_total, season_year=self.context.get('season').year) return away_score_total
def get_rounds_total(self, obj): key = 'player_' + str(obj.id) + '_rounds_total' rounds_total = getFromCache(key, self.context.get('season').year) if rounds_total is None: rounds_total = obj.throw_set.filter( season=self.context.get('season'), match__is_validated=True).count() setToCache(key, rounds_total, season_year=self.context.get('season').year) return rounds_total
def get(self, request): season = getSeason(request) self.queryset = self.queryset.filter(season=season) key = 'all_matches' all_matches = getFromCache(key) if all_matches is None: serializer = MatchListSerializer(self.queryset, many=True, context={'season': season}) all_matches = serializer.data setToCache(key, all_matches) return Response(all_matches)
def get_throws_total(self, obj): key = 'player_' + str(obj.id) + '_throws_total' throws_total = getFromCache(key) if throws_total is None: throws_total = Throw.objects.filter( match__is_validated=True, season=self.context.get('season'), player=obj).count() * 4 setToCache(key, throws_total) self.throws = throws_total return self.throws
def list(self, request): season = getSeason(request) key = 'all_teams' all_teams = getFromCache(key) if all_teams is None: self.queryset = self.queryset.filter( playersinteam__season=season).distinct() serializer = TeamListSerializer(self.queryset, many=True, context={'season': season}) all_teams = serializer.data setToCache(key, all_teams) return Response(all_teams)
def get_pike_percentage(self, obj): key = 'player_' + str(obj.id) + '_pike_percentage' pike_percentage = getFromCache(key) if pike_percentage is None: pike_count = self.pikes total_count = self.throws try: pike_percentage = round((pike_count / total_count) * 100, 2) except (ZeroDivisionError, TypeError): pike_percentage = 0 setToCache(key, pike_percentage) if pike_percentage is None: return 0 return pike_percentage
def get_pike_percentage(self, obj): key = 'player_' + str(obj.id) + '_pike_percentage' pike_percentage = getFromCache(key, self.context.get('season').year) if pike_percentage is None: pike_count = self.pikes total_count = self.throws try: pike_percentage = round((pike_count / total_count) * 100, 2) except (ZeroDivisionError, TypeError): pike_percentage = 0 if pike_percentage is None: pike_percentage = 0 setToCache(key, pike_percentage, season_year=self.context.get('season').year) return pike_percentage
def get_zeros_total(self, obj): key = 'player_' + str(obj.id) + '_zeros_total' zeros_total = getFromCache(key) if zeros_total is None: zeros_total = Throw.objects.filter( match__is_validated=True, season=self.context.get('season'), player=obj).annotate( count=Count('pk', filter=Q(score_first=0)) + Count('pk', filter=Q(score_second=0)) + Count('pk', filter=Q(score_third=0)) + Count('pk', filter=Q(score_fourth=0))).aggregate( Sum('count'))['count__sum'] setToCache(key, zeros_total) # zeros = Throw.objects.filter(season=self.context.get('season'), player=obj).aggregate(first=Count('pk',filter=Q(score_first=0)),second=Count('pk',filter=Q(score_second=0)),third=Count('pk',filter=Q(score_third=0)),fourth=Count('pk',filter=Q(score_fourth=0))) # self.zeros = zeros['first'] + zeros['second'] + zeros['third'] + zeros['fourth'] self.zeros = zeros_total return self.zeros
def get_gteSix_total(self, obj): key = 'player_' + str(obj.id) + '_gteSix_total' gteSix_total = getFromCache(key) if gteSix_total is None: gteSix_total = Throw.objects.exclude( Q(score_first='h') | Q(score_second='h') | Q(score_third='h') | Q(score_fourth='h')).filter( match__is_validated=True, season=self.context.get('season'), player=obj).annotate( count=Count('pk', filter=Q(score_first__gte=6)) + Count('pk', filter=Q(score_second__gte=6)) + Count('pk', filter=Q(score_third__gte=6)) + Count('pk', filter=Q(score_fourth__gte=6))).aggregate( Sum('count'))['count__sum'] if gteSix_total is None: setToCache(key, 0) else: setToCache(key, gteSix_total) return gteSix_total
def get_pikes_total(self, obj): key = 'player_' + str(obj.id) + '_pikes_total' pikes_total = getFromCache(key, self.context.get('season').year) if pikes_total is None: pikes_total = Throw.objects.filter( match__is_validated=True, season=self.context.get('season'), player=obj).annotate( count=Count('pk', filter=Q(score_first='h')) + Count('pk', filter=Q(score_second='h')) + Count('pk', filter=Q(score_third='h')) + Count('pk', filter=Q(score_fourth='h'))).aggregate( Sum('count'))['count__sum'] if pikes_total is None: pikes_total = 0 setToCache(key, pikes_total, season_year=self.context.get('season').year) # pikes = Throw.objects.filter(season=self.context.get('season'), player=obj).aggregate(first=Count('pk',filter=Q(score_first=-1)),second=Count('pk',filter=Q(score_second=-1)),third=Count('pk',filter=Q(score_third=-1)),fourth=Count('pk',filter=Q(score_fourth=-1))) # self.pikes = pikes['first'] + pikes['second'] + pikes['third'] + pikes['fourth'] self.pikes = pikes_total return self.pikes