コード例 #1
0
    def post(self, request, *args, **kwargs):
        """
        Like recipe
        """
        recipe = get_object_or_404(Recipes, pk=kwargs['recipe_pk'])
        if recipe.private and recipe.chef != request.user:
            return self.invalid_request(
                'The recipe is private and the requestor is not the owner')

        try:
            like = Likes.objects.get(recipe=recipe, chef=request.user)
            return self.invalid_request('Like already exists')
        except Likes.DoesNotExist:
            like = Likes.objects.create(recipe=recipe, chef=request.user)
            recipe.update_likes()
            if request.user != recipe.chef:  # Don't send notification when user do a self like
                Notification.create_new_like(like)
            return Response({'response': {'return': True}})