def indicator_edit(request, slug, *args, **kwargs): context = {'page': 'indicators'} indicator = Indicator.get_or_none(slug) if request.method == 'POST': form = IndicatorForm(request.POST, instance=indicator) if form.is_valid(): with transaction.atomic(): form.save() messages.success( request, _("Indicator “{name}” has been updated.").format( name=indicator)) return redirect('indicators') else: # django form validation errors logger.debug("django form errors") pass else: form = IndicatorForm(instance=indicator) context.update({'form': form, 'indicator': indicator}) return render(request, kwargs.get('template_name', 'indicator_edit.html'), context)
def indicator_edit(request, slug, *args, **kwargs): context = {'page': 'indicators'} indicator = Indicator.get_or_none(slug) if request.method == 'POST': form = IndicatorForm(request.POST, instance=indicator) if form.is_valid(): with transaction.atomic(): form.save() messages.success(request, _("Indicator “{name}” has been updated.") .format(name=indicator)) return redirect('indicators') else: # django form validation errors logger.debug("django form errors") pass else: form = IndicatorForm(instance=indicator) context.update({ 'form': form, 'indicator': indicator}) return render(request, kwargs.get('template_name', 'indicator_edit.html'), context)
def dashboard(request, entity_uuid=None, indicator_slug=None, period_str=None, *args, **kwargs): context = {'page': 'dashboard'} # entity context.update(process_entity_filter(request, entity_uuid)) root = context['entity'] if context['entity'] else Entity.get_root() context.update(process_period_filter(request, period_str, 'period')) if context['period'] is None: context['period'] = MonthPeriod.current().previous() all_indicators = Indicator.get_all_sorted() # Indicator.get_all_routine() indicator = Indicator.get_or_none(indicator_slug) context.update({ 'root': root, 'completeness': OrderedDict([ (child, get_cached_data('completeness', dps=child, period=context['period'], indicator=indicator)) for child in root.get_children() ]), 'indicators': all_indicators, 'indicator': indicator, 'lineage': [Entity.PROVINCE] }) # totals context.update({ 'mean_completeness': numpy.mean( [e['completeness'] for e in context['completeness'].values()]), 'mean_promptness': numpy.mean( [e['promptness'] for e in context['completeness'].values()]), }) # evolution of pw_anc_receiving_sp3 pwsp3 = get_timed_records(Indicator.get_by_number(59), root, context['all_periods']) perioda = context['all_periods'][0] periodb = context['all_periods'][-1] context.update({ 'sp3_title': "{num} : {name} entre {pa} et {pb}" .format(num=pwsp3['indicator'].number, name=pwsp3['indicator'].name, pa=perioda.strid, pb=periodb.strid), 'sp3_fname': "palu-evol-sp3-_{pa}_{pb}" .format(pa=perioda.strid, pb=periodb.strid), 'sp3_categories': [p[1].name for p in pwsp3['periods']], 'sp3_series': [{'name': pwsp3['indicator'].name, 'data': pwsp3['points']} ], }) return render(request, kwargs.get('template_name', 'dashboard.html'), context)
def add_error(row, column=None, indicator=None, error=None, text=None): data['errors'].append({ 'row': row, 'column': column, 'indicator': Indicator.get_or_none(indicator.slug) if indicator else None, 'slug': error, 'text': text, })
def json_data_record_for(request, period_str, entity_uuid, indicator_slug): entity = Entity.get_or_none(entity_uuid) if entity is None: raise Http404(_("Unknown entity UUID `{u}`").format(u=entity_uuid)) period = process_period_filter(request, period_str, 'period').get('period') if period is None: raise Http404(_("Unknown period `{p}`").format(p=period_str)) indicator = Indicator.get_or_none(indicator_slug) if indicator is None: raise Http404(_("Unknown indicator `{s}`").format(s=indicator_slug)) return JsonResponse(DataRecord.get_for(period, entity, indicator), safe=False)
def json_data_record_for(request, period_str, entity_uuid, indicator_slug): entity = Entity.get_or_none(entity_uuid) if entity is None: raise Http404(_("Unknown entity UUID `{u}`").format(u=entity_uuid)) period = process_period_filter(request, period_str, 'period').get('period') if period is None: raise Http404(_("Unknown period `{p}`").format(p=period_str)) indicator = Indicator.get_or_none(indicator_slug) if indicator is None: raise Http404(_("Unknown indicator `{s}`").format(s=indicator_slug)) return JsonResponse(DataRecord.get_for(period, entity, indicator), safe=False)
def dashboard(request, entity_uuid=None, indicator_slug=None, period_str=None, *args, **kwargs): context = {'page': 'dashboard'} # entity context.update(process_entity_filter(request, entity_uuid)) root = context['entity'] if context['entity'] else Entity.get_root() context.update(process_period_filter(request, period_str, 'period')) if context['period'] is None: context['period'] = MonthPeriod.current().previous() all_indicators = Indicator.get_all_sorted() # Indicator.get_all_routine() indicator = Indicator.get_or_none(indicator_slug) context.update({ 'root': root, 'completeness': OrderedDict([(child, get_cached_data('completeness', dps=child, period=context['period'], indicator=indicator)) for child in root.get_children()]), 'indicators': all_indicators, 'indicator': indicator, 'lineage': [Entity.PROVINCE] }) # totals context.update({ 'mean_completeness': numpy.mean( [e['completeness'] for e in context['completeness'].values()]), 'mean_promptness': numpy.mean([e['promptness'] for e in context['completeness'].values()]), }) # evolution of pw_anc_receiving_sp3 pwsp3 = get_timed_records(Indicator.get_by_number(59), root, context['all_periods']) perioda = context['all_periods'][0] periodb = context['all_periods'][-1] context.update({ 'sp3_title': "{num} : {name} entre {pa} et {pb}".format( num=pwsp3['indicator'].number, name=pwsp3['indicator'].name, pa=perioda.strid, pb=periodb.strid), 'sp3_fname': "palu-evol-sp3-_{pa}_{pb}".format(pa=perioda.strid, pb=periodb.strid), 'sp3_categories': [p[1].name for p in pwsp3['periods']], 'sp3_series': [{ 'name': pwsp3['indicator'].name, 'data': pwsp3['points'] }], }) return render(request, kwargs.get('template_name', 'dashboard.html'), context)
def batch_create(cls, data, partner, source=UPLOAD, arrival_status=None, auto_validate=False): now = timezone.now() # make sure we can rollback if something goes wrong with transaction.atomic(): for ident, dp in data.items(): # skip errors if ident == 'errors': continue slug = dp['slug'] period = dp['period'] entity = dp['entity'] indic = Indicator.get_or_none(slug) dr = cls.get_or_none(indicator=indic, period=period, entity=entity) num = dp['numerator'] denum = dp['denominator'] if dr and (dr.numerator != num or dr.denominator != denum): # do not manualy update DHIS data if dr.source == dr.DHIS and source == dr.UPLOAD: continue old_values = {'numerator': dr.numerator, 'denominator': dr.denominator} action = 'updated' dr.numerator = num dr.denominator = denum dr.record_update(partner) elif dr and dr.source == dr.UPLOAD and source == dr.DHIS: # mark data as updated by DHIS even though it's identical dr.record_update(partner) elif dr is None: old_values = None action = 'created' if arrival_status is None: arrival_status = indic.arrival_status_on( on=now, period=period) dr = cls.objects.create( indicator=indic, period=period, entity=entity, numerator=num, denominator=denum, source=source, created_by=partner) if auto_validate: dr.auto_validate(on=now) else: # new data are identical to datarecord continue data[ident].update({ 'action': action, 'id': dr.id, 'previous': old_values}) return data