-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
272 lines (211 loc) · 8.34 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
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
"""Models for Restaurant Meetup app."""
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import json
db = SQLAlchemy()
# Association Tables
favorites = db.Table('favorites',
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('restaurant_id', db.String, db.ForeignKey('restaurants.id'))
)
user_meetups = db.Table('user_meetups',
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('meetup_id', db.Integer, db.ForeignKey('meetups.id'))
)
class User(db.Model):
"""A user."""
__tablename__ = 'users'
# TODO: come back and make nullable=False
id = db.Column(db.Integer, autoincrement=True,
primary_key=True)
username = db.Column(db.String) #unique=True
fname = db.Column(db.String)
lname = db.Column(db.String)
email = db.Column(db.String, unique=True)
password_hash = db.Column(db.String)
image_url = db.Column(db.String, default='/v1615521119/users/kawaiiavocado_ey59sf.png')
about = db.Column(db.Text)
favorites = db.relationship('Restaurant', secondary=favorites,
backref='fans')
# hosted_meetups = list of Meetup objects hosted by this user
hosted_meetups = db.relationship('Meetup', order_by='Meetup.date')
meetups = db.relationship('Meetup', secondary=user_meetups,
backref='attendees', order_by='Meetup.date')
messages_sent = db.relationship('Message', foreign_keys='Message.sender_id',
backref='sender')
messages_received = db.relationship('Message', foreign_keys='Message.recipient_id',
backref='recipient')
notifications = db.relationship('Notification', backref='user')
def __repr__(self):
return f'<User {self.fname} {self.lname}>'
def to_dict(self, include_email=False):
data = {
'id': self.id,
'username': self.username,
'fname': self.fname,
'lname': self.lname,
'image_url': self.image_url,
'about': self.about
# TODO: link to favorites and meetups
}
# default: don't send user email back unless toldß
if include_email:
data['email'] = self.email
return data
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class Message(db.Model):
"""A message."""
__tablename__ = 'messages'
id = db.Column(db.Integer, autoincrement=True,
primary_key=True)
sender_id = db.Column(db.Integer, db.ForeignKey('users.id'))
recipient_id = db.Column(db.Integer, db.ForeignKey('users.id'))
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
# sender = user who wrote the message
# recipient = user who received the message
def __repr__(self):
return f'<Message from {self.sender_id} to {self.recipient_id}>'
def to_dict(self):
data = {
'id': self.id,
'sender_id': self.sender_id,
'recipient_id': self.recipient_id,
'body': self.body,
'timestamp': self.timestamp,
'sender': self.sender.to_dict(),
'recipient': self.recipient.to_dict()
}
return data
class Notification(db.Model):
"""A notification."""
__tablename__ = 'notifications'
id = db.Column(db.Integer, autoincrement=True,
primary_key=True)
name = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
payload_json = db.Column(db.Text)
# 'READ', 'UNREAD', 'DELETED'
status = db.Column(db.String(10), default='UNREAD')
# user = user this notification is for
def __repr__(self):
return f'<Notification {self.name} for User {self.user_id}>'
def get_data(self):
return json.loads(str(self.payload_json))
def to_dict(self):
data = {
'id': self.id,
'name': self.name,
'status': self.status,
'timestamp': self.timestamp,
'data': self.get_data()
}
return data
class Restaurant(db.Model):
"""A restaurant."""
__tablename__ = 'restaurants'
# TODO: come back and make nullable=False
id = db.Column(db.String, primary_key=True)
name = db.Column(db.String)
cuisine = db.Column(db.String)
address = db.Column(db.String)
longitude = db.Column(db.Float)
latitude = db.Column(db.Float)
image_url = db.Column(db.String)
# meetups = list of Meetup objects at this restaurant
# fans = list of User objects favoriting this restaurant
def __repr__(self):
return f'<Restaurant {self.name}>'
def to_dict(self):
data = {
'id': self.id,
'name': self.name,
'cuisine': self.cuisine,
'address': self.address,
'long': self.longitude,
'lat': self.latitude,
'image_url': self.image_url
}
return data
class Meetup(db.Model):
"""A meetup."""
__tablename__ = 'meetups'
# TODO: come back and make nullable=False
id = db.Column(db.Integer, autoincrement=True,
primary_key=True)
name = db.Column(db.String)
date = db.Column(db.DateTime)
capacity = db.Column(db.Integer)
attendees_count = db.Column(db.Integer)
description = db.Column(db.Text)
image_url = db.Column(db.String, default='/v1615521225/avocadopattern_ddagxx.jpg')
# 'CANCELLED' 'ACTIVE'
status = db.Column(db.String(10), default='ACTIVE')
restaurant_id = db.Column(db.String,
db.ForeignKey('restaurants.id'))
host_id = db.Column(db.Integer,
db.ForeignKey('users.id'))
host = db.relationship('User')
restaurant = db.relationship('Restaurant', backref="meetups")
# attendees = list of User objects attending
comments = db.relationship('Comment', backref='meetup',
order_by='Comment.timestamp')
def __repr__(self):
return f'<Meetup {self.name} at {self.restaurant.name}>'
def to_dict(self):
data = {
'id': self.id,
'name': self.name,
'date': self.date,
'capacity': self.capacity,
'attendees_count': self.attendees_count,
'description': self.description,
'status': self.status,
'image_url': self.image_url,
'restaurant_id': self.restaurant_id,
'host_id': self.host_id,
'host': self.host.to_dict(),
'restaurant': self.restaurant.to_dict()
# TODO: link to restaurant and host
}
return data
class Comment(db.Model):
"""A meetup comment."""
__tablename__ = 'comments'
id = db.Column(db.Integer, autoincrement=True,
primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
meetup_id = db.Column(db.Integer, db.ForeignKey('meetups.id'))
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
text = db.Column(db.Text)
writer = db.relationship('User')
# meetup = Meetup this comment is for
def __repr__(self):
return f'<Comment on Meetup {self.meetup_id} by User {self.user_id}>'
def to_dict(self):
data = {
'id': self.id,
'text': self.text,
'timestamp': self.timestamp,
'writer': self.writer.to_dict(),
'meetup_id': self.meetup_id
}
return data
def connect_to_db(flask_app, db_uri='postgresql:///restaurants', echo=False):
flask_app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
flask_app.config['SQLALCHEMY_ECHO'] = echo
flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = flask_app
db.init_app(flask_app)
print('Connected to the db!')
if __name__ == '__main__':
from server import app
# Call connect_to_db(app, echo=False) if your program output gets
# too annoying; this will tell SQLAlchemy not to print out every
# query it executes.
connect_to_db(app)