示例#1
0
def download_csv():
    """
    Generate a data download.
    """
    f = cStringIO.StringIO()

    writer = UnicodeCSVDictWriter(f, [
        'lobbyist_first_name',
        'lobbyist_last_name',
        'report_period',
        'recipient_name',
        'recipient_type',
        'legislator_first_name',
        'legislator_last_name',
        'legislator_office',
        'legislator_party',
        'legislator_district',
        'event_date',
        'category',
        'description',
        'cost',
        'organization_name',
        'organization_industry',
        'group',
        'ethics_board_id',
        'is_solicitation'
    ])

    writer.writeheader()

    expenditures = Expenditure.select()

    for ex in expenditures:
        row = {
            'lobbyist_first_name': ex.lobbyist.first_name,
            'lobbyist_last_name': ex.lobbyist.last_name,
            'report_period': ex.report_period,
            'recipient_name': ex.recipient,
            'recipient_type': ex.recipient_type,
            'legislator_first_name': ex.legislator.first_name if ex.legislator else None,
            'legislator_last_name': ex.legislator.last_name if ex.legislator else None,
            'legislator_office': ex.legislator.office if ex.legislator else None,
            'legislator_party': ex.legislator.party if ex.legislator else None,
            'legislator_district': ex.legislator.district if ex.legislator else None,
            'event_date': ex.event_date,
            'category': ex.category,
            'description': ex.description,
            'cost': ex.cost,
            'organization_name': ex.organization.name,
            'organization_industry': ex.organization.category,
            'group': ex.group.name if ex.group else None,
            'ethics_board_id': ex.ethics_id,
            'is_solicitation': ex.is_solicitation
        }

        writer.writerow(row)

    return f.getvalue().decode('utf-8')
示例#2
0
def get_ago():
    """
    Generate a datetime that will include 24 reporting periods
    for which we have data.
    """
    most_recent = Expenditure.select().order_by(Expenditure.report_period.desc()).limit(1)[0].report_period

    ago = datetime.date(most_recent.year - 2, most_recent.month + 1, 1)

    return ago
示例#3
0
def get_ago():
    """
    Generate a datetime that will include 24 reporting periods
    for which we have data.
    """
    most_recent = Expenditure.select().order_by(Expenditure.report_period.desc()).limit(1)[0].report_period

    # Get the previous month. If previous month is December, set to 12. 
    month = most_recent.month + 1
    if month > 12: 
        month = 12

    ago = datetime.date(most_recent.year - 2, month, 1)

    return ago
示例#4
0
def get_ago():
    """
    Generate a datetime that will include 24 reporting periods
    for which we have data.
    """
    most_recent = Expenditure.select().order_by(
        Expenditure.report_period.desc()).limit(1)[0].report_period

    # Get the previous month. If previous month is December, set to 12.
    month = most_recent.month + 1
    if month > 12:
        month = 12

    ago = datetime.date(most_recent.year - 2, month, 1)

    return ago
示例#5
0
def index():
    """
    Example view demonstrating rendering a simple HTML page.
    """
    context = make_context()

    ago = get_ago()

    expenditures = Expenditure.select().where(Expenditure.report_period >= ago)
    organizations = Organization.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
    lobbyists = Lobbyist.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()
    legislators = Legislator.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()

    for legislator in legislators:
        legislator.total_spending = legislator.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))

    legislators_total_spending = sorted(legislators, key=lambda l: l.total_spending, reverse=True)[:10]
    
    categories_total_spending = {}

    for org in organizations:
        org.total_spending = org.expenditures.where(Expenditure.report_period >= ago).aggregate(fn.Sum(Expenditure.cost))

        if not org.total_spending:
            continue

        if org.category in categories_total_spending:
            categories_total_spending[org.category] += org.total_spending
        else:
            categories_total_spending[org.category] = org.total_spending

    organizations_total_spending = sorted(organizations, key=lambda o: o.total_spending, reverse=True)[:10]
    categories_total_spending = sorted(categories_total_spending.items(), key=lambda c: c[1], reverse=True)

    context['senators'] = Legislator.select().where(Legislator.office == 'Senator')
    context['representatives'] = Legislator.select().where(Legislator.office == 'Representative')
    context['expenditures'] = expenditures
    context['total_spending'] = expenditures.aggregate(fn.Sum(Expenditure.cost)) 
    context['total_expenditures'] = expenditures.count()
    context['total_organizations'] = organizations.count()
    context['total_lobbyists'] = lobbyists.count()
    context['organizations_total_spending'] = organizations_total_spending
    context['legislators_total_spending'] = legislators_total_spending
    context['categories_total_spending'] = categories_total_spending

    return render_template('index.html', **context)
