Skip to content

Commit ae8577d

Browse files
author
Katja Durrani
committed
add comments section to book detail page, logged in users can post comments
1 parent 9279063 commit ae8577d

File tree

8 files changed

+165
-7
lines changed

8 files changed

+165
-7
lines changed

books/forms.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.conf import settings
88
from django.core.files.uploadedfile import InMemoryUploadedFile
99
from django import forms
10-
from .models import Book
10+
from .models import Book, Comment
1111

1212

1313
class PostBookForm(forms.ModelForm):
@@ -142,3 +142,15 @@ def get_file_extension(self, extension):
142142

143143
class InvalidExtension(Exception):
144144
"""Raise for invalid image extension"""
145+
146+
147+
class PostCommentForm(forms.ModelForm):
148+
149+
class Meta:
150+
model = Comment
151+
fields = (
152+
"comment",
153+
)
154+
widgets = {
155+
"comment": forms.Textarea(attrs={"class": "uk-textarea"})
156+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 2.2.10 on 2020-03-08 11:54
2+
3+
from django.conf import settings
4+
import django.core.validators
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
13+
('books', '0021_auto_20191115_0211'),
14+
]
15+
16+
operations = [
17+
migrations.AlterField(
18+
model_name='book',
19+
name='year_published',
20+
field=models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(2020)]),
21+
),
22+
migrations.CreateModel(
23+
name='Comment',
24+
fields=[
25+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
26+
('comment', models.TextField(help_text='Have you read the book or heard about it, want to read it? Add a comment.', max_length=1000, null=True)),
27+
('comment_author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
28+
('comment_book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='books.Book')),
29+
],
30+
),
31+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.2.10 on 2020-03-08 12:09
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('books', '0022_auto_20200308_1154'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='comment',
15+
name='last_updated',
16+
field=models.DateTimeField(auto_now_add=True, null=True),
17+
),
18+
migrations.AddField(
19+
model_name='comment',
20+
name='published_date',
21+
field=models.DateTimeField(blank=True, null=True),
22+
),
23+
]

books/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,16 @@ class BookHolder(models.Model):
190190
date_requested = models.DateTimeField(blank=True, null=True)
191191
date_borrowed = models.DateTimeField(blank=True, null=True)
192192
date_returned = models.DateTimeField(blank=True, null=True)
193+
194+
class Comment(models.Model):
195+
comment = models.TextField(
196+
max_length=1000,
197+
help_text="Have you read the book or heard about it, want to read it? Add a comment.",
198+
null=True,
199+
)
200+
# @TODO decide what to do when a user is deleted; could have a 'former member' user?
201+
# what when a book is deleted?
202+
comment_author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
203+
comment_book = models.ForeignKey(Book, on_delete=models.CASCADE)
204+
published_date = models.DateTimeField(blank=True, null=True)
205+
last_updated = models.DateTimeField(auto_now_add=True, null=True)

books/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
name="book_interest",
4848
),
4949
path("<int:pk>/success", views.BookEmailSuccess.as_view(), name="email_success"),
50+
path("<int:pk>/success_comment", views.BookCommentSuccess.as_view(), name="comment_success"),
5051
path("search", views.BookSearchResultsView.as_view(), name="search_results"),
5152
path("<int:pk>/", views.BookDetailView.as_view(), name="book_detail"),
5253
path(

books/views.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from django.db.models import Q
1111

1212
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
1515
from .notifications import (
1616
notify_owner_of_request,
1717
notify_of_loan_or_return,
@@ -106,10 +106,28 @@ def get(self, request, supercategory):
106106

107107
class BookDetailView(TemplateView):
108108
template_name = "books/book_detail.html"
109+
form_class = PostCommentForm
109110

110111
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})
111117

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})
113131

114132

115133
class BaseLoanView(DetailView):
@@ -225,6 +243,10 @@ def change_status(self, book, holder):
225243
class BookEmailSuccess(TemplateView):
226244
template_name = "books/success.html"
227245

246+
247+
class BookCommentSuccess(TemplateView):
248+
template_name = "books/success_comment.html"
249+
228250
class BookChangeStatus(TemplateView):
229251
template_name = "books/book_change_status.html"
230252

templates/books/book_detail.html

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ <h1>{{ ct_book.title }}</h1>
4747
{% if ct_book.year_published %}
4848
<p>Year published: {{ ct_book.year_published }}</p>
4949
{% endif %}
50-
51-
5250
</div>
5351

5452
<div class="uk-container-expand uk-margin-top">
@@ -68,12 +66,56 @@ <h1>{{ ct_book.title }}</h1>
6866
{% if ct_book.at_framework %}<p><strong>At Framework Library</strong></p>{% endif %}
6967
<p>Owned by {{ ct_book.owner.username }}</p>
7068

69+
70+
{% block comments %}
71+
{% if comments %}
72+
<h3>Comments</h3>
73+
<ul class="uk-container-expand uk-margin-top">
74+
{% for comment in comments %}
75+
<li>
76+
<p>{{ comment.comment }}</p>
77+
<p>Posted by: {{ comment.comment_author.username }}</p>
78+
</li>
79+
{% endfor %}
80+
</ul>
81+
{% endif %}
82+
83+
{% if user.is_authenticated %}
84+
<h4>Post a comment</h4>
85+
<form method="POST" class="uk-form book__form maxw600">
86+
{% csrf_token %}
87+
{% for field in form %}
88+
<div class="uk-form-row uk-margin">
89+
<label for="{{ field.id_for_label }}" class="uk-form-label">{{ field.label }}: </label>
90+
<div class="uk-form-controls">
91+
{{ field }}
92+
{% if field.errors %}
93+
<div class="uk-alert uk-alert-warning">
94+
{{ field.errors }}
95+
</div>
96+
{% endif %}
97+
{% if field.help_text %}
98+
<div class="uk-alert">
99+
{{ field.help_text }}
100+
</div>
101+
{% endif %}
102+
</div>
103+
</div>
104+
{% endfor %}
105+
<button type="submit" class="uk-button uk-button-bookupdate">Save</button>
106+
</form>
107+
<p>&nbsp;</p>
108+
{% endif %}
109+
110+
{% endblock %}
111+
112+
71113
{% if ct_book.status == 'AV' %}
72114

73115
{% if user.is_authenticated %}
74116
<a class="uk-button uk-button-secondary maxw600" href="{% url 'book_request_item' pk=ct_book.id %}">Request this book</a>
75117
{% else %}
76-
<a href="{% url 'login' %}">Log in to request this book</a>
118+
<a href="{% url 'login' %}">Log in to request this book, or post a comment.</a>
77119
{% endif %}
78120

79121
{% else %}

templates/books/success_comment.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
5+
Thanks, your comment has been saved. <br />&nbsp;<br />
6+
7+
{% if ct_book %}
8+
&laquo; Back to page for <a href="{% url 'book_detail' pk=ct_book.id %}"> {{ ct_book.title }}</a>
9+
<br />&nbsp;<br />
10+
{% endif %}
11+
12+
&laquo; To <a href="/books">all books</a>
13+
14+
{% endblock %}

0 commit comments

Comments
 (0)