-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
132 lines (121 loc) · 3.79 KB
/
database.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
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from models import *
db = create_engine('mysql+mysqldb://phub:@localhost/phub?charset=utf8')
Session = sessionmaker(bind=db)
def add_committee_members(row, obj, verbose):
obj['committees'] = []
session = Session()
for r in session.query(committee_member).filter(committee_member.legislator_id == row.id):
if verbose:
obj['committees'].append(get_committee_by_id(r.committee_id, verbose))
else:
obj['committees'].append(r.committee_id)
session.close()
def add_votes(row, obj, verbose):
obj['votes'] = []
session = Session()
for r in session.query(vote).filter(vote.legislator_id == row.id):
if verbose:
obj['votes'].append({
'bill': get_bill_by_id(r.bill_id, verbose),
'result': r.result
})
else:
obj['votes'].append({
'bill_id': r.bill_id,
'result': r.result
})
session.close()
def add_bill_committees(row, obj, verbose):
obj['committees'] = []
session = Session()
for r in session.query(bill_committee).filter(bill_committee.bill_id == row.id):
if verbose:
obj['committees'].append(get_committee_by_id(r.committee_id, verbose))
else:
obj['committees'].append(r.committee_id)
session.close()
"""
Get all legislators from the database
"""
def get_legislators(args, verbose):
result = []
session = Session()
query = session.query(legislator).order_by(legislator.id).filter_by(**args)
session.close()
for row in query:
obj = legislator.get_obj(row)
add_committee_members(row, obj, verbose)
add_votes(row, obj, verbose)
result.append(obj)
return result
"""
Get all committees from the database
"""
def get_committees(args, verbose):
result = []
session = Session()
query = session.query(committee).order_by(committee.id).filter_by(**args)
session.close()
for row in query:
obj = committee.get_obj(row)
if verbose:
obj['chair'] = get_legislator_by_id(obj['chair'], False)
result.append(obj)
return result
"""
Get all bills from the database
"""
def get_bills(args, verbose):
result = []
session = Session()
query = session.query(bill).order_by(bill.id).filter_by(**args)
session.close()
for row in query:
obj = bill.get_obj(row)
add_bill_committees(row, obj, verbose)
if verbose:
obj['sponsor'] = get_legislator_by_id(obj['sponsor'], False)
result.append(obj)
return result
"""
Get a legislator by its id from the database
"""
def get_legislator_by_id(legislator_id, verbose):
session = Session()
row = session.query(legislator).filter(legislator.id == legislator_id).first()
session.close()
obj = {}
if row:
obj = legislator.get_obj(row)
add_committee_members(row, obj, verbose)
add_votes(row, obj, verbose)
return obj
"""
Get a committee by its id from the database
"""
def get_committee_by_id(committee_id, verbose):
session = Session()
row = session.query(committee).filter(committee.id == committee_id).first()
session.close()
obj = {}
if row:
obj = committee.get_obj(row)
if verbose:
obj['chair'] = get_legislator_by_id(obj['chair'], False)
return obj
"""
Get a bill by its id from the database
"""
def get_bill_by_id(bill_id, verbose):
session = Session()
row = session.query(bill).filter(bill.id == bill_id).first()
session.close()
obj = {}
if row:
obj = bill.get_obj(row)
add_bill_committees(row, obj, verbose)
if verbose:
obj['sponsor'] = get_legislator_by_id(obj['sponsor'], False)
return obj