Coverage for book\views.py: 100%
46 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-29 10:02 +0100
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-29 10:02 +0100
1from django.contrib import messages
2from django.contrib.auth import get_user
3from django.shortcuts import render, get_object_or_404, redirect
4from django.views import View
5from author.models import Author
6from .forms import BookForm
7from .models import Book
10class BookList(View):
11 def get(self, request):
12 """
13 Handle GET requests for the view.
15 Args:
16 request (HttpRequest): The HTTP request object.
18 Returns:
19 HttpResponse: The HTTP response object containing the rendered template.
20 """
21 return render(request, "book/books.html",
22 {'books': Book.objects.all().order_by('id')})
24 def post(self, request, book_id):
25 """
26 Handle the HTTP POST request to delete a book.
28 Args:
29 request (HttpRequest): The HTTP request object.
30 book_id (int): The ID of the book to be deleted.
32 Returns:
33 HttpResponseRedirect: A redirect response to the 'books' URL.
35 Raises:
36 Http404: If the book with the specified ID does not exist.
37 """
38 book = get_object_or_404(Book, id=book_id)
39 book.delete()
40 messages.add_message(request, messages.SUCCESS,
41 f'The book "{book.title}" by {book.author.name} '
42 f'was removed')
43 return redirect('books')
46class BookDetail(View):
47 def get(self, request, book_id=-1):
48 """
49 Handles the GET request for the book view.
51 Args:
52 request (HttpRequest): The HTTP request object.
53 book_id (int, optional): The ID of the book. Defaults to -1.
55 Returns:
56 HttpResponse: The rendered book template with the context.
57 """
58 context = {'object_list': Author.objects.all()}
59 if book_id >= 0:
60 queryset = Book.objects.filter(id=book_id)
61 context['book'] = get_object_or_404(queryset, id=book_id)
62 return render(request, "book/book.html", context)
64 def post(self, request, book_id=-1):
65 """
66 Handle the HTTP POST request for creating or updating a book.
68 Args:
69 request (HttpRequest): The HTTP request object.
70 book_id (int, optional): The ID of the book to be updated.
71 Defaults to -1.
73 Returns:
74 HttpResponseRedirect: A redirect response to the 'books' URL.
76 """
77 if book_id < 0:
78 book_form = BookForm(data=request.POST)
79 action_message = 'added'
80 else:
81 book = get_object_or_404(Book, id=book_id)
82 book_form = BookForm(data=request.POST, instance=book)
83 action_message = 'updated'
85 if book_form.is_valid():
86 book = book_form.save(commit=False)
87 book.title = request.POST["title"]
88 book.author = Author.objects.filter(id=request.POST["author"])[0]
89 book.save()
90 messages.add_message(request, messages.SUCCESS,
91 f'The book "{book.title}" by '
92 f'{book.author.name} was '
93 f'{action_message}')
94 return redirect('books')
97def toggle_reader(request, book_id):
98 """
99 Add or remove the reader to a book.
101 Parameters:
102 - request: The HTTP request object.
103 - book_id: The ID of the book to toggle the reader.
105 Returns:
106 - A redirect response to the 'books' page.
108 """
109 book = get_object_or_404(Book, id=book_id)
110 if book.reader:
111 book.reader = None
112 message = f'You returned the book "{book.title}" by {book.author.name}'
113 else:
114 book.reader = get_user(request)
115 message = f'You took the book "{book.title}" by {book.author.name}'
116 book.save()
117 messages.add_message(request, messages.SUCCESS, message)
118 return redirect('books')