def monthly_chunk_periods(from_date, to_date): # dates should be from_date, to_date and the first and last days of every month included months = list(set(utils.monthrange(from_date, to_date))) dates = [(max(utils.start_of_month(x[1], x[0]), from_date), min(utils.end_of_month(x[1], x[0]), to_date)) for x in months] periods = [{'from': dt[0], 'to': dt[1]} for dt in dates] return periods
def expense_trends(request): # dynamically generate the dates today = datetime.datetime.now().date() last_year = datetime.date(today.year-1, today.month, today.day) all_dts = ['%sM%02d' % (x[0], x[1]) for x in list(monthrange(last_year,today))] cols = dict(zip(all_dts, all_dts)) raw_data = QueryManager().path_drilldown('SAV', cols, 'equity.retearnings.opexp', excl_contra=['4150']) # pull out top 5 total_exp = raw_data.sum(axis=1) total_exp.sort() tbl_data = raw_data.loc[total_exp.index[:5]] tbl_data.loc['rest'] = raw_data.loc[total_exp.index[5:]].sum(axis=0) tbl_data.index = tbl_data.index.map(display_name) data = {} data['chart_data'] = {} data['chart_data']['dates'] = list(tbl_data.columns) data['chart_data']['values'] = dict((pth, [-int(tbl_data.loc[pth, x]) for x in data['chart_data']['dates']]) for pth in tbl_data.index) return HttpResponse(json.dumps(data), content_type='application/json')