-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
241 lines (170 loc) · 7.96 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
"""Data model classes for a crime database
"""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Category(db.Model):
"""Data model for a category."""
__tablename__ = 'categories'
category_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
category_name = db.Column(db.String(50), nullable=False)
count_crime = db.Column(db.Integer, nullable=False)
label = db.Column(db.Integer)
crimes = db.relationship('Crime', backref='category')
def __repr__(self):
"""printing the category table as a string."""
return f'<Category category_id={self.category_id} category_name={self.category_name}\
count_crime ={self.count_crime} label = {self.label}>'
class Neighborhood(db.Model):
"""Data model for a neighborhood."""
__tablename__ = 'neighborhoods'
neigh_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
neigh_name = db.Column(db.String(50))
neigh_latitude = db.Column(db.Float)
neigh_longitude = db.Column(db.Float)
zipcode = db.Column(db.Integer)
score = db.Column(db.Integer)
count_crime = db.Column(db.Integer, nullable = False)
crimes = db.relationship('Crime', backref='neighborhood')
def get_crimes_by_category(self):
"""Return a dictionary of crime categories and sum of crimes.
Example:
{'Larceny Theft': 1000}
"""
result = {}
for crime in self.crimes:
cat_name = crime.category.category_name
if cat_name in result:
result[cat_name] += 1
else:
result[cat_name] = 1
return result
def get_coordinates_by_category(self):
"""Return a dictionary of crime categories and list of coordinates.
Example:
{'Larceny Theft': [(33.3333,-122,9000),()]}
"""
result = {}
for crime in self.crimes:
cat_name = crime.category.category_name.strip()
if cat_name in result:
result[cat_name].append((crime.latitude, crime.longitude))
else:
result[cat_name] = [(crime.latitude, crime.longitude)]
return result
def __repr__(self):
"""for printing."""
return f'<Neighborhood neigh_id={self.neigh_id} neigh_name={self.neigh_name}\
neigh_latitude={self.neigh_latitude} neigh_longitude={self.neigh_longitude} score={self.score}\
zipcode={self.zipcode} \
count_crime={self.count_crime}>'
class Resolution(db.Model):
"""Data model for a resolution."""
__tablename__ = 'resolutions'
resolution_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
resolution_name = db.Column(db.String(100))
count_crime = db.Column(db.Integer, nullable = False)
crimes = db.relationship('Crime', backref='resolution')
def __repr__(self):
"""for printing."""
return f'<Resolution resolution_id={self.resolution_id} resolution_name={self.resolution_name}\
neigh_latitude={self.neigh_latitude} neigh_longitude={self.neigh_longitude}\
description={self.discription} count_crime={self.count_crime}>'
class Subcategory(db.Model):
"""Data model for a resolution."""
__tablename__ = 'subcategories'
subcategory_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
subcategory_name = db.Column(db.String(100))
count_crime = db.Column(db.Integer, nullable = False)
crimes = db.relationship('Crime', backref='subcategory')
def __repr__(self):
"""for printing."""
return f'<Resolution resolution_id={self.resolution_id} resolution_name={self.resolution_name}\
neigh_latitude={self.neigh_latitude} neigh_longitude={self.neigh_longitude}\
description={self.discription} count_crime={self.count_crime}>'
class Crime(db.Model):
"""Data model for a crime."""
__tablename__ = 'crimes'
crime_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
subcategory_id = db.Column(db.Integer, db.ForeignKey('subcategories.subcategory_id'))
resolution_id = db.Column(db.Integer, db.ForeignKey('resolutions.resolution_id'))
neighborhood_id = db.Column(db.Integer, db.ForeignKey('neighborhoods.neigh_id'))
crime_date = db.Column(db.String(50))
crime_time = db.Column(db.String(50))
crime_day = db.Column(db.String(50))
intersection = db.Column(db.String(100))
police_district = db.Column(db.String(100))
latitude = db.Column(db.Float, nullable = False)
longitude = db.Column(db.Float, nullable = False)
label = db.Column(db.Integer)
def __repr__(self):
"""for printing."""
return f'<Crime crime_id={self.crime_id} crime_date={self.crime_date}\
crime_time={self.crime_time} crime_day={self.crime_day}\
police_district={self.police_district} intersection={self.intersection}\
latitude={self.latitude} longitude={self.longitude} label={self.label}>'
class User(db.Model):
"""Data model for a user."""
__tablename__ = 'users'
user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50), nullable=False)
email= db.Column(db.String(50), nullable=False)
password=db.Column(db.String(100), nullable=False)
phone_num=db.Column(db.String(100))
def __repr__(self):
"""printing the user table as a string."""
return f'<User user_id={self.user_id} name={self.name}\
email ={self.email} user_password={self.password} phone_num={self.phone_num}>'
class Route(db.Model):
"""Data model for a route"""
__tablename__ = 'routes'
route_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
route_name = db.Column(db.String(50))
route_start = db.Column(db.String(100))
route_end = db.Column(db.String(100))
score = db.Column(db.Float)
user = db.relationship('User', backref='routes')
neighborhoods = db.relationship('Neighborhood',
secondary='routeneighs',
backref='routes')
def to_dict(self):
"""make a dictionary of a route"""
return {"route_id":self.route_id,
"user_id": self.user_id,
"route_name": self.route_name,
"route_start": self.route_start,
"route_end": self.route_end,
"score": self.score}
def get_route_id(self):
"""return the route id"""
return self.route_id
def __repr__(self):
"""for printing."""
return f'<route_id={self.route_id} route_name={self.route_name}\
route_start={self.route_start}\
route_end={self.route_end} \
score={self.score}>'
class RouteNeigh(db.Model):
"""Data model for a route"""
__tablename__ = 'routeneighs'
route_neigh_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
route_id = db.Column(db.Integer, db.ForeignKey('routes.route_id'))
neigh_id = db.Column(db.Integer, db.ForeignKey('neighborhoods.neigh_id'))
def __repr__(self):
"""for printing."""
return f'<route_id={self.route_id} neigh_id={self.neigh_id}>'
def connect_to_db(app):
"""Connect the database to our Flask app."""
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///crimeData'
app.config['SQLALCHEMY_ECHO'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == '__main__':
from server import app
connect_to_db(app)
print('Connected to db!')
db.create_all()