diff --git a/.gitignore b/.gitignore index 6202183..3f04b96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -*__pycahce__* +*__pycache__* db.sqlite3 \ No newline at end of file diff --git a/bookshelf_management/apps/mgmt/static/booklist.js b/bookshelf_management/apps/mgmt/static/booklist.js index 71eb246..632f830 100644 --- a/bookshelf_management/apps/mgmt/static/booklist.js +++ b/bookshelf_management/apps/mgmt/static/booklist.js @@ -8,7 +8,6 @@ list = {} } - list = { '218': true } $('.bookshelf-entry').each( function() { const bookId = $(this).attr('id').split('-')[1] diff --git a/bookshelf_management/apps/mgmt/templates/index.html b/bookshelf_management/apps/mgmt/templates/bookshelf.html similarity index 100% rename from bookshelf_management/apps/mgmt/templates/index.html rename to bookshelf_management/apps/mgmt/templates/bookshelf.html diff --git a/bookshelf_management/apps/mgmt/views.py b/bookshelf_management/apps/mgmt/views.py index bf21aa4..9e8eb70 100644 --- a/bookshelf_management/apps/mgmt/views.py +++ b/bookshelf_management/apps/mgmt/views.py @@ -1,6 +1,6 @@ from .models import Book, Bookshelf, BookshelfToBook from django.http import HttpResponse -from django.shortcuts import render +from django.shortcuts import render, redirect @@ -12,8 +12,15 @@ from django.views import generic def insertBookToBookshelf(bookId, bookshelfId): - newEntry = BookshelfToBook.objects.create(bookId, bookshelfId) - newEntry.save() + existing = BookshelfToBook.objects.filter(fk_books=bookId, fk_bookshelves=bookshelfId) + if existing.count() > 0: + print('attempted duplicate insert') + return + book = Book.objects.get(id=bookId) + bookshelf = Bookshelf.objects.get(id=bookshelfId) + if book != None and bookshelf != None: + newEntry = BookshelfToBook(fk_books=book, fk_bookshelves=bookshelf) + newEntry.save() def booksInBookshelf(request, bookshelfId): @@ -21,30 +28,29 @@ def booksInBookshelf(request, bookshelfId): idList = BookshelfToBook.objects.filter(fk_bookshelves=bookshelfId).values_list('fk_books', flat=True) books = Book.objects.filter(id__in=idList) - if request.method == 'POST': - # create a form instance and populate it with data from the request: - form = (request.POST) - print(form) - # check whether it's valid: - if form.bookid != None: - insertBookToBookshelf(form.bookid, bookshelfId) - # redirect to a new URL: - return render(request, 'index.html', context=context) - - # if a GET (or any other method) we'll create a blank form - else: - form = NewBookForm() - context = { 'books': books, 'total': len(books), 'bookshelf': bookshelfName, - 'bookshelfId': bookshelfId, - 'form': form + 'bookshelfId': bookshelfId } + if request.method == 'POST': + # create a form instance and populate it with data from the request: + form = (request.POST) + # print(form) + # check whether it's valid: + if form.get('bookid') != None: + insertBookToBookshelf(form.get('bookid'), bookshelfId) + # redirect to a new URL: + return redirect(request.META['HTTP_REFERER']) - return render(request, 'index.html', context=context) + # if a GET (or any other method) we'll create a blank form + else: + form = NewBookForm() + context['form'] = form + + return render(request, 'bookshelf.html', context=context) def bookshelfList(request):