Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion books/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django import forms
from .models import Book
from .models import Book, Comment


class PostBookForm(forms.ModelForm):
Expand Down Expand Up @@ -142,3 +142,15 @@ def get_file_extension(self, extension):

class InvalidExtension(Exception):
"""Raise for invalid image extension"""


class PostCommentForm(forms.ModelForm):

class Meta:
model = Comment
fields = (
"comment",
)
widgets = {
"comment": forms.Textarea(attrs={"class": "uk-textarea"})
}
31 changes: 31 additions & 0 deletions books/migrations/0022_auto_20200308_1154.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.2.10 on 2020-03-08 11:54

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('books', '0021_auto_20191115_0211'),
]

operations = [
migrations.AlterField(
model_name='book',
name='year_published',
field=models.PositiveIntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(2020)]),
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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)),
('comment_author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('comment_book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='books.Book')),
],
),
]
23 changes: 23 additions & 0 deletions books/migrations/0023_auto_20200308_1209.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.10 on 2020-03-08 12:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('books', '0022_auto_20200308_1154'),
]

operations = [
migrations.AddField(
model_name='comment',
name='last_updated',
field=models.DateTimeField(auto_now_add=True, null=True),
),
migrations.AddField(
model_name='comment',
name='published_date',
field=models.DateTimeField(blank=True, null=True),
),
]
13 changes: 13 additions & 0 deletions books/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ class BookHolder(models.Model):
date_requested = models.DateTimeField(blank=True, null=True)
date_borrowed = models.DateTimeField(blank=True, null=True)
date_returned = models.DateTimeField(blank=True, null=True)

class Comment(models.Model):
comment = models.TextField(
max_length=1000,
help_text="Have you read the book or heard about it, want to read it? Add a comment.",
null=True,
)
# @TODO decide what to do when a user is deleted; could have a 'former member' user?
# what when a book is deleted?
comment_author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
comment_book = models.ForeignKey(Book, on_delete=models.CASCADE)
published_date = models.DateTimeField(blank=True, null=True)
last_updated = models.DateTimeField(auto_now_add=True, null=True)
1 change: 1 addition & 0 deletions books/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
name="book_interest",
),
path("<int:pk>/success", views.BookEmailSuccess.as_view(), name="email_success"),
path("<int:pk>/success_comment", views.BookCommentSuccess.as_view(), name="comment_success"),
path("search", views.BookSearchResultsView.as_view(), name="search_results"),
path("<int:pk>/", views.BookDetailView.as_view(), name="book_detail"),
path(
Expand Down
28 changes: 25 additions & 3 deletions books/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from django.db.models import Q

from bookx.context_processors import books_action
from .models import Book, Category, LoanStatus
from .forms import PostBookForm # , RequestBookForm
from .models import Book, Category, Comment, LoanStatus
from .forms import PostBookForm, PostCommentForm # , RequestBookForm
from .notifications import (
notify_owner_of_request,
notify_of_loan_or_return,
Expand Down Expand Up @@ -106,10 +106,28 @@ def get(self, request, supercategory):

class BookDetailView(TemplateView):
template_name = "books/book_detail.html"
form_class = PostCommentForm

def get(self, request, pk):
form = self.form_class()
comments = Comment.objects.filter(comment_book_id=pk)
# import ipdb
# ipdb.set_trace()
return render(request, self.template_name, {"form": form, "bookid": pk, "comments": comments})

return render(request, self.template_name)
def post(self, request, pk):
form = self.form_class(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.published_date = timezone.now()
comment.comment_book = Book.objects.get(id=pk)
if request.user.is_authenticated:
comment.comment_author = request.user
comment.save()
return HttpResponseRedirect(
reverse_lazy("comment_success", kwargs={"pk": pk})
)
return render(request, self.template_name, {"form": form})


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


class BookCommentSuccess(TemplateView):
template_name = "books/success_comment.html"

class BookChangeStatus(TemplateView):
template_name = "books/book_change_status.html"

Expand Down
48 changes: 45 additions & 3 deletions templates/books/book_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ <h1>{{ ct_book.title }}</h1>
{% if ct_book.year_published %}
<p>Year published: {{ ct_book.year_published }}</p>
{% endif %}


</div>

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


{% block comments %}
{% if comments %}
<h3>Comments</h3>
<ul class="uk-container-expand uk-margin-top">
{% for comment in comments %}
<li>
<p>{{ comment.comment }}</p>
<p>Posted by: {{ comment.comment_author.username }}</p>
</li>
{% endfor %}
</ul>
{% endif %}

{% if user.is_authenticated %}
<h4>Post a comment</h4>
<form method="POST" class="uk-form book__form maxw600">
{% csrf_token %}
{% for field in form %}
<div class="uk-form-row uk-margin">
<label for="{{ field.id_for_label }}" class="uk-form-label">{{ field.label }}: </label>
<div class="uk-form-controls">
{{ field }}
{% if field.errors %}
<div class="uk-alert uk-alert-warning">
{{ field.errors }}
</div>
{% endif %}
{% if field.help_text %}
<div class="uk-alert">
{{ field.help_text }}
</div>
{% endif %}
</div>
</div>
{% endfor %}
<button type="submit" class="uk-button uk-button-bookupdate">Save</button>
</form>
<p>&nbsp;</p>
{% endif %}

{% endblock %}


{% if ct_book.status == 'AV' %}

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

{% else %}
Expand Down
14 changes: 14 additions & 0 deletions templates/books/success_comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html' %}

{% block content %}

Thanks, your comment has been saved. <br />&nbsp;<br />

{% if ct_book %}
&laquo; Back to page for <a href="{% url 'book_detail' pk=ct_book.id %}"> {{ ct_book.title }}</a>
<br />&nbsp;<br />
{% endif %}

&laquo; To <a href="/books">all books</a>

{% endblock %}