<reponame>Umayr7/profiles-rest-api
from rest_framework.views  import APIView
from rest_framework.response import Response
from rest_framework import status, viewsets, filters
from rest_framework.authentication import TokenAuthentication
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings
from rest_framework.permissions import IsAuthenticated

from profiles_api import serializers, models, permissions

class HelloApiView(APIView):
    """ Test Api View """
    serializer_class = serializers.HelloSerializer

    def get(self, request, format=None):
        """ Returns a list of APIView features """
        an_apiview=[
            'Uses Http methods as function (get, post, put, patch, delete)',
            'Is similar to a traditional Django view',
            'Gives you the most control over your application logic',
            'Is manually mapped to URLS',
        ]

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

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

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}'
            return Response( {'message':message} )
        else:
            return Response(serializer.errors, status=status.HTTP_404_BAD_REQUEST)

    def put(self,request, pk=None):
        """ Handle updating an object """
        return Response( {'methtod':'PUT'} )

    def patch(self,request, pk=None):
        """ Handle partial update of an object """
        return Response( {'methtod':'PATCH'} )

    def delete(self,request, pk=None):
        """ Delete an object """
        return Response( {'methtod':'DELETE'} )


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

    def list(self, request):
        """ Return a Hello message """
        a_viewset = [
            'Uses actions (list, create, retrieve, update, partial_update)',
            'Automatically map to URLs using Routers',
            'Provides more functionality with less code',
        ]

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

    def create(self,request):
        """ Creating a new Hello object message """
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}'
            return Response( {'message':message} )
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def retrieve(self, request, pk=None):
        """ Handle gentting an object by primary key """
        return Response( {'http_method':'GET'} )

    def update(self, request, pk=None):
        """ Handle updating and 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

class UserProfileFeedViewSet(viewsets.ModelViewSet):
    """ Handle Creating, reading and updating profile feed items """
    serializer_class = serializers.ProfileFeedItemSerializer
    queryset = models.ProfileFeedItem.objects.all()
    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.UpdateOwnStatus, IsAuthenticated)

    def perform_create(self, serializer):
        """ Sets the user profile to the logged in user """
        serializer.save(user_profile=self.request.user)

from rest_framework.views  import APIView
from rest_framework.response import Response
from rest_framework import status, viewsets, filters
from rest_framework.authentication import TokenAuthentication
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings
from rest_framework.permissions import IsAuthenticated

from profiles_api import serializers, models, permissions

class HelloApiView(APIView):
    """ Test Api View """
    serializer_class = serializers.HelloSerializer

    def get(self, request, format=None):
        """ Returns a list of APIView features """
        an_apiview=[
            'Uses Http methods as function (get, post, put, patch, delete)',
            'Is similar to a traditional Django view',
            'Gives you the most control over your application logic',
            'Is manually mapped to URLS',
        ]

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

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

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}'
            return Response( {'message':message} )
        else:
            return Response(serializer.errors, status=status.HTTP_404_BAD_REQUEST)

    def put(self,request, pk=None):
        """ Handle updating an object """
        return Response( {'methtod':'PUT'} )

    def patch(self,request, pk=None):
        """ Handle partial update of an object """
        return Response( {'methtod':'PATCH'} )

    def delete(self,request, pk=None):
        """ Delete an object """
        return Response( {'methtod':'DELETE'} )


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

    def list(self, request):
        """ Return a Hello message """
        a_viewset = [
            'Uses actions (list, create, retrieve, update, partial_update)',
            'Automatically map to URLs using Routers',
            'Provides more functionality with less code',
        ]

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

    def create(self,request):
        """ Creating a new Hello object message """
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}'
            return Response( {'message':message} )
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def retrieve(self, request, pk=None):
        """ Handle gentting an object by primary key """
        return Response( {'http_method':'GET'} )

    def update(self, request, pk=None):
        """ Handle updating and 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

class UserProfileFeedViewSet(viewsets.ModelViewSet):
    """ Handle Creating, reading and updating profile feed items """
    serializer_class = serializers.ProfileFeedItemSerializer
    queryset = models.ProfileFeedItem.objects.all()
    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.UpdateOwnStatus, IsAuthenticated)

    def perform_create(self, serializer):
        """ Sets the user profile to the logged in user """
        serializer.save(user_profile=self.request.user)
Test Api View Returns a list of APIView features Create a hello message with our posted name Handle updating an object Handle partial update of an object Delete an object Test Api ViewSet Return a Hello message Creating a new Hello object message Handle gentting an object by primary key Handle updating and object Handle updating part of an object Handle Removing an object Handle creating and updating profiles Handle creating user authentication tokens Handle Creating, reading and updating profile feed items Sets the user profile to the logged in user