-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
167 lines (127 loc) · 3.27 KB
/
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
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
159
160
161
162
163
164
165
166
167
"""SQLAlchemy models for shareBnB."""
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def connect_db(app):
"""Connect to database."""
app.app_context().push()
db.app = app
db.init_app(app)
class Property(db.Model):
"""Current properties in the system. """
__tablename__ = 'properties'
id = db.Column(
db.Integer,
primary_key=True,
)
name = db.Column(
db.String(50),
nullable=False,
)
description = db.Column(
db.Text,
nullable=False,
default="",
)
address = db.Column(
db.String(50),
nullable=False,
)
price = db.Column(
db.Integer,
nullable=False,
)
backyard = db.Column(
db.Boolean,
nullable=False,
default=False,
)
pool = db.Column(
db.Boolean,
nullable=False,
default=False,
)
user_id = db.Column(
db.Integer,
db.ForeignKey('users.id', ondelete='CASCADE'),
nullable=False,
)
images = db.relationship('Image', backref="property")
# backref'ed in User Model
# user = db.relationship('Property', backref="properties")
def serialize(self):
"""Serialize property to a dict of property info."""
return {
"id": self.id,
"name": self.name,
"description": self.description,
"address": self.address,
"price": self.price,
"backyard": self.backyard,
"pool": self.pool,
"user_id": self.user_id,
"images": [image.serialize() for image in self.images],
}
class User(db.Model):
"""Current users in the system. """
__tablename__ = 'users'
id = db.Column(
db.Integer,
primary_key=True,
)
username = db.Column(
db.String(30),
unique=True,
nullable=False,
)
first_name = db.Column(
db.String(30),
nullable=False,
)
last_name = db.Column(
db.String(50),
nullable=False,
)
email = db.Column(
db.String(50),
nullable=False,
)
properties = db.relationship('Property', backref="user")
def serialize(self):
"""Serialize property to a dict of property info."""
return {
"id": self.id,
"username": self.username,
"first_name": self.first_name,
"last_name": self.last_name,
"email": self.email,
}
class Image(db.Model):
"""Current images for properties in the system."""
__tablename__ = 'images'
id = db.Column(
db.Integer,
primary_key=True,
)
property_id = db.Column(
db.Integer,
db.ForeignKey('properties.id', ondelete='CASCADE'),
nullable=False,
)
aws_key = db.Column(
db.String,
nullable=False
)
url = db.Column(
db.String,
nullable=False,
)
# backref'd in Property Model
# property = db.relationship('Property', backref="images")
def serialize(self):
"""Serialize property to a dict of property info."""
return {
"id": self.id,
"property_id": self.property_id,
"aws_key": self.aws_key,
"url":self.url,
}