Beispiel #1
0
    def per_nature(self, institution):
        PerNature.objects.filter(institution=institution).delete()
        PerNatureByYear.objects.filter(institution=institution).delete()
        PerNatureByMonth.objects.filter(institution=institution).delete()

        data = Expense.objects.all()
        data = filter_for_institution(data, institution)

        date_ranges = get_date_ranges_from_data(institution, data)

        data = data \
            .values('nature__id') \
            .annotate(expensed=Sum('expensed')) \
            .order_by('-expensed')

        years = [d.year for d in Expense.objects.dates('date', 'year')]
        years = ensure_years_in_range(date_ranges, years)

        per_natures_to_create = list()
        per_natures_by_year_to_create = list()
        per_natures_by_month_to_create = list()

        for item in data:
            # Totals
            nature = ExpenseNature.objects.get(id=item['nature__id'])
            p = PerNature(
                institution=institution,
                date_start=date_ranges['cdf'],
                date_end=date_ranges['cdt'],
                nature=nature,
                expensed=item['expensed']
            )
            per_natures_to_create.append(p)

            # Totals for Legislature
            per_natures_to_create += self._per_nature_total_for_legislature(
                institution, nature
            )

            # By Year
            year_to_create, month_to_create = self._per_nature_by_year(
                years, institution, nature
            )
            per_natures_by_year_to_create += year_to_create
            per_natures_by_month_to_create += month_to_create

        PerNature.objects.bulk_create(per_natures_to_create)
        PerNatureByMonth.objects.bulk_create(per_natures_by_month_to_create)
        PerNatureByYear.objects.bulk_create(per_natures_by_year_to_create)
Beispiel #2
0
    def per_nature(self, institution):
        PerNature.objects.filter(institution=institution).delete()
        PerNatureByYear.objects.filter(institution=institution).delete()
        PerNatureByMonth.objects.filter(institution=institution).delete()

        data = Expense.objects.all()
        data = filter_for_institution(data, institution)

        date_ranges = get_date_ranges_from_data(institution, data)

        data = data \
            .values('nature__id') \
            .annotate(expensed=Sum('expensed')) \
            .order_by('-expensed')

        years = [d.year for d in Expense.objects.dates('date', 'year')]
        years = ensure_years_in_range(date_ranges, years)

        per_natures_to_create = list()
        per_natures_by_year_to_create = list()
        per_natures_by_month_to_create = list()

        for item in data:
            # Totals
            nature = ExpenseNature.objects.get(id=item['nature__id'])
            p = PerNature(institution=institution,
                          date_start=date_ranges['cdf'],
                          date_end=date_ranges['cdt'],
                          nature=nature,
                          expensed=item['expensed'])
            per_natures_to_create.append(p)

            # Totals for Legislature
            per_natures_to_create += self._per_nature_total_for_legislature(
                institution, nature)

            # By Year
            year_to_create, month_to_create = self._per_nature_by_year(
                years, institution, nature)
            per_natures_by_year_to_create += year_to_create
            per_natures_by_month_to_create += month_to_create

        PerNature.objects.bulk_create(per_natures_to_create)
        PerNatureByMonth.objects.bulk_create(per_natures_by_month_to_create)
        PerNatureByYear.objects.bulk_create(per_natures_by_year_to_create)
