<reponame>kelly-rose/kellygram<filename>kellygram/images/views.py
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response

from kellygram.images import serializers
from . import models


class Feed(APIView):
    def get(self, request, format=None):
        user = request.user
        following_users = user.following.all()
        # print(following_users)
        image_list=[]
        for following_user in following_users:
            # print(following_user.images.all()[:2])
            user_images = following_user.images.all()[:2]
            # print(user_images)
            for image in user_images:
                # print(image)
                image_list.append(image)
        print(image_list)
        sorted_list = sorted(image_list, key=lambda image: image.updated_at, reverse=True)
        # sorted_list = sorted(image_list,key=get_key,reverse=True)
        print(sorted_list)
        serializer = serializers.ImageSerializer(sorted_list,many=True)
        return  Response(serializer.data)

# def get_key(image):
#     print(image)
#     return image.updated_at


class LikeImage(APIView):
    def post(self, request, image_id, format=None):  # 1-41
        user = request.user

        try:
            found_image = models.Image.objects.get(id=image_id)
        except models.Image.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        try:
            preexisiting_like = models.Like.objects.get(
                    creator=user,
                    image=found_image
            )
            preexisiting_like.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)

        except models.Like.DoesNotExist:
            new_like = models.Like.objects.create(
                creator=user,
                image=found_image
            )

            new_like.save()
            return Response(status=status.HTTP_201_CREATED)

class CommentOnImage(APIView):
    def post(self, request, image_id, format=None):  # 1-41
        user = request.user
        try:
            found_image = models.Image.objects.get(id=image_id)
        except models.Image.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = serializers.CommentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(creator=user, image=found_image)
            return Response(data=serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(data=serializer.errors, status=status.HTTP_201_CREATED)


class Comment(APIView):
    def delete(self, request, comment_id, format=None):
        user = request.user

        try:
            comment = models.Comment.objects.get(id=comment_id, creator=user)
            comment.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except models.Comment.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response

from kellygram.images import serializers
from . import models


class Feed(APIView):
    def get(self, request, format=None):
        user = request.user
        following_users = user.following.all()
        # print(following_users)
        image_list=[]
        for following_user in following_users:
            # print(following_user.images.all()[:2])
            user_images = following_user.images.all()[:2]
            # print(user_images)
            for image in user_images:
                # print(image)
                image_list.append(image)
        print(image_list)
        sorted_list = sorted(image_list, key=lambda image: image.updated_at, reverse=True)
        # sorted_list = sorted(image_list,key=get_key,reverse=True)
        print(sorted_list)
        serializer = serializers.ImageSerializer(sorted_list,many=True)
        return  Response(serializer.data)

# def get_key(image):
#     print(image)
#     return image.updated_at


class LikeImage(APIView):
    def post(self, request, image_id, format=None):  # 1-41
        user = request.user

        try:
            found_image = models.Image.objects.get(id=image_id)
        except models.Image.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        try:
            preexisiting_like = models.Like.objects.get(
                    creator=user,
                    image=found_image
            )
            preexisiting_like.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)

        except models.Like.DoesNotExist:
            new_like = models.Like.objects.create(
                creator=user,
                image=found_image
            )

            new_like.save()
            return Response(status=status.HTTP_201_CREATED)

class CommentOnImage(APIView):
    def post(self, request, image_id, format=None):  # 1-41
        user = request.user
        try:
            found_image = models.Image.objects.get(id=image_id)
        except models.Image.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = serializers.CommentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(creator=user, image=found_image)
            return Response(data=serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(data=serializer.errors, status=status.HTTP_201_CREATED)


class Comment(APIView):
    def delete(self, request, comment_id, format=None):
        user = request.user

        try:
            comment = models.Comment.objects.get(id=comment_id, creator=user)
            comment.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
        except models.Comment.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
# print(following_users) # print(following_user.images.all()[:2]) # print(user_images) # print(image) # sorted_list = sorted(image_list,key=get_key,reverse=True) # def get_key(image): #     print(image) #     return image.updated_at # 1-41 # 1-41