forked from vvoinarovych/LibraryManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (44 loc) · 2.42 KB
/
app.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
from flask import Flask
from database.database import db
from database.config import SQLALCHEMY_DATABASE_URI
from controller.routes import routes
from database.database import Book, User, BorrowRecord
from datetime import date
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
app.register_blueprint(routes)
from sqlalchemy import inspect
def init_db():
with app.app_context():
print("Starting to initialize the database...")
db.drop_all()
db.create_all()
inspector = inspect(db.engine)
if 'books' in inspector.get_table_names():
print("Table 'books' exists.")
else:
print("Table 'books' does not exist.")
user1 = User(name='Jan Kowalski', email='[email protected]')
user2 = User(name='Maria Nowak', email='[email protected]')
user3 = User(name='Piotr Wiśniewski', email='[email protected]')
db.session.add_all([user1, user2, user3])
db.session.commit()
book1 = Book(title='Programowanie w Pythonie dla opornych', author='Jan Kowalski', published_date=date(2022, 5, 10), available=False)
book2 = Book(title='Flask w 24 godziny: Przewodnik po kawie', author='Maria Nowak', published_date=date(2021, 8, 15), available=False)
book3 = Book(title='Jak zostać mistrzem w PHP', author='Zbigniew Lis', published_date=date(2019, 12, 1), available=False)
book4 = Book(title='Czysty kod: Bajka o programowaniu', author='Jan Kowalski', published_date=date(2020, 2, 20), available=True)
book5 = Book(title='Historia nieudanych start-upów', author='Maria Nowak', published_date=date(2023, 3, 18), available=True)
db.session.add_all([book1, book2, book3, book4, book5])
db.session.commit()
borrow_record1 = BorrowRecord(user_id=1, book_id=1, borrow_date=date(2025, 1, 4), return_date=None)
borrow_record2 = BorrowRecord(user_id=2, book_id=2, borrow_date=date(2025, 1, 4), return_date=None)
borrow_record3 = BorrowRecord(user_id=3, book_id=3, borrow_date=date(2025, 1, 4), return_date=None)
db.session.add_all([borrow_record1, borrow_record2, borrow_record3])
db.session.commit()
print("Test data has been added.")
if __name__ == '__main__':
print("Starting the Flask application...")
init_db()
app.run(debug=True)