<reponame>sachinwb/profile-rest-api<filename>profiles_app/views.py<gh_stars>0
from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication
from rest_framework import filters
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings

from profiles_app import serializers
from profiles_app import models
from profiles_app import permissions


class HelloApiView(APIView):

    """test API View"""
    serializer_class=serializers.HelloSerializer

    def get(self,request,format=None):
        """Return a list of APIView features"""
        an_apiview=[
        'uses HTTP methods as function (get,post,patch,put,delete)',
        'is similar to a traditional django view',
        'gives you the most control over you application logic',
        'is mapped manually to urls'
        ]

        return Response({'msg':'Hello!','an_apiview':an_apiview})

    def post(self,request):
        """Create a hello msg with our name"""
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get('name')
            msg=f'Hello {name} '
            return Response({'msg':msg})
        else:
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    def put(self,request,pk=None):
         """handle updating an object"""
         return Response({'method':'put'})

    def patch(self,request,pk=None):
         """handle partially update on an object"""
         return Response({'method':'patch'})

    def delete(self,request,pk=None):
        """handle deletion operation on an object"""
        return Response({'method':'delete'})


class HelloViewSet(viewsets.ViewSet):
    """Test API ViewSet"""
    serializer_class=serializers.HelloSerializer

    def list(self,request):
        """Return a hello msg"""

        a_viewset={
        'uses actions (list,create,retrieve,update,partial_update)',
        'Automatically maps to URLs using Router',
        'Provides more functionality with less code',
        }

        return Response({'msg':'Hello!','a_viewset':a_viewset})

    def create(self,request):
        """Create a new Hello Msg"""
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get('name')
            msg=f'Hello {name}!'
            return Response({'msg':msg})

        else:
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    def retrieve(self,request,pk=None):
        """Handles getting an object by its ID"""
        return Response({'http_method':'GET'})

    def update(self,request,pk=None):
        """Handles updating an object"""
        return Response({'http_method':'PUT'})

    def partial_update(self,request,pk=None):
        """Handle updating part of an object"""
        return Response({'http_method':'PATCH'})

    def destroy(self,request,pk=None):
        """Handle removing an object"""
        return Response({'http_method':'DELETE'})


class UserProfileViewset(viewsets.ModelViewSet):
    """Handle Creating and Updating profiles"""
    serializer_class=serializers.UserProfileSerializer
    queryset=models.UserProfile.objects.all()
    authentication_classes=[TokenAuthentication,]
    permission_classes=[permissions.UpdateOwnProfile,]
    filter_backends = [filters.SearchFilter,]
    search_fields = ['name','email',]

class UserLoginApiView(ObtainAuthToken):
    """Handle creating user authentication tokens"""
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES

from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication
from rest_framework import filters
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings

from profiles_app import serializers
from profiles_app import models
from profiles_app import permissions


class HelloApiView(APIView):

    """test API View"""
    serializer_class=serializers.HelloSerializer

    def get(self,request,format=None):
        """Return a list of APIView features"""
        an_apiview=[
        'uses HTTP methods as function (get,post,patch,put,delete)',
        'is similar to a traditional django view',
        'gives you the most control over you application logic',
        'is mapped manually to urls'
        ]

        return Response({'msg':'Hello!','an_apiview':an_apiview})

    def post(self,request):
        """Create a hello msg with our name"""
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get('name')
            msg=f'Hello {name} '
            return Response({'msg':msg})
        else:
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    def put(self,request,pk=None):
         """handle updating an object"""
         return Response({'method':'put'})

    def patch(self,request,pk=None):
         """handle partially update on an object"""
         return Response({'method':'patch'})

    def delete(self,request,pk=None):
        """handle deletion operation on an object"""
        return Response({'method':'delete'})


class HelloViewSet(viewsets.ViewSet):
    """Test API ViewSet"""
    serializer_class=serializers.HelloSerializer

    def list(self,request):
        """Return a hello msg"""

        a_viewset={
        'uses actions (list,create,retrieve,update,partial_update)',
        'Automatically maps to URLs using Router',
        'Provides more functionality with less code',
        }

        return Response({'msg':'Hello!','a_viewset':a_viewset})

    def create(self,request):
        """Create a new Hello Msg"""
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get('name')
            msg=f'Hello {name}!'
            return Response({'msg':msg})

        else:
            return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    def retrieve(self,request,pk=None):
        """Handles getting an object by its ID"""
        return Response({'http_method':'GET'})

    def update(self,request,pk=None):
        """Handles updating an object"""
        return Response({'http_method':'PUT'})

    def partial_update(self,request,pk=None):
        """Handle updating part of an object"""
        return Response({'http_method':'PATCH'})

    def destroy(self,request,pk=None):
        """Handle removing an object"""
        return Response({'http_method':'DELETE'})


class UserProfileViewset(viewsets.ModelViewSet):
    """Handle Creating and Updating profiles"""
    serializer_class=serializers.UserProfileSerializer
    queryset=models.UserProfile.objects.all()
    authentication_classes=[TokenAuthentication,]
    permission_classes=[permissions.UpdateOwnProfile,]
    filter_backends = [filters.SearchFilter,]
    search_fields = ['name','email',]

class UserLoginApiView(ObtainAuthToken):
    """Handle creating user authentication tokens"""
    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
# Create your views here. test API View Return a list of APIView features Create a hello msg with our name handle updating an object handle partially update on an object handle deletion operation on an object Test API ViewSet Return a hello msg Create a new Hello Msg Handles getting an object by its ID Handles updating an object Handle updating part of an object Handle removing an object Handle Creating and Updating profiles Handle creating user authentication tokens