Пример #1
0
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
                li.id,
                li.title,
                li.address
            from libraryapp_library li
            """)

            all_libraries = []
            dataset = db_cursor.fetchall()

            for row in dataset:
                library = Library()
                library.id = row['id']
                library.title = row['title']
                library.address = row['address']

                all_libraries.append(library)

        template = 'libraries/list.html'
        context = {'all_libraries': all_libraries}

        return render(request, template, context)
def book_details(request, book_id):
    if request.method == 'GET':
        book = get_book(book_id)
        template_name = 'books/detail.html'
        return render(request, template_name, {'book': book})

    elif request.method == 'POST':
        form_data = request.POST

        # Check if this POST is for editing a book
        if ("actual_method" in form_data
                and form_data["actual_method"] == "PUT"):
            book = Book.objects.get(pk=book_id)
            book.title = form_data['title']
            book.author = form_data['author']
            book.ISBN_num = form_data['ISBN_num']
            book.year_published = form_data['year_published']

            library = Library()
            library.id = form_data['location']
            book.location = library

            book.save()

            return redirect(reverse('libraryapp:books'))

        # Check if this POST is for deleting a book
        if ("actual_method" in form_data
                and form_data["actual_method"] == "DELETE"):
            book = Book.objects.get(pk=book_id)
            book.delete()

            return redirect(reverse('libraryapp:books'))
Пример #3
0
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)
Пример #4
0
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.id,
            l.title,
            l.address
            FROM libraryapp_library l;
        """)

        all_libraries = []
        dataset = db_cursor.fetchall()

        for row in dataset:
            lib = Library()
            lib.id = row["id"]
            lib.title = row["title"]
            lib.address = row["address"]

            all_libraries.append(lib)

    template_name = 'libraries/list.html'

    context = {'all_libraries': all_libraries}

    return render(request, template_name, context)
Пример #5
0
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'))
Пример #6
0
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.title,
                l.address
            from libraryapp_library l
            """)

            all_libraries = []
            dataset = db_cursor.fetchall()

            for row in dataset:
                lib = Library()
                lib.id = row["id"]
                lib.title = row["title"]
                lib.address = row["address"]

                all_libraries.append(lib)

        template_name = 'libraries/list.html'

        context = {
            'all_libraries': all_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
            (
                title,
                address
            )
            VALUES (?, ?)
            """,
            (form_data['title'], form_data['address']))

        return redirect(reverse('libraryapp:libraries'))
Пример #7
0
def create_librarian(cursor, row):
    _row = sqlite3.Row(cursor, row)

    librarian = Librarian()
    librarian.id = _row["librarian_id"]
    librarian.first_name = _row["first_name"]
    librarian.last_name = _row["last_name"]
    librarian.location = _row["location_id"]
    librarian.username = _row["username"]

    library = Library()
    library.id = _row["library_id"]
    library.title = _row["library_name"]
    library.address = _row["library_address"]

    librarian.library = library

    return librarian
Пример #8
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"]

    # 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"]

    # Return a tuple containing the library and the
    # book built from the data in the current row of
    # the data set
    return (
        library,
        book,
    )
Пример #9
0
def get_libraries():
    with sqlite3.connect(Connection.db_path) as conn:
        conn.row_factory = sqlite3.Row

        db_cursor = conn.cursor()
        db_cursor.execute("""
        SELECT id, title, address
        FROM libraryapp_library
        """)

        all_libraries = []
        dataset = db_cursor.fetchall()

        for row in dataset:
            library = Library()
            library.id = row['id']
            library.title = row['title']
            library.address = row['address']

            all_libraries.append(library)

        return all_libraries
Пример #10
0
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
Пример #11
0
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
Пример #12
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_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
Пример #13
0
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,
    )
Пример #14
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["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)
Пример #15
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,
    )
Пример #16
0
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'))
Пример #17
0
def library_list(request):
    if request.method == 'GET':
        ## SQL METHOD
        # with sqlite3.connect(Connection.db_path) as conn:
        #     conn.row_factory = create_library
        #     db_cursor = conn.cursor()

        #     db_cursor.execute("""
        #         SELECT
        #             li.id,
        #             li.title,
        #             li.address,
        #             b.id book_id,
        #             b.title book_title,
        #             b.author,
        #             b.year_published,
        #             b.isbn
        #         FROM libraryapp_library li
        #         JOIN libraryapp_book b ON li.id = b.location_id
        #     """)

        #     libraries = db_cursor.fetchall()
        
        ## ORM METHOD
        libraries = Library.objects.all()
        
        for library in libraries:
            library.books = Book.objects.filter(location_id=library.id)

            # # SQL METHOD   
            # # 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)

        template = 'libraries/list.html'
        context = {
            # 'all_libraries': library_groups.values()
            'all_libraries': libraries
        }

        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_library
        #     (
        #         title, address
        #     )
        #     VALUES (?, ?)
        #     """,
        #     (form_data['title'], form_data['address']))
        
        # # ORM METHOD
        new_library = Library(
            title =  form_data['title'], 
            address = form_data['address']
        )
        new_library.save()

        return redirect(reverse('libraryapp:libraries'))

## SQL METHOD    
# 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.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,)