forked from aaronsw/watchdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
667 lines (570 loc) · 20.9 KB
/
schema.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import simplejson as json
import web
import smartersql as sql
from settings import db
sql.Table.db = db
class State(sql.Table):
@property
def _uri_(self):
return 'http://watchdog.net/us/%s#it' % self.code.lower()
# states.json
code = sql.String(2, primary=True)
name = sql.String(256)
status = sql.String(256)
wikipedia = sql.URL()
fipscode = sql.String(2)
#senators = sql.Backreference('Politician', 'district')
districts = sql.Backreference('District', 'state', order='name asc')
class District(sql.Table):
@property
def _uri_(self):
return 'http://watchdog.net/us/%s#it' % self.name.lower()
# districts.json
name = sql.String(10, primary=True)
district = sql.Integer()
state = sql.Reference(State) #@@renames to state_id
voting = sql.Boolean()
wikipedia = sql.URL()
#politician = sql.Backreference('Politician', 'district')
@property
def districtth(self):
if self.district == 0:
return 'at-large'
else:
return web.nthstr(self.district)
@property
def representatives(self):
ids = [x.id for x in db.select(
'curr_politician', where="district_id = $self.name", vars=locals())]
return Politician.select(where=web.sqlors('id=', ids))
# almanac.json
almanac = sql.URL()
area_sqmi = sql.Number() #@@Square Miles
cook_index = sql.String() #@@integer
poverty_pct = sql.Percentage()
median_income = sql.Dollars()
est_population = sql.Number() # most recent population estimate
est_population_year = sql.Year() # year of the estimate
# shapes.json
outline = sql.String(export=False) #@@geojson
# centers.json
center_lat = sql.Float() #@@ latlong type
center_lng = sql.Float()
zoom_level = sql.Integer()
earmark_per_capita = sql.Float()
class Zip(sql.Table):
zip = sql.String(5, primary=True)
city = sql.String()
state = sql.String(2) #@@ reference state...(@@no AE=Armed Forces Europe)
gini = sql.Float()
# @@other IRS stuff
class Zip4(sql.Table):
zip = sql.String(5, primary=True) #@@references zip, (seems some ZIP+4s aren't in ctyst?)
plus4 = sql.String(4, primary=True)
district = sql.Reference(District)
#--alter table zip4 drop constraint zip4_pkey;
#--COPY zip4 FROM '/home/watchdog/web/data/load/zip4.tsv';
#alter table zip4 add primary key (zip, plus4);
#--alter table zip4 add constraint "zip4_district_fkey" FOREIGN KEY (district) REFERENCES district(name) #@@
#--GRANT ALL ON zip4 TO watchdog;
class GovtrackID(sql.URL):
towhatever = lambda f: (lambda self, x, *a: f(self,
'http://www.govtrack.us/congress/person.xpd?id=' + x, *a))
toxml = towhatever(sql.URL.toxml)
ton3 = towhatever(sql.URL.ton3)
class Politician(sql.Table):
@property
def _uri_(self): return 'http://watchdog.net/p/%s#it' % self.id
# politicians.json
id = sql.String(256, primary=True)
district = sql.Reference(District) # Moved to Congress table
wikipedia = sql.URL()
# govtrack.json --@@get from votesmart?
bioguideid = sql.String()
opensecretsid = sql.String()
govtrackid = GovtrackID()
gender = sql.String(1)
birthday = sql.String() #@@date
firstname = sql.String()
middlename = sql.String()
lastname = sql.String()
election_status = sql.String()
def xmllines(self):
sameas = lambda x: ' <owl:sameAs xmlns:owl="http://www.w3.org/2002/07/owl#" \
rdf:resource="%s" />' % x
return [sameas(x) for x in self.akas()]
def n3lines(self, indent):
sameas = lambda x: indent + '<http://www.w3.org/2002/07/owl#sameAs> <%s>;' % x
return [sameas(x) for x in self.akas()]
def akas(self):
if self.bioguideid:
yield 'http://www.rdfabout.com/rdf/usgov/congress/people/' + self.bioguideid
if self.wikipedia:
yield 'http://dbpedia.org/resource/' + self.wikipedia.split('/')[-1]
@property
def name(self):
if hasattr(self, 'nickname') and self.nickname:
return self.nickname + ' ' + self.lastname
else:
return self.fullname
@property
def fullname(self):
return (self.firstname or '') + ' ' + (self.middlename or '') + ' ' + (self.lastname or '')
@property
def title(self):
dist = self.district_id
if self.is_current:
return 'Sen.' if State.where(code=dist) else 'Rep.'
else:
return ''
@property
def handshakes(self):
return db.query('select * from handshakes '
'where politician_id=$self.id '
'order by year desc, pol2corp+corp2pol desc',
vars=locals()).list()
@property
def opponents(self):
return db.select('past_elections',
where='district_id=$self.district_id and politician_id != $self.id',
order='year desc', vars=locals()).list()
@property
def past_votes(self):
return db.select('past_elections',
where='district_id=$self.district_id and politician_id = $self.id',
order='year desc', vars=locals()).list()
@property
def is_current(self):
latest_congress = 111
return latest_congress in self.congresses
#@@ cache query results?
@property
def congresses(self):
return [r.congress_num for r in
db.select('congress',
where='politician_id = $self.id',
order='congress_num',
vars=locals())]
officeurl = sql.URL()
party = sql.String() # Moved to Congress table
religion = sql.String()
n_bills_introduced = sql.Number()
n_bills_enacted = sql.Number()
n_bills_debated = sql.Number()
n_bills_cosponsored = sql.Number()
n_speeches = sql.Number()
words_per_speech = sql.Number()
# voteview.json
icpsrid = sql.Integer()
nominate = sql.Float() #@@
predictability = sql.Percentage()
# earmarks.json
amt_earmark_requested = sql.Dollars(default=0)
n_earmark_requested = sql.Number(default=0)
n_earmark_received = sql.Number(default=0)
amt_earmark_received = sql.Dollars(default=0)
# photos.json
photo_path = sql.URL()
photo_credit_url = sql.URL()
photo_credit_text = sql.String()
# fec
money_raised = sql.Dollars()
pct_spent = sql.Percentage()
pct_self = sql.Percentage()
pct_indiv = sql.Percentage()
pct_pac = sql.Percentage()
pct_labor = sql.Percentage()
pct_instate = sql.Percentage()
pct_smalldonor = sql.Percentage()
# votesmart
nickname = sql.String()
votesmartid = sql.String()
birthplace = sql.String()
education = sql.String()
# punch
chips2008 = sql.Percentage()
progressive2008 = sql.Percentage()
progressiveall = sql.Percentage()
# opensecrets
pct_pac_business = sql.Percentage()
# almanac.json
n_vote_received = sql.Number()
pct_vote_received = sql.Percentage()
last_elected_year = sql.Number()
bills_sponsored = sql.Backreference('Bill', 'sponsor')
earmarks_sponsored = sql.Backreference('Earmark_sponsor', 'politician')
class Congress(sql.Table):
politician = sql.Reference(Politician, primary=True)
congress_num = sql.Integer(primary=True)
#district = sql.Reference(District) #@@ renames to district_id
district_id = sql.String(10, primary=True) # Can't make this a reference to district. District table only has CURRENT districts in it.
party = sql.String()
current_member = sql.Boolean()
class Politician_FEC_IDs(sql.Table):
politician = sql.Reference(Politician, primary=True)
fec_id = sql.String(primary=True)
# cycle = sql.Integer()
class Interest_Group(sql.Table): # @@capitalization looks dorky..
id = sql.Serial(primary=True)
groupname = sql.String(10)
category_id = sql.String(10) # references category,
longname = sql.String(unique=True)
class Interest_group_rating(sql.Table):
"""interest group scores for politicians"""
politician = sql.Reference(Politician)
# almanac.json
year = sql.Year()
group = sql.Reference(Interest_Group)
rating = sql.Integer() # typically 0-100
class Bill(sql.Table):
@property
def _uri_(self):
return 'http://watchdog.net/b/%s#it' % self.id
id = sql.String(primary=True)
session = sql.Integer()
type = sql.String(5)
number = sql.Integer()
introduced = sql.Date()
title = sql.String()
sponsor = sql.Reference(Politician) #@@rename to sponsor_id
summary = sql.String()
maplightid = sql.String(10)
interest_group_support = sql.Backreference('Interest_group_bill_support', 'bill', order='support desc')
@property
def name(self):
typemap = {
'h': 'H.R.',
's': 'S.',
'hj': 'H.J.Res.',
'sj': 'S.J.Res.',
'hc': 'H.Con.Res.',
'sc': 'S.Con.Res.',
'hr': 'H.Res.',
'sr': 'S.Res.'
}
return typemap[self.type] + ' ' + str(self.number)
@property
def votes_by_party(self):
"""Get the votes of the political parties for a bill."""
result = db.select(['politician p, position v'],
what="v.vote, count(v.vote), p.party",
where="v.politician_id = p.id and v.bill_id = $self.id "
"AND v.vote is not null",
group="p.party, v.vote",
vars = locals()
).list()
d = {}
for r in result:
d.setdefault(r.party, {})
d[r.party][r.vote] = r.count
return d
@property
def votes_by_caucus(self):
caucuses = json.load(file('import/load/manual/caucuses.json'))
members = sum([x['members'] for x in caucuses], [])
result = db.select(['position'],
where=web.sqlors('politician_id=', members) +
'AND bill_id=' + web.sqlquote(self.id),
vars=locals()
).list()
if not result: return None
votemap = dict((r.politician_id, r.vote) for r in result)
d = {}
for c in caucuses:
cdict = d[c['name']] = {}
for m in c['members']:
v = votemap.get(m)
cdict.setdefault(v, 0)
cdict[v] += 1
return d
class Roll(sql.Table):
id = sql.String(primary=True)
type = sql.String()
question = sql.String()
required = sql.String()
result = sql.String()
bill = sql.Reference(Bill)
@property
def _uri_(self):
return 'http://watchdog.net/r/us/%s#it' % self.id
#@@@@@ DON'T REPEAT YOURSELF
@property
def votes_by_party(self):
"""Get the votes of the political parties for a bill."""
result = db.select(['politician p, vote v'],
what="v.vote, count(v.vote), p.party",
where="v.politician_id = p.id and v.roll_id = $self.id "
"AND v.vote is not null",
group="p.party, v.vote",
vars = locals()
).list()
d = {}
for r in result:
d.setdefault(r.party, {})
d[r.party][r.vote] = r.count
return d
@property
def votes_by_caucus(self):
caucuses = json.load(file('import/load/manual/caucuses.json'))
members = sum([x['members'] for x in caucuses], [])
result = db.select(['vote'],
where=web.sqlors('politician_id=', members) +
'AND roll_id=' + web.sqlquote(self.id),
vars=locals()
).list()
if not result: return None
votemap = dict((r.politician_id, r.vote) for r in result)
d = {}
for c in caucuses:
cdict = d[c['name']] = {}
for m in c['members']:
v = votemap.get(m)
cdict.setdefault(v, 0)
cdict[v] += 1
return d
class Vote(sql.Table):
roll = sql.Reference(Roll, primary=True)
politician = sql.Reference(Politician, primary=True)
vote = sql.Int2()
class Position(sql.Table):
bill = sql.Reference(Bill, primary=True)
politician = sql.Reference(Politician, primary=True)
vote = sql.Int2()
class Interest_group_bill_support(sql.Table):
bill = sql.Reference(Bill, primary=True)
group = sql.Reference(Interest_Group, primary=True)
support = sql.Int2()
class Group_politician_similarity(sql.Table):
group = sql.Reference(Interest_Group, primary=True)
politician = sql.Reference(Politician, primary=True)
agreed = sql.Integer()
total = sql.Integer()
class Category (sql.Table):
id = sql.String(10, primary=True)
name = sql.String()
industry = sql.String()
sector = sql.String()
class Committee (sql.Table):
id = sql.String(primary=True) # FEC ID
name = sql.String()
treasurer = sql.String()
street1 = sql.String()
street2 = sql.String()
city = sql.String()
state = sql.String()
zip = sql.String()
connected_org_name = sql.String()
candidate_id = sql.String()
type = sql.String()
class Contribution (sql.Table):
id = sql.Serial(primary=True)
fec_record_id = sql.String()
microfilm_loc = sql.String()
report_id = sql.String()
recipient = sql.Reference(Committee)
# contributor
name = sql.String()
street = sql.String()
city = sql.String()
state = sql.String()
zip = sql.String()
occupation = sql.String()
employer = sql.String()
employer_stem = sql.String()
committee = sql.String() # sigh: sometimes committee, sometimes candidate
sent = sql.Date()
amount = sql.Float()
#@@INDEX by employer_stem
class Earmark(sql.Table):
id = sql.Integer(primary=True)
house_request = sql.Dollars()
senate_request = sql.Dollars()
final_amt = sql.Dollars()
budget_request = sql.Dollars()
prereduction_amt = sql.Dollars()
description = sql.String()
city = sql.String() # eventually a ref, we hope
county = sql.String()
state = sql.String() #@@ref?
bill = sql.String() #@@ref
bill_section = sql.String()
bill_subsection = sql.String()
project_heading = sql.String()
district = sql.Integer()
presidential = sql.String()
undisclosed = sql.String()
intended_recipient = sql.String()
recipient_stem = sql.String()
notes = sql.String()
sponsors = sql.Backreference('Earmark_sponsor', 'earmark')
class Earmark_sponsor(sql.Table):
earmark = sql.Reference(Earmark, primary=True)
politician = sql.Reference(Politician, primary=True)
## Lobbyiest stuff:
class lob_organization(sql.Table):
id = sql.Number(primary=True) #@@TODO: design stable ids
name = sql.String()
filings = sql.Backreference('lob_filing', 'org')
class lob_person(sql.Table):
id = sql.Number(primary=True) #@@TODO: design stable ids
prefix = sql.String()
firstname = sql.String()
middlename = sql.String()
lastname = sql.String()
suffix = sql.String()
contact_name = sql.String()
filings = sql.Backreference('lob_filing', 'lobbyist')
class lob_pac(sql.Table):
id = sql.Number(primary=True) #@@TODO: design stable ids
name = sql.String()
filings = sql.Backreference('lob_pac_filings', 'pac')
class lob_filing(sql.Table):
id = sql.Number(primary=True) #Uses xml file number as a stable id.
year = sql.Year()
type = sql.String()
signed_date = sql.Date()
amendment = sql.Boolean()
certified = sql.Boolean()
comments = sql.String()
senate_id = sql.Number()
house_id = sql.Number()
filer_type = sql.String(1)
lobbyist = sql.Reference(lob_person)
org = sql.Reference(lob_organization)
pacs = sql.Backreference('lob_pac_filings', 'filing')
contributions = sql.Backreference('lob_contribution', 'filing', order='amount asc')
@property
def house_url(self):
return "http://disclosures.house.gov/lc/xmlform.aspx?id=%d" % self.id
class lob_contribution(sql.Table):
filing = sql.Reference(lob_filing)
date = sql.Date()
type = sql.String()
contributor = sql.String()
payee = sql.String()
recipient = sql.String()
amount = sql.Dollars()
politician = sql.Reference(Politician)
class lob_pac_filings(sql.Table):
pac = sql.Reference(lob_pac)
filing = sql.Reference(lob_filing)
class Expenditure (sql.Table):
id = sql.Serial(primary=True)
candidate_name = sql.String()
committee = sql.String()
expenditure_date = sql.Date()
recipient = sql.String()
filer_id = sql.String(10)
report_id = sql.Integer()
amount = sql.String(20)
class SOI(sql.Table):
#district_id = sql.String(10, primary=True)
district = sql.Reference(District, primary=True)
# irs/soi
bracket_low = sql.Integer(primary=True)
agi = sql.Float()
n_dependents = sql.Float()
n_eitc = sql.Float()
n_filers = sql.Float()
n_prepared = sql.Float()
tot_charity = sql.Float()
tot_eitc = sql.Float()
tot_tax = sql.Float()
avg_dependents = sql.Float()
avg_eitc = sql.Float()
avg_income = sql.Float()
avg_taxburden = sql.Float()
pct_charity = sql.Percentage()
pct_eitc = sql.Percentage()
pct_prepared = sql.Percentage()
class Census_meta(sql.Table):
internal_key = sql.String(10, primary=True)
census_type = sql.Integer(primary=True)
hr_key = sql.String(512)
label = sql.String()
class Census_data(sql.Table):
#district_id = sql.String(10, primary=True)
district = sql.Reference(District, primary=True)
internal_key = sql.String(10, primary=True)
census_type = sql.Integer(primary=True)
value = sql.Float()
class Pol_contacts(sql.Table):
politician = sql.Reference(Politician, primary=True)
contact = sql.String()
contacttype = sql.String(1) # E=email, W=wyr, I=ima, Z=zipauth
captcha = sql.Boolean()
class Pol_phones(sql.Table):
politician = sql.Reference(Politician, primary=True)
city = sql.String(primary=True)
phone1 = sql.String(15)
phone2 = sql.String(15)
tollfree = sql.String(15)
class Census_Population(sql.Table):
state_id = sql.String(2) # STATE
county_id = sql.String(3) # COUNTY
blockgrp_id = sql.String(1) # BLOCKGRP
block_id = sql.String(4) # BLOCK
district_id = sql.String(2) # CD110
tract_id = sql.String(6) # TRACT
zip_id = sql.String(5) # ZCTA
sumlev = sql.String(16) # SUMLEV
area_land = sql.Integer() # AREALAND
area_land.sql_type = 'bigint'
population = sql.Integer()
class Handshakes(sql.Table):
politician = sql.Reference(Politician, primary=True)
corporation = sql.String(primary=True)
pol2corp = sql.Dollars()
corp2pol = sql.Dollars()
year = sql.Integer()
class Past_Elections(sql.Table):
politician_id = sql.String(256, primary=True) # don't refer `politician` table as lost candidates might not be there
district = sql.Reference(District, primary=True)
year = sql.Year(primary=True)
type = sql.String(10, primary=True) #Gen for general, SpGen for special-general elections
votes_received = sql.Integer()
pct_votes_received = sql.Percentage()
expenditure = sql.Dollars()
class Exempt_Org(sql.Table):
ein = sql.Integer(primary=True)
primary_name = sql.String()
careof_name = sql.String()
street = sql.String()
city = sql.String()
state = sql.String(2)
zip = sql.String()
group_exemption_num = sql.String()
subsection_code = sql.String()
affiliation = sql.String()
classification_code = sql.String()
ruling_date = sql.String()
deductibility_code = sql.String()
foundation_code = sql.String()
activity_code = sql.String()
organization_code = sql.String()
exempt_org_status_code = sql.String()
advance_ruling_expiration = sql.String()
tax_period = sql.String()
asset_code = sql.String()
income_code = sql.String()
filing_requirement_code = sql.String()
accounting_period = sql.String()
asset_amt = sql.BigInteger()
income_amt = sql.BigInteger()
form_990_revenue_amt = sql.BigInteger()
ntee_code = sql.String()
sort_name = sql.String()
def init():
db.query("CREATE VIEW census AS select * from census_meta NATURAL JOIN census_data")
db.query("CREATE INDEX contribution_recipient_id_idx ON contribution (recipient_id)")
db.query("CREATE INDEX contribution_zip_idx ON contribution (zip)")
db.query("CREATE INDEX contribution_empl_stem_idx ON contribution (LOWER(employer_stem))")
db.query("CREATE INDEX contribution_occupation_idx ON contribution (LOWER(occupation));")
db.query("CREATE INDEX contribution_lower_name_zip_idx ON contribution (LOWER(name), zip);")
db.query("ANALYZE contribution;")
db.query("CREATE VIEW curr_politician AS SELECT politician.* FROM politician, congress WHERE politician.id = politician_id AND congress_num='111' AND current_member = 't' ;")
db.query("GRANT ALL on curr_politician TO watchdog;")
try:
db.query("GRANT ALL on census TO watchdog")
except:
pass # group doesn't exist