コード例 #1
0
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'

            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_number, year, location_id, librarian_id)
            VALUES (?,?,?,?,?,?)""",
                (form_data['title'], form_data['author'],
                 form_data['isbn_number'], form_data['year'],
                 form_data["location"], request.user.librarian.id))

        return redirect(reverse('libraryapp:books'))
コード例 #2
0
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,
    )
コード例 #3
0
def book_list(request):
    if request.method == 'GET':
        with sqlite3.connect(Connection.db_path) as conn:
            # conn.row_factory = model_factory(Book)
            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)

        template = 'books/list.html'
        context = {'all_books': all_books}

        return render(request, template, context)
コード例 #4
0
def create_book(cursor, row):
    _row = sqlite3.Row(cursor,row)

    book = Book()
    book.id = _row["book_id"]
    book.author = _row["author"]
    book.isbn_number = _row["isbn_number"]
    book.title = _row["title"]
    book.year = _row["year"]

    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
コード例 #5
0
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)