def book_list(request): if request.method == "GET": with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select b.id, b.title, b.author, b.year_published, l.title as location from libraryapp_book b join libraryapp_library l ON b.location_id = l.id """) all_books = [] dataset = db_cursor.fetchall() for row in dataset: book = Book() book.id = row["id"] book.title = row["title"] book.author = row["author"] book.year_published = row["year_published"] book.library = row["location"] all_books.append(book) template_name = 'books/list.html' context = {'all_books': all_books} return render(request, template_name, context) elif request.method == 'POST': form_data = request.POST with sqlite3.connect(Connection.db_path) as conn: db_cursor = conn.cursor() db_cursor.execute( """ INSERT INTO libraryapp_book ( title, author, isbn, year_published, location_id, librarian_id ) VALUES (?, ?, ?, ?, ?, ?) """, (form_data['title'], form_data['author'], form_data['isbn'], form_data['year_published'], request.user.librarian.id, form_data["location"])) return redirect(reverse('libraryapp:books'))
def create_book(cursor, row): _row = sqlite3.Row(cursor, row) book = Book() book.id = _row["book_id"] book.author = _row["author"] book.isbn = _row["isbn"] book.title = _row["title"] book.publisher = _row["publisher"] book.year_published = _row["year_published"] librarian = Librarian() librarian.id = _row["librarian_id"] librarian.first_name = _row["first_name"] librarian.last_name = _row["last_name"] library = Library() library.id = _row["library_id"] library.name = _row["library_name"] book.librarian = librarian book.library = library return book