def add_expenditure(jwt): try: new_expense = Expenditure(**request.json) except TypeError: abort(422) new_expense.insert() return jsonify({ "success": True, "expenditure": new_expense.dict_form(), })
def load_expenditures(): """ Load expenditures from expenditures.csv into database """ Expenditure.query.delete() with open(expenditure_file) as f: for _ in range(1): next(f) for row in f: row = row.rstrip() expenditure_data = row.split(",") print(expenditure_data) id = expenditure_data[0] category_id = expenditure_data[1] price = expenditure_data[2] date_of_expenditure = expenditure_data[3] expenditure_userid = expenditure_data[4] where_bought = expenditure_data[5] description = expenditure_data[6] expenditure = Expenditure( id=id, category_id=category_id, price=price, date_of_expenditure=get_datetime(date_of_expenditure), expenditure_userid=expenditure_userid, where_bought=where_bought, description=description) db.session.add(expenditure) db.session.commit()
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')
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
def add_expenditure(): """ Add new expenditure to the database """ from models import Budget, Expenditure, User, Category from utils import expenditure_total_amount_and_avg, budget_totals, get_dates_for_budget, get_progress, get_budget_per_category, connect_to_db # Set the value of the user id of the user in the session id = session.get('id') # Get values from the form category_id = int(request.form.get("category")) price = request.form.get("price") date_of_expenditure = request.form.get("date") where_bought = request.form.get("wherebought") description = request.form.get("description") start_date, end_date = get_dates_for_budget(category_id, id) # Create a new expenditure object to insert into the expenditures table new_expenditure = Expenditure( category_id = category_id, price = price, date_of_expenditure = date_of_expenditure, where_bought = where_bought, description = description, expenditure_userid = id ) # Insert the new expenditure into the expenditures table and commit the insert db.session.add(new_expenditure) db.session.commit() # Unpacking the function call total_cat_price, avg_cat_expenditures = expenditure_total_amount_and_avg(category_id, id, start_date, end_date) budget_minus_expenses = budget_totals(category_id, id, total_cat_price) cat_budget = get_budget_per_category(category_id, id) category_progress = get_progress(budget_minus_expenses, cat_budget) expenditure_info = { 'total_cat_price': total_cat_price, 'avg_cat_expenditures': avg_cat_expenditures, 'category_id': category_id, 'expenditure_id': new_expenditure.id, 'date_of_expenditure': new_expenditure.date_of_expenditure.strftime('%Y-%m-%d'), 'where_bought': new_expenditure.where_bought, 'description': new_expenditure.description, 'price': str(new_expenditure.price), 'category': new_expenditure.category.category, 'cat_budget_minus_expenses': budget_minus_expenses, 'category_progress': category_progress } return jsonify(expenditure_info)
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
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
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)
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)