-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_models.py
109 lines (91 loc) · 3.36 KB
/
data_models.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
104
105
106
107
108
109
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class Author(db.Model):
"""
Represents an author in the library database.
Attributes:
id (int): Primary key, unique identifier for each author.
name (str): Name of the author, cannot be null.
birth_date (date): Birth date of the author, cannot be null.
date_of_death (date): Date of death of the author, can be null.
"""
__tablename__ = 'authors'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(100), nullable=False)
birth_date = db.Column(db.Date, nullable=False)
date_of_death = db.Column(db.Date, nullable=True)
def __init__(self,
name,
birth_date,
date_of_death=None):
"""
Initializes an Author instance.
"""
self.name = name
self.birth_date = birth_date
self.date_of_death = date_of_death
def __repr__(self):
"""
Provides a string representation of an Author instance for debugging.
Returns:
str: String representation of the Author.
"""
return f"<Author {self.name}>"
def __str__(self):
"""
Provides a string representation of an Author instance for display.
Returns:
str: Name of the Author.
"""
return self.name
class Book(db.Model):
"""
Represents a book in the library database.
Attributes:
id (int): Primary key, unique identifier for each book.
isbn (str): International Standard Book Number, unique for each book,
cannot be null.
title (str): Title of the book, cannot be null.
publication_year (int): Year the book was published, cannot be null.
author_id (int): Foreign key linking to the author of the book,
cannot be null.
author (Author): Relationship to the Author model.
rating (int): Rating of the book, can be null.
"""
__tablename__ = 'books'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
isbn = db.Column(db.String(13), unique=True, nullable=False)
title = db.Column(db.String(200), nullable=False)
publication_year = db.Column(db.Integer, nullable=False)
author_id = db.Column(db.Integer, db.ForeignKey('authors.id'), nullable=False)
rating = db.Column(db.Integer, nullable=True) # Add this line
author = db.relationship('Author', backref=db.backref('books', lazy=True))
def __init__(self,
isbn,
title,
publication_year,
author_id,
rating=None):
"""
Initializes a Book instance.
"""
self.isbn = isbn
self.title = title
self.publication_year = publication_year
self.author_id = author_id
self.rating = rating # Add this line
def __repr__(self):
"""
Provides a string representation of a Book instance for debugging.
Returns:
str: String representation of the Book.
"""
return f"<Book {self.title} (ISBN: {self.isbn})>"
def __str__(self):
"""
Provides a string representation of a Book instance for display.
Returns:
str: Title of the Book.
"""
return self.title