-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
346 lines (271 loc) · 7.08 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""SQLAlchemy models for ShareBNB."""
from datetime import datetime
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
bcrypt = Bcrypt()
db = SQLAlchemy()
# TODO: USER DEFAULT IMAGE URL
DEFAULT_USER_IMAGE_URL = "testimage.jpg"
# TODO: POOL DEFAULT IMAGE URL
DEFAULT_POOL_IMAGE_URL = ""
# USERS
class User(db.Model):
"""User in the system."""
__tablename__ = 'users'
username = db.Column(
db.Text,
nullable=False,
unique=True,
primary_key=True
)
image_url = db.Column(
db.Text,
)
email = db.Column(
db.Text,
nullable=False,
unique=True,
)
location = db.Column(
db.Text,
)
password = db.Column(
db.Text,
nullable=False,
)
# reserved_pools = db.relationship(
# 'Pools',
# secondary='owner',
# backref='booker'
# )
# owned_pools = db.relationship(
# 'Pools',
# secondary='booker',
# backref='owner'
# )
def serialize(self):
""" returns self """
return {
"username" : self.username,
"email" : self.email,
"location" : self.location,
"image_url" : self.image_url,
# "reserved_pools" : self.reserved_pools,
# "owned_pools" : self.owned_pools
}
@classmethod
def signup(cls, username, email, password, location, image_url=DEFAULT_USER_IMAGE_URL):
"""Sign up user.
Hashes password and adds user to system.
"""
hashed_pwd = bcrypt.generate_password_hash(password).decode('UTF-8')
user = User(
username=username,
email=email,
password=hashed_pwd,
image_url=image_url,
location=location
)
db.session.add(user)
return user
@classmethod
def authenticate(cls, username, password):
"""Find user with `username` and `password`.
This is a class method (call it on the class, not an individual user.)
It searches for a user whose password hash matches this password
and, if it finds such a user, returns that user object.
If this can't find matching user (or if password is wrong), returns
False.
"""
user = cls.query.filter_by(username=username).first()
if user:
is_auth = bcrypt.check_password_hash(user.password, password)
if is_auth:
return user
return False
def __repr__(self):
return f"<User #{self.username}, {self.email}>"
# Messages
class Message(db.Model):
"Messages between users in the system"
__tablename__ = "messages"
id = db.Column(
db.Integer,
primary_key=True
)
# userid to
sender_username = db.Column(
db.Text,
db.ForeignKey('users.username'),
nullable=False
)
# userid from
recipient_username = db.Column(
db.Text,
db.ForeignKey('users.username'),
nullable=False
)
# text
title = db.Column(
db.Text,
# nullable=False,
)
# text
body = db.Column(
db.Text,
nullable=False,
)
#listing message is associated with
listing = db.Column(
db.Integer,
nullable=False,
)
# timestamp
timestamp = db.Column(
db.DateTime,
nullable=False,
default=datetime.utcnow,
)
def serialize(self):
""" returns self """
return {
"id" : self.id,
"sender_username" : self.sender_username,
"recipient_username" : self.recipient_username,
"body" : self.body,
"title" : self.title,
"listing" : self.listing,
"timestamp" : self.timestamp
}
# POOLS
class Pool(db.Model):
""" Pool in the system """
__tablename__ = 'pools'
id = db.Column(
db.Integer,
primary_key=True,
)
owner_username = db.Column(
db.Text,
db.ForeignKey("users.username"),
nullable=False,
)
rate = db.Column(
db.Numeric(10,2),
nullable=False
)
size = db.Column(
db.Text,
nullable=False,
)
description = db.Column(
db.Text,
nullable=False,
)
city = db.Column(
db.Text,
nullable=False,
)
orig_image_url = db.Column(
db.Text,
nullable=False,
)
small_image_url = db.Column(
db.Text,
nullable=False,
)
def serialize(self):
""" returns self """
return {
"id" : self.id,
"owner_username" : self.owner_username,
"rate" : self.rate,
"size" : self.size,
"description" : self.description,
"city" : self.city,
"orig_image_url": self.orig_image_url,
"small_image_url": self.small_image_url
}
class Reservation(db.Model):
""" Connection of a User and Pool that they reserve """
__tablename__ = "reservations"
id = db.Column(
db.Integer,
primary_key=True
)
booked_username = db.Column(
db.Text,
db.ForeignKey("users.username", ondelete="CASCADE"),
)
pool_id = db.Column(
db.Integer,
db.ForeignKey("pools.id", ondelete="CASCADE"),
)
reservation_date_created = db.Column(
db.DateTime,
nullable=False,
default=datetime.utcnow,
)
start_date = db.Column(
db.DateTime,
nullable=False,
)
end_date = db.Column(
db.DateTime,
nullable=False,
)
def serialize(self):
""" returns self """
return {
"id" : self.id,
"username" : self.booked_username,
"pool_id" : self.pool_id,
"reservation_date_created" : self.reservation_date_created,
"start_date" : self.start_date,
"end_date" : self.end_date,
}
class UserImage(db.Model):
""" Connection from the user to their profile images. """
__tablename__ = "user_images"
id = db.Column(
db.Integer,
primary_key=True
)
username = db.Column(
db.Text,
db.ForeignKey("users.username", ondelete="CASCADE"),
)
image_path = db.Column(
db.Text,
nullable = False
)
class PoolImage(db.Model):
""" One to many table connecting a pool to many image paths """
__tablename__ = "pool_images"
id = db.Column(
db.Integer,
primary_key=True
)
pool_owner = db.Column(
db.Text,
db.ForeignKey("users.username", ondelete="CASCADE"),
)
image_url = db.Column(
db.Text,
nullable = False
)
def serialize(self):
""" returns self """
return {
"id" : self.id,
"pool_owner" : self.pool_owner,
"image_url" : self.image_url,
}
# db
def connect_db(app):
"""Connect this database to provided Flask app.
You should call this in your Flask app.
"""
app.app_context().push()
db.app = app
db.init_app(app)