Exemplo n.º 1
0
    def get(self, request):
        journalists = Journalist.objects.all()
        serializer = JournalistSerializer(journalists,
                                          many=True,
                                          context={'request': request})

        return Response(serializer.data)
Exemplo n.º 2
0
    def post(self, request):
        serializer = JournalistSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def put(self, request, pk):
     journalist = self.get_object(pk)
     serializer = JournalistSerializer(journalist, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
 def get(self, request):
     # code is identical to function-based views
     journalists = Journalist.objects.filter()
     # context argument required for the link field in serializer
     serializer = JournalistSerializer(
         journalists, many=True, context={'request': request})
     return Response(serializer.data)
Exemplo n.º 5
0
 def get(self, request):
     journalist = Journalist.objects.all()
     serializer = JournalistSerializer(journalist,
                                       many=True,
                                       context={"request": request})
     # added the many=True so that the serializer can accept a queryset.
     return Response(serializer.data)
Exemplo n.º 6
0
 def get(self, request):
     #articles = Article.objects.filter(active=True)
     #alternativa è quella di chiamare un altro metodo
     journalists = get_list_or_404(Journalist)
     serializer = JournalistSerializer(journalists,
                                       many=True,
                                       context={'request': request})
     return Response(serializer.data)
Exemplo n.º 7
0
 def put(self, request, pk):
     journalist = get_object_or_404(Journalist, pk=pk)
     serializer = JournalistSerializer(journalist,
                                       data=request.data,
                                       context={'request': request})
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
    def post(self, request):
        serializer = JournalistSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# @api_view(["GET", "POST"])
# def article_list_create_api_view(request):

#     if request.method == "GET":
#         articles = Article.objects.filter(is_active=True)
#         serializer = ArticleSerializer(articles, many=True)
#         return Response(serializer.data)

#     elif request.method == "POST":
#         serializer = ArticleSerializer(data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# @api_view(["GET", "PUT", "DELETE"])
# def article_detail_api_view(request, pk):
#     try:
#         article = Article.objects.get(pk=pk)
#     except Article.DoesNotExist:
#         return Response({
#             "error": {
#                 "code": 404,
#                 "message": "Article not found!"
#             }
#         }, status=status.HTTP_404_NOT_FOUND)

#     if request.method == "GET":
#         serializer = ArticleSerializer(article)
#         return Response(serializer.data)

#     elif request.method == "PUT":
#         serializer = ArticleSerializer(article, data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

#     elif request.method == "DELETE":
#         article.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 9
0
    def post(self, request):
        serializer = JournalistSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# @api_view(['GET', 'POST'])
# def article_list_create_api_view(request):
#     if request.method == 'GET':
#         articles = Article.objects.filter(active=True)
#         serializer = ArticleSerializer(articles, many=True)
#         return Response(serializer.data)
#     elif request.method == 'POST':
#         serializer = ArticleSerializer(data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data,status=status.HTTP_201_CREATED)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# @api_view(['GET','PUT','DELETE'])
# def article_detail_api_view(request,pk):
#     try:
#         article = Article.objects.get(pk=pk)
#     except Article.DoesNotExist :
#         return Response({'error': {
#             'code': 404,
#             'message': 'Article not found'
#         }}, status=status.HTTP_404_NOT_FOUND)

#     if request.method == 'GET':
#         serializer = ArticleSerializer(article)
#         return Response(serializer.data)

#     elif request.method == 'PUT':
#         serializer = ArticleSerializer(article, data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data, status=status.HTTP_200_OK)

#     elif request.method == 'DELETE':
#         article.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)
    def post(self, request):
        serializer = JournalistSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# class JournalistListCreateAPIView(APIView):

#     def get(self, request):
#         journalists = Journalist.objects.all()
#         serializer = JournalistSerializer(journalists,
#                                           many=True,
#                                           context={'request': request})
#         return Response(serializer.data)

#     def post(self, request):
#         serializer = JournalistSerializer(data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data, status=status.HTTP_201_CREATED)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 11
0
 def get(self, request, pk):
     journalist = self.get_object(pk)
     serializer = JournalistSerializer(journalist)
     return Response(serializer.data)
Exemplo n.º 12
0
 def get(self, request):
     journalist = Journalist.objects.all()
     serializer = JournalistSerializer(
         journalist, many=True,
         context={'request': request})  # request is when using hyperlink
     return Response(serializer.data)
Exemplo n.º 13
0
 def get(self, request):
     journalist = Journalist.objects.all(
     )  # Get the journalist. Returns a QuerySet (multiple objects)
     serializer = JournalistSerializer(
         journalist, many=True)  # Passing a QuerySet into a serializer
     return Response(serializer.data)
Exemplo n.º 14
0
 def get(self, request, pk):
     journalist = get_object_or_404(Journalist, pk=pk)
     serializer = JournalistSerializer(journalist,
                                       context={'request': request})
     return Response(serializer.data)
Exemplo n.º 15
0
 def get(self, request):
     journalists = Journalist.objects.all()
     # Set many=True since articles is a QuerySet of mulitiple articles
     serializer = JournalistSerializer(journalists, many=True)
     return Response(serializer.data)