示例#6
0
def index():
    """
    Example view demonstrating rendering a simple HTML page.
    """
    context = make_context()

    ago = get_ago()

    expenditures = Expenditure.select().where(Expenditure.report_period >= ago)
    organizations = Organization.select().join(Expenditure).where(
        Expenditure.report_period >= ago).distinct()
    lobbyists = Lobbyist.select().join(Expenditure).where(
        Expenditure.report_period >= ago).distinct()
    legislators = Legislator.select().join(Expenditure).where(
        Expenditure.report_period >= ago).distinct()

    for legislator in legislators:
        legislator.total_spending = legislator.expenditures.where(
            Expenditure.report_period >= ago).aggregate(
                fn.Sum(Expenditure.cost))

    legislators_total_spending = sorted(legislators,
                                        key=lambda l: l.total_spending,
                                        reverse=True)[:10]

    categories_total_spending = {}

    for org in organizations:
        org.total_spending = org.expenditures.where(
            Expenditure.report_period >= ago).aggregate(
                fn.Sum(Expenditure.cost))

        if not org.total_spending:
            continue

        if org.category in categories_total_spending:
            categories_total_spending[org.category] += org.total_spending
        else:
            categories_total_spending[org.category] = org.total_spending

    organizations_total_spending = sorted(organizations,
                                          key=lambda o: o.total_spending,
                                          reverse=True)[:10]
    categories_total_spending = sorted(categories_total_spending.items(),
                                       key=lambda c: c[1],
                                       reverse=True)

    context['senators'] = Legislator.select().where(
        Legislator.office == 'Senator')
    context['representatives'] = Legislator.select().where(
        Legislator.office == 'Representative')
    context['expenditures'] = expenditures
    context['total_spending'] = expenditures.aggregate(fn.Sum(
        Expenditure.cost))
    context['total_expenditures'] = expenditures.count()
    context['total_organizations'] = organizations.count()
    context['total_lobbyists'] = lobbyists.count()
    context['organizations_total_spending'] = organizations_total_spending
    context['legislators_total_spending'] = legislators_total_spending
    context['categories_total_spending'] = categories_total_spending

    return render_template('index.html', **context)
示例#7
0
def download_csv():
    """
    Generate a data download.
    """
    f = cStringIO.StringIO()

    writer = UnicodeCSVDictWriter(f, [
        'lobbyist_first_name', 'lobbyist_last_name', 'report_period',
        'recipient_name', 'recipient_type', 'legislator_first_name',
        'legislator_last_name', 'legislator_office', 'legislator_party',
        'legislator_district', 'event_date', 'category', 'description', 'cost',
        'organization_name', 'organization_industry', 'group',
        'ethics_board_id', 'is_solicitation'
    ])

    writer.writeheader()

    expenditures = Expenditure.select()

    for ex in expenditures:
        row = {
            'lobbyist_first_name':
            ex.lobbyist.first_name,
            'lobbyist_last_name':
            ex.lobbyist.last_name,
            'report_period':
            ex.report_period,
            'recipient_name':
            ex.recipient,
            'recipient_type':
            ex.recipient_type,
            'legislator_first_name':
            ex.legislator.first_name if ex.legislator else None,
            'legislator_last_name':
            ex.legislator.last_name if ex.legislator else None,
            'legislator_office':
            ex.legislator.office if ex.legislator else None,
            'legislator_party':
            ex.legislator.party if ex.legislator else None,
            'legislator_district':
            ex.legislator.district if ex.legislator else None,
            'event_date':
            ex.event_date,
            'category':
            ex.category,
            'description':
            ex.description,
            'cost':
            ex.cost,
            'organization_name':
            ex.organization.name,
            'organization_industry':
            ex.organization.category,
            'group':
            ex.group.name if ex.group else None,
            'ethics_board_id':
            ex.ethics_id,
            'is_solicitation':
            ex.is_solicitation
        }

        writer.writerow(row)

    return f.getvalue().decode('utf-8')