|
10 | 10 | from django.db.models import Q |
11 | 11 |
|
12 | 12 | from bookx.context_processors import books_action |
13 | | -from .models import Book, Category, LoanStatus |
14 | | -from .forms import PostBookForm # , RequestBookForm |
| 13 | +from .models import Book, Category, Comment, LoanStatus |
| 14 | +from .forms import PostBookForm, PostCommentForm # , RequestBookForm |
15 | 15 | from .notifications import ( |
16 | 16 | notify_owner_of_request, |
17 | 17 | notify_of_loan_or_return, |
@@ -106,10 +106,28 @@ def get(self, request, supercategory): |
106 | 106 |
|
107 | 107 | class BookDetailView(TemplateView): |
108 | 108 | template_name = "books/book_detail.html" |
| 109 | + form_class = PostCommentForm |
109 | 110 |
|
110 | 111 | def get(self, request, pk): |
| 112 | + form = self.form_class() |
| 113 | + comments = Comment.objects.filter(comment_book_id=pk) |
| 114 | + # import ipdb |
| 115 | + # ipdb.set_trace() |
| 116 | + return render(request, self.template_name, {"form": form, "bookid": pk, "comments": comments}) |
111 | 117 |
|
112 | | - return render(request, self.template_name) |
| 118 | + def post(self, request, pk): |
| 119 | + form = self.form_class(request.POST) |
| 120 | + if form.is_valid(): |
| 121 | + comment = form.save(commit=False) |
| 122 | + comment.published_date = timezone.now() |
| 123 | + comment.comment_book = Book.objects.get(id=pk) |
| 124 | + if request.user.is_authenticated: |
| 125 | + comment.comment_author = request.user |
| 126 | + comment.save() |
| 127 | + return HttpResponseRedirect( |
| 128 | + reverse_lazy("comment_success", kwargs={"pk": pk}) |
| 129 | + ) |
| 130 | + return render(request, self.template_name, {"form": form}) |
113 | 131 |
|
114 | 132 |
|
115 | 133 | class BaseLoanView(DetailView): |
@@ -225,6 +243,10 @@ def change_status(self, book, holder): |
225 | 243 | class BookEmailSuccess(TemplateView): |
226 | 244 | template_name = "books/success.html" |
227 | 245 |
|
| 246 | + |
| 247 | +class BookCommentSuccess(TemplateView): |
| 248 | + template_name = "books/success_comment.html" |
| 249 | + |
228 | 250 | class BookChangeStatus(TemplateView): |
229 | 251 | template_name = "books/book_change_status.html" |
230 | 252 |
|
|
0 commit comments