Пример #1
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'})
Пример #2
0
def organizations():
    """
    Legislator list page.
    """
    context = make_context()

    context['organizations'] = Organization.select().order_by(Organization.name)

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

    context['organizations'] = Organization.select().order_by(
        Organization.name)

    return render_template('organization_list.html', **context)
Пример #4
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)
Пример #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 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 _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)
 def organizations(self):
     organizations = []
     for organization in Organization.select():
         organizations.append(organization.dict())
     return organizations