-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInputParser.py
112 lines (96 loc) · 4.17 KB
/
UserInputParser.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#import cgitb
#cgitb.enable()
from PageConstructor import PageConstructor
from DataSource import DataSource
from Bill import Bill
from Senator import Senator
from Committee import Committee
class UserInputParser:
"""Parses the user's input and passes the information between
PageConstructor and DataSource."""
def __init__(self, params):
# A constructor that stores the provided parameters internally, as well
# as calling both PageConstructor() and DataSource().
self.page_type = params['page_type']
self.params = params
self.page_maker = PageConstructor()
self.db_source = DataSource()
def generateHtmlPageOutput(self):
# Determines which type of page we're constructing, then calls the
# appropriate make____Page() method to generate the HTML.
HTML_string = ""
# The extra bits on the conditionals check that it's actually given a
# specific senator/state/bill by the CGI parameters. If not, it'll just
# go into the else.
function_dictionary = {"senator": self.makeSenatorPage,
"bill": self.makeBillPage, "state": self.makeStatePage,
"committee": self.makeCommitteePage,
"session": self.makeSessionPage,
"bill_index": self.makeBillIndexPage,
"senator_index": self.makeSenatorIndexPage,
"home": self.makeHomePage}
try:
HTML_string = function_dictionary[self.page_type]()
except:
HTML_string = self.makeErrorPage()
return HTML_string
# The "make" methods all do about the same thing, but with enough
# variations that it'd be a pain to make them all one. They get the major
# info we need from the database, then call the requisite PageConstructor
# method to make the page, after which they get it back as a string and
# return it.
#
# For Senators, Bills & Committees, the object returned by DataSource.py
# will be an object of the appropriate type; otherwise, it'll be a list of
# objects.
def makeSenatorPage(self):
id_tag = self.params["senator"]
senator_obj = self.db_source.getSenatorWithCommittees(id_tag)
senator_vote_pair = self.db_source.getVotesBySenator(id_tag,0)
self.page_maker.makeSenatorPage(senator_obj,senator_vote_pair)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeSenatorIndexPage(self):
senator_list = self.db_source.getSenatorList()
self.page_maker.makeSenatorIndexPage(senator_list)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeBillPage(self):
id_tag = self.params["bill"]
bill_obj = self.db_source.getBillWithVotes(id_tag)
self.page_maker.makeBillPage(bill_obj)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeBillIndexPage(self):
bill_list = self.db_source.getBillList()
self.page_maker.makeBillIndexPage(bill_list)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeStatePage(self):
state_name = self.params["state"]
senator_list = self.db_source.getSenatorsInState(state_name)
self.page_maker.makeStatePage(state_name, senator_list)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeCommitteePage(self):
committee_id = self.params["committee"]
committee_obj = self.db_source.getCommitteeWithMembers(committee_id)
self.page_maker.makeCommitteePage(committee_obj)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeSessionPage(self):
session_id = self.params["session"]
session = self.db_source.getSessionObject(session_id)
self.page_maker.makeSessionPage(session)
HTML_string = self.page_maker.getPage()
return HTML_string
def makeHomePage(self):
self.page_maker.makeHomepage()
HTML_string = self.page_maker.getPage()
return HTML_string
def makeErrorPage(self):
self.page_maker.makeErrorPage()
HTML_string = self.page_maker.getPage()
return HTML_string