def library_list(request): with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select l.libraryId, l.library_name, l.library_address, from libraryapp_library l """) all_libraries = [] dataset = db_cursor.fetchall() for row in dataset: library = Library() library.id = row["id"] library.name = row["name"] library.address = row["address"] all_libraries.append(lib) template_name = 'library/list.html' context = {'all_libraries': all_libraries} return render(request, template_name, context)
def create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.name = _row["name"] library.address = _row["address"] # Note: You are adding a blank books list to the library object # This list will be populated later (see below) library.books = [] book = Book() book.id = _row["book_id"] book.title = _row["book_title"] book.author = _row["author"] book.isbn = _row["isbn"] book.year_published = _row["year_published"] book.publisher = _row["publisher"] # Return a tuple containing the library and the # book built from the data in the current row of # the data set return ( library, book, )
def library_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 l.id, l.name, l.address from libraryapp_library l """) libraries = [] dataset = db_cursor.fetchall() for row in dataset: library = Library() library.id = row["id"] library.name = row["name"] library.address = row["address"] libraries.append(library) template_name = 'libraries/list.html' context = {'libraries': libraries} 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_library ( name, address ) VALUES (?, ?) """, (form_data['name'], form_data['address'])) return redirect(reverse('libraryapp:libraries'))
def create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.name = _row["name"] library.address = _row["address"] library.books = [] book = Book() book.id = _row["book_id"] book.title = _row["book_title"] book.author = _row["author"] book.publisher = _row["publisher"] book.year_published = _row["year_published"] book.isbn = _row["isbn"] return ( library, book, )
def list_libraries(request): if request.method == 'GET': all_libraries = Library.objects.all() for library in all_libraries: library.books = Book.objects.filter(location_id=library.id) template = 'libraries/list.html' context = {'all_libraries': all_libraries} return render(request, template, context) elif request.method == 'POST': form_data = request.POST new_library = Library() new_library.name = form_data['name'] new_library.address = form_data['address'] new_library.save() return redirect(reverse('libraryapp:libraries'))
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.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.location = library return book
def library_list(request): if request.method == 'GET': # with sqlite3.connect(Connection.db_path) as conn: # conn.row_factory = create_library # db_cursor = conn.cursor() # db_cursor.execute(""" # SELECT # li.id library_id, # li.name, # li.address, # b.id book_id, # b.title, # b.author, # b.year_published, # b.publisher, # b.isbn # FROM libraryapp_library li # JOIN libraryapp_book b ON li.id = b.library_id # """) # libraries = db_cursor.fetchall() # # Start with an empty dictionary # library_groups = {} # # Iterate the list of tuples # for (library, book) in libraries: # # If the dictionary does have a key of the current # # library's `id` value, add the key and set the value # # to the current library # if library.id not in library_groups: # library_groups[library.id] = library # library_groups[library.id].books.append(book) # # If the key does exist, just append the current # # book to the list of books for the current library # else: # library_groups[library.id].books.append(book) all_libraries = Library.objects.all() all_books = Book.objects.all() template = 'libraries/list.html' context = {'all_libraries': all_libraries, 'all_books': all_books} return render(request, template, 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_library # ( # name, address # ) # VALUES (?, ?) # """, # (form_data['name'], form_data['address'])) new_library = Library() new_library.name = form_data['name'] new_library.address = form_data['address'] new_library.save() return redirect(reverse('libraryapp:libraries')) # def create_library(cursor, row): # _row = sqlite3.Row(cursor, row) # library = Library() # library.id = _row["library_id"] # library.name = _row["name"] # library.address = _row["address"] # # Note: You are adding a blank books list to the library object # # This list will be populated later (see below) # library.books = [] # book = Book() # book.id = _row["book_id"] # book.title = _row["title"] # book.author = _row["author"] # book.isbn = _row["isbn"] # book.year_published = _row["year_published"] # book.publisher = _row["publisher"] # # Return a tuple containing the library and the # # book built from the data in the current row of # # the data set # return (library, book,)