예제 #1
0
파일: app.py 프로젝트: strogo/stl-lobbying
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)
예제 #2
0
def district(state, district):
    legs = Legislator.members_for_state_and_district(state, district)

    context = {
        'legislators': legs,
        'humanized_district': Legislator.humanized_district(state, district),
        'geojson_url': Legislator.get_district_geojson_url(state, district)
    }

    return render_template_wctx('pages/congressional_district.html', context=context)
예제 #3
0
파일: app.py 프로젝트: strogo/stl-lobbying
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)    
예제 #4
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)
예제 #5
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'})
예제 #6
0
def confirm_reps(token='', msg=None, umi=None, user=None):
    """
    Confirm members of congress and submit message (if message present)

    @param token: token to identify the user / message
    @type token: string
    @return:
    @rtype:
    """

    form = forms.MessageForm(request.form, append_get_params(url_for_with_prefix('app_router.' + inspect.stack()[0][3], token=token)))

    moc = umi.members_of_congress

    context = {
        'form': form,
        'msg': msg,
        'user': user,
        'umi': umi,
        'legislators': moc
    }

    if msg is not None and request.method == 'POST' and form.validate() and request.form.getlist('legislator_choices[]'):
        if not request.form.get('donotsend', False):
            legs = [moc[int(i)] for i in request.form.getlist('legislator_choices[]')]
            msg.queue_to_send(legs)
            emailer.NoReply.message_queued(user, legs, msg).send()
        return redirect(url_for_with_prefix('app_router.message_sent', token=token))
    else:
        if msg is not None:
            form.populate_data_from_message(msg)
            context['legs_buckets'] = Legislator.get_leg_buckets_from_emails(umi.members_of_congress, json.loads(msg.to_originally))
        return render_template_wctx('pages/confirm_reps.html', context=context)
예제 #7
0
def confirm_reps(token='', msg=None, umi=None, user=None):
    """
    Confirm members of congress and submit message (if message present)

    @param token: token to identify the user / message
    @type token: string
    @return:
    @rtype:
    """

    form = forms.MessageForm(request.form, append_get_params(url_for_with_prefix('app_router.' + inspect.stack()[0][3], token=token)))

    moc = umi.members_of_congress

    context = {
        'form': form,
        'message': msg,
        'user': user,
        'umi': umi,
        'legislators': moc
    }

    if msg is not None and request.method == 'POST' and form.validate():
        msg.queue_to_send([moc[i] for i in [v for v in range(0, len(moc)) if request.form.get('legislator_' + str(v))]])
        return render_template_wctx('pages/message_sent.html', context=context)
    else:
        if msg is not None:
            form.populate_data_from_message(msg)
            context['legs_buckets'] = Legislator.get_leg_buckets_from_emails(umi.members_of_congress, json.loads(msg.to_originally))
        return render_template_wctx('pages/confirm_reps.html', context=context)
예제 #8
0
파일: app.py 프로젝트: strogo/stl-lobbying
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)
예제 #9
0
파일: app.py 프로젝트: strogo/stl-lobbying
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)
예제 #10
0
def process_inbound_message(user, umi, msg, send_email=False):

    legs = Legislator.get_leg_buckets_from_emails(umi.members_of_congress, json.loads(msg.to_originally))
    msg.set_legislators(legs['contactable'])

    if msg.has_legislators() and msg.is_free_to_send():
        emailer.NoReply.message_queued(user, legs['contactable'], msg).send()
        msg.queue_to_send()
    if legs['does_not_represent'] or legs['non_existent']:
        emailer.NoReply.message_undeliverable(user, legs, msg).send()

    return jsonify({'status': 'success'})
예제 #11
0
def process_inbound_message(user, umi, msg, send_email=False):

    legs = Legislator.get_leg_buckets_from_emails(umi.members_of_congress, json.loads(msg.to_originally))
    msg.set_legislators(legs['contactable'])

    if msg.is_free_to_send():
        msg.queue_to_send()

    if send_email:
        emailer.NoReply.message_receipt(user, legs, msg).send()

    return jsonify({'status': 'success'})
예제 #12
0
def create_admin(app, csrf):
    from models import db, User, Legislator, AdminUser, UserMessageInfo, Message, MessageLegislator, Topic
    from flask.ext.admin.menu import MenuLink

    admin = Admin(index_view=views.MyAdminIndexView(url=settings.BASE_PREFIX + '/admin'))
    admin.add_link(MenuLink('Logout', url=settings.BASE_PREFIX + '/admin/logout'))
    admin.add_view(User.ModelView(User, db.session))
    admin.add_view(Legislator.ModelView(Legislator, db.session))
    admin.add_view(AdminUser.ModelView(AdminUser, db.session))
    admin.add_view(UserMessageInfo.ModelView(UserMessageInfo, db.session))
    admin.add_view(Message.ModelView(Message, db.session))
    admin.add_view(MessageLegislator.ModelView(MessageLegislator, db.session))
    admin.add_view(Topic.ModelView(Topic, db.session))

    return admin
예제 #13
0
파일: app.py 프로젝트: strogo/stl-lobbying
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' })
예제 #14
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)
예제 #15
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)
예제 #16
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)