Coverage for books/views.py: 75.00%

64 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-23 15:55 -0700

1from typing import ClassVar 1a

2 

3from django.contrib.auth.mixins import LoginRequiredMixin 1a

4from django.db.models import Q 1a

5from django.urls import reverse 1a

6from django.views.generic import ( 1a

7 CreateView, 

8 DeleteView, 

9 DetailView, 

10 ListView, 

11 UpdateView, 

12) 

13 

14from .models import Book, Review 1a

15 

16 

17class BookCreateView(LoginRequiredMixin, CreateView): 1a

18 model = Book 1a

19 fields: ClassVar[list[str]] = [ 1a

20 "title", 

21 "author", 

22 "description", 

23 "price", 

24 "publisher", 

25 "pubdate", 

26 "cover", 

27 ] 

28 

29 def form_valid(self, form): 1a

30 form.instance.creator = self.request.user 1a

31 return super().form_valid(form) 1a

32 

33 

34class BookDetailView(DetailView): 1a

35 model = Book 1a

36 context_object_name = "book" 1a

37 login_url = "account_login" 1a

38 

39 

40class BookListView(ListView): 1a

41 model = Book 1a

42 context_object_name = "book_list" 1a

43 login_url = "account_login" 1a

44 

45 paginate_by = 5 1a

46 

47 

48class BookUpdateView(LoginRequiredMixin, UpdateView): 1a

49 model = Book 1a

50 fields: ClassVar[list[str]] = [ 1a

51 "title", 

52 "author", 

53 "description", 

54 "price", 

55 "publisher", 

56 "pubdate", 

57 "cover", 

58 ] 

59 action = "Update" 1a

60 

61 

62class ReviewCreateView(LoginRequiredMixin, CreateView): 1a

63 model = Review 1a

64 fields: ClassVar[list[str]] = ["book", "review"] 1a

65 

66 def get_initial(self): 1a

67 # initial_data = super(ReviewCreateView, self).get_initial() 

68 initial_data = super().get_initial() 

69 book = Book.objects.get(id=self.kwargs["book_id"]) 

70 initial_data["book"] = book 

71 return initial_data 

72 

73 def get_context_data(self): 1a

74 # context = super(ReviewCreateView, self).get_context_data() 

75 context = super().get_context_data() 

76 book = Book.objects.get(id=self.kwargs["book_id"]) 

77 context["book"] = book 

78 context["title"] = "Add a new review" 

79 return context 

80 

81 def form_valid(self, form): 1a

82 form.instance.creator = self.request.user 

83 return super().form_valid(form) 

84 

85 def get_success_url(self): 1a

86 return reverse("book_detail", args=[self.object.book_id]) 

87 

88 

89class ReviewDetailView(LoginRequiredMixin, DetailView): 1a

90 model = Review 1a

91 context_object_name = "review" 1a

92 

93 

94class ReviewUpdateView(LoginRequiredMixin, UpdateView): 1a

95 model = Review 1a

96 fields: ClassVar[list[str]] = ["book", "review"] 1a

97 action = "Update" 1a

98 

99 def get_success_url(self): 1a

100 return reverse("book_detail", args=[self.object.book_id]) 

101 

102 

103class ReviewDeleteView(DeleteView): 1a

104 model = Review 1a

105 

106 def get_success_url(self): 1a

107 return reverse("book_detail", args=[self.object.book_id]) 

108 

109 

110class SearchResultsListView(ListView): 1a

111 model = Book 1a

112 context_object_name = "book_list" 1a

113 template_name = "books/search_results.html" 1a

114 

115 def get_queryset(self): 1a

116 query = self.request.GET.get("q") 

117 return Book.objects.filter( 

118 Q(title__icontains=query) | Q(author__icontains=query) 

119 )