Beispiel #3
0
def show_per_nature(request, filter_spec):

    institution, legislature = parse_filter(filter_spec)
    data = PerNature.objects.filter(institution=institution, legislature=legislature)

    date_ranges = get_date_ranges_from_data(institution, data, consolidated_data=True)

    time_series = []
    for d in data:
        l = []
        cummulative = .0
        time_series.append(dict(label=d.nature.name, data=l))

        per_year_data = PerNatureByYear.objects.filter(institution=institution).filter(nature=d.nature)
        per_year_data = per_year_data.filter(year__gte=date_ranges['cdf'].year)
        per_year_data = per_year_data.filter(year__lte=date_ranges['cdt'].year)
        for item in per_year_data:
            cummulative = cummulative + float(item.expensed)
            l.append([int(date(item.year, 1, 1).strftime("%s000")), cummulative])

    # mbm_years = list of years (3, right now - 2011, 2012, 2013) with 12 months inside
    # each month in turn carries a dict with month name and value or null.
    def nature_is_empty(years):
        for year in years:
            for month in year['data']:
                if month['data'] != 'null' and month['data'] > 0.0001:
                    return False
        return True

    natures_mbm = []
    for nature in ExpenseNature.objects.all():
        mbm_years = []
        expensed_by_month = PerNatureByMonth.objects.filter(institution=institution)
        expensed_by_month = expensed_by_month.filter(nature=nature)

        all_years = [d.year for d in expensed_by_month.dates('date', 'year')]
        all_years = ensure_years_in_range(date_ranges, all_years)

        for year in all_years:
            mbm_series = []
            for month in range(1, 13):
                month_date = date(year, month, 1)

                mname = month_date.strftime("%b")
                try:
                    item = expensed_by_month.get(date=month_date)
                    expensed = float(item.expensed)
                except PerNatureByMonth.DoesNotExist:
                    expensed = 'null'

                mbm_series.append(dict(month=mname, data=str(expensed)))
            mbm_years.append(dict(label=year, data=mbm_series))

        if not nature_is_empty(mbm_years):
            natures_mbm.append(dict(name=nature.name, data=mbm_years))

    c = {'data': data, 'years_data': time_series, 'natures_mbm': natures_mbm,
         'colors': generate_colors(len(data), 0.93, 0.8)}

    c.update(date_ranges)

    return new_render(request, filter_spec, 'per_nature.html', c)
Beispiel #4
0
def show_per_nature(request, filter_spec, export_format=''):

    institution, legislature = parse_filter(filter_spec)
    data = PerNature.objects.filter(institution=institution,
                                    legislature=legislature)

    if export_format:
        data = data.values(
            'institution__siglum',
            'legislature__date_start',
            'legislature__date_end',
            'nature__name',
            'expensed',
        )
        field_header_map = {
            'institution__siglum': u'Instituição',
            'legislature__date_start': u'Início da legislatura',
            'legislature__date_end': u'Fim da legislatura',
            'nature__name': u'Tipo de gasto',
            'expensed': u'Valor',
        }
        return _export_csv(request, data, field_header_map)

    date_ranges = get_date_ranges_from_data(institution,
                                            data,
                                            consolidated_data=True)

    if legislature:
        start_year = legislature.date_start.year
        end_year = legislature.date_end.year
    else:
        start_year = date_ranges['cdf'].year
        end_year = date_ranges['cdt'].year

    time_series = []
    for d in data:
        aux = []
        cummulative = .0
        time_series.append(dict(label=d.nature.name, data=aux))

        per_year_data = PerNatureByYear.objects.filter(
            institution=institution).filter(nature=d.nature)
        per_year_data = per_year_data.filter(year__gte=start_year)
        per_year_data = per_year_data.filter(year__lte=end_year)
        for item in per_year_data:
            cummulative = cummulative + float(item.expensed)
            aux.append(
                [int(date(item.year, 1, 1).strftime("%s000")), cummulative])

    # mbm_years = list of years (3, right now - 2011, 2012, 2013) with 12 months inside
    # each month in turn carries a dict with month name and value or null.
    def nature_is_empty(years):
        for year in years:
            for month in year['data']:
                if month['data'] != 'null' and month['data'] > 0.0001:
                    return False
        return True

    natures_mbm = []
    for nature in ExpenseNature.objects.all():
        mbm_years = []
        expensed_by_month = PerNatureByMonth.objects.filter(
            institution=institution)
        expensed_by_month = expensed_by_month.filter(nature=nature)

        if legislature:
            all_years = range(start_year, end_year + 1)
        else:
            all_years = [
                d.year for d in expensed_by_month.dates('date', 'year')
            ]
            all_years = ensure_years_in_range(date_ranges, all_years)

        for year in all_years:
            mbm_series = []
            for month in range(1, 13):
                month_date = date(year, month, 1)

                mname = month_date.strftime("%b")
                try:
                    item = expensed_by_month.get(date=month_date)
                    expensed = float(item.expensed)
                except PerNatureByMonth.DoesNotExist:
                    expensed = 'null'

                mbm_series.append(dict(month=mname, data=str(expensed)))
            mbm_years.append(dict(label=year, data=mbm_series))

        if not nature_is_empty(mbm_years):
            natures_mbm.append(dict(name=nature.name, data=mbm_years))

    c = {
        'data': data,
        'years_data': time_series,
        'natures_mbm': natures_mbm,
        'colors': generate_colors(len(data), 0.93, 0.8)
    }

    c.update(date_ranges)

    return new_render(request, filter_spec, 'per_nature.html', c)
