This repository has been archived by the owner on May 9, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding api code for games in courtside
- Loading branch information
Showing
4 changed files
with
48 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,65 +1,28 @@ | |||
from django.contrib.auth.models import User | from .models import Game | ||
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS | from .serializers import GameSerializer | ||
from tastypie import fields | |||
|
|
||
from game.models import Game | from django.http import Http404 | ||
from register.models import Sport, Player | |||
|
|
||
from rest_framework.views import APIView | |||
from rest_framework.response import Response | |||
|
|
||
class SportResource(ModelResource): | class GameList(APIView): | ||
class Meta: | |||
queryset = Sport.objects.all() | |||
resource_name = 'sport' | |||
allowed_methods = ['get'] | |||
include_resource_uri = False | |||
excludes = ['id'] | |||
filtering = { | |||
'sport': ('exact') | |||
} | |||
|
|
||
def get(self, request, format=None): | |||
games = Game.objects.filter(active=True) | |||
serialized_games = GameSerializer(games, many=True) | |||
return Response(serialized_games.data) | |||
|
|
||
class UserResource(ModelResource): | class GameDetail(APIView): | ||
class Meta: | |||
queryset = User.objects.all() | |||
resource_name = 'user' | |||
excludes = ['password', 'is_active', 'is_staff', 'is_superuser', 'id'] | |||
allowed_methods = ['get'] | |||
include_resource_uri = False | |||
allowed_methods = ['get'] | |||
filtering = { | |||
'username': ALL, | |||
'email': ALL | |||
} | |||
|
|
||
def get_object(self, pk): | |||
try: | |||
return Game.objects.get(pk=pk) | |||
except Game.DoesNotExist: | |||
raise Http404 | |||
|
|
||
class PlayerResource(ModelResource): | |||
user = fields.ForeignKey(UserResource, 'user', full=True) | |||
sports = fields.ManyToManyField(SportResource, 'sports', full=True) | |||
class Meta: | |||
include_resource_uri = False | |||
queryset = Player.objects.all() | |||
resource_name = 'player' | |||
fields = ['sports', 'image_url', 'gender', 'user'] | |||
allowed_methods = ['get'] | |||
filtering = { | |||
'user': ALL_WITH_RELATIONS, | |||
'gender' : ALL, | |||
'sports' : ALL_WITH_RELATIONS, | |||
} | |||
|
|
||
|
def get(self, request, pk, format=None): | ||
class GameResource(ModelResource): | game = self.get_object(pk) | ||
sport = fields.ForeignKey(SportResource, 'sport', full=True) | serialized_game = GameSerializer(game) | ||
owner = fields.ForeignKey(UserResource, 'owner', full=True) | return Response(serialized_game.data) | ||
players = fields.ManyToManyField(PlayerResource, 'players', full=True) | |||
class Meta: | |||
resource_name = 'game' | |||
queryset = Game.objects.all() | |||
include_resource_uri = False | |||
allowed_methods = ['get'] | |||
filtering = { | |||
'game': ALL_WITH_RELATIONS, | |||
'owner': ALL_WITH_RELATIONS, | |||
'players' : ALL_WITH_RELATIONS, | |||
'sport' : ALL_WITH_RELATIONS, | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
from .models import Game | |||
|
|||
from rest_framework import serializers | |||
|
|||
class GameSerializer(serializers.ModelSerializer): | |||
class Meta: | |||
model = Game | |||
fields = ( | |||
'id', | |||
'owner', | |||
'sport', | |||
'start_date_and_time', | |||
'active', | |||
'restrictions' | |||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters