def followed_show_list_json(request): """ List all followed TV shows or create a new followed TV show. """ if request.method == 'GET': if request.user.is_active: followed_show_list = FollowedShow.objects.filter(user=request.user) else: followed_show_list = None serializer = FollowedShowSerializer(followed_show_list, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = FollowedShowSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def followed_show_detail_json(request, pk): """ Retrieve, update or delete a followed TV show. """ try: followed_show = FollowedShow.objects.get(pk=pk) except FollowedShow.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = FollowedShowSerializer(followed_show) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = FollowedShowSerializer(followed_show, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': followed_show.delete() return HttpResponse(status=200)