-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
258 lines (195 loc) · 5.46 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
"""SQLAlchemy models for Follow Your Rep"""
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
from utilities.api_utils import *
from sqlalchemy import update
bcrypt = Bcrypt()
db = SQLAlchemy()
def connect_db(app):
"""Connect this database to app.py"""
db.app = app
db.init_app(app)
class User(db.Model):
"""USER MODEL"""
__tablename__ = "users"
__table_args__ = (
db.UniqueConstraint('id', 'email', name='id_email_unique_components'),
)
id = db.Column(
db.Integer,
primary_key = True,
autoincrement = True,
)
name = db.Column(
db.String(100),
nullable = False,
)
email = db.Column(
db.String(50),
unique = True,
nullable = False,
)
password = db.Column(
db.Text,
nullable = False
)
profile_url = db.Column(
db.Text,
nullable = False,
default="/static/images/profile_default.jpg",
)
reps_following = db.relationship(
'Representative', secondary='users_reps', backref="users"
)
bills_following = db.relationship(
'Bill', secondary='users_bills', backref="users"
)
def __repr__(self):
first_half = f"<id: {self.id}; Name: {self.first_name};"
second_half = f"profile_url: {self.profile_url} ; Email: {self.email}>"
return f"{first_half} {second_half}"
@classmethod
def register(cls, email, password, name):
"""Register user and hash's the password"""
hashed = bcrypt.generate_password_hash(password)
hashed_utf8 = hashed.decode("utf8")
return cls(
email = email,
password = hashed_utf8,
name = name,
)
@classmethod
def authenticate(cls, email, password):
"""Authenticates user"""
user = User.query.filter_by(email = email).first()
if user and bcrypt.check_password_hash(user.password, password):
return user
else:
return False
class Representative(db.Model):
__tablename__ = "representatives"
id = db.Column(
db.Text,
primary_key = True,
)
first_name = db.Column(
db.Text,
)
last_name = db.Column(
db.Text,
)
chamber = db.Column(
db.Text,
)
state = db.Column(
db.Text,
)
party = db.Column (
db.Text,
)
twitter = db.Column(
db.Text,
)
facebook = db.Column(
db.Text,
)
next_election = db.Column(
db.Text,
)
def __repr__(self):
first = f"<Name: {self.first_name} {self.last_name}; Chamber: {self.chamber};"
second =f"State: {self.state}; Party: {self.party}; Twitter: {self.twitter_link};"
third = f"Facebook: {self.facebook}; Next Election {self.next_election}>"
return f"{first} {second} {third}"
class Bill(db.Model):
__tablename__ = "bills"
id = db.Column(
db.Text,
primary_key = True
)
sponsor_id = db.Column(
db.Text,
)
introduced_date = db.Column(
db.Text,
)
title = db.Column(
db.Text,
)
short_title = db.Column(
db.Text,
)
govtrack_url = db.Column(
db.Text,
)
committees = db.Column(
db.Text,
)
primary_subject = db.Column(
db.Text,
)
latest_major_action = db.Column(
db.Text,
)
latest_major_action_date = db.Column(
db.Text,
)
sponsor_name = db.Column(
db.Text,
)
sponsor_state = db.Column(
db.Text,
)
sponsor_party = db.Column(
db.Text
)
@classmethod
def update_all_bills(self):
bills = Bill.query.all()
for bill in bills:
response = APIUtils.get_bill_by_id(bill.id)
Bill.query.filter_by(id = bill.id).update({
Bill.latest_major_action: response.get('latest_major_action'),
Bill.latest_major_action_date: response.get('latest_major_action_date')
}, synchronize_session = False)
db.session.commit()
return {'status':'success'}
def __repr__(self):
first = f"<Bill_id: {self.id}; Sponsor_id: {self.sponsor_id}>"
return f"{first}"
class User_Rep(db.Model):
"""Mapping of a user and Representative"""
__tablename__ = "users_reps"
id = db.Column(
db.Integer,
primary_key = True,
autoincrement = True,
)
user_id = db.Column(
db.Integer,
db.ForeignKey('users.id'),
)
rep_id = db.Column(
db.Text,
db.ForeignKey('representatives.id')
)
def __repr__(self):
return f"<User_id {self.rep_id} is following Rep_id {self.rep_id}>"
class User_Bill(db.Model):
"""Mapping of a User and a Bill"""
__tablename__ = "users_bills"
id = db.Column(
db.Integer,
primary_key = True,
autoincrement = True,
)
user_id = db.Column(
db.Integer,
db.ForeignKey('users.id'),
)
bill_id = db.Column(
db.Text,
db.ForeignKey('bills.id'),
)
def __repr__(self):
return f"<User_id {self.user_id} is following Bill_id: {self.bill_id}>"