mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-13 21:33:46 +00:00
create view and logic to serve 2P and 3P data
This commit is contained in:
1 parent
0be586a222
commit
08e60d6838
1 file changed
+43
-12
+43
-12
@@ -4,6 +4,9 @@ from django.core import serializers
|
|||||||
from .models import PlayerData
|
from .models import PlayerData
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from .serializers import PlayerDataSerializer
|
from .serializers import PlayerDataSerializer
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from django.db.models import Avg
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@@ -13,40 +16,68 @@ class PlayerDataList(generics.ListCreateAPIView):
|
|||||||
queryset = PlayerData.objects.all().order_by('id')
|
queryset = PlayerData.objects.all().order_by('id')
|
||||||
serializer_class = PlayerDataSerializer
|
serializer_class = PlayerDataSerializer
|
||||||
# pagination_class = None
|
# pagination_class = None
|
||||||
|
|
||||||
|
|
||||||
# Fetch player data by team
|
# Fetch player data by team
|
||||||
class PlayerDataByTeamList(generics.ListAPIView):
|
class PlayerDataByTeamList(generics.ListAPIView):
|
||||||
serializer_class = PlayerDataSerializer
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
team = self.kwargs['team']
|
team = self.kwargs['team']
|
||||||
return PlayerData.objects.filter(team=team)
|
return PlayerData.objects.filter(team=team)
|
||||||
|
|
||||||
|
|
||||||
# Fetch player data by season
|
# Fetch player data by season
|
||||||
class PlayerDataBySeasonList(generics.ListAPIView):
|
class PlayerDataBySeasonList(generics.ListAPIView):
|
||||||
serializer_class = PlayerDataSerializer
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
season = self.kwargs['season']
|
season = self.kwargs['season']
|
||||||
return PlayerData.objects.filter(season=season)
|
return PlayerData.objects.filter(season=season)
|
||||||
|
|
||||||
|
|
||||||
# Fetch player data by player name
|
# Fetch player data by player name
|
||||||
class PlayerDataByNameList(generics.ListAPIView):
|
class PlayerDataByNameList(generics.ListAPIView):
|
||||||
serializer_class = PlayerDataSerializer
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
name = self.kwargs['name']
|
name = self.kwargs['name']
|
||||||
return PlayerData.objects.filter(name__icontains=name)
|
return PlayerData.objects.filter(name__icontains=name)
|
||||||
|
|
||||||
|
|
||||||
# Fetch Top 20 players by PTS DESC for the season specified.
|
# Fetch Top 20 players by PTS DESC for the season specified.
|
||||||
class TopScorersbySeasonList(generics.ListAPIView):
|
class TopScorersbySeasonList(generics.ListAPIView):
|
||||||
serializer_class = PlayerDataSerializer
|
serializer_class = PlayerDataSerializer
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
season = self.kwargs['season']
|
season = self.kwargs['season']
|
||||||
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
return PlayerData.objects.filter(season=season).order_by('-PTS')[:20]
|
||||||
|
|
||||||
|
|
||||||
|
# Fetch and calculate the average 3P made, 3P attemps, 2P made, 2P attempts for all players in each season.
|
||||||
|
# Will use APIview here instead of generics.ListAPIView to avoid having to create a serializer to generate the queryset. avg_three_made and avg_two_made are new data points at the time of creation when compared to the per_game model.
|
||||||
|
class ThreeTopPointTrends(APIView):
|
||||||
|
def get(self, request, format=None):
|
||||||
|
seasons = PlayerData.objects.values_list(
|
||||||
|
'season', flat=True).distinct().order_by('season')
|
||||||
|
data = []
|
||||||
|
|
||||||
|
for season in seasons:
|
||||||
|
season_data = PlayerData.objects.filter(season=season)
|
||||||
|
avg_three_made = season_data.aggregate(Avg('three_fg'))[
|
||||||
|
'three_fg_avg']
|
||||||
|
avg_three_attempts = season_data.aggregate(Avg('three_attempts'))[
|
||||||
|
'three_attempts_avg']
|
||||||
|
avg_two_made = season_data.aggregate(Avg('two_fg'))['two_fg_avg']
|
||||||
|
avg_two_attempts = season_data.aggregate(Avg('two_attempts'))[
|
||||||
|
'two_attempts_avg']
|
||||||
|
|
||||||
|
data.append({
|
||||||
|
'season': season,
|
||||||
|
'avg_three_made': avg_three_made,
|
||||||
|
'avg_three_attempts': avg_three_attempts,
|
||||||
|
'avg_two_made': avg_two_made,
|
||||||
|
'avg_two_attempts': avg_two_attempts,
|
||||||
|
})
|
||||||
|
|
||||||
|
return Response(data)
|
||||||
Reference in new issue
Block a user