Пример #1
0
def legislators():
    """
    Legislator list page.
    """
    context = make_context()

    senate_list = Legislator.select().where(Legislator.office == 'Senator')
    house_list = Legislator.select().where(Legislator.office == 'Representative')

    context['senate_list'] = senate_list
    context['house_list'] = house_list

    return render_template('legislator_list.html', **context)    
Пример #2
0
def legislators():
    """
    Legislator list page.
    """
    context = make_context()

    senate_list = Legislator.select().where(Legislator.office == 'Senator')
    house_list = Legislator.select().where(
        Legislator.office == 'Representative')

    context['senate_list'] = senate_list
    context['house_list'] = house_list

    return render_template('legislator_list.html', **context)
Пример #3
0
def sitemap():
    """
    Renders a sitemap.
    """
    context = make_context()
    context['pages'] = []

    now = datetime.date.today().isoformat()

    context['pages'].append(('/', now))
    context['pages'].append(('/methodology/', now))
    context['pages'].append(('/legislators/', now))
    context['pages'].append(('/organizations/', now))

    for legislator in Legislator.select():
        context['pages'].append((url_for('_legislator',
                                         slug=legislator.slug), now))

    for organization in Organization.select():
        context['pages'].append((url_for('_organization',
                                         slug=organization.slug), now))

    sitemap = render_template('sitemap.xml', **context)

    return (sitemap, 200, {'content-type': 'application/xml'})
Пример #4
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)
Пример #5
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)
Пример #6
0
def sitemap():
    """
    Renders a sitemap.
    """
    context = make_context()
    context['pages'] = []

    now = datetime.date.today().isoformat()

    context['pages'].append(('/', now))
    context['pages'].append(('/methodology/', now))
    context['pages'].append(('/legislators/', now))
    context['pages'].append(('/organizations/', now))

    for legislator in Legislator.select():
        context['pages'].append((url_for('_legislator', slug=legislator.slug), now))

    for organization in Organization.select():
        context['pages'].append((url_for('_organization', slug=organization.slug), now))

    sitemap = render_template('sitemap.xml', **context)

    return (sitemap, 200, { 'content-type': 'application/xml' })
Пример #7
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)
Пример #8
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)