알람은 유저가 url을 입력하기 보단
좋아요, 댓글, 팔로우 url이 실행될 때 알람기능도 동시에 이루어 져야 한다.
20번째 view를 생성하는 것이 아니라 function을 생성했다.
인자로는
생성자, 대상, 타입, 이미지, 코멘트이다.
그리고 이 function은
Notification 모델에 각각의 튜플값들을 생성한다.
들어가야 할 곳은 세 곳!
1. Follow
class FollowUser(APIView):
def post(self, request, id, format=None) :
user = request.user
#notification
try:
user_to_follow = models.User.objects.get(id=id)
except models.User.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
user.following.add(user_to_follow)
user.save()
notification_views.create_notification(user, user_to_follow,'follow')
return Response(status=status.HTTP_200_OK)
인자는 유저와, 팔로우한 유저, 타입은 follow
2.Like
class LikeImage(APIView):
#notification
def post(self, request , id , format=None):
user = request.user
try :
found_image = models.Image.objects.get(id=id)
except models.Image.DoesNotExist:
return Response(status=status.HTTP_204_NO_CONTENT)
try:
preexisting_like = models.Like.objects.get(
creator = user,
image=found_image
)
return Response(status=status.HTTP_304_NOT_MODIFIED)
except :
new_like = models.Like.objects.create(
creator = user,
image = found_image
)
notification_views.create_notification(
user, found_image.creator, 'like', found_image)
new_like.save()
return Response(status=status.HTTP_201_CREATED)
인자는 유저와, 이미지를 생성한 유저, 타입은 like, 이미지
3. Comment
class CommentOnImage(APIView):
def post(self, request, id, format=None):
user = request.user
try:
found_image = models.Image.objects.get(id=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)
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)
인자는 유저, 이미지를 생성한 유저, 타입은 comment, 이미지, 메세지이다(이때 시리얼라이즈에서 {"message":"Comment"}와 같은 타입으로 입력되기 때문에)
'BackEnd > Django' 카테고리의 다른 글
[Django]단일 이미지 보기 (0) | 2018.11.07 |
---|---|
[Django]내 이미지 댓글 지우기 [질문중] (0) | 2018.11.07 |
[Django]RuntimeError: Model class Moonstargram.notifications.models.Notification doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. (0) | 2018.11.07 |
[Django]Notification API (0) | 2018.11.07 |
[Django]Notification App 만들기 (0) | 2018.11.06 |