def player_list(request, format=None): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Player.objects.all() serializer = PlayerSerializer(snippets, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = PlayerSerializer(data=request.data) print("In player list") ##d = Team(id=1, name="Thomas Davis") ##c = Player(id=9, name="Thomas", DOB='28/01/1998') ##d.players.add(c) ##data = request.data['Home_Team'] ##snippet = Player.objects.get(name=name) print(request.data['TeamID']) if serializer.is_valid(): player = Player(name=request.data['name'], DOB=request.data['DOB']) player.save() print(player.name) print(player.id) ##serializer.save() addPlayer = Player(id=player.id, name=request.data['name'], DOB=request.data['DOB']) team = Team(id=request.data['TeamID']) team.players.add(addPlayer) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, id, format=None): #Uses the "get_object"-method to retrieve a player and edits it if data is valid player = self.get_object(id) serializer = PlayerSerializer(player, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ActionSerializer(serializers.ModelSerializer): player = PlayerSerializer(read_only=True) class Meta: model = Action fields = '__all__'
class TripInvitationSerializer(serializers.ModelSerializer): player = PlayerSerializer() trip = TripSerializer() payment = PaymentSerializer() terms_signature = Base64ImageField() class Meta: model = TripInvitation fields = [ "uid", "status", "player", "trip", "payment", "total_amount_due", "form_information", "terms", "terms_signature", "terms_names", "terms_accepted_on", "additional_terms", "is_valid", "invoice_link", ] read_only_fields = [ "uid", "player", "trip", "payment", "total_amount_due", "is_valid", "invoice_link", ]
def create(self, request, *args, **kwargs): player = request.user.player serialized_player = PlayerSerializer(player, context={'request': request}) request.data['owner'] = player.pk request.data['players'] = [player.pk] return super(GameViewSet, self).create(request, *args, **kwargs)
def player_detail_name(request, name, format=None): """ Retrieve, update or delete a code snippet. """ try: snippet = Player.objects.filter(name=name) except Player.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': print("hello") print(snippet) playername = str(snippet) serializer = PlayerSerializer(snippet, many=True) ##queryset = Player.objects.raw("select id, name from players_Player where name = "+"'"+playername+"'") ##serializer = PlayerSerializer(list(queryset), many=True) return Response(serializer.data) elif request.method == 'PUT': serializer = PlayerSerializer(snippet, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) user = serializer.validated_data['user'] token, created = Token.objects.get_or_create(user=user) player = Player.objects.get(pk=user.id) return Response({ 'token': token.key, 'user': PlayerSerializer().to_representation(player) })
def state(player): """ Returns a dictionary with the game state in it. """ #get a game_map for the area. gm = player.game_map gm.populate() data = {} data['player'] = PlayerSerializer(player).data data['monsters'] = MonsterSerializer(gm.monster_set, many=True).data data['items'] = [] data['message'] = player.get_messages() return data
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) token = Token.objects.create(user=serializer.instance) token_data = {"token": token.key} player = Player.objects.get(pk=serializer.instance.id) return Response( { 'user': PlayerSerializer().to_representation(player), **token_data }, status=status.HTTP_201_CREATED, headers=headers)
def player_detail(request, pk, format=None): """ Retrieve, update or delete a code snippet. """ try: snippet = Player.objects.get(pk=pk) except Player.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = PlayerSerializer(snippet) return Response(serializer.data) elif request.method == 'PUT': serializer = PlayerSerializer(snippet, data=request.data) print("In player detail") if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': snippet.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, id, format=None): #By using the "get_object"-method, gets a specific player and returns it player = self.get_object(id) serializer = PlayerSerializer(player) return Response(serializer.data)
def post(self, request, format=None): serializer = PlayerSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): players = Player.objects.all() serializer = PlayerSerializer(players, many=True) return Response(serializer.data)
class TeamSerializer(serializers.ModelSerializer): players = PlayerSerializer(many=True, read_only=True) class Meta: model = Team fields = '__all__'