Beispiel #5
0
    def handle(self, *args, **options):
        global debug_enabled

        if "debug" in args:
            debug_enabled = True

        institution = None
        if "almg" in args:
            institution = Institution.objects.get(siglum='ALMG')

        if "senado" in args:
            institution = Institution.objects.get(siglum='Senado')

        if "cmbh" in args:
            institution = Institution.objects.get(siglum='CMBH')

        if "cmsp" in args:
            institution = Institution.objects.get(siglum='CMSP')

        if "camarafederal" in args:
            institution = Institution.objects.get(siglum='CDF')

        # Per nature
        PerNature.objects.filter(institution=institution).delete()
        PerNatureByYear.objects.filter(institution=institution).delete()

        data = Expense.objects.all()
        data = filter_for_institution(data, institution)

        date_ranges = get_date_ranges_from_data(institution, data)

        data = data.values('nature__id')
        data = data.annotate(expensed=Sum('expensed')).order_by('-expensed')

        years = [d.year for d in Expense.objects.dates('date', 'year')]
        years = ensure_years_in_range(date_ranges, years)

        per_natures_to_create = list()
        per_natures_by_year_to_create = list()
        for item in data:
            nature = ExpenseNature.objects.get(id=item['nature__id'])
            p = PerNature(institution=institution,
                          date_start=date_ranges['cdf'],
                          date_end=date_ranges['cdt'],
                          nature=nature,
                          expensed=item['expensed'])
            per_natures_to_create.append(p)

            for year in years:
                year_data = Expense.objects.filter(nature=nature)
                year_data = year_data.filter(date__year=year)
                year_data = year_data.values('nature__id')
                year_data = year_data.annotate(expensed=Sum("expensed"))

                if year_data:
                    year_data = year_data[0]
                else:
                    year_data = dict(expensed='0.')

                p = PerNatureByYear(institution=institution,
                                    year=year,
                                    nature=nature,
                                    expensed=float(year_data['expensed']))
                per_natures_by_year_to_create.append(p)
        PerNature.objects.bulk_create(per_natures_to_create)
        PerNatureByYear.objects.bulk_create(per_natures_by_year_to_create)
