Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
adding api code for games in courtside
Browse files Browse the repository at this point in the history
  • Loading branch information
myusuf3 committed Sep 28, 2013
1 parent 7cb81d9 commit c949d8c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 58 deletions.
77 changes: 20 additions & 57 deletions courtside/game/api.py
@@ -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,
}
15 changes: 15 additions & 0 deletions courtside/game/serializers.py
@@ -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'
)
3 changes: 2 additions & 1 deletion courtside/settings.py
Expand Up @@ -152,7 +152,8 @@
'django.contrib.comments', 'django.contrib.comments',
'debug_toolbar', 'debug_toolbar',
'south', 'south',
'djcelery' 'djcelery',
'rest_framework',
) )


# A sample logging configuration. The only tangible logging # A sample logging configuration. The only tangible logging
Expand Down
11 changes: 11 additions & 0 deletions courtside/urls.py
Expand Up @@ -2,6 +2,11 @@
from django.contrib import admin from django.contrib import admin
admin.autodiscover() admin.autodiscover()


from rest_framework.urlpatterns import format_suffix_patterns
from game import api



urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^about/', 'game.views.about', name='about'), url(r'^about/', 'game.views.about', name='about'),
Expand Down Expand Up @@ -42,4 +47,10 @@


# comments application # comments application
(r'^comments/', include('django.contrib.comments.urls')), (r'^comments/', include('django.contrib.comments.urls')),

# API
url(r'^api/games/$', api.GameList.as_view()),
url(r'^api/games/(?P<pk>[0-9]+)/$', api.GameDetail.as_view()),
) )

urlpatterns = format_suffix_patterns(urlpatterns)

0 comments on commit c949d8c

Please sign in to comment.