-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
1093 lines (883 loc) · 36 KB
/
Copy pathapp.py
File metadata and controls
1093 lines (883 loc) · 36 KB
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" Main Bottle dispatcher for voteview.com """
# pylint: disable=W0703,C0103,no-member
from __future__ import print_function
import re
import traceback
import os
import glob
import datetime
import time
import bottle
from model.config import config
from model.search_votes import query
import model.download_votes # Namespace issue
from model.email_contact import send_email, newsletter_subscribe
from model.search_members import (member_lookup, get_members_by_congress,
get_members_by_party,
district_member_lookups)
from model.articles import get_article_meta, list_articles, list_all_articles
from model.search_meta import meta_lookup
from model.bio_data import (congress_to_year,
assemble_person_meta,
twitter_card)
from model.prep_votes import (prep_votes, sort_votes_by_column,
prep_person_votes_fast)
from model.geo_lookup import (address_to_lat_long,
lat_long_to_districts,
lat_long_to_polygon)
from model.search_assemble import assemble_search
from model.slide_carousel import generate_slides
from model.loyalty import get_loyalty
from model.date_helper import fix_date
import model.download_excel
import model.stash_cart
import model.party_data
import model.log_quota
# Turn this off on production:
if config["server"]:
bottle.debug(True)
else:
bottle.debug(False)
# Setup
app = application = bottle.Bottle()
bottle.BaseTemplate.defaults['get_url'] = app.get_url
bottle.BaseTemplate.defaults['template'] = bottle.template
bottle.TEMPLATE_PATH.append('/static/bookreader')
bottle.TEMPLATE_PATH.append('/static/bookreader/BookReader')
# Debug timing to improve speed
time_labels = []
time_nums = []
def clear_time():
""" Clear debug timings for the page load. """
global time_labels, time_nums
time_labels = []
time_nums = []
def time_it(label):
""" Add an additional debug timing to the page load. """
global time_labels, time_nums
time_labels.append(label)
time_nums.append(time.time())
def zip_times():
""" Calculate the time diffs for each step and return. """
global time_labels, time_nums
time_nums_diff = [0] + [time_nums[i + 1] - time_nums[i]
for i in range(len(time_nums) - 1)]
return list(zip(time_labels, time_nums_diff))
def get_base(urlparts):
""" Extracts the base domain URL for the site. """
domain = urlparts.scheme + "://" + urlparts.netloc + "/"
return domain
def default_value(input_var, value=None):
""" Helper function for handling bottle arguments and setting defaults """
return input_var if input_var else value
#
# Web pages
#
@app.route('/static/<path:path>')
def callback(path):
"""
Pass through static content -- nginx should handle this, but if it doesn't,
bottle will
"""
return bottle.static_file(path, "./static")
@app.route("/")
@app.route("/search/")
@app.route("/search/<search_string>")
def index(search_string=""):
""" Generates main index vote and search page. """
clear_time()
time_it("begin")
base_url = get_base(bottle.request.urlparts)
try:
arg_dict = {}
for k, v in bottle.request.params.items():
arg_dict[k] = v
except Exception:
pass
time_it("assembleArgs")
try:
if "fromDate" in arg_dict:
arg_dict["fromDate"] = arg_dict["fromDate"].replace("/", "-")
arg_dict["fromDate"] = re.sub(
r"[^0-9\-\ ]", "", arg_dict["fromDate"])
if "toDate" in arg_dict:
arg_dict["toDate"] = arg_dict["toDate"].replace("/", "-")
arg_dict["toDate"] = re.sub(r"[^0-9\-\ ]", "", arg_dict["toDate"])
if "fromCongress" in arg_dict:
arg_dict["fromCongress"] = int(arg_dict["fromCongress"])
if "toCongress" in arg_dict:
arg_dict["toCongress"] = int(arg_dict["toCongress"])
if "support" in arg_dict and "," in arg_dict["support"]:
try:
arg_dict["supportMin"], arg_dict["supportMax"] = [
int(x) for x in arg_dict["support"].split(",")]
except Exception:
pass
elif "support" in arg_dict:
try:
support = int(arg_dict["support"])
arg_dict["supportMin"] = support - 1
arg_dict["supportMax"] = support + 1
except Exception:
pass
time_it("doneAssembly")
# Randomly sample some slides to show.
slides = generate_slides()
output = bottle.template("views/search",
args=arg_dict, search_string=search_string,
time_set=zip_times(), base_url=base_url,
slides=slides)
except Exception:
output = bottle.template(
"views/error", error_message=traceback.format_exc())
# error_message="Error: One or more of the parameters you used to call
# this page was misspecified.")
return output
# Static Pages with no arguments, just passthrough the template.
# Really we should just cache these, but anyway.
@app.route("/about")
def about():
""" Simple passthrough to about page. """
output = bottle.template('views/about')
return output
@app.route("/quota")
@app.route("/abuse")
def quota():
""" Simple passthrough to quota expired page. """
output = bottle.template("views/quota")
return output
@app.route("/data")
def data():
""" Load the data page -- need to populate the maximum congress. """
max_congress = config["max_congress"]
all_articles = list_all_articles()
current_year = datetime.datetime.now().year
slides = generate_slides()
output = bottle.template("views/data",
max_congress=max_congress,
articles=all_articles,
year=current_year,
slides=slides)
return output
@app.route("/past_data")
def past_data():
""" Index page listing past full database zips. """
# My suspicion is that the below listdir actually won't error but let's
# make explicit what happens if the diretory is missing.
if not os.path.isdir("static/db"):
return bottle.template("views/past_data", folder_files=[])
blacklist = [".gitkeep"]
folder_files = [x for x in reversed(sorted(os.listdir("static/db/"))) if
x not in blacklist]
return bottle.template("views/past_data", folder_files=folder_files)
# Pages that have arguments
@app.route("/congress")
@app.route("/congress/<chamber:re:house|senate>")
@app.route("/congress/<chamber:re:house|senate>/<congress_num:int>")
@app.route("/congress/<chamber:re:house|senate>/<congress_num:int>/<tab_view>")
@app.route("/congress/<chamber:re:house|senate>/<tab_view>")
def display_congress(chamber="senate", congress_num=-1, tab_view=""):
""" View a given congress """
max_congress = config["max_congress"]
# Constrain chamber to senate/house
if chamber != "senate":
chamber = "house"
# Argument order weirdness in bottle: try to combine chamber/text
if tab_view and tab_view != "text":
try:
congress_num = int(tab_view)
tab_view = ""
except Exception:
tab_view = ""
congress_num = max_congress
if congress_num == -1:
congress_num = max_congress
# Get meta args for NOMINATE
meta = meta_lookup()
member_label = ("Senators" if chamber.title() == "Senate"
else "Representatives")
output = bottle.template("views/congress",
chamber=chamber,
congress=congress_num,
max_congress=max_congress,
dimweight=meta["nominate"]["second_dimweight"],
nom_beta=meta["nominate"]["beta"],
tabular_view=tab_view,
member_label=member_label)
return output
@app.route("/district")
@app.route("/district/<search_text>")
def district(search_text=""):
""" Show the district lookup page. """
meta = meta_lookup()
output = bottle.template("views/district", search=search_text,
dimweight=meta["nominate"]["second_dimweight"],
nom_beta=meta["nominate"]["beta"])
return output
@app.route("/parties")
@app.route("/parties/<party>/<cong_start>")
@app.route("/parties/<party>")
def parties(party="all", cong_start=-1):
""" Show the parties at a glance or party page. """
max_congress = config["max_congress"]
if isinstance(cong_start, str):
cong_start = -1
# Just default for now
try:
party = int(party)
except Exception:
output = bottle.template("views/parties_glance",
max_congress=max_congress)
return output
if cong_start == -1:
cong_start = int(max_congress)
else:
try:
cong_start = int(cong_start)
except Exception:
cong_start = 0
# Try to clamp invalid party IDs
if party > 8000:
party = 200
party_data = model.party_data.get_party_data(party)
output = bottle.template("views/parties", party=party,
party_data=party_data,
party_name_full=party_data["fullName"],
cong_start=cong_start)
return output
@app.route("/committees")
@app.route("/committees/<committee>")
@app.route("/committees/<committee>/<cong_start>")
def committees(committee="all", cong_start=-1):
""" Show committees overview or individual committee page. """
max_congress = config["max_congress"]
if committee == "all":
output = bottle.template("views/committees_glance",
max_congress=max_congress)
return output
if isinstance(cong_start, str):
try:
cong_start = int(cong_start)
except Exception:
cong_start = -1
if cong_start == -1:
cong_start = int(max_congress)
output = bottle.template("views/committees",
committee=committee,
cong_start=cong_start,
max_congress=max_congress)
return output
@app.route("/api/getmembersbycommittee")
def getmembersbycommittee():
""" Get members of a committee for a given congress. """
from model.config import db
from model.bio_data import congress_to_year
from model.search_parties import party_noun
short_name = default_value(bottle.request.params.short_name, "")
chamber = default_value(bottle.request.params.chamber, "")
try:
congress = int(default_value(bottle.request.params.congress, 0))
except Exception:
congress = 0
if not short_name or not chamber or not congress:
return {"error": "Missing parameters"}
doc = db.voteview_committees.find_one({
"short_name": short_name,
"chamber": chamber,
"congress": congress
})
if not doc:
return {"error": "Committee not found", "results": []}
results = []
seen_icpsr = set()
for m in doc.get("members", []):
icpsr = m.get("icpsr")
# Deduplicate by icpsr (data may have duplicates from name merging)
if icpsr and icpsr in seen_icpsr:
continue
if icpsr:
seen_icpsr.add(icpsr)
member_info = {
"icpsr": icpsr,
"bioname": m.get("bioname", ""),
"state_abbrev": m.get("state_abbrev", ""),
"party_code": m.get("party_code"),
"role": m.get("role", "Member"),
"rank": m.get("rank", 0),
}
# Party noun (matches search_members.py:231)
if member_info["party_code"]:
try:
member_info["party_noun"] = party_noun(
member_info["party_code"])
except Exception:
member_info["party_noun"] = ""
if icpsr:
# Enrich from voteview_members (matches search_members.py pattern)
vm = db.voteview_members.find_one(
{"icpsr": icpsr, "congress": congress},
{"nominate.dim1": 1, "congresses": 1, "chamber": 1,
"state_abbrev": 1}
)
if vm:
nom = vm.get("nominate", {})
member_info["nominate"] = {"dim1": nom.get("dim1")}
# min_elected: matches party tab (app.py getmembersbyparty)
congresses_field = vm.get("congresses", [])
if congresses_field:
member_info["min_elected"] = congress_to_year(
int(congresses_field[0][0]), 0)
member_info["chamber"] = vm.get("chamber", "")
if not member_info["state_abbrev"]:
member_info["state_abbrev"] = vm.get(
"state_abbrev", "")
# image_url: matches search_members.py:237-241
if os.path.isfile("static/img/bios/%s.jpg"
% str(icpsr).zfill(6)):
member_info["image_url"] = str(icpsr).zfill(6) + ".jpg"
else:
member_info["image_url"] = "silhouette.png"
results.append(member_info)
return {"results": results, "congress": congress}
@app.route("/api/getloyalty")
def getloyalty(party_code="", cong_number=""):
""" Get party loyalty for a given party-congress. """
party_code = default_value(bottle.request.params.party_code, party_code)
cong_number = default_value(bottle.request.params.congress, cong_number)
return get_loyalty(party_code, cong_number)
@app.route("/articles/<slug>")
def display_article(slug=""):
""" Display a blog article. """
if (not slug or not os.path.isfile(
os.path.join("static/articles", slug, slug + ".json"))):
return bottle.template(
"views/error",
error_message=(
"The article you selected is not a valid article ID."))
meta_set = get_article_meta(slug)
if not meta_set:
meta_set = {"title": "test"}
output = bottle.template("views/articles", slug=slug, meta=meta_set)
return output
@app.route("/person")
@app.route("/person/<icpsr>")
@app.route("/person/<icpsr>/<slug_name>")
def person(icpsr=0, slug_name=""):
""" Display a congressperson's bio page. """
# Truthfully this is a hack -- we want the route decorator above to match
# which means we need to include the argument as a function argument but
# we don't actually care about the variable.
del slug_name
clear_time()
time_it("begin")
if not icpsr:
icpsr = default_value(bottle.request.params.icpsr, 0)
# Easter Egg
keith = default_value(bottle.request.params.keith, 0)
# Pull member by ICPSR
person_response = member_lookup({"icpsr": icpsr}, 1)
time_it("memberLookup")
if "errormessage" in person_response:
output = bottle.template(
"views/error", error_message=person_response["errormessage"])
return output
# Extract the actual result.
person_extracted = person_response["results"][0]
# Assemble data
person_extracted = assemble_person_meta(person_extracted, keith)
twitter_card_result = twitter_card(person_extracted)
# Go to the template.
output = bottle.template("views/person", person=person_extracted,
time_set=zip_times(), skip=0,
twitter_card=twitter_card_result)
return output
def count_images(publication, file_number):
"""Return the number of scans in the directory."""
format_string = 'static/img/scans/{}/{:>03}/*'
glob_string = format_string.format(publication, file_number)
image_paths = glob.glob(glob_string)
return len(image_paths)
@app.route('/source_images/<publication>/BookReader')
@app.route('/source_images/<publication>/<file_number>/<page_number>',
name='source_images')
def source_images(publication, file_number, page_number, **kwargs):
""" Book reader image fetcher. """
del kwargs
return bottle.template(
'views/source_images',
publication=publication,
file_number=file_number,
page_number=page_number,
num_leafs=count_images(publication, file_number),
)
def mark_linkable_sources(sources):
""" Which sources are linkable. """
publications_currently_linkable = ['House Journal', ]
new_sources = []
for source in sources:
new = source.copy()
new['is_linkable'] = source[
'publication'] in publications_currently_linkable
new_sources.append(new)
return new_sources
@app.route("/rollcall")
@app.route("/rollcall/<rollcall_id>")
def display_rollcall(rollcall_id=""):
""" Display a single rollcall. """
# Error handling: User did not specify valid query parameters.
if not rollcall_id:
return bottle.template(
"views/error",
error_message="You did not provide a rollcall ID."
)
elif "," in rollcall_id:
return bottle.template(
"views/error",
error_message="You may only view one rollcall ID at a time."
)
# Get the rollcall and also whether or not to collapse minor parties.
rollcall = model.download_votes.download_votes_api(rollcall_id, "Web")
map_parties = int(default_value(bottle.request.params.map_parties, 1))
# After we got the rollcall, we found it didn't exist.
if "rollcalls" not in rollcall or "errormessage" in rollcall:
return bottle.template(
"views/error",
error_message=rollcall["errormessage"])
# Get NOMINATE params
meta = meta_lookup()
# Get sponsor info.
sponsor = {}
if "rollcalls" in rollcall and "sponsor" in rollcall["rollcalls"][0]:
try:
sponsor = [x for x in rollcall["rollcalls"][0]["votes"]
if x["icpsr"] == rollcall["rollcalls"][0]["sponsor"]][0]
except Exception:
sponsor = {}
# Subset the rollcall to the stuff we care about.
current_rollcall = rollcall["rollcalls"][0]
current_rollcall["date_user"] = fix_date(current_rollcall["date"])
# Make derived quantities we care about.
def suffix_gen(n):
""" Helper to quickly convert number n -> ordinal suffix nth """
subscript = (n // 10 % 10 != 1) * (n % 10 < 4) * n % 10
return "%d%s" % (n, "tsnrhtdd"[subscript::4])
plot_title = "Plot Vote: %s Congress > %s > %s" % (
suffix_gen(current_rollcall["congress"]),
current_rollcall["chamber"],
current_rollcall["rollnumber"])
notes = []
if int(current_rollcall["congress"]) < 86:
notes.append("State Boundaries depicted are as of the %s Congress." %
suffix_gen(current_rollcall["congress"]))
if (int(current_rollcall["congress"]) < 91 and
current_rollcall["chamber"] == "House"):
notes.append("Some states contain At-Large districts with more than "
"one representative.")
note_text = (("<strong><u>NOTE</u></strong><br/><ul>%s</ul>" %
" ".join(["<li>%s</li>" % note for note in notes]) + "</ul>")
if notes else "")
# Bill title text
titles = (current_rollcall.get('cg_official_titles', []) +
current_rollcall.get('cg_short_titles_for_portions', []))
if titles:
title_text = "; ".join(title for title in titles)
else:
title_text = ""
# Display template.
output = bottle.template(
"views/vote",
rollcall=current_rollcall,
dimweight=meta["nominate"]["second_dimweight"],
nom_beta=meta["nominate"]["beta"],
map_parties=map_parties,
sponsor=sponsor,
sources=mark_linkable_sources(current_rollcall.get("dtl_sources", [])),
note_text=note_text,
title_text=title_text,
plot_title=plot_title
)
return output
# Stash saved links redirect
@app.route("/s/<savedhash>")
def saved_hash_redirect(savedhash):
""" Redirect from a clean URL to a saved hash redirect. """
error_invalid = (
"Invalid redirect ID. This link is not valid. Please notify the person"
" who provided this link to you that it is not operational.")
if not savedhash:
return bottle.template("views/error", error_message=error_invalid)
status = model.stash_cart.check_exists(savedhash.strip())["status"]
if status:
return bottle.template("views/error", error_message=error_invalid)
bottle.redirect("/search/?q=saved: %s" % savedhash)
return {}
#
#
# API methods
#
#
@app.route("/api/getmembersbycongress", method="POST")
@app.route("/api/getmembersbycongress")
def getmembersbycongress():
""" Get all the members of the current congress. """
start_time = time.time()
which_congress = default_value(bottle.request.params.congress, 0)
chamber = default_value(bottle.request.params.chamber, "").title()
if chamber != "Senate" and chamber != "House":
chamber = ""
api = default_value(bottle.request.params.api, "")
out = get_members_by_congress(which_congress, chamber, api)
if api == "Web_Congress" and "results" in out:
for i in range(0, len(out["results"])):
member_row = out["results"][i]
if "congresses" not in member_row:
continue
member_row["min_elected"] = congress_to_year(
member_row["congresses"][0][0], 0)
out["results"][i] = member_row
out["timeElapsed"] = time.time() - start_time
return out
@app.route("/api/geocode")
def geocode():
""" Geocode a query parameter. """
geo_query = default_value(bottle.request.params.q, "")
if not geo_query:
return {"status": 1, "error_message": "No address specified."}
return address_to_lat_long(bottle.request, geo_query)
@app.route("/api/districtPolygonLookup")
def lookup_district_polygon():
""" Converts a latitude-longtiude pair to district polygon. """
try:
latitude = float(default_value(bottle.request.params.lat, 0))
longitude = float(default_value(bottle.request.params.long, 0))
except Exception:
return {"status": 1, "error_message": "Invalid lat/long coordinates."}
current_congress_polygon = lat_long_to_polygon(bottle.request,
latitude,
longitude)
if current_congress_polygon:
return {"polygon": current_congress_polygon}
return {"polygon": []}
@app.route("/api/districtLookup")
def district_lookup():
""" Convert a latitude-longitude pair to district information. """
try:
latitude = float(default_value(bottle.request.params.lat, 0))
longitude = float(default_value(bottle.request.params.long, 0))
except Exception:
return {"status": 1, "error_message": "Invalid lat/long coordinates."}
results = lat_long_to_districts(bottle.request, latitude, longitude)
if isinstance(results, dict) and "status" in results: # Quota error.
return results
if "results" not in results or not results["results"]:
return {"status": 1, "error_message": "No matches."}
return district_member_lookups(results)
@app.route("/api/getmembersbyparty")
def getmembersbyparty():
""" Get all the members of a current party. """
start_time = time.time()
member_id = default_value(bottle.request.params.id, 0)
try:
congress = int(default_value(bottle.request.params.congress, 0))
except Exception:
congress = 0
api = default_value(bottle.request.params.api, "")
out = get_members_by_party(member_id, congress, api)
if api == "Web_Party" and "results" in out:
for i in range(0, len(out["results"])):
member_row = out["results"][i]
if "congresses" not in member_row:
continue
member_row["min_elected"] = congress_to_year(
member_row["congresses"][0][0], 0)
out["results"][i] = member_row
out["timeElapsed"] = time.time() - start_time
return out
@app.route("/api/getmembers", method="POST")
@app.route("/api/getmembers")
def getmembers():
""" Get all members matching a query. """
qdict = {}
distinct = 0
api = "Web"
# Transparently pass through the entire query dictionary
for key, value in bottle.request.params.items():
if key == 'distinct':
distinct = int(default_value(value, 0))
elif key == 'api':
api = default_value(value, "Web")
else:
qdict[key] = default_value(value)
return member_lookup(qdict, distinct=distinct, api=api)
@app.route("/api/searchAssemble", method="POST")
@app.route("/api/searchAssemble")
def api_assemble_search():
""" Assemble a full search query. """
search_query = default_value(bottle.request.params.q)
next_id = default_value(bottle.request.params.nextId, 0)
out = assemble_search(search_query, next_id, bottle)
return out
@app.route("/api/getMemberVotesAssemble")
def assemble_member_votes(icpsr=0, qtext="", skip=0):
""" Assembles a member's votes. """
icpsr = default_value(bottle.request.params.icpsr, 0)
qtext = default_value(bottle.request.params.qtext, "")
skip = default_value(bottle.request.params.skip, 0)
try:
sort_col = int(bottle.request.params.get("sortCol", 0))
except (ValueError, TypeError):
sort_col = 0
try:
sort_dir = int(bottle.request.params.get("sortDir", -1))
except (ValueError, TypeError):
sort_dir = -1
if sort_dir not in [-1, 1]:
sort_dir = -1
if sort_col not in [0, 2, 3, 4, 5]:
sort_col = 0
if not icpsr:
output = bottle.template(
"views/error", error_message="No member specified.")
bottle.response.headers["Nextid"] = 0
return output
person_response = member_lookup({"icpsr": icpsr})
if "error" not in person_response:
person_extracted = person_response["results"][0]
else:
output = bottle.template(
"views/error", error_message=person_response["errormessage"])
bottle.response.headers["Nextid"] = 0
return output
if qtext:
qtext = qtext + " AND (voter: " + str(person_extracted["icpsr"]) + ")"
else:
qtext = "voter: " + str(person_extracted["icpsr"])
if sort_col != 0:
# query() handles freeform search syntax and returns date-sorted
# rollcall IDs; the compound voter+date index makes that fast. Then a
# single $elemMatch fetch projects only the voter's own entry, and we
# sort in Python.
vote_query = query(qtext, row_limit=5000, jsapi=1, ids_only=1,
sort_dir=-1, request=bottle.request)
if "errormessage" in vote_query:
return bottle.template("views/error",
error_message=vote_query["errormessage"])
rollcall_ids = [v["id"] for v in vote_query.get("rollcalls", [])]
votes = prep_person_votes_fast(rollcall_ids, person_extracted)
votes = sort_votes_by_column(votes, sort_col, sort_dir)
next_id = 0
else:
# Date sort: cursor-based pagination
if skip:
vote_query = query(qtext, row_limit=25, jsapi=1,
sort_skip=skip, sort_dir=sort_dir,
request=bottle.request)
else:
vote_query = query(qtext, row_limit=25, jsapi=1,
sort_dir=sort_dir, request=bottle.request)
if "errormessage" in vote_query:
return bottle.template("views/error",
error_message=vote_query["errormessage"])
votes = prep_votes(vote_query, person_extracted)
next_id = vote_query["next_id"]
output = bottle.template(
"views/member_votes", person=person_extracted, votes=votes,
skip=skip, next_id=next_id)
bottle.response.headers["Nextid"] = next_id
return output
@app.route("/api/search", method="POST")
@app.route("/api/search")
def search():
""" Executes an API search for votes and members. """
search_query = default_value(bottle.request.params.q)
startdate = default_value(bottle.request.params.startdate)
enddate = default_value(bottle.request.params.enddate)
chamber = default_value(bottle.request.params.chamber)
icpsr = default_value(bottle.request.params.icpsr)
rapi = default_value(bottle.request.params.rapi, 0)
res = query(search_query, startdate, enddate, chamber, icpsr=icpsr,
rapi=rapi, request=bottle.request)
return res
@app.route("/api/getPartyData", method="POST")
@app.route("/api/getPartyData")
def get_party_name():
""" Return party data by ID. """
party_id = default_value(bottle.request.params.id)
return model.party_data.get_party_data(party_id)
@app.route("/api/download", method="POST")
@app.route("/api/download")
@app.route("/api/download/<rollcall_id>")
def download_votes(rollcall_id=""):
""" Downloads rollcall vote or votes. """
if not rollcall_id:
rollcall_id = default_value(bottle.request.params.rollcall_id)
apitype = default_value(bottle.request.params.apitype, "Web")
results = model.download_votes.download_votes_api(rollcall_id, apitype)
return results
@app.route("/api/exportJSON", method="POST")
@app.route("/api/exportJSON")
def stash_export_json():
""" Exports current stash as JSON. """
stash_id = default_value(bottle.request.params.id, "")
return model.download_votes.download_stash(stash_id)
@app.route("/api/download_excel", method="POST")
@app.route("/api/download_excel")
def download_excel():
""" Download Excel file of votes. """
try:
stash_id = default_value(bottle.request.params.stash, "")
except Exception:
stash_id = ""
try:
ids = bottle.request.params.getall("ids")
except Exception:
ids = []
try:
if isinstance(ids, list):
ids = ",".join(ids)
except Exception:
pass
if stash_id:
status_code, result = model.download_excel.download_stash(stash_id)
else:
status_code, result = model.download_excel.download_excel(ids)
if status_code != 0:
return {"errormessage": result}
bottle.response.content_type = 'application/vnd.ms-excel'
current_date_string = datetime.datetime.now().strftime("%Y%m%d_%H%M")
output_filename = current_date_string + "_voteview_download.xls"
bottle.response.headers["Content-Disposition"] = "inline; filename=%s" % (
output_filename)
return result
@app.route("/api/newsletter", method="POST")
@app.route("/api/newsletter")
def newsletter():
""" Subscribe to newsletter. """
try:
email = bottle.request.params.update_email
update_action = bottle.request.params.update_action
res = newsletter_subscribe(email, update_action)
return res
except Exception:
return {"error": (
"An unknown error occurred while processing your request.")}
@app.route("/api/contact", method="POST")
@app.route("/api/contact")
def contact():
""" Send a contact email. """
# pylint: disable=E1136
try:
title = bottle.request.params.title
body = bottle.request.params.body
email = bottle.request.params.email
person_name = bottle.request.params.yourname
recaptcha = bottle.request.params["g-recaptcha-response"]
ip_address = bottle.request.get("REMOTE_ADDR")
res = send_email(title=title,
body=body,
person_name=person_name,
email=email,
recaptcha=recaptcha,
client_ip=ip_address,
test=0)
return res
except Exception:
return {"error": (
"You must fill out the entire form before submitting.")}
@app.route("/api/stash/<verb:re:init|add|del|get|empty>")
def stash(verb):
""" Dispatch tasks to stash; add, delete, get, empty, etc. """
try:
stash_id = default_value(bottle.request.params.id, "")
votes = bottle.request.params.getall("votes")
except Exception:
votes = []
return model.stash_cart.verb_dispatch(verb, stash_id, votes)
@app.route("/api/shareableLink")
@app.route("/api/shareableLink", method="POST")
def stash_share_link():
""" Generate a shareable link for a given stash ID. """
try:
base_url = get_base(bottle.request.urlparts)
share_id = default_value(bottle.request.params.id, "")
text = default_value(bottle.request.params.text, "")
except Exception:
return {"errorMessage": "Invalid ID or text"}
return model.stash_cart.shareable_link(share_id, text, base_url=base_url)
@app.route("/api/downloaddata", method="POST")
@app.route("/api/downloaddata")
def download_data():
""" Returns a particular data file. """