from django.shortcuts import render
from django.http import HttpResponse

from .models import Greeting

# Create your views here.
def index(request):
    return HttpResponse('Hello from Python!')


def db(request):

    greeting = Greeting()
    greeting.save()

    greetings = Greeting.objects.all()

    return render(request, 'db.html', {'greetings': greetings})


from rest_framework import permissions, viewsets

from authentication_app.models import Account
from authentication_app.permissions import IsAccountOwner
from authentication_app.serializers import AccountSerializer

'''
    @name : AccountViewSerializer
    @desc : Defines the serializer for the account view.
'''
class AccountViewSerializer(viewsets.ModelViewSet):
    lookup_field = 'username'
    queryset = Account.objects.all()
    serializer_class = AccountSerializer

    def get_permissions(self):
        if self.reqiest.method in permissions.SAFE_METHODS:
            return (permissions.AllowAny(),)
        if self.request.method == 'POST':
            return (permissions.AllowAny(),)

        return (permissions.IsAuthenticated(), IsAccountOwner(),)

    def create(self, request):
        serializer = self.serializer_class(data=reqiest.data)

        if serializer.is_valid():
            Account.objects.create_user(**serializer.validated_data)
            return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

        return Response({
            'status' : 'Bad Request',
            'message' : 'Account could not be created with the received data.'
        }, status=status.HTTP_400_BAD_REQUEST)

Add the view serializer for the account model.
Add the view serializer for the account model.

+ from rest_framework import permissions, viewsets
- from django.shortcuts import render
- from django.http import HttpResponse
  
- from .models import Greeting
+ from authentication_app.models import Account
+ from authentication_app.permissions import IsAccountOwner
+ from authentication_app.serializers import AccountSerializer
  
- # Create your views here.
- def index(request):
-     return HttpResponse('Hello from Python!')
+ '''
+     @name : AccountViewSerializer
+     @desc : Defines the serializer for the account view.
+ '''
+ class AccountViewSerializer(viewsets.ModelViewSet):
+     lookup_field = 'username'
+     queryset = Account.objects.all()
+     serializer_class = AccountSerializer
  
+     def get_permissions(self):
+         if self.reqiest.method in permissions.SAFE_METHODS:
+             return (permissions.AllowAny(),)
+         if self.request.method == 'POST':
+             return (permissions.AllowAny(),)
  
- def db(request):
+         return (permissions.IsAuthenticated(), IsAccountOwner(),)
  
-     greeting = Greeting()
-     greeting.save()
+     def create(self, request):
+         serializer = self.serializer_class(data=reqiest.data)
  
-     greetings = Greeting.objects.all()
+         if serializer.is_valid():
+             Account.objects.create_user(**serializer.validated_data)
+             return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
  
-     return render(request, 'db.html', {'greetings': greetings})
+         return Response({
+             'status' : 'Bad Request',
+             'message' : 'Account could not be created with the received data.'
+         }, status=status.HTTP_400_BAD_REQUEST)
  
- 
Add the view serializer for the account model.
## Code Before:
from django.shortcuts import render
from django.http import HttpResponse

from .models import Greeting

# Create your views here.
def index(request):
    return HttpResponse('Hello from Python!')


def db(request):

    greeting = Greeting()
    greeting.save()

    greetings = Greeting.objects.all()

    return render(request, 'db.html', {'greetings': greetings})


## Instruction:
Add the view serializer for the account model.
## Code After:
from rest_framework import permissions, viewsets

from authentication_app.models import Account
from authentication_app.permissions import IsAccountOwner
from authentication_app.serializers import AccountSerializer

'''
    @name : AccountViewSerializer
    @desc : Defines the serializer for the account view.
'''
class AccountViewSerializer(viewsets.ModelViewSet):
    lookup_field = 'username'
    queryset = Account.objects.all()
    serializer_class = AccountSerializer

    def get_permissions(self):
        if self.reqiest.method in permissions.SAFE_METHODS:
            return (permissions.AllowAny(),)
        if self.request.method == 'POST':
            return (permissions.AllowAny(),)

        return (permissions.IsAuthenticated(), IsAccountOwner(),)

    def create(self, request):
        serializer = self.serializer_class(data=reqiest.data)

        if serializer.is_valid():
            Account.objects.create_user(**serializer.validated_data)
            return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

        return Response({
            'status' : 'Bad Request',
            'message' : 'Account could not be created with the received data.'
        }, status=status.HTTP_400_BAD_REQUEST)
