-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbookmanager.py
103 lines (86 loc) · 2.58 KB
/
bookmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
DATABASE_FILE = "sqlite:///{}".format(
os.path.join(
PROJECT_DIR,
"book_database.sqlite3"
)
)
app = Flask(__name__, static_folder='static')
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_FILE
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
class Base(DeclarativeBase):
"""DeclarativeBase is a class that is used to create
a new base class for declarative class definitions.
Args:
DeclarativeBase (class): A class that is used to create
"""
pass
# SQLAlchemy is an ORM (Object Relational Mapper) that allows
# you to interact with databases using Python objects.
db = SQLAlchemy(app, model_class=Base)
class Book(db.Model):
"""Book model
Args:
db (class): An instance of SQLAlchemy
Returns:
class: A class that represents a book model
"""
id: Mapped[int] = mapped_column(
db.Integer,
primary_key=True
)
title: Mapped[str] = mapped_column(
db.String(80),
unique=True,
nullable=False
)
def __repr__(self):
return "<Title: {}>".format(self.title)
# Home page route
@app.route("/", methods=["GET", "POST"])
def home():
books = None
if request.form:
try:
book = Book(title=request.form.get("title"))
db.session.add(book)
db.session.commit()
except Exception as e:
print("Failed to add book")
print(e)
# Get all books
books = Book.query.all()
return render_template("home.html", books=books)
# Update book title route
@app.route("/update", methods=["POST"])
def update():
try:
new_title = request.form.get("new_title")
old_title = request.form.get("old_title")
book = Book.query.filter_by(title=old_title).first()
book.title = new_title
db.session.commit()
except Exception as e:
print("Couldn't update book title")
print(e)
return redirect("/")
# Delete book route
@app.route("/delete", methods=["POST"])
def delete():
title = request.form.get("title")
book = Book.query.filter_by(title=title).first()
db.session.delete(book)
db.session.commit()
return redirect("/")
with app.app_context():
# Create tables
db.create_all()
# Run the app
app.run(host='0.0.0.0', port=8087, debug=True)