Exemple #1
0
def indicator_browser(request, entity_code=None, period_str=None, \
                    section_index='1', sub_section=None):
    context = {'category': 'indicator_data'}
    web_provider = request.user.get_profile()

    root = web_provider.first_target()

    periods = []
    speriod = eperiod = None
    entity = None
    #section_index = int(section_index) - 1

    # find entity or default to provider target
    # raise 404 on wrong provided entity code
    if entity_code:
        entity = get_object_or_404(Entity, slug=entity_code)

    if not entity:
        entity = web_provider.first_target()
    context.update({'entity': entity})

    # define a list of all possible periods.
    # this is the list of all existing MonthPeriod anterior to current
    def all_anterior_periods(period):
        return MonthPeriod.objects\
                          .filter(start_on__lte=period.start_on)\
                          .order_by('start_on')

    all_periods = all_anterior_periods(current_reporting_period())

    # retrieve Periods from string
    # if period_string include innexistant periods -> 404.
    if period_str:
        speriod_str, eperiod_str = period_str.split('-')
        try:
            speriod = MonthPeriod.find_create_from(
                                                  year=int(speriod_str[-4:]),
                                                  month=int(speriod_str[:2]),
                                                  dont_create=True)
            eperiod = MonthPeriod.find_create_from(
                                                  year=int(eperiod_str[-4:]),
                                                  month=int(eperiod_str[:2]),
                                                  dont_create=True)

            # loop on Period.next() from start one to end one.
            period = speriod
            while period.middle() <= eperiod.middle():
                periods.append(period)
                period = period.next()
        except:
            raise Http404(_(u"Requested period interval (%(period_str)s) "
                            u"includes inexistant periods.")
                          % {'period': period_str})

    # in case user did not request a specific interval
    # default to current_reporting_period
    if not speriod or not eperiod:
        speriod = eperiod = current_reporting_period()
        periods = [speriod]

    # if end period is before start period, redirect to opposite
    if eperiod.middle() < speriod.middle():
        return redirect('indicator_data',
                        entity_code=entity.slug,
                        period_str='%s-%s' % (eperiod.pid, speriod.pid))

    # periods variables
    context.update({'period_str': '%s-%s' % (speriod.pid, eperiod.pid),
                    'speriod': speriod, 'eperiod': eperiod})
    context.update({'periods': [(p.pid, p.middle()) for p in periods],
                    'all_periods': [(p.pid, p.middle()) for p in all_periods]})

    # check permissions on this entity and raise 403
    provider_can_or_403('can_view_indicator_data', web_provider, entity)

    # build entities browser
    context.update({'root': root, \
                    'paths': entities_path(root, entity)})

    # from pnlp_core.indicators import INDICATOR_SECTIONS

    # context.update({'sections': \
    #                 sorted(INDICATOR_SECTIONS.values(), \
    #                       cmp=lambda a, b: int(a['id'].strip('a').strip('b')) \
    #                                     - int(b['id'].strip('a').strip('b')))})

    # try:
    #     section = INDICATOR_SECTIONS[section_index]
    #     if not sub_section:
    #         if len(section['sections']):
    #             sub_section = section['sections'].keys()[0]
    #     sname = 'pnlp_core.indicators.section%s' % section_index.__str__()
    #     if sub_section:
    #         sname = '%s_%s' % (sname, sub_section.__str__())
    #     sm = import_path(sname)
    # except:
    #     raise
    #     raise Http404(_(u"This section does not exist."))

    # section 1 specifics
    if section_index == '1':
        context.update({'contact': contact_for(entity)})

    # context.update({'section': section, 'sub_section': sub_section})

    context.update({'section': {}, 'sub_section': sub_section})

    # context.update({'widgets': [widget(entity=entity, periods=periods) \
    #                             for widget in sm.WIDGETS]})

    return render(request, 'indicator_data.html', context)
Exemple #2
0
def data_browser(request, entity_code=None, period_str=None):
    context = {'category': 'raw_data'}
    web_provider = request.user.get_profile()

    root = web_provider.first_target()

    period = None
    entity = None

    # find period from string or default to current reporting
    if period_str:
        try:
            period = MonthPeriod.find_create_from(year=int(period_str[-4:]),
                                                  month=int(period_str[:2]),
                                                  dont_create=True)
        except:
            pass
    if not period:
        period = current_reporting_period()

    # find entity or default to provider target
    # raise 404 on wrong provided entity code
    if entity_code:
        entity = get_object_or_404(Entity, slug=entity_code)

    if not entity:
        entity = web_provider.first_target()
    context.update({'entity': entity})

    # check permissions on this entity and raise 403
    provider_can_or_403('can_view_raw_data', web_provider, entity)

    # build entities browser
    context.update({'root': root, \
                    'paths': entities_path(root, entity)})

    # build periods list
    all_periods = raw_data_periods_for(entity)
    if period_str and not period in all_periods:
        raise Http404(_(u"No report for that period"))

    try:
        # get validated report for that period and location
        report = NutritionReport.validated.get(entity=entity, period=period)
    except NutritionReport.DoesNotExist:
        # district users need to be able to see the generated report
        # which have been created based on their validations/data.
        # if a district is looking at its root district and report exist
        # but not validated, we show it (with period) and no valid flag
        if web_provider.first_role().slug == 'district' and root == entity:
            try:
                report = NutritionReport.unvalidated.get(entity=entity,
                                                         period=period)
                if not period in all_periods:
                    all_periods.insert(0, period)
            except:
                report = None
        else:
            report = None

    # send period variables to template
    context.update({'periods': [(p.middle().strftime('%m%Y'), p.middle()) \
                                for p in all_periods], \
                    'period': period})

    if report:
        context.update({'report': report})
        #form = MalariaReportForm(instance=report)
        form = {}
        context.update({'form': form})
    else:
        context.update({'no_report': True})

    return render(request, 'raw_data.html', context)