Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
add presidential csv
Browse files Browse the repository at this point in the history
  • Loading branch information
rshorey committed Apr 9, 2019
1 parent c7b0b89 commit b7d07e7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cycle_2020/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
re_path(r'candidates/$', views.candidates, name='candidates'),
re_path(r'candidates_csv/$', views.candidates_csv, name='candidates_csv'),
re_path(r'inaugural/$', views.inaugural, name='inaugural'),
re_path(r'presidential_csv/$', views.presidential_csv, name='presidential_csv')

]
62 changes: 62 additions & 0 deletions cycle_2020/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,68 @@ def rows_with_totals():
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response


def presidential_csv(request):
deadline = request.GET.get('deadline')

results = Candidate.objects.filter(office='P').order_by('party', 'name')
filename = "Candidates_{}.csv".format(time.strftime("%Y%m%d-%H%M%S"))

candidate_fields = ['name', 'office', 'party', 'incumbent']
filing_fields = ['filing_id','form','filer_id',
'committee_name', 'cash_on_hand_close_of_period', 'is_amendment',
'period_total_receipts','cycle_total_receipts',
'period_total_disbursements', 'cycle_total_disbursements',
'period_individuals_unitemized', 'cycle_individuals_unitemized',
'period_individuals_itemized', 'cycle_individuals_itemized',
'period_transfers_from_authorized','cycle_transfers_from_authorized']

def rows_with_totals():
yield candidate_fields+filing_fields+['cycle_candidate_donations_plus_loans','period_candidate_donations_plus_loans']
for result in results:
if deadline:
filing = result.filing_by_deadline(deadline)
else:
filing = result.most_recent_filing()

row = []
for f in candidate_fields:
value = getattr(result, f)
if value is None:
row.append("")
else:
row.append(value)
for f in filing_fields:
if not filing:
row.append("")
continue
value = getattr(filing, f)
if value is None:
row.append("")
else:
row.append(value)
if filing:
cycle_candidate_donations_plus_loans = filing.cycle_candidate_donations_plus_loans #this has to be done separately bc it's a property.
period_candidate_donations_plus_loans = filing.period_candidate_donations_plus_loans
if not filing or cycle_candidate_donations_plus_loans is None:
row.append("")
else:
row.append(cycle_candidate_donations_plus_loans)
if not filing or period_candidate_donations_plus_loans is None:
row.append("")
else:
row.append(period_candidate_donations_plus_loans)
yield row


pseudo_buffer = Echo()
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in rows_with_totals()),
content_type="text/csv")

response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response

def inaugural(request):
form = InauguralForm(request.GET)
if not request.GET:
Expand Down

0 comments on commit b7d07e7

Please sign in to comment.