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.isbn, b.author, b.year_published, b.publisher, b.librarian_id, b.library_id FROM libraryapp_book b """) all_books = [] dataset = db_cursor.fetchall() for row in dataset: book = Book() book.id = row['id'] book.title = row['title'] book.isbn = row['isbn'] book.author = row['author'] book.publisher = row['publisher'] book.year_published = row['year_published'] book.librarian_id = row['librarian_id'] book.library_id = row['library_id'] all_books.append(book) template = 'books/book_list.html' context = { '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_book ( title, author, isbn, publisher, year_published, librarian_id, library_id ) VALUES (?, ?, ?, ?, ?, ?, ?) """, (form_data['title'], form_data['author'], form_data['isbn'], form_data['publisher'], form_data['year_published'], request.user.librarian.id, form_data["library"])) return redirect(reverse('libraryapp:books'))
def book_list(request): if request.method == 'GET': all_books = Book.objects.all() template = 'books/list.html' context = {'all_books': all_books} return render(request, template, context) elif request.method == 'POST': form_data = request.POST new_book = Book() new_book.title = form_data['title'] new_book.author = form_data['author'] new_book.ISBN_num = form_data['ISBN_num'] new_book.year_published = form_data['year_published'] librarian = Librarian() librarian.id = request.user.librarian.id new_book.librarian = librarian library = Library() library.id = form_data['location'] new_book.location = library new_book.save() return redirect(reverse('libraryapp:books'))
def book_list(request): if request.method == 'GET': ## SQL METHOD # with sqlite3.connect(Connection.db_path) as conn: # conn.row_factory = model_factory(Book) # db_cursor = conn.cursor() # db_cursor.execute(""" # select # b.id, # b.title, # b.isbn, # b.author, # b.year_published, # b.librarian_id, # b.location_id # from libraryapp_book b # """) # all_books = db_cursor.fetchall() # ORM Method all_books = Book.objects.all() template = 'books/list.html' context = {'all_books': all_books} return render(request, template, context) elif request.method == 'POST': form_data = request.POST ## SQL METHOD # 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"])) # ORM METHOD new_book = Book(title=form_data['title'], author=form_data['author'], isbn=form_data['isbn'], year_published=form_data['year_published'], librarian_id=request.user.librarian.id, location_id=form_data["location"]) new_book.save() return redirect(reverse('libraryapp:books'))
def create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.title = _row["title"] 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 = list() 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.location_id = _row["location_id"] book.librarian_id = _row["librarian_id"] # 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 categories_detail(request, category_id): try: c = Category.objects.get(pk=category_id) books = Book.objects.filter(category_id=category_id) print(books) except Exception as e: raise JsonResponse({"error": str(e)}, status=404) if request.method == 'GET': resp = [b.to_json() for b in books] return JsonResponse(resp, safe=False) elif request.method == 'PUT': data = QueryDict(request.body) c.title = data.get('title', c.title) c.save() return JsonResponse(c.to_json()) elif request.method == 'DELETE': c.delete() return JsonResponse(c.to_json()) elif request.method == 'POST': data = request.POST b = Book() b.title = data.get('title', '') b.author = data.get('author', '') b.category_id = c b.save() return JsonResponse(b.to_json())
def create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.title = _row["title"] 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.ISBNnumber = _row["ISBNnumber"] book.YearPublished = _row["YearPublished"] # 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 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
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.isbn, b.author, b.year_published, b.librarian_id, b.location_id from libraryapp_book b """) all_books = [] dataset = db_cursor.fetchall() for row in dataset: book = Book() book.id = row['id'] book.title = row['title'] book.isbn = row['isbn'] book.author = row['author'] book.year_published = row['year_published'] book.librarian_id = row['librarian_id'] book.location_id = row['location_id'] all_books.append(book) template = 'books/list.html' context = { 'all_books': all_books } return render(request, template, 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"] 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 create_book(cursor, row): _row = sqlite3.Row(cursor, row) book = Book() book.id = _row["book_id"] book.Author = _row["Author"] book.ISBNNumber = _row["ISBNNumber"] book.bookTitle = _row["bookTitle"] book.YearPublished = _row["YearPublished"] 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.title = _row["library_name"] book.librarian = librarian book.location = library return book
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.title = _row['library_name'] book.librarian = librarian book.location = library return book
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.ISBN_number, b.author, b.year, b.librarian_id, b.location_id from libraryapp_book b """) all_books = [] dataset = db_cursor.fetchall() for row in dataset: book = Book() book.id = row['id'] book.title = row['title'] book.ISBN_number = row['ISBN_number'] book.author = row['author'] book.year = row['year'] book.librarian_id = row['librarian_id'] book.location_id = row['location_id'] all_books.append(book) template = 'books/list.html' #holding the path and filename of the template created in templates/list.html context = {'all_books': all_books} return render(request, template, context)
def create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.title = _row["title"] library.address = _row["address"] 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"] # 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 create_library(cursor, row): _row = sqlite3.Row(cursor, row) library = Library() library.id = _row["id"] library.title = _row["title"] library.address = _row["address"] library.books = [] book = Book() book.id = _row["id"] book.title = _row["book_title"] book.author = _row["author"] book.isbn_number = _row["isbn_number"] book.year = _row["year"] return ( library, book, )
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.isbn, # b.author, # b.year_published, # b.librarian_id, # b.library_id, # b.publisher # from libraryapp_book b # """) # all_books = [] # dataset = db_cursor.fetchall() # for row in dataset: # book = Book() # book.id = row['id'] # book.title = row['title'] # book.isbn = row['isbn'] # book.author = row['author'] # book.year_published = row['year_published'] # book.librarian_id = row['librarian_id'] # book.library_id = row['library_id'] # book.publisher = row['publisher'] # all_books.append(book) all_books = Book.objects.all() template = 'books/list.html' context = { '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_book # ( # title, author, isbn, # year_published, library_id, librarian_id, # publisher # ) # VALUES (?, ?, ?, ?, ?, ?, ?) # """, # (form_data['title'], form_data['author'], # form_data['isbn'], form_data['year_published'], # request.user.librarian.id, form_data["location"], # form_data['publisher'])) new_book = Book() new_book.title = form_data['title'] new_book.author = form_data['author'] new_book.isbn = form_data['isbn'] new_book.year_published = form_data['year_published'] new_book.publisher = form_data['publisher'] new_book.librarian_id = request.user.librarian.id new_book.library_id = form_data['library'] new_book.save() return redirect(reverse('libraryapp:books'))
def book_list(request): if request.method == 'GET': # opens, and then closes connection, because we use 'with' with sqlite3.connect(Connection.db_path) as conn: # make a row factory. sqlite3.Row basically puts keys on our tuples conn.row_factory = sqlite3.Row # create a cursor object db_cursor = conn.cursor() # define the SQL Query, which will be saved on the cursor db_cursor.execute(''' select b.id, b.title, b.isbn, b.author, b.year_published, b.librarian_id, b.location_id from libraryapp_book b ''') all_books = [] # get the data dataset = db_cursor.fetchall() # loop through dataset, create book instances, append to all_books for row in dataset: book = Book() book.id = row['id'] book.title = row['title'] book.isbn = row['isbn'] book.author = row['author'] book.year_published = row['year_published'] book.librarian_id = row['librarian_id'] book.location_id = row['location_id'] all_books.append(book) # variable name template assigned to the path to our HTML template # that will be used for this view template = 'books/list.html' # dictionary of values passing into the template context = {'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_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.id, form_data['location'])) return redirect(reverse('libraryapp:books'))
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.isbn, # b.author, # b.year, # b.librarian_id, # b.location_id # from libraryapp_book b # """) # all_books = [] # dataset = db_cursor.fetchall() # for row in dataset: # book = Book() # book.id = row['id'] # book.title = row['title'] # book.isbn = row['isbn'] # book.author = row['author'] # book.year = row['year'] # book.librarian_id = row['librarian_id'] # book.location_id = row['location_id'] # all_books.append(book) all_books = Book.objects.all() title = request.GET.get('title', None) # title = request.GET['title'] if title is not None: all_books = all_books.filter(title__contains=title) template = 'books/list.html' context = { '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_book # ( # title, author, isbn, # year, 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"])) # instantiate... new_book = Book( title = form_data['title'], author = form_data['author'], isbn = form_data['isbn'], year = form_data['year_published'], librarian_id = request.user.librarian.id, location_id = form_data["location"] ) # and then save to the db print(new_book.librarian.user.username) new_book.save() # Or... # Use a shortcut to do both at the same time # new_book = Book.objects.create( # title = form_data['title'], # author = form_data['author'], # isbn = form_data['isbn'], # year = form_data['year_published'], # location_id = request.user.librarian.id, # librarian_id = form_data["location"] # ) return redirect(reverse('libraryapp:books'))
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.isbn, b.author, b.year_published, b.librarian_id, b.location_id from libraryapp_book b """) all_books = [] dataset = db_cursor.fetchall() for row in dataset: book = Book() book.id = row['id'] book.title = row['title'] book.isbn = row['isbn'] book.author = row['author'] book.year_published = row['year_published'] book.librarian_id = row['librarian_id'] book.location_id = row['location_id'] all_books.append(book) # When a view wants to generate some HTML representations of data, it needs to specify a template to use. # The template variable is holding the path and filename of the template you just created. template = 'books/list.html' # the render() method is invoked. That method takes the HTTP request as the first argument, # the template to be used as the second argument, and then a dictionary containing the data to be used in the template. # In this case, the dictionary has a single property labeled all_books and its value is the list of book objects that the view generates. # The key name is able to be used in the template, which is why the template has the following loop. context = { '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_book ( title, author, isbn, year_published, location_id, librarian_id ) VALUES (?, ?, ?, ?, ?, ?) """, (form_data['title'], form_data['author'], form_data['isbn'], form_data['year_published'], form_data["location"], request.user.librarian.id)) return redirect(reverse('libraryapp:books'))