app.library package

Submodules

app.library.forms module

library/forms.py

Forms module for the Library Blueprint.

This module defines Flask forms for the Library Blueprint using Flask-WTF and WTForms. It includes a form called AddBookForm for adding new books to the library database.

Classes:
  • AddBookForm: Form for adding new books with fields for title, author, ISBN, category, and a submit button.

Usage:

This form can be used in Flask routes within the Library Blueprint to handle the addition of new books.

class app.library.forms.AddBookForm

Bases: FlaskForm

Add form for adding new data.

author = <UnboundField(StringField, ('Author',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>
category = <UnboundField(StringField, ('Category',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>
isbn = <UnboundField(StringField, ('ISBN',), {'validators': [<wtforms.validators.Optional object>, <wtforms.validators.Length object>]})>
submit = <UnboundField(SubmitField, ('Add Book',), {})>
title = <UnboundField(StringField, ('Title',), {'validators': [<wtforms.validators.InputRequired object>, <wtforms.validators.Length object>]})>

app.library.models module

library/models.py

Models module for the Library Blueprint.

This module defines the database model for the Library Blueprint using SQLAlchemy. It includes a class called Book, representing the Book model in the database.

Classes:
  • Book: Model class for representing books in the library database.

- id

Primary key identifier for the book.

Type:

int

- title

Title of the book.

Type:

str

- author

Author of the book.

Type:

str

- isbn

ISBN of the book (nullable).

Type:

str

- category

Category of the book.

Type:

str

Usage:

This class is used to interact with the database and manage book-related data.

Example

from app.library.models import Book

# Example query to retrieve all books all_books = Book.query.all()

# Example query to add a new book to the database new_book = Book(title=’Example Book’, author=’John Doe’, category=’Fiction’) db.session.add(new_book) db.session.commit()

class app.library.models.Book

Bases: Model

This is the book class in my app

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

author
category
id
isbn
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

title

app.library.routes module

library/routes.py

Routes module for the Library Blueprint.

This module defines routes for the Library Blueprint, including the main library catalogue page, CRUD operations for managing books, and additional functionalities.

Routes:
  • /library: Library catalogue page displaying a list of books.

  • /library/add_book: Route to add a new book to the catalogue.

  • /library/view_book/<int:book_id>: Route to view details of a specific book.

  • /library/update_book/<int:book_id>: Route to update book information.

  • /library/delete_book/<int:book_id>: Route to delete a book from the catalogue.

Functions:
  • CRUD: Create, Read, Update, Delete / RESTful

  • library: Displays the main library catalogue page.

  • add_book: Adds a new book to the catalogue.

  • view_book: Displays details of a specific book.

  • update_book: Updates information for a specific book.

  • delete_book: Deletes a book from the catalogue.

Usage:

These routes can be used in Flask applications to handle library-related functionalities.

Example

from app.library.routes import catalogue

# Register the Library Blueprint in the main Flask application app.register_blueprint(catalogue, url_prefix=’/library’)

app.library.routes.add_book()

Route to add a new book.

This route handles the addition of a new book to the library catalogue. It requires authentication and uses the ‘AddBookForm’ for input validation.

- GET

Render the ‘add_book.html’ template with the book addition form.

- POST

Validate the form data, add the new book to the database, and redirect to the main library page.

Returns:

Renders the ‘add_book.html’ template for GET requests. Redirects to the main library page after successfully adding a new book for POST requests.

Return type:

render_template or redirect

Usage:

Access this route via ‘/library/add_book’ to add a new book to the library.

Example

  1. Access ‘/library/add_book’ via GET to view the book addition form.

  2. Submit the form with valid data to add a new book and be redirected to the main library page.

app.library.routes.backup_database()

This Function will create a database backup in backup folder

app.library.routes.delete_book(book_id)

Route to delete a book.

This route allows the user to delete a specific book from the library catalogue. It takes the book_id as a parameter to identify the book.

Parameters:

book_id (-) – The unique identifier of the book to delete.

Returns:

Redirects to the library catalogue page after successfully deleting the book.

Return type:

Redirect

Usage:

Access this route via ‘/library/delete_book/<book_id>’ to delete a specific book.

Example

Access ‘/library/delete_book/1’ to delete the book with ID 1.

app.library.routes.library()

Library Catalogue Page.

This route displays the main library catalogue page, showing a list of books. It supports optional filtering by category using query parameters.

Parameters:

category (-) – Filter books by category.

Returns:

Renders the ‘library.html’ template with the list of books and categories.

Return type:

render_template

Usage:

Access this route via ‘/library’ to view the main library catalogue page. Optionally, append ‘?category=<category_name>’ to filter books by a specific category.

Example

To view all books: ‘/library’ To view books in the ‘Fiction’ category: ‘/library?category=Fiction’

app.library.routes.update_book(book_id)

Route to update book information.

This route allows the user to update the information of a specific book in the library catalogue. It takes the book_id as a parameter to identify the book.

Parameters:

book_id (-) – The unique identifier of the book to update.

Returns:

Renders the ‘update_book.html’ template with the current details of the book. Redirect: Redirects to the library catalogue page after successfully updating the book.

Return type:

render_template

Usage:

Access this route via ‘/library/update_book/<book_id>’ to update details of a specific book.

Example

Access ‘/library/update_book/1’ to update details of the book with ID 1.

app.library.routes.view_book(book_id)

Route to view details of a specific book.

This route displays detailed information about a specific book from the library catalogue. It takes the book_id as a parameter to identify the book.

Parameters:

book_id (-) – The unique identifier of the book to view.

Returns:

Renders the ‘view_book.html’ template with details of the specified book.

Return type:

render_template

Usage:

Access this route via ‘/library/view_book/<book_id>’ to view details of a specific book.

Example

Access ‘/library/view_book/1’ to view details of the book with ID 1.

Module contents

Library Module for Cataloguing Books.

This module defines the library functionality of the application, including routes, models, and forms for managing a book catalogue.

Classes:
  • AddBookForm: Form for adding new books to the catalogue.

  • Book: Model class representing a book in the catalogue.

Routes:
  • library: Blueprint for managing library-related routes, including displaying, adding, viewing, updating, and deleting books.

Usage:

This module is part of the Flask application and handles book-related functionality.