-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
executable file
·235 lines (178 loc) · 4.78 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
"""SQLAlchemy models."""
import os
import datetime
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.types import Boolean
try:
from API_KEYS import GEO_KEY
except ModuleNotFoundError:
GEO_KEY = os.environ['GEO_KEY']
bcrypt = Bcrypt()
db = SQLAlchemy()
class User(db.Model):
"""User in the system."""
__tablename__ = 'users'
id = db.Column(
db.Integer,
primary_key=True,
)
email = db.Column(
db.Text,
nullable=False
)
username = db.Column(
db.Text,
nullable=False,
unique=True,
)
bio = db.Column(
db.Text,
)
longitude = db.Column(
db.Float,
)
latitude = db.Column(
db.Float,
)
location = db.Column(
db.Text
)
password = db.Column(
db.Text,
nullable=False,
)
def get_location_coords(self, address):
import googlemaps
gmaps = googlemaps.Client(key=GEO_KEY)
# Geocoding address
location = gmaps.geocode(address)[0]
self.location = location['formatted_address']
coordinates=location['geometry']['location']
self.longitude=round(coordinates['lng'],5)
self.latitude=round(coordinates['lat'],5)
return self.longitude, self.latitude
@classmethod
def signup(cls, username, email, password):
"""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
)
db.session.add(user)
return user
@classmethod
def password_season(cls, password):
hashed_pwd = bcrypt.generate_password_hash(password).decode('UTF-8')
return hashed_pwd
@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 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
class Discovery(db.Model):
"""Connection of a follower <-> followed_user."""
__tablename__ = 'discoveries'
__table_args__ = (
db.PrimaryKeyConstraint('user_id', 'business_id'),
)
user_id = db.Column(
db.Integer,
db.ForeignKey('users.id',ondelete="cascade")
)
business_id = db.Column(
db.Integer,
db.ForeignKey('businesses.id',ondelete="cascade")
)
favorite = db.Column(
db.Boolean,
default=False
)
timestamp = db.Column(
db.DateTime,
nullable=False,
default=datetime.datetime.now()
)
notes = db.Column(
db.Text,
default="No notes yet"
)
user= db.relationship(
"User",
cascade = "all,delete",
backref="discoveries"
)
class Business(db.Model):
"""Mapping user likes to warbles."""
__tablename__ = 'businesses'
id = db.Column(
db.Integer,
primary_key=True
)
yelp_id = db.Column(
db.Text,
unique=True,
)
name = db.Column(
db.Text
)
customers = db.relationship(
"User",
secondary="discoveries",
backref="businesses"
)
discoveries =db.relationship(
"Discovery",
backref="business")
class Business_Cat(db.Model):
"""Mapping Businesses to Categories"""
__tablename__ = 'business_cat'
bus_id = db.Column(
db.Integer,
db.ForeignKey('businesses.id', ondelete="cascade"),
primary_key=True,
)
cat_id = db.Column(
db.Integer,
db.ForeignKey('categories.id', ondelete="cascade"),
primary_key=True,
)
class Category(db.Model):
"""A yelp defined category"""
__tablename__ = 'categories'
id = db.Column(
db.Integer,
primary_key=True,
)
name = db.Column(
db.String(140),
nullable=False,
)
term = db.Column(
db.String(140),
nullable=False,
)
businesses = db.relationship(
"Business",
secondary="business_cat",
backref="categories"
)
def connect_db(app):
"""Connect this database to provided Flask app.
You should call this in your Flask app.
"""
db.app = app
db.init_app(app)