Uriel Libano
Uriel Libano

Reputation: 55

How to execute a function relative the request method with django rest framework?

I'm trying to just show a message in terminal if a post requisition, for exemple, be executed. Here I've made these function "get" to be executed but it also doesn't work. :/

 if request.method == 'POST':
     print("Requistion made")

views.py:

from rest_framework import viewsets 
from piecesapp.api import serializers
from piecesapp import models


from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt


class PiecesViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.PiecesSerializer
    queryset = models.Pieces.objects.all()
    def get(self, request, format=None):
        print("Oi")
        return Response(serializer_class.data)

    
class BoardViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.BoardSerializer
    queryset = models.Board.objects.all()

PS: I will remove this libs, it's just because i've tried some tutorials e it doesn't work.

urls.py:

from django.urls import  include, path 
from rest_framework import routers 
from . import views 

router = routers.DefaultRouter()

router.register(r'pieces', views.PiecesViewSet)
router.register(r'board', views.BoardViewSet)

urlpatterns = router.urls

models.py:

from django.db import models
import uuid

# Create your models here.
COLOR_CHOICES = (
    ('white','WHITE'),
    ('black', 'BLACK'),    
)
PICES_NAMES = (
    ('king','KING'),
    ('queen','QUEEN'),
    ('rook','ROOK'),
    ('bishop','BISHOP'),
    ('knight','KNIGHT'),
    ('pawn','PAWN'),
)
WHITE_KING_POSITION = ['e1']
BLACK_KING_POSITION = ['e8'] 

WHITE_QUEEN_POSITION = ['d1']
BLACK_QUEEN_POSITION = ['d8']

WHITE_ROOK_POSITION = ['a1','h1']
BLACK_ROOK_POSITION = ['a8','h8']

WHITE_BISHOP_POSITION = ['c1','f1']
BLACK_BISHOP_POSITION = ['c8','f8']

WHITE_KNIGHT_POSITION = ['b1', 'g1']   
BLACK_KNIGHT_POSITION = ['b8', 'g8']   

WHITE_PAWN_POSITION = ['a2', 'b2','c2','d2','e2','f2','g2','h2'] 
BLACK_PAWN_POSITION = ['a7', 'b7','c7','d7','e7','f7','g7','h7'] 

PIECE_POSITION_HORIZONTAL = (
    ('a','A'),
    ('b','B'),
    ('c','C'),
    ('d','D'),
    ('e','E'),
    ('f','F'),
    ('g','G'),
    ('h','H'),
)
PIECE_POSITION_VERTICAL = (
    ('1','1'),
    ('2','2'),
    ('3','3'),
    ('4','4'),
    ('5','5'),
    ('6','6'),
    ('7','7'),
    ('8','8'),
)
BOARD_DEFAULT_ID = 1
class Pieces(models.Model):
    piece_id = models.AutoField(primary_key=True,editable= False) 
    piece_name = models.CharField(choices = PICES_NAMES, max_length=255)
    piece_color = models.CharField(choices = COLOR_CHOICES, max_length=5)  
    initial_position_h = models.CharField(default = "a", choices=PIECE_POSITION_HORIZONTAL, max_length=50)
    initial_position_v = models.IntegerField(default = 1, choices=PIECE_POSITION_VERTICAL)
        
    chess_board = [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], ['1', '2', '3', '4', '5', '6', '7', '8']]
 
    def __int__(self):
        return self.piece_id
  
class Board(models.Model):
    boardpiece_id = models.ForeignKey(Pieces, on_delete=models.CASCADE)
    position_x = models.CharField(choices = PIECE_POSITION_HORIZONTAL, default="PIECE POSITION", max_length=50)
    position_y = models.CharField(choices = PIECE_POSITION_VERTICAL, default="PIECE POSITION",max_length = 50)
    def __str__(self):
        return str(self.boardpiece_id)
   

It's basically a API to show some possible chess plays

Upvotes: 1

Views: 903

Answers (1)

satyajit
satyajit

Reputation: 694

try something like this:

from rest_framework.views import APIView

class PiecesAPIView(APIView):
    def get(self,request,*args,**kwargs):
        queryset = models.Pieces.objects.all()
        serializer = serializers.PiecesSerializer(queryset, many=True, context={'request': request})
        data = serializer.data
        if data:
            return Response({
                'success' : 'True',
                'message' : 'Data retrieved successfully',
                'data'    : data,
            },status=200)
        else:
            #put some logic here

Upvotes: 1

Related Questions