def post(self, request, image_id, format=None): 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: preexisting_like = models.Like.objects.get( creator= user, image= found_image ) return Response(status=status.HTTP_304_NOT_MODIFIED) except models.Like.DoesNotExist: new_like = models.Like.objects.create( creator=user, image=found_image ) new_like.save() notification_views.create_notification(user, found_image.creator, 'like', found_image) return Response(status=status.HTTP_201_CREATED)
def post(self, request, image_id, format=None): user = request.user print(user) try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) # serializer는 현재 message 데이터만 받았다. # creator가 없으면 is_valid에서 오류가 발생한다. # 그렇기에 creator속성을 read_only로 제한하여 무시?하도록 한다. # 위 부분은 더 알아봐야할 듯 하다. serializer = serializers.CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, image=found_image) notification_views.create_notification(user, found_image.creator, 'comment', found_image, serializer.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_201_CREATED)
def post(self, request, image_id, format=None): user = request.user try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_204_NO_CONTENT) serializer = serializers.CommentSerializer(data=request.data) #serializer이 잘 작동하는지 확인해보는 것이다. if serializer.is_valid(): serializer.save(creator=user, image=found_image) print("correct serializer") notification_views.create_notification(user, found_image.creator, 'comment', found_image, serializer.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: print("Bad request") return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request, image_id, format=None): user = request.user try: found_image = models.Image.objects.get(pk=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) #try문을 진행하고 만약 except 조건과 같은 문제라면 except문을 실행한다. try: new_like = found_image.likes.get(creator=user) return Response(status=status.HTTP_304_NOT_MODIFIED) # except found_image.DoesNotExist: except models.Like.DoesNotExist: new_like = models.Like.objects.create( creator=user, image=found_image, ) new_like.save() notification_views.create_notification(user, found_image.creator, 'like', found_image) # try문을 통하여 좋아요가 존재하는지 확인하고 만약 존재하지 않는다면 except문이 실행된다. return Response(status=status.HTTP_201_CREATED)
def post(self, request, user_id, format=None): user = request.user try: user_to_follow = models.User.objects.get(id=user_id) except models.User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) user.following.add(user_to_follow) user.save() # notification for follow notification_views.create_notification(user, user_to_follow, 'follow') return Response(status=status.HTTP_200_OK)
def post(self, request, image_id, format=None): try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: #https://docs.djangoproject.com/en/2.2/ref/models/instances/#django.db.models.Model.DoesNotExist return Response(status=status.HTTP_404_NOT_FOUND) try: preexisting_like = models.Like.objects.get(creator=request.user, image=found_image) return Response(status=status.HTTP_304_NOT_MODIFIED) except models.Like.DoesNotExist: new_like = models.Like.objects.create(creator=request.user, image=found_image) #notification for Like notification_views.create_notification(request.user, found_image.creator, 'like', found_image) new_like.save() return Response(status=status.HTTP_201_CREATED)
def post(self, request, image_id, format=None): #get 이 아니라 post. --> DB에 무언가를 생성. user = request.user serializer = serializers.CommentSerializer( data=request.data) #시리얼라이저를 통해 디비에 데이터를 추가하려함. # commentserial 의 모델을 바탕(Meta 클래스에서 지정한데로) 으로 해서 만들어진데~ # request.data? https://www.django-rest-framework.org/api-guide/requests/#data # --> dict 형 , ex) {"message":"test"} try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if serializer.is_valid( ): # https://www.django-rest-framework.org/api-guide/serializers/#validation serializer.save( creator=user, image=found_image ) #https://www.django-rest-framework.org/api-guide/serializers/#saving-instances # --> serializer 에 image 필드가 없는데도 가능한가봐!!(원래 모델에는 있어서!?) # --> For turning the JSON object that comes to the API into a python object!! # 참조!! https://www.django-rest-framework.org/api-guide/serializers/#serializing-objects # https://www.django-rest-framework.org/api-guide/serializers/#deserializing-objects #notification for Comment notification_views.create_notification(user, found_image.creator, 'comment', found_image, serializer.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)