def currentLoans(self, request, pk=None):
     user = request.user.id
     print(request.user)
     myBorrowed = BookInstance.objects.filter(borrower=request.user.id)
     print(myBorrowed)
     serializer = BookInstanceSerializer(myBorrowed, many=True)
     return Response(serializer.data)
 def reserve(self, request, pk=None):
     user = request.user.id
     print(request.user)
     queryset = BookInstance.objects.all()
     bookInstance = get_object_or_404(queryset, pk=pk)
     bookInstance.status = dict(BookInstance.LOAN_STATUS).get('r')
     bookInstance.save()
     serializer = BookInstanceSerializer(bookInstance)
     return Response(serializer.data)
 def create(self, request):
     print(request.data)
     bookId = request.data['book']
     queryset = Book.objects.all()
     book = get_object_or_404(queryset, pk=bookId)
     newInstance = BookInstance.create(book)
     newInstance.save()
     serializer = BookInstanceSerializer(newInstance)
     return Response(serializer.data)
class BookSerializer(BulkSerializerMixin, serializers.ModelSerializer):
    # author = AuthorSerializer(many=True, read_only=True) # will be read not written

    author = serializers.PrimaryKeyRelatedField(many=True, read_only=False, queryset=Author.objects.all())
    author_names = serializers.StringRelatedField(many=True, source='author',read_only=True)
    copies = BookInstanceSerializer(many=True, read_only=True)
    # copies = serializers.ListField()

    class Meta:
        model = Book
        list_serializer_class = BulkListSerializer
        fields = '__all__'
 def returnBook(self, request, pk=None):
     user = request.user.id
     print(request.user)
     queryset = BookInstance.objects.all()
     bookInstance = get_object_or_404(queryset, pk=pk)
     # make sure this user already has the book out on loan
     if bookInstance.status != 'o' or bookInstance.borrower.id != request.user.id:
         content = {
             'status': 'you have not borrowed this book on the system'
         }
         return Response(content, status=status.HTTP_400_BAD_REQUEST)
     bookInstance.status = 'a'
     bookInstance.borrower = None
     bookInstance.save()
     serializer = BookInstanceSerializer(bookInstance)
     return Response(serializer.data)
 def checkoutBulk(self, request):
     print(request.user)
     queryset = BookInstance.objects.all()
     updatedBookInstances = []
     for instance in request.data:
         bookInstance = get_object_or_404(queryset, id=instance['id'])
         # make sure book is not already on loan
         # if bookInstance.status == dict(BookInstance.LOAN_STATUS).get('o'):
         if bookInstance.status == 'o':
             content = {'status': 'book already on loan'}
             return Response(content, status=status.HTTP_400_BAD_REQUEST)
         # bookInstance.status = dict(BookInstance.LOAN_STATUS).get('o')
         bookInstance.status = 'o'
         bookInstance.borrower = request.user
         updatedBookInstances.append(bookInstance)
     # if everything is ok, save all now
     for book in updatedBookInstances:
         book.save()
     serializer = BookInstanceSerializer(updatedBookInstances, many=True)
     return Response(serializer.data)