Example #1
0
def report_chooser(request, report_type, period_type,
                   period_str=None, export=False):

    if not report_type in ('children', 'maternal', 'commodities'):
        raise Http404(u"Invalid report type")

    if (not period_type in ('monthly', 'annual', 'quarterly', 'weekly')
        or (report_type == 'commodities' and period_type == 'weekly')):
        raise Http404(u"Invalid period type")

    # web views and export views are named the same.
    # difference is the _export suffix.
    func_suffix = '_export' if export else ''

    try:
        view = import_path('unfpa_web.views.%(report_type)s.'
                           '%(period_type)s_%(report_type)s%(suffix)s'
                           % {'report_type': report_type,
                              'period_type': period_type,
                              'suffix': func_suffix})
    except:
        raise Http404(u"Incorrect URL.")

    try:
        if '-' in period_str:
            indice, year = period_str.split('-')
            indice = int(indice)
            year = int(year)
        else:
            indice = None
            year = int(period_str)
    except:
        raise Http404(u"Incorrect period.")

    if period_type == 'weekly':
        period = WeekPeriod.find_create_by_weeknum(year, indice)
    elif period_type == 'monthly':
        period = MonthPeriod.find_create_from(year, month=indice)
    elif period_type == 'quarterly':
        period = QuarterPeriod.find_create_by_quarter(year, indice)
    elif period_type == 'annual':
        period = YearPeriod.find_create_from(year)
    else:
        # default period is current Month
        period = MonthPeriod.find_create_by_date(date.today())

    return view(request, period)
Example #2
0
    def handle(self, *args, **options):

        current_period = MonthPeriod.find_create_by_date(date.today())

        now = datetime.now()

        # find our date of first report (begining of activities)
        first_report = date.today()
        try:
            child = ChildrenMortalityReport.objects.all().order_by('dod')[0].dod
        except:
            child = None
        try:
            mat = MaternalMortalityReport.objects.all().order_by('dod')[0].dod
        except:
            mat = None
        try: 
            preg = PregnancyReport.objects.all().order_by('creatd_on')[0].creatd_on
        except:
            preg = None
        try: 
            bir = BirthReport.objects.all().order_by('creatd_on')[0].created_on
        except:
            bir = None
        try: 
            commod = RHCommoditiesReport.objects.all().order_by('period')[0].period.start_on
            commod = date(commod.year, commod.month, commod.day)
        except:
            commod = None
        if child:
            first_report = child
        if mat and mat < first_report:
            first_report = mat
        if preg and preg < first_report:
            first_report = preg
        if bir and bir < first_report:
            first_report = bir
        if commod and commod < first_report:
            first_report = commod

        first_period = MonthPeriod.find_create_by_date(first_report)

        for year in range(first_period.start_on.year, current_period.end_on.year + 1):

            # create year
            y = YearPeriod.find_create_from(year)
            print(y)

            # create quarter
            for quarter in y.quarters_:
                if quarter.start_on > now:
                    break
                quarter.save()
                print(u"\t%s" % quarter)

            # create months:
            for month in y.months:
                if month.start_on > now:
                    break
                month.save()
                print(u"\t\t%s" % month)

                # create weeks
                for week in month.weeks:
                    if week.start_on > now:
                       break
                    week.save()
                    print(u"\t\t\t%s" % week)