Beispiel #6
0
def show_per_nature(request, filter_spec, export_format=''):

    institution, legislature = parse_filter(filter_spec)
    data = PerNature.objects.filter(institution=institution, legislature=legislature)

    if export_format:
        data = data.values(
            'institution__siglum',
            'legislature__date_start',
            'legislature__date_end',
            'nature__name',
            'expensed',
        )
        field_header_map = {
            'institution__siglum': u'Instituição',
            'legislature__date_start': u'Início da legislatura',
            'legislature__date_end': u'Fim da legislatura',
            'nature__name': u'Tipo de gasto',
            'expensed': u'Valor',
        }
        return _export_csv(request, data, field_header_map)

    date_ranges = get_date_ranges_from_data(institution, data, consolidated_data=True)

    if legislature:
        start_year = legislature.date_start.year
        end_year = legislature.date_end.year
    else:
        start_year = date_ranges['cdf'].year
        end_year = date_ranges['cdt'].year

    time_series = []
    for d in data:
        aux = []
        cummulative = .0
        time_series.append(dict(label=d.nature.name, data=aux))

        per_year_data = PerNatureByYear.objects.filter(institution=institution).filter(nature=d.nature)
        per_year_data = per_year_data.filter(year__gte=start_year)
        per_year_data = per_year_data.filter(year__lte=end_year)
        for item in per_year_data:
            cummulative = cummulative + float(item.expensed)
            aux.append([int(date(item.year, 1, 1).strftime("%s000")), cummulative])

    # mbm_years = list of years (3, right now - 2011, 2012, 2013) with 12 months inside
    # each month in turn carries a dict with month name and value or null.
    def nature_is_empty(years):
        for year in years:
            for month in year['data']:
                if month['data'] != 'null' and month['data'] > 0.0001:
                    return False
        return True

    natures_mbm = []
    for nature in ExpenseNature.objects.all():
        mbm_years = []
        expensed_by_month = PerNatureByMonth.objects.filter(institution=institution)
        expensed_by_month = expensed_by_month.filter(nature=nature)

        if legislature:
            all_years = range(start_year, end_year + 1)
        else:
            all_years = [d.year for d in expensed_by_month.dates('date', 'year')]
            all_years = ensure_years_in_range(date_ranges, all_years)

        for year in all_years:
            mbm_series = []
            for month in range(1, 13):
                month_date = date(year, month, 1)

                mname = month_date.strftime("%b")
                try:
                    item = expensed_by_month.get(date=month_date)
                    expensed = float(item.expensed)
                except PerNatureByMonth.DoesNotExist:
                    expensed = 'null'

                mbm_series.append(dict(month=mname, data=str(expensed)))
            mbm_years.append(dict(label=year, data=mbm_series))

        if not nature_is_empty(mbm_years):
            natures_mbm.append(dict(name=nature.name, data=mbm_years))

    c = {'data': data, 'years_data': time_series, 'natures_mbm': natures_mbm,
         'colors': generate_colors(len(data), 0.93, 0.8)}

    c.update(date_ranges)

    return new_render(request, filter_spec, 'per_nature.html', c)
    def handle(self, *args, **options):
        institutions = []
        if "almg" in args:
            institutions.append(Institution.objects.get(siglum='ALMG'))

        if "senado" in args:
            institutions.append(Institution.objects.get(siglum='Senado'))

        if "cmbh" in args:
            institutions.append(Institution.objects.get(siglum='CMBH'))

        if "cmsp" in args:
            institutions.append(Institution.objects.get(siglum='CMSP'))

        if "cdep" in args:
            institutions.append(Institution.objects.get(siglum='CDF'))

        for institution in institutions:
            print u'Consolidating data for %s' % (institution.name)

            # Per nature
            PerNature.objects.filter(institution=institution).delete()
            PerNatureByYear.objects.filter(institution=institution).delete()
            PerNatureByMonth.objects.filter(institution=institution).delete()

            data = Expense.objects.all()
            data = filter_for_institution(data, institution)

            date_ranges = get_date_ranges_from_data(institution, data)

            data = data.values('nature__id')
            data = data.annotate(expensed=Sum('expensed')).order_by('-expensed')

            years = [d.year for d in Expense.objects.dates('date', 'year')]
            years = ensure_years_in_range(date_ranges, years)

            per_natures_to_create = list()
            per_natures_by_year_to_create = list()
            per_natures_by_month_to_create = list()

            for item in data:
                # Totals
                nature = ExpenseNature.objects.get(id=item['nature__id'])
                p = PerNature(institution=institution,
                              date_start=date_ranges['cdf'],
                              date_end=date_ranges['cdt'],
                              nature=nature,
                              expensed=item['expensed'])
                per_natures_to_create.append(p)

                # Totals for Legislature
                for legislature in institution.legislature_set.all():
                    pargs = (institution.siglum, nature.name, legislature.date_start.year, legislature.date_end.year)
                    print u'[%s] Consolidating nature %s totals for legislature %d-%d…' % pargs
                    legislature_data = Expense.objects.filter(nature=nature)
                    legislature_data = legislature_data.filter(mandate__legislature=legislature)

                    legislature_ranges = get_date_ranges_from_data(institution, legislature_data)

                    legislature_data = legislature_data.values('nature__id')
                    legislature_data = legislature_data.annotate(expensed=Sum('expensed')).order_by('-expensed')

                    if legislature_data:
                        legislature_data = legislature_data[0]
                    else:
                        legislature_data = dict(expensed='0.')

                    p = PerNature(institution=institution,
                                  legislature=legislature,
                                  date_start=legislature_ranges['cdf'],
                                  date_end=legislature_ranges['cdt'],
                                  nature=nature,
                                  expensed=legislature_data['expensed'])
                    per_natures_to_create.append(p)

                # By Year
                for year in years:
                    print u'[%s] Consolidating nature %s totals for year %d…' % (institution.siglum, nature.name, year)
                    year_data = Expense.objects.filter(nature=nature)
                    year_data = year_data.filter(date__year=year)
                    year_data = filter_for_institution(year_data, institution)

                    # By Month
                    last_date = year_data and year_data.order_by('-date')[0].date or date.today()
                    for month in range(1, 13):
                        print u'[%s] Consolidating nature %s totals for %d-%d…' % (institution.siglum, nature.name, year, month)
                        month_date = date(year, month, 1)

                        if month_date >= last_date:
                            break

                        mdata = year_data.filter(date__month=month)
                        mdata = mdata.values('nature__id')
                        mdata = mdata.annotate(expensed=Sum('expensed')).order_by('-expensed')

                        if mdata:
                            mdata = mdata[0]
                        else:
                            mdata = dict(expensed='0.')

                        p = PerNatureByMonth(institution=institution,
                                             date=month_date,
                                             nature=nature,
                                             expensed=float(mdata['expensed']))
                        per_natures_by_month_to_create.append(p)

                    year_data = year_data.values('nature__id')
                    year_data = year_data.annotate(expensed=Sum("expensed"))

                    if year_data:
                        year_data = year_data[0]
                    else:
                        year_data = dict(expensed='0.')

                    p = PerNatureByYear(institution=institution,
                                        year=year,
                                        nature=nature,
                                        expensed=float(year_data['expensed']))
                    per_natures_by_year_to_create.append(p)

            PerNature.objects.bulk_create(per_natures_to_create)
            PerNatureByMonth.objects.bulk_create(per_natures_by_month_to_create)
            PerNatureByYear.objects.bulk_create(per_natures_by_year_to_create)

            # Legislator
            PerLegislator.objects.filter(institution=institution).delete()

            data = Expense.objects.all()
            data = filter_for_institution(data, institution)

            date_ranges = get_date_ranges_from_data(institution, data)

            data = data.values('mandate__legislator__id')
            data = data.annotate(expensed=Sum('expensed'))

            per_legislators_to_create = list()
            for item in data:
                legislator = Legislator.objects.get(id=int(item['mandate__legislator__id']))

                # Totals for Legislature
                for legislature in institution.legislature_set.all():
                    pargs = (institution.siglum, legislator.name, legislature.date_start.year, legislature.date_end.year)
                    print u'[%s] Consolidating legislator %s totals for legislature %d-%d…' % pargs

                    legislature_data = Expense.objects.filter(mandate__legislature=legislature)
                    legislature_data = legislature_data.filter(mandate__legislator=legislator)

                    legislature_ranges = get_date_ranges_from_data(institution, legislature_data)

                    legislature_data = legislature_data.values('mandate__legislator__id')
                    legislature_data = legislature_data.annotate(expensed=Sum('expensed')).order_by('-expensed')

                    if legislature_data:
                        legislature_data = legislature_data[0]
                    else:
                        legislature_data = dict(expensed='0.')

                    p = PerLegislator(institution=institution,
                                      legislature=legislature,
                                      date_start=date_ranges['cdf'],
                                      date_end=date_ranges['cdt'],
                                      legislator=legislator,
                                      expensed=legislature_data['expensed'])
                    per_legislators_to_create.append(p)

                print u'[%s] Consolidating totals for legislator %s…' % (institution.siglum, legislator.name)
                p = PerLegislator(institution=institution,
                                  date_start=date_ranges['cdf'],
                                  date_end=date_ranges['cdt'],
                                  legislator=legislator,
                                  expensed=item['expensed'])
                per_legislators_to_create.append(p)
            PerLegislator.objects.bulk_create(per_legislators_to_create)

        if 'agnostic' in args:
            # Institution-agnostic consolidations - biggest suppliers
            print u'Consolidating institution-agnostic totals…'

            BiggestSupplierForYear.objects.all().delete()
            years = [d.year for d in Expense.objects.dates('date', 'year')]
            for year in years:
                print u'Consolidating supplier totals for year %d…' % year
                data = Expense.objects.filter(date__year=year)
                data = data.values('supplier__id')
                data = data.annotate(expensed=Sum('expensed')).order_by('-expensed')

                biggest_suppliers_for_year_to_add = list()
                for item in data:
                    supplier = Supplier.objects.get(id=item['supplier__id'])

                    b = BiggestSupplierForYear(supplier=supplier,
                                               year=year,
                                               expensed=item['expensed'])
                    biggest_suppliers_for_year_to_add.append(b)
                BiggestSupplierForYear.objects.bulk_create(biggest_suppliers_for_year_to_add)
