def png_map_for(request, perioda_str, periodb_str, entity_name, indicator_number, with_title=True, with_legend=True): entity = Entity.get_by_short_name(entity_name) if entity is None: raise Http404( _("Unknown entity with name `{u}`").format(u=entity_name)) if perioda_str is None and periodb_str is None \ and indicator_number is None: periods = None indicator = None with_title = False fname = "initial.png" else: with_title = True perioda = process_period_filter(request, perioda_str, 'perioda').get('perioda') periodb = process_period_filter(request, periodb_str, 'periodb').get('periodb') periods = MonthPeriod.all_from(perioda, periodb) if not len(periods): raise Http404( _("Unknown period interval `{pa}` `{pb}`").format( pa=perioda_str, pb=periodb_str)) indicator = Indicator.get_by_number(indicator_number) if indicator is None: raise Http404( _("Unknown indicator `{s}`").format(s=indicator_number)) fname = fname_for(entity, periods, indicator) fpath = os.path.join('png_map', fname) abspath = os.path.join(settings.EXPORT_REPOSITORY, fpath) if not os.path.exists(abspath): try: gen_map_for(entity, periods, indicator, save_as=abspath, with_title=with_title, with_index=with_title) except IOError: logger.error("Missing map png folder in exports.") raise # return redirect('export', fpath=fpath) return serve_exported_files(request, fpath=fpath)
def png_map_for(request, perioda_str, periodb_str, entity_name, indicator_number, with_title=True, with_legend=True): entity = Entity.get_by_short_name(entity_name) if entity is None: raise Http404(_("Unknown entity with name `{u}`") .format(u=entity_name)) if perioda_str is None and periodb_str is None \ and indicator_number is None: periods = None indicator = None with_title = False fname = "initial.png" else: with_title = True perioda = process_period_filter( request, perioda_str, 'perioda').get('perioda') periodb = process_period_filter( request, periodb_str, 'periodb').get('periodb') periods = MonthPeriod.all_from(perioda, periodb) if not len(periods): raise Http404(_("Unknown period interval `{pa}` `{pb}`") .format(pa=perioda_str, pb=periodb_str)) indicator = Indicator.get_by_number(indicator_number) if indicator is None: raise Http404(_("Unknown indicator `{s}`") .format(s=indicator_number)) fname = fname_for(entity, periods, indicator) fpath = os.path.join('png_map', fname) abspath = os.path.join(settings.EXPORT_REPOSITORY, fpath) if not os.path.exists(abspath): try: gen_map_for(entity, periods, indicator, save_as=abspath, with_title=with_title, with_index=with_title) except IOError: logger.error("Missing map png folder in exports.") raise # return redirect('export', fpath=fpath) return serve_exported_files(request, fpath=fpath)
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 view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section1'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) all_indicators = Indicator.objects.all() if indicator_slug: qs = all_indicators.filter(slug=indicator_slug) indicator = qs.get() else: qs = indicator = None context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'elements': build_context(periods=periods, entity=context['entity'], qs=qs), 'indicators': all_indicators, 'indicator': indicator, }) # absolute URI for links context.update({'baseurl': request.build_absolute_uri()}) return render(request, kwargs.get('template_name', 'analysis_section1.html'), context)
def analysis(request, section_id, entity_uuid=None, perioda_str=None, periodb_str=None, *args, **kwargs): ''' Generic view for simple sections using an entity and periods ''' context = {'page': 'analysis_section{}'.format(section_id)} if section_id not in SECTIONS: raise Http404(_("Unknown section ID `{sid}`").format(sid=section_id)) section = import_path('dmd.analysis.section{}'.format(section_id)) # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) context.update({ 'section': section_id, 'section_name': SECTIONS.get(section_id), 'elements': section.build_context(periods=periods, entity=context['entity']) }) # absolute URI for links context.update({'baseurl': request.build_absolute_uri()}) return render( request, kwargs.get('template_name', 'analysis_section{}.html'.format(section_id)), context)
def analysis(request, section_id, entity_uuid=None, perioda_str=None, periodb_str=None, *args, **kwargs): ''' Generic view for simple sections using an entity and periods ''' context = {'page': 'analysis_section{}'.format(section_id)} if section_id not in SECTIONS: raise Http404(_("Unknown section ID `{sid}`").format(sid=section_id)) section = import_path('dmd.analysis.section{}'.format(section_id)) # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) context.update({ 'section': section_id, 'section_name': SECTIONS.get(section_id), 'elements': section.build_context(periods=periods, entity=context['entity']) }) # absolute URI for links context.update({'baseurl': request.build_absolute_uri()}) return render(request, kwargs.get('template_name', 'analysis_section{}.html'.format(section_id)), context)
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 validation_tallysheet_download(request, period_str=None, *args, **kwargs): context = {} dps = request.user.partner.validation_location context.update(process_period_filter(request, period_str)) period = context.get('period') or MonthPeriod.current().previous() file_name = validation_tallysheet_fname_for(dps, period) file_content = generate_validation_tally_for(dps, period).getvalue() response = HttpResponse(file_content, content_type='application/' 'vnd.openxmlformats-officedocument' '.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename="%s"' % file_name response['Content-Length'] = len(file_content) return response
def raw_data(request, entity_uuid=None, period_str=None, *args, **kwargs): context = {'page': 'raw_data'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling period context.update(process_period_filter(request, period_str)) context.update({ 'records': DataRecord.objects.filter( entity=context['entity'], period=context['period']).order_by('indicator__number') }) return render(request, kwargs.get('template_name', 'raw_data.html'), context)
def raw_data(request, entity_uuid=None, period_str=None, *args, **kwargs): context = {"page": "raw_data"} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling period context.update(process_period_filter(request, period_str)) context.update( { "records": DataRecord.objects.filter(entity=context["entity"], period=context["period"]).order_by( "indicator__number" ) } ) return render(request, kwargs.get("template_name", "raw_data.html"), context)
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section2'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods # context.update(process_period_filter(request, period_str, 'period')) context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) def cached_data_list(entity, periods, indicator): return [get_cached_data('section2-arrivals', entity=entity, period=period, indicator=indicator) for period in periods] context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'arrivals': OrderedDict( [(indicator, cached_data_list(context['entity'], periods, indicator)) for indicator in Indicator.get_all_routine()]) }) # evolution graph cp = { 'periods': [period.to_tuple() for period in periods], 'points': [get_cached_data('section2-points', entity=context['entity'], period=period) for period in periods] } perioda = periods[0] periodb = periods[-1] context.update({ 'cp_title': "Évolution de la complétude à {name} entre {pa} et {pb}" .format(name=context['entity'].short_name, pa=perioda.strid, pb=periodb.strid), 'cp_fname': "completeness-_{pa}_{pb}" .format(pa=perioda.strid, pb=periodb.strid), 'cp_categories': [p[1].name for p in cp['periods']], 'cp_series': [{'name': "Complétude", 'data': cp['points']}] }) # absolute URI for links context.update({ 'baseurl': request.build_absolute_uri(), 'lineage': [Entity.PROVINCE]}) return render(request, kwargs.get('template_name', 'analysis_section2.html'), context)
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section2'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods # context.update(process_period_filter(request, period_str, 'period')) context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) def cached_data_list(entity, periods, indicator): return [ get_cached_data('section2-arrivals', entity=entity, period=period, indicator=indicator) for period in periods ] context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'arrivals': OrderedDict([(indicator, cached_data_list(context['entity'], periods, indicator)) for indicator in Indicator.get_all_routine()]) }) # evolution graph cp = { 'periods': [period.to_tuple() for period in periods], 'points': [ get_cached_data('section2-points', entity=context['entity'], period=period) for period in periods ] } perioda = periods[0] periodb = periods[-1] context.update({ 'cp_title': "Évolution de la complétude à {name} entre {pa} et {pb}".format( name=context['entity'].short_name, pa=perioda.strid, pb=periodb.strid), 'cp_fname': "completeness-_{pa}_{pb}".format(pa=perioda.strid, pb=periodb.strid), 'cp_categories': [p[1].name for p in cp['periods']], 'cp_series': [{ 'name': "Complétude", 'data': cp['points'] }] }) # absolute URI for links context.update({ 'baseurl': request.build_absolute_uri(), 'lineage': [Entity.PROVINCE] }) return render(request, kwargs.get('template_name', 'analysis_section2.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)