Exemple #1
0
def _legislator(slug):
    """
    Legislator detail page.
    """
    context = make_context()

    ago = get_ago()

    legislators = Legislator.select()
    legislator = Legislator.get(Legislator.slug==slug)

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

    legislators_total_spending = sorted(legislators, key=lambda l: l.total_spending, reverse=True)
    
    legislator_rank = None

    for i, l in enumerate(legislators_total_spending):
        if l.id == legislator.id:
            legislator_rank = i + 1

    org_spending = {}

    for ex in legislator.expenditures:
        if ex.organization.id in org_spending:
            org_spending[ex.organization.id] += ex.cost
        else:
            org_spending[ex.organization.id] = ex.cost

    top_organizations = []
    top_categories = {}

    for org_id, spending in org_spending.items():
        org = Organization.get(Organization.id == org_id)
        org.total_spending = spending
        top_organizations.append(org)

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

    top_organizations = sorted(top_organizations, key=lambda o: o.total_spending, reverse=True)[:10]
    top_categories = sorted(top_categories.items(), key=lambda c: c[1], reverse=True)

    context['legislator'] = legislator
    context['expenditures_recent'] = legislator.expenditures.where(Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
    context['total_spending'] = sum([e.cost for e in legislator.expenditures]) 
    context['total_spending_recent'] = sum([e.cost for e in legislator.expenditures.where(Expenditure.report_period >= ago)]) 
    context['total_expenditures'] = legislator.expenditures.count()
    context['total_expenditures_recent'] = legislator.expenditures.where(Expenditure.report_period >= ago).count()
    context['top_organizations'] = top_organizations 
    context['legislator_rank'] = legislator_rank
    context['top_categories'] = top_categories

    return render_template('legislator.html', **context)
Exemple #2
0
def _organization(slug):
    """
    Organization detail page.
    """
    context = make_context()

    ago = get_ago()
    
    organization = Organization.get(Organization.slug==slug)
    organizations = Organization.select().join(Expenditure).where(Expenditure.report_period >= ago).distinct()

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

    organizations_total_spending = sorted(organizations, key=lambda o: o.total_spending, reverse=True)
    
    organization_rank = None

    for i, o in enumerate(organizations_total_spending):
        if o.id == organization.id:
            organization_rank = i + 1

    legislator_spending = {}

    for ex in organization.expenditures:
        # Groups or old/non-attributable expenses
        if not ex.legislator:
            continue

        if ex.legislator.id in legislator_spending:
            legislator_spending[ex.legislator.id] += ex.cost
        else:
            legislator_spending[ex.legislator.id] = ex.cost

    top_legislators = []

    for legislator_id, spending in legislator_spending.items():
        legislator = Legislator.get(Legislator.id == legislator_id)
        legislator.total_spending = spending
        top_legislators.append(legislator)

    top_legislators = sorted(top_legislators, key=lambda o: o.total_spending, reverse=True)[:10]

    context['organization'] = organization
    context['expenditures_recent'] = organization.expenditures.where(Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
    context['total_spending'] = sum([e.cost for e in organization.expenditures]) 
    context['total_spending_recent'] = sum([e.cost for e in organization.expenditures.where(Expenditure.report_period >= ago)]) 
    context['total_expenditures'] = organization.expenditures.count()
    context['total_expenditures_recent'] = organization.expenditures.where(Expenditure.report_period >= ago).count()
    context['top_legislators'] = top_legislators 
    context['organization_rank'] = organization_rank

    return render_template('organization.html', **context)
Exemple #3
0
def _organization(slug):
    """
    Organization detail page.
    """
    context = make_context()

    ago = get_ago()

    organization = Organization.get(Organization.slug == slug)
    organizations = Organization.select().join(Expenditure).where(
        Expenditure.report_period >= ago).distinct()

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

    organizations_total_spending = sorted(organizations,
                                          key=lambda o: o.total_spending,
                                          reverse=True)

    organization_rank = None

    for i, o in enumerate(organizations_total_spending):
        if o.id == organization.id:
            organization_rank = i + 1

    legislator_spending = {}

    for ex in organization.expenditures:
        # Groups or old/non-attributable expenses
        if not ex.legislator:
            continue

        if ex.legislator.id in legislator_spending:
            legislator_spending[ex.legislator.id] += ex.cost
        else:
            legislator_spending[ex.legislator.id] = ex.cost

    top_legislators = []

    for legislator_id, spending in legislator_spending.items():
        legislator = Legislator.get(Legislator.id == legislator_id)
        legislator.total_spending = spending
        top_legislators.append(legislator)

    top_legislators = sorted(top_legislators,
                             key=lambda o: o.total_spending,
                             reverse=True)[:10]

    context['organization'] = organization
    context['expenditures_recent'] = organization.expenditures.where(
        Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
    context['total_spending'] = sum(
        [e.cost for e in organization.expenditures])
    context['total_spending_recent'] = sum([
        e.cost for e in organization.expenditures.where(
            Expenditure.report_period >= ago)
    ])
    context['total_expenditures'] = organization.expenditures.count()
    context['total_expenditures_recent'] = organization.expenditures.where(
        Expenditure.report_period >= ago).count()
    context['top_legislators'] = top_legislators
    context['organization_rank'] = organization_rank

    return render_template('organization.html', **context)
Exemple #4
0
def _legislator(slug):
    """
    Legislator detail page.
    """
    context = make_context()

    ago = get_ago()

    legislators = Legislator.select()
    legislator = Legislator.get(Legislator.slug == slug)

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

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

    legislator_rank = None

    for i, l in enumerate(legislators_total_spending):
        if l.id == legislator.id:
            legislator_rank = i + 1

    org_spending = {}

    for ex in legislator.expenditures:
        if ex.organization.id in org_spending:
            org_spending[ex.organization.id] += ex.cost
        else:
            org_spending[ex.organization.id] = ex.cost

    top_organizations = []
    top_categories = {}

    for org_id, spending in org_spending.items():
        org = Organization.get(Organization.id == org_id)
        org.total_spending = spending
        top_organizations.append(org)

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

    top_organizations = sorted(top_organizations,
                               key=lambda o: o.total_spending,
                               reverse=True)[:10]
    top_categories = sorted(top_categories.items(),
                            key=lambda c: c[1],
                            reverse=True)

    context['legislator'] = legislator
    context['expenditures_recent'] = legislator.expenditures.where(
        Expenditure.report_period >= ago).order_by(Expenditure.cost.desc())
    context['total_spending'] = sum([e.cost for e in legislator.expenditures])
    context['total_spending_recent'] = sum([
        e.cost for e in legislator.expenditures.where(
            Expenditure.report_period >= ago)
    ])
    context['total_expenditures'] = legislator.expenditures.count()
    context['total_expenditures_recent'] = legislator.expenditures.where(
        Expenditure.report_period >= ago).count()
    context['top_organizations'] = top_organizations
    context['legislator_rank'] = legislator_rank
    context['top_categories'] = top_categories

    return render_template('legislator.html', **context)