Beispiel #8
0
def show_per_nature(request, filter_spec):

    institution, legislature = parse_filter(filter_spec)
    data = PerNature.objects.filter(institution=institution,
                                    legislature=legislature)

    date_ranges = get_date_ranges_from_data(institution,
                                            data,
                                            consolidated_data=True)

    time_series = []
    for d in data:
        l = []
        cummulative = .0
        time_series.append(dict(label=d.nature.name, data=l))

        per_year_data = PerNatureByYear.objects.filter(
            institution=institution).filter(nature=d.nature)
        per_year_data = per_year_data.filter(year__gte=date_ranges['cdf'].year)
        per_year_data = per_year_data.filter(year__lte=date_ranges['cdt'].year)
        for item in per_year_data:
            cummulative = cummulative + float(item.expensed)
            l.append(
                [int(date(item.year, 1, 1).strftime("%s000")), cummulative])

    # mbm_years = list of years (3, right now - 2011, 2012, 2013) with 12 months inside
    # each month in turn carries a dict with month name and value or null.
    def nature_is_empty(years):
        for year in years:
            for month in year['data']:
                if month['data'] != 'null' and month['data'] > 0.0001:
                    return False
        return True

    natures_mbm = []
    for nature in ExpenseNature.objects.all():
        mbm_years = []
        expensed_by_month = PerNatureByMonth.objects.filter(
            institution=institution)
        expensed_by_month = expensed_by_month.filter(nature=nature)

        all_years = [d.year for d in expensed_by_month.dates('date', 'year')]
        all_years = ensure_years_in_range(date_ranges, all_years)

        for year in all_years:
            mbm_series = []
            for month in range(1, 13):
                month_date = date(year, month, 1)

                mname = month_date.strftime("%b")
                try:
                    item = expensed_by_month.get(date=month_date)
                    expensed = float(item.expensed)
                except PerNatureByMonth.DoesNotExist:
                    expensed = 'null'

                mbm_series.append(dict(month=mname, data=str(expensed)))
            mbm_years.append(dict(label=year, data=mbm_series))

        if not nature_is_empty(mbm_years):
            natures_mbm.append(dict(name=nature.name, data=mbm_years))

    c = {
        'data': data,
        'years_data': time_series,
        'natures_mbm': natures_mbm,
        'colors': generate_colors(len(data), 0.93, 0.8)
    }

    c.update(date_ranges)

    return new_render(request, filter_spec, 'per_nature.html', c)
