mirror of
https://github.com/nprasad2077/nbaStats.git
synced 2026-11-04 09:15:35 +00:00
177 lines
5.9 KiB
Python
177 lines
5.9 KiB
Python
from django.shortcuts import render
|
|
from django.http import JsonResponse
|
|
from django.core import serializers
|
|
from .models import PlayerData
|
|
from rest_framework import generics
|
|
from .serializers import PlayerDataSerializer, HistogramDataSerializer
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from django.db.models import Avg
|
|
from collections import Counter
|
|
import math
|
|
|
|
|
|
# Create your views here.
|
|
|
|
# API view to list all PlayerData instances
|
|
class PlayerDataList(generics.ListCreateAPIView):
|
|
queryset = PlayerData.objects.all().order_by('id')
|
|
serializer_class = PlayerDataSerializer
|
|
# pagination_class = None
|
|
|
|
|
|
# Fetch player data by team
|
|
class PlayerDataByTeamList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
team = self.kwargs['team']
|
|
return PlayerData.objects.filter(team=team)
|
|
|
|
|
|
# Fetch player data by season
|
|
class PlayerDataBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season)
|
|
|
|
|
|
# Fetch player data by player name
|
|
class PlayerDataByNameList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
name = self.kwargs['name']
|
|
return PlayerData.objects.filter(name__icontains=name)
|
|
|
|
|
|
# Fetch Top 20 players by PTS DESC for the season specified.
|
|
class TopScorersbySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
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 ThreeTwoTopPointTrends(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)
|
|
|
|
# Fetch Top 20 players by AST DESC for season specified.
|
|
|
|
|
|
class TopAssistsBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-AST')[:20]
|
|
|
|
# Fetch Top 20 players by TRB DESC for season specified.
|
|
|
|
|
|
class TopReboundsBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-TRB')[:20]
|
|
|
|
# Fetch Top 20 players by BLK DESC for specified season.
|
|
|
|
|
|
class TopBlocksBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-BLK')[:20]
|
|
|
|
# Fetch Top 20 players by STL DESC for season specified.
|
|
|
|
|
|
class TopStealsBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-STL')[:20]
|
|
|
|
# Fetch Top 20 players by ORB DESC for season.
|
|
|
|
|
|
class TopOffensiveReboundsBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-ORB')[:20]
|
|
|
|
|
|
# Fetch Top 20 players by DRB DESC for season.
|
|
|
|
class TopDefensiveReboundsBySeasonList(generics.ListAPIView):
|
|
serializer_class = PlayerDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
return PlayerData.objects.filter(season=season).order_by('-DRB')[:20]
|
|
|
|
# PTS Histogram
|
|
|
|
|
|
class PointsPerGameHistogramBySeasonList(generics.ListAPIView):
|
|
serializer_class = HistogramDataSerializer
|
|
|
|
def get_queryset(self):
|
|
season = self.kwargs['season']
|
|
queryset = PlayerData.objects.filter(season=season)
|
|
|
|
# Get the points per game for each player
|
|
points_per_game_list = [float(player.PTS) for player in queryset]
|
|
|
|
# Define histogram bins (e.g., 0-5, 5-10, 10-15, etc.)
|
|
bin_ranges = range(0, int(math.ceil(max(points_per_game_list))) + 1, 5)
|
|
histogram_data = Counter()
|
|
|
|
# Fill the histogram with data
|
|
for ppg in points_per_game_list:
|
|
bin_index = next(
|
|
(i for i, r in enumerate(bin_ranges) if r >= ppg), -1)
|
|
if bin_index >= 1:
|
|
bin_label = f"{bin_ranges[bin_index - 1]}-{bin_ranges[bin_index]}"
|
|
histogram_data[bin_label] += 1
|
|
|
|
# Convert histogram_data to list of dicts for serialization
|
|
histogram_list = [{"range": k, "count": v}
|
|
for k, v in histogram_data.items()]
|
|
|
|
return histogram_list
|