-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
116 lines (88 loc) · 2.72 KB
/
models.py
File metadata and controls
116 lines (88 loc) · 2.72 KB
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
110
111
112
113
114
115
116
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
__tablename__='users'
id = db.Column(db.String(),nullable=False,primary_key=True)
username = db.Column(db.String(120),nullable=True)
reviews = db.relationship('Review',backref='user')
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def __repr__(self):
return f'<User : id={self.id} username:{self.username}>'
def format(self):
return ({
"id":self.id,
"username":self.username,
"reviews":self.reviews
})
class Book(db.Model):
__tablename__ = 'books'
id = db.Column(db.String, primary_key=True)
title = db.Column(db.String())
author = db.Column(db.String())
category = db.Column(db.String())
image_link = db.Column(db.String())
summary =db.Column(db.Text)
reviews = db.relationship('Review',backref='book')
def __init__(self,id,title,author,category,summary,image_link):
self.id = id
self.title=title
self.author=author
self.category=category
self.summary=summary
self.image_link=image_link
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def __repr__(self):
return f'<Book : id={self.id} title:{self.title} author:{self.author}>'
def format(self):
return ({
"id":self.id,
"title":self.title,
"author":self.author,
"category":self.category,
"summary":self.summary,
"image_link":self.image_link
})
class Review(db.Model):
__tablename__ = 'reviews'
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.DateTime)
edited = db.Column(db.DateTime)
title = db.Column(db.String(120))
comment = db.Column(db.Text)
rating = db.Column(db.Integer)
user_id = db.Column(db.String,db.ForeignKey('users.id'))
book_id= db.Column(db.String,db.ForeignKey('books.id'))
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def __repr__(self):
return f'<Review : id={self.id} title:{self.title} comment:{self.comment}>'
def format(self):
return ({
"id":self.id,
"created":self.created,
"edited":self.edited,
"comment":self.comment,
"rating":self.rating,
"user_id":self.user_id,
"book_id":self.book_id,
})