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
15 changes: 13 additions & 2 deletions books/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +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):
openlibcover = forms.CharField(max_length=200, widget=forms.HiddenInput())
Expand Down Expand Up @@ -142,3 +141,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)
2 changes: 2 additions & 0 deletions books/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@
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(
"<int:pk>/<str:action>",
login_required(views.BookChangeStatus.as_view()),
name="book_change_status",
),
path("ajax_calls/search", views.autocompleteModel, name="ajax_search")
]
49 changes: 44 additions & 5 deletions books/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import reverse_lazy
from django.http import HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.core.exceptions import ImproperlyConfigured
from django.views.generic import TemplateView, DetailView, ListView
from django.views.generic.edit import DeleteView, UpdateView
Expand All @@ -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 @@ -99,17 +99,36 @@ def get(self, request, supercategory):
{
"categories": categories,
"books": books,
"supercat": query_param,
"supercat": query_param
},

)


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 +244,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 Expand Up @@ -257,3 +280,19 @@ def get(self, request):
self.template_name,
{"categories": categories, "books": books, "query": query}
)

def autocompleteModel(request):
if request.is_ajax():
q = request.GET.get('term', '').capitalize()
title_qs = Book.objects.filter(title__icontains=q).exclude(status="NA")
author_qs = Book.objects.filter(author__icontains=q).exclude(status="NA")
results = []
for r in title_qs:
results.append(r.title)
for r in author_qs:
results.append(r.author)
data = JsonResponse(results, safe=False)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
Loading