-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageConstructor.py
416 lines (359 loc) · 18.2 KB
/
PageConstructor.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import cgitb
#cgitb.enable()
from DataSource import DataSource
from Bill import Bill
from Senator import Senator
from Committee import Committee
import datetime
class PageConstructor:
"""Contains all of the methods to access and fill out the HTML templates."""
global STATE_LIST
global STATE_ABBREVIATION_LIST
STATE_LIST = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',
'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan',
'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming']
STATE_ABBREVIATION_LIST = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE',
'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD',
'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY',
'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT',
'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
def __init__(self):
# The constructor opens a connection to the database, creates an empty
# string of HTML, and creates an empty dictionary to be filled with
# database-specific replacements to the HTML templates.
self.page = ""
self.replacements = {}
self.dbSource = DataSource()
def readTemplate(self):
# A method that reads template.html, adds the template to self.page, and
# adds the HTML for the dropdown menus to self.replacements.
templateFile = open("Website/template.html", "r")
templateString = templateFile.read()
self.page = templateString
self.replacements["SenatorDropdown"] = ""
i = 0
while i < len(STATE_LIST):
self.replacements["SenatorDropdown"] += (
'<li><a href = "index.py?page_type=state&state='
+ STATE_ABBREVIATION_LIST[i] + '">'
+ STATE_LIST[i] + '</a></li>')
i+=1
committee_list = self.dbSource.getCommitteesBySession(114)
self.replacements["CommitteeDropdown"] = ""
for entry in committee_list:
self.replacements["CommitteeDropdown"] += ('<li>' +
entry.getCommitteeLink() + "</li>")
def makeHomepage(self):
# Gets HTML from HomepageTemplate.html and places it in the body of the
# generated page.
self.readTemplate()
pageFile = open("Website/HomepageTemplate.html", "r")
pageString = pageFile.read()
self.replacements["results"] = pageString
def makeBillPage(self, bill):
# Gets HTML from BillPageTemplate.html, fills in the info of the bill
# passed to it, and places it in the body of the generated page.
self.readTemplate()
billFile = open("Website/BillPageTemplate.html", "r")
billString = billFile.read()
session_link = ('<a href="index.py?page_type=session&session=' +
str(bill.getSession()) + '">' + str(bill.getSession()) +
"</a>")
votes = (str(len(bill.getYea_Votes())) + " yea | " +
str(len(bill.getNay_Votes())) + " nay | " +
str(len(bill.getAbstaining())) + " abstain | " +
str(len(bill.getAbsent())) + " absent")
# Loops through the vote data and addsa tally to a dictonary based
# on the party of the senator with keyed values that correspond
# to the votes in order
# all yea votes
party_dict = {}
for senator in bill.getYea_Votes():
party_votes = party_dict.get(senator.getParty())
if party_votes == None:
party_dict[senator.getParty()] = [1,0,0,0]
else:
party_votes[0] = party_votes[0] + 1
party_dict[senator.getParty()] = party_votes
# all nay votes
for senator in bill.getNay_Votes():
party_votes = party_dict.get(senator.getParty())
if party_votes == None:
party_dict[senator.getParty()] = [0,1,0,0]
else:
party_votes[1] = party_votes[1] + 1
party_dict[senator.getParty()] = party_votes
# all abstaining votes
for senator in bill.getAbstaining():
party_votes = party_dict.get(senator.getParty())
if party_votes == None:
party_dict[senator.getParty()] = [0,0,1,0]
else:
party_votes[2] = party_votes[2] + 1
party_dict[senator.getParty()] = party_votes
# all absent votes
for senator in bill.getAbsent():
party_votes = party_dict.get(senator.getParty())
if party_votes == None:
party_dict[senator.getParty()] = [0,0,0,1]
else:
party_votes[2] = party_votes[2] + 1
party_dict[senator.getParty()] = party_votes
# Displays the the vote tallys for each party in the vote rolls
breakdown_string = ""
for party in party_dict.iterkeys():
breakdown_string += "<p>" + party + ": "
breakdown_string += (str(party_dict.get(party)[0]) + " yea | " +
str(party_dict.get(party)[1]) + " nay | " +
str(party_dict.get(party)[2]) + " abstain | " +
str(party_dict.get(party)[3]) + " absent" + "</p>" )
# Table headers: Vote | Senator | Party | State
table_string = ""
for s in bill.getYea_Votes():
table_string += ('<tr><td>Yea</td>' +
'<td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td><td>' + s.getStateLink() +
'</td></tr>')
for s in bill.getNay_Votes():
table_string += ('<tr><td>Nay</td>' +
'<td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td><td>' + s.getStateLink() +
'</td></tr>')
for s in bill.getAbstaining():
table_string += ('<tr><td>Abstain</td>' +
'<td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td><td>' + s.getStateLink() +
'</td></tr>')
for s in bill.getAbsent():
table_string += ('<tr><td>Absent</td>' +
'<td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td><td>' + s.getStateLink() +
'</td></tr>')
fill_tags = {"BillName": bill.getQuestion(),
"VotesBreakdown": breakdown_string,
"BillType": bill.getType(),
"BillSession": session_link,
"BillDate": bill.getVoteDate().isoformat(),
"BillVotes": votes,
"SenatorTable": table_string}
content_string = billString.format(**fill_tags)
self.replacements["results"] = content_string
def makeCommitteePage(self, committee):
# Gets HTML from CommitteePageTemplate.html, fills in the info of the
# committee passed to it, and places it in the body of the generated
# page.
self.readTemplate()
committeeFile = open("Website/CommitteePageTemplate.html", "r")
committeeString = committeeFile.read()
session_link = ('<a href="index.py?page_type=session&session=' +
str(committee.getSession()) + '">' +
str(committee.getSession()) + "</a>")
# Table headers: Position | Senator | Party | State
table_string = ""
for senator_pair in committee.getSenators():
# A senator_pair has a senator object at index 0 and a string
# describing the senator's role in the committee at index 1.
senator = senator_pair[0]
table_string += ('<tr><td>' + senator_pair[1] +
'</td><td>' +
senator.getSenatorLink() + '</td>' +
'<td>' + senator.getParty() +
'</td><td>' +
senator.getStateLink() + '</td></tr>')
associated_string = ""
if committee.isSuper():
associated_list = committee.getAssociated()
if len(associated_list)>0:
associated_string += ("<h5>Sub-Committees:</h5>" +
'<ul id="committees">')
for pair in associated_list:
associated_string += ('<li><a href = "index.py?' +
"page_type=committee&committee='" +
str(pair[0]) + '">' + str(pair[1]) +
'</a></li>')
associated_string += "</ul>"
else:
pair = committee.getAssociated()[0]
associated_string += ("<p><strong>Super-Committee: </strong>" +
'<a href = "index.py?' +
"page_type=committee&committee='" +
str(pair[0]) + '">' + str(pair[1]) + '</a></p>')
fill_tags = {"CommitteeName": committee.getName(),
"SessionNumber": session_link,
"Supercommittee": associated_string,
"SenatorTable": table_string}
content_string = committeeString.format(**fill_tags)
self.replacements["results"] = content_string
def makeSenatorPage(self, senator, vote_pair_list):
# Gets HTML from SenatorPageTemplate.html, fills in the info of the
# senator passed to it, and places it in the body of the generated page.
self.readTemplate()
senatorFile = open("Website/SenatorPageTemplate.html", "r")
senatorString = senatorFile.read()
committee_list = ""
for committee_pair in senator.getCommittees():
# A committee_pair contains a committee object at index 0 and a
# string detailing the senator's role at index 1.
committee_list += ("<li>" + committee_pair[1] + " of the " +
committee_pair[0].getCommitteeLink() +
' during the <a href="index.py?page_type='
+ 'session&session=' +
str(committee_pair[0].getSession()) + '">' +
str(committee_pair[0].getSession()) +
"<sup>th</sup></a>" +
" congressional session</li>")
# Adds in a wikipedia link if it exists
wiki_string = ""
wiki_link = senator.getWikiLink()
if wiki_link != "":
wiki_string = '<p><strong>Wikipedia:</strong> ' + wiki_link + '</p>'
# Checks for empty birthdays
date_string = ""
if senator.getBirthday() != None:
date_string = senator.getBirthday().isoformat()
# Table headers: Date | Number | Bill Name | Vote
table_string = ""
for bill_pair in vote_pair_list:
# A bill_pair contains a bill object at index 0 and a string of
# the senator's vote at index 1.
if bill_pair[1] == "not_voting":
bill_pair[1] = "Absent"
elif bill_pair[1] == "present":
bill_pair[1] = "Abstain"
else:
bill_pair[1] = bill_pair[1].capitalize()
table_string += ('<tr><td>' +
bill_pair[0].getVoteDate().isoformat() +
'</td><td>' +
str(bill_pair[0].getRoll()) +
'</td><td>' +
bill_pair[0].getBillLink() +
'</td><td>' + bill_pair[1] +
'</td></tr>')
fill_tags = {"SenatorName": senator.getName(),
"Wikipedia": wiki_string,
"SenatorParty": senator.getParty(),
"SenatorStateLink": senator.getStateLink(),
"Birthday": date_string,
"Currently": senator.isCurrent(),
"CommitteeMemberList": committee_list,
"BillTable": table_string}
content_string = senatorString.format(**fill_tags)
self.replacements["results"] = content_string
def makeSenatorIndexPage(self, senator_list):
# Gets HTML from SenatorIndexPageTemplate.html, fills in the table with
# info on all of the senators, and places the HTML in the body of the
# generated page.
self.readTemplate()
senatorIndexFile = open("Website/SenatorIndexPageTemplate.html", "r")
senatorIndexString = senatorIndexFile.read()
# Table headers: Senator | Party | State
table_string = ""
for senator in senator_list:
table_string += ('<tr><td>' +
senator.getSenatorLink() +
'</td><td>' + senator.getParty() +
'</td><td>' +
senator.getStateLink() + '</td></tr>')
fill_tags = {"SenatorTable": table_string}
content_string = senatorIndexString.format(**fill_tags)
self.replacements["results"] = content_string
def makeBillIndexPage(self, bill_list):
# Gets HTML from BillIndexPageTemplate.html, fills in the table with
# info on all of the bills, and places the HTML in the body of the
# generated page.
self.readTemplate()
billIndexFile = open("Website/BillIndexPageTemplate.html", "r")
billIndexString = billIndexFile.read()
# Table headers: Date | # | Bill
table_string = ""
for bill in bill_list:
table_string += ('<tr><td>' + bill.getSessionLink() + '</td><td>' +
bill.getVoteDate().isoformat() +
'</td><td>' +
" s" + str(bill.getRoll()) +
'</td><td>' + bill.getBillLink() +
'</td></tr>')
fill_tags = {"BillData": table_string}
content_string = billIndexString.format(**fill_tags)
self.replacements["results"] = content_string
def makeStatePage(self, state_name, senator_list):
# Gets HTML from StatePageTemplate.html, fills in the table with
# info on the state's senators, and places the HTML in the body of the
# generated page.
self.readTemplate()
stateFile = open("Website/StatePageTemplate.html", "r")
stateString = stateFile.read()
# Table headers: Current? | Senator | Party
table_string = ""
for s in senator_list:
table_string += ('<tr><td>' + str(s.isCurrent()) +
'</td><td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td></tr>')
full_state_name = STATE_LIST[STATE_ABBREVIATION_LIST.index(state_name)]
fill_tags = {"StateName": full_state_name,
"SenatorTable": table_string}
content_string = stateString.format(**fill_tags)
self.replacements["results"] = content_string
def makeSessionPage(self, session):
# Gets HTML from SessionPageTemplate.html, fills in the two tables with
# info on the session's senators and bills, and places the HTML in the
# body of the generated page.
self.readTemplate()
sessionFile = open("Website/SessionPageTemplate.html", "r")
sessionString = sessionFile.read()
c_list_string = ""
committee_list = session.getCommittees()
for c in committee_list:
if c.isSuper():
c_list_string += ('<li>' + c.getCommitteeLink() + ' </li>')
# Table headers: Senator | Party | State
s_table_string = ""
senator_list = session.getSenators()
for s in senator_list:
s_table_string += ('<tr><td>' + s.getSenatorLink() +
'</td><td>' + s.getParty() +
'</td><td>' + s.getStateLink() + '</td></tr>')
# Table headers: Date | # | Bill
b_table_string = ""
bill_list = session.getBills()
for b in bill_list:
b_table_string += ('<tr><td>' +
b.getVoteDate().isoformat() +
'</td><td>' + str(b.getRoll()) +
'</td><td>' + b.getBillLink() + '</td></tr>')
fill_tags = {"sessionID": str(session.getId()),
"StartDate": session.getStart_Date().isoformat(),
"EndDate": session.getEnd_Date().isoformat(),
"CommitteeList": c_list_string,
"SenatorTable": s_table_string,
"BillTable": b_table_string}
content_string = sessionString.format(**fill_tags)
self.replacements["results"] = content_string
def makeErrorPage(self):
# Gets HTML from ErrorPageTemplate.html and places it in the body of the
# generated page.
self.readTemplate()
errorFile = open("Website/ErrorPageTemplate.html", "r")
errorString = errorFile.read()
self.replacements["results"] = errorString
def getPage(self):
# When the appropriate make function has been called, this places the
# edited body of the page in between the header and the footer, fleshes
# out the menu dropdowns, and returns the finished page.
self.page = self.page.format(**self.replacements)
return self.page