-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
159 lines (104 loc) · 5.23 KB
/
model.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
""" Models and databse functions for Booklist App """
from flask_sqlalchemy import SQLAlchemy
# This is the connection to the PostgreSQL database; we're getting
# this through the Flask-SQLAlchemy helper library. On this, we can
# find the `session` object, where we do most of our interactions
# (like committing, etc.)
db = SQLAlchemy()
#####################################################################
# Model definitions
class User(db.Model):
""" User of booklist app. """
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(60), nullable=False)
last = db.Column(db.String(60), nullable=False)
email = db.Column(db.String(60), nullable=True)
password = db.Column(db.String(60), nullable=True)
goodreads_id = db.Column(db.Integer, nullable=True)
def __repr__(self):
"""Helpful representation when printed."""
return "<User user_id=%s name=%s> email=%s" % (self.user_id,
self.name, self.email)
class Lista(db.Model):
"""List belonging to each user."""
__tablename__ ="lists"
list_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
list_name = db.Column(db.String(60), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
#define relationship to user
user = db.relationship("User", backref=db.backref("lists",order_by=list_id))
def __repr__(self):
"""Provide helpful representation when printed."""
m = "<Lista list_id=%s list_name=%s user_id=%s>"
return m %(self.list_id, self.list_name, self.user_id)
class List_Book(db.Model):
"""Association table connects lists table to books table"""
__tablename__ = "list_books"
list_book_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
list_id = db.Column(db.Integer, db.ForeignKey('lists.list_id'))
book_id = db.Column(db.Integer, db.ForeignKey('books.book_id'))
sequence = db.Column(db.Integer, nullable=True)
book_read = db.Column(db.Boolean, nullable=False, default=False)
#define relationships to Book
book = db.relationship("Book", backref=db.backref("list_books",order_by=list_book_id))
#define relationships to Lista
lista = db.relationship("Lista", backref=db.backref("list_books",order_by=list_book_id))
def __repr__(self):
"""Provide helpful representation when printed."""
m = "<List_Book list_book_id=%s list_id=%s book_id=%s>"
return m %(self.list_book_id, self.list_id, self.book_id)
class Book(db.Model):
"""Book on the booklist app."""
__tablename__ = "books"
book_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
book_title = db.Column(db.String (200), nullable=True)
book_author = db.Column(db.String (200), nullable=True)
book_author_2 = db.Column(db.String(200), nullable=True)
book_cover = db.Column(db.String(300), nullable=True)
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
def __repr__(self):
"""Provide helpful representation when printed."""
m = "<Book book_id=%s book_title=%s book_author=%s book_cover=%s >"
return m %(self.book_id, self.book_title, self.book_author, self.book_cover)
class PL_Book(db.Model):
""" Association table connects public_list_books table to books table."""
__tablename__ = "pl_books"
pl_book_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
book_id = db.Column(db.Integer, db.ForeignKey('books.book_id'))
pl_id = db.Column(db.Integer,db.ForeignKey('public_lists.pl_id'))
order = db.Column(db.Integer, nullable=True)
def __repr__(self):
"""Provide helpful representation when printed."""
m = "<PL_Book pl_book_id=%s book_id=%s pl_id=%s order=%s >"
return m %(self.pl_book_id, self.book_id, self.pl_id, self.order)
#define relationships to Book
book = db.relationship("Book", backref=db.backref("pl_books",order_by=pl_book_id))
#define relationships to Public_List
public_list = db.relationship("Public_List", backref=db.backref("public_lists",order_by=pl_book_id))
class Public_List(db.Model):
""" Public list on the booklist app."""
__tablename__ = "public_lists"
pl_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
pl_name = db.Column(db.String(200),nullable=False)
def __repr__(self):
"""Provide helpful representation when printed."""
m = "<Public_List pl_id=%s pl_name=%s >"
return m %(self.pl_id, self.pl_name)
#####################################################################
# Helper functions
def connect_to_db(app, dburi='postgresql:///booklist'):
""" Connect the database to my Flask app."""
#Configure to use PostgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = dburi
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if I run this module interactively, it will
# leave me in a state of being able to work with the database
# directly.
from server import app
connect_to_db(app)
print "Connected to DB."