Beispiel #1
0
 def put(self, request, pk, format=None):
     book = self.get_object(pk)
     serializer = BookSerializer(book, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def create(self, request):
     logger.info('post method of a book')
     if request.user.role == "Admin":
         return Response({}, status=status.HTTP_400_BAD_REQUEST)
     serializer = BookSerializer(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_500_INTERNAL_SERVER_ERROR)
Beispiel #3
0
 def update(self, request, id):
     logger.info('update of a book')
     if request.user.role == "Admin":
         return Response({}, status=status.HTTP_400_BAD_REQUEST)
     serializer = BookSerializer(instance=Book.objects.get(id=id), 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_500_INTERNAL_SERVER_ERROR)
Beispiel #4
0
def get_books_by_genre(request, genre_id):
    logger.info('books by genre')
    try:
        books_by_genre = Book.objects.filter(genre=Genre.objects.get(id=genre_id))
    except Genre.DoesNotExist as e:
        return Response({'error': str(e)})
    if request.method == "GET":
        serializer = BookSerializer(data=books_by_genre, many=True)
        if serializer.is_valid():
            serializer.save()
        return Response(serializer.data)
Beispiel #5
0
 def create(self, request):
     book_data = request.data
     new_book = Book.objects.create(name=book_data['name'],
                                    price=book_data['price'],
                                    created_at=book_data['created_at'],
                                    description=book_data['description'],
                                    genre=book_data['genre'],
                                    num_pages=book_data['num_pages'])
     new_book.save()
     serializer = BookSerializer(new_book)
     return Response(serializer.data)
Beispiel #6
0
 def create(self, request):
     journal_data = request.data
     new_journal = Journal.objects.create(
         name=journal_data['name'],
         price=journal_data['price'],
         created_at=journal_data['created_at'],
         description=journal_data['description'],
         genre=journal_data['genre'],
         num_pages=journal_data['num_pages'])
     new_journal.save()
     serializer = BookSerializer(new_journal)
     return Response(serializer.data)
Beispiel #7
0
 def get(self, request, pk, format=None):
     book = self.get_object(pk)
     serializer = BookSerializer(book)
     return Response(serializer.data)
Beispiel #8
0
 def post(self, request, format=None):
     serializer = BookSerializer(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)
Beispiel #9
0
 def get(self, request, format=None):
     books = Book.objects.all()
     serializer = BookSerializer(books, many=True)
     return Response(serializer.data)
Beispiel #10
0
 def retrieve(self, request, id):
     queryset = Book.objects.all()
     task = get_object_or_404(queryset, id=id)
     serializer = BookSerializer(task)
     return Response(serializer.data)
Beispiel #11
0
 def book_list(self, request):
     list = Book.objects.all()
     serializer = BookSerializer(list, many=True)
     return Response(serializer.data)
Beispiel #12
0
 def book_list(self, request):
     logger.info('list of a books')
     list = Book.objects.all()
     serializer = BookSerializer(list, many=True)
     return Response(serializer.data)
Beispiel #13
0
 def retrieve(self, request, pk=None):
     queryset = Book.objects.all()
     user = get_object_or_404(queryset, pk=pk)
     serializer = BookSerializer(user)
     return Response(serializer.data)
Beispiel #14
0
 def list(self, request):
     queryset = Book.objects.all()
     serializer = BookSerializer(queryset, many=True)
     return Response(serializer.data)