Beispiel #9
0
    def handle(self, *args, **options):
        institutions = []
        if "almg" in args:
            institutions.append(Institution.objects.get(siglum='ALMG'))

        if "algo" in args:
            institutions.append(Institution.objects.get(siglum='ALGO'))

        if "senado" in args:
            institutions.append(Institution.objects.get(siglum='Senado'))

        if "cmbh" in args:
            institutions.append(Institution.objects.get(siglum='CMBH'))

        if "cmsp" in args:
            institutions.append(Institution.objects.get(siglum='CMSP'))

        if "cdep" in args:
            institutions.append(Institution.objects.get(siglum='CDF'))

        for institution in institutions:
            print u'Consolidating data for %s' % (institution.name)

            # Per nature
            PerNature.objects.filter(institution=institution).delete()
            PerNatureByYear.objects.filter(institution=institution).delete()
            PerNatureByMonth.objects.filter(institution=institution).delete()

            data = Expense.objects.all()
            data = filter_for_institution(data, institution)

            date_ranges = get_date_ranges_from_data(institution, data)

            data = data.values('nature__id')
            data = data.annotate(
                expensed=Sum('expensed')).order_by('-expensed')

            years = [d.year for d in Expense.objects.dates('date', 'year')]
            years = ensure_years_in_range(date_ranges, years)

            per_natures_to_create = list()
            per_natures_by_year_to_create = list()
            per_natures_by_month_to_create = list()

            for item in data:
                # Totals
                nature = ExpenseNature.objects.get(id=item['nature__id'])
                p = PerNature(institution=institution,
                              date_start=date_ranges['cdf'],
                              date_end=date_ranges['cdt'],
                              nature=nature,
                              expensed=item['expensed'])
                per_natures_to_create.append(p)

                # Totals for Legislature
                for legislature in institution.legislature_set.all():
                    pargs = (institution.siglum, nature.name,
                             legislature.date_start.year,
                             legislature.date_end.year)
                    print u'[%s] Consolidating nature %s totals for legislature %d-%d…' % pargs
                    legislature_data = Expense.objects.filter(nature=nature)
                    legislature_data = legislature_data.filter(
                        mandate__legislature=legislature)

                    legislature_ranges = get_date_ranges_from_data(
                        institution, legislature_data)

                    legislature_data = legislature_data.values('nature__id')
                    legislature_data = legislature_data.annotate(
                        expensed=Sum('expensed')).order_by('-expensed')

                    if legislature_data:
                        legislature_data = legislature_data[0]
                    else:
                        legislature_data = dict(expensed='0.')

                    p = PerNature(institution=institution,
                                  legislature=legislature,
                                  date_start=legislature_ranges['cdf'],
                                  date_end=legislature_ranges['cdt'],
                                  nature=nature,
                                  expensed=legislature_data['expensed'])
                    per_natures_to_create.append(p)

                # By Year
                for year in years:
                    print u'[%s] Consolidating nature %s totals for year %d…' % (
                        institution.siglum, nature.name, year)
                    year_data = Expense.objects.filter(nature=nature)
                    year_data = year_data.filter(date__year=year)
                    year_data = filter_for_institution(year_data, institution)

                    # By Month
                    last_date = year_data and year_data.order_by(
                        '-date')[0].date or date.today()
                    for month in range(1, 13):
                        print u'[%s] Consolidating nature %s totals for %d-%d…' % (
                            institution.siglum, nature.name, year, month)
                        month_date = date(year, month, 1)

                        if month_date >= last_date:
                            break

                        mdata = year_data.filter(date__month=month)
                        mdata = mdata.values('nature__id')
                        mdata = mdata.annotate(
                            expensed=Sum('expensed')).order_by('-expensed')

                        if mdata:
                            mdata = mdata[0]
                        else:
                            mdata = dict(expensed='0.')

                        p = PerNatureByMonth(institution=institution,
                                             date=month_date,
                                             nature=nature,
                                             expensed=float(mdata['expensed']))
                        per_natures_by_month_to_create.append(p)

                    year_data = year_data.values('nature__id')
                    year_data = year_data.annotate(expensed=Sum("expensed"))

                    if year_data:
                        year_data = year_data[0]
                    else:
                        year_data = dict(expensed='0.')

                    p = PerNatureByYear(institution=institution,
                                        year=year,
                                        nature=nature,
                                        expensed=float(year_data['expensed']))
                    per_natures_by_year_to_create.append(p)

            PerNature.objects.bulk_create(per_natures_to_create)
            PerNatureByMonth.objects.bulk_create(
                per_natures_by_month_to_create)
            PerNatureByYear.objects.bulk_create(per_natures_by_year_to_create)

            # Legislator
            PerLegislator.objects.filter(institution=institution).delete()

            data = Expense.objects.all()
            data = filter_for_institution(data, institution)

            date_ranges = get_date_ranges_from_data(institution, data)

            data = data.values('mandate__legislator__id')
            data = data.annotate(expensed=Sum('expensed'))

            per_legislators_to_create = list()
            for item in data:
                legislator = Legislator.objects.get(
                    id=int(item['mandate__legislator__id']))

                # Totals for Legislature
                for legislature in institution.legislature_set.all():
                    pargs = (institution.siglum, legislator.name,
                             legislature.date_start.year,
                             legislature.date_end.year)
                    print u'[%s] Consolidating legislator %s totals for legislature %d-%d…' % pargs

                    legislature_data = Expense.objects.filter(
                        mandate__legislature=legislature)
                    legislature_data = legislature_data.filter(
                        mandate__legislator=legislator)

                    legislature_ranges = get_date_ranges_from_data(
                        institution, legislature_data)

                    legislature_data = legislature_data.values(
                        'mandate__legislator__id')
                    legislature_data = legislature_data.annotate(
                        expensed=Sum('expensed')).order_by('-expensed')

                    if legislature_data:
                        legislature_data = legislature_data[0]
                    else:
                        legislature_data = dict(expensed='0.')

                    p = PerLegislator(institution=institution,
                                      legislature=legislature,
                                      date_start=date_ranges['cdf'],
                                      date_end=date_ranges['cdt'],
                                      legislator=legislator,
                                      expensed=legislature_data['expensed'])
                    per_legislators_to_create.append(p)

                print u'[%s] Consolidating totals for legislator %s…' % (
                    institution.siglum, legislator.name)
                p = PerLegislator(institution=institution,
                                  date_start=date_ranges['cdf'],
                                  date_end=date_ranges['cdt'],
                                  legislator=legislator,
                                  expensed=item['expensed'])
                per_legislators_to_create.append(p)
            PerLegislator.objects.bulk_create(per_legislators_to_create)

        if 'agnostic' in args:
            # Institution-agnostic consolidations - biggest suppliers
            print u'Consolidating institution-agnostic totals…'

            BiggestSupplierForYear.objects.all().delete()
            years = [d.year for d in Expense.objects.dates('date', 'year')]
            for year in years:
                print u'Consolidating supplier totals for year %d…' % year
                data = Expense.objects.filter(date__year=year)
                data = data.values('supplier__id')
                data = data.annotate(
                    expensed=Sum('expensed')).order_by('-expensed')

                biggest_suppliers_for_year_to_add = list()
                for item in data:
                    supplier = Supplier.objects.get(id=item['supplier__id'])

                    b = BiggestSupplierForYear(supplier=supplier,
                                               year=year,
                                               expensed=item['expensed'])
                    biggest_suppliers_for_year_to_add.append(b)
                BiggestSupplierForYear.objects.bulk_create(
                    biggest_suppliers_for_year_to_add)