13.1. REST

13.1.1. Product List

Code 13.20. shop/api.py
from http import HTTPStatus
from django.http import JsonResponse
from django.views import View
from shop.models import Product


class ProductListAPI(View):
    http_method_names = ['get']

    def get(self, request):
        products = Product.objects.all().values()
        response = list(products)
        return JsonResponse(status=HTTPStatus.OK, data=response, safe=False)
Code 13.21. urls.py
path('api/v1/products/', ProductListAPI.as_view(), name='product-list-api'),

13.1.2. Product Get

from http import HTTPStatus
from django.http import JsonResponse
from django.views import View
from shop.models import Product


class ProductGetAPI(View):
    http_method_names = ['get']

    def get(self, request, pk):
        try:
            product = Product.objects.get(pk=pk)
        except Product.DoesNotExist:
            response = {'reason': 'Not found', 'detail': 'Product does not exists'}
            return JsonResponse(status=HTTPStatus.NOT_FOUND, data=response, safe=False)
        else:
            response = vars(product)
            response.pop('_state')
            return JsonResponse(status=HTTPStatus.OK, data=response)
Code 13.22. urls.py
path('api/v1/product/<int:pk>/', ProductDetailAPI.as_view(), name='product-detail-api'),

13.1.3. Product Create

import json
from decimal import Decimal
from http import HTTPStatus
from django.core.exceptions import ValidationError
from django.http import JsonResponse
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from shop.models import Product


class ProductCreateAPI(View):
    http_method_names = ['post']

    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        data = json.loads(request.body.decode('utf-8'))
        if Product.objects.filter(ean13=data['ean13']).exists():
            response = {'reason': 'Already Reported', 'detail': 'Product already exists'}
            return JsonResponse(status=HTTPStatus.ALREADY_REPORTED, data=response, safe=False)
        try:
            Product.objects.create(
                ean13=data['ean13'],
                name=data['name'],
                price=Decimal(str(data['price'])))
        except ValidationError as error:
            response = {'reason': 'Bad request', 'detail': str(error)}
            return JsonResponse(status=HTTPStatus.UNPROCESSABLE_ENTITY, data=response, safe=False)
        except Exception as error:
            response = {'reason': 'Bad request', 'detail': str(error)}
            return JsonResponse(status=HTTPStatus.BAD_REQUEST, data=response, safe=False)
        else:
            response = {'reason': 'Ok', 'detail': 'Product created'}
            return JsonResponse(status=HTTPStatus.CREATED, data=response, safe=False)
Code 13.23. urls.py
path('api/v1/product/', ProductDetailAPI.as_view(), name='product-detail-api'),