def box(request, indicator):
    indicators = [indicator]
    db_regions = Region.objects.all()
    sub_indicators = request.GET.get('sub_indicators')
    if sub_indicators == 'yes':
        indicators.extend(get_object_or_404(Indicator, indicator=indicator).\
            sub_indicators.all().values_list('indicator', flat=True))

    json = []
    for indicator_item in indicators:
        target = get_object_or_404(Indicator, indicator=indicator_item).target

        regions = []
        for db_region in db_regions:
            region = {}
            region['name'] = db_region.name
            values = get_values(None, db_region, indicator_item)

            all_value = []
            max_value = {'country': None, 'value': None}
            min_value = {'country': None, 'value': None}
            for value in values:
                loads_value = simplejson.loads(value['value'])
                vkeys = [key for key in loads_value.keys() if key.startswith('Value')]
                vkeys.sort()
                current_v = None
                for key in vkeys:
                    current_vkey = calc_values(loads_value.get(key))
                    if current_vkey is not None and current_vkey != '':
                        current_v = current_vkey
                if current_v is not None:
                    all_value.append(current_v)
                    if max_value['value'] is None or current_v > max_value['value']:
                        max_value['value'] = current_v
                        max_value['country'] = value['response_set__entity__name']
                    if min_value['value'] is None or current_v < min_value['value']:
                        min_value['value'] = current_v
                        min_value['country'] = value['response_set__entity__name']
            region['max_value'] = max_value
            region['min_value'] = min_value

            all_value.sort()
            region = set_quartiles(all_value, region)
            region['num_countries'] = values.count()
            regions.append(region)

        values = get_values('all', db_region, indicator_item)
        sources = get_sources(values)

        json_item = {}
        json_item['indicator'] = indicator_item
        json_item['target'] = target
        json_item['regions'] = regions
        json_item['sources'] = sources
        json.append(json_item)

    json = simplejson.dumps(json)
    return HttpResponse(json, content_type='application/json')
def graph(request, indicator):
    indicators = [indicator]
    region_id = request.GET.get('region', 0)
    region = None
    if region_id != 'all':
        region = get_object_or_404(Region, id=region_id)
    sub_indicators = request.GET.get('sub_indicators')
    if sub_indicators == 'yes':
        indicators.extend(get_object_or_404(Indicator, indicator=indicator).\
            sub_indicators.all().values_list('indicator', flat=True))

    json = []
    for indicator_item in indicators:
        target = get_object_or_404(Indicator, indicator=indicator_item).target
        values = get_values(region_id, region, indicator_item)
        sources = get_sources(values)

        countries = []
        for value in values:
            country = {}
            country['name'] = value['response_set__entity__name']
            loads_value = simplejson.loads(value['value'])
            skeys = [key for key in loads_value.keys() if key.startswith('Source')]
            skeys.sort()
            source = None
            for key in skeys:
                source_key = loads_value.get(key)
                if source_key is not None and source_key != '':
                    source = source_key
            source_id = None
            for sources_item in sources:
                if sources_item['name'] == source:
                    source_id = sources_item['id']
            country['source'] = source_id
            vkeys = [key for key in loads_value.keys() if key.startswith('Value')]
            vkeys.sort()
            score = None
            for key in vkeys:
                score_key = calc_values(loads_value.get(key))
                if score_key is not None and score_key != '':
                    score = score_key
            country['score'] = score
            countries.append(country)

        json_item = {}
        if region_id != 'all':
            json_item['region'] = region.name
        else:
            json_item['region'] = 'All regions'
        json_item['indicator'] = indicator_item
        json_item['target'] = target
        json_item['sources'] = sources
        json_item['countries'] = countries
        json.append(json_item)

    json = simplejson.dumps(json)
    return HttpResponse(json, content_type='application/json')
def aggregate(request, indicator):
    indicators = [indicator]
    region_id = request.GET.get('region', 0)
    region = None
    if region_id != 'all':
        region = get_object_or_404(Region, id=region_id)
    sub_indicators = request.GET.get('sub_indicators')
    if sub_indicators == 'yes':
        indicators.extend(get_object_or_404(Indicator, indicator=indicator).\
            sub_indicators.all().values_list('indicator', flat=True))

    json = []
    for indicator_item in indicators:
        values = get_values(region_id, region, indicator_item)
        sources = get_sources(values)

        score_items = []
        for value in values:
            loads_value = simplejson.loads(value['value'])
            vkeys = [key for key in loads_value.keys() if key.startswith('Value')]
            vkeys.sort()
            score_item = None
            for key in vkeys:
                score_key = calc_values(loads_value.get(key))
                if score_key is not None and score_key != '':
                    score_item = score_key
            if score_item is not None:
                score_items.append(score_item)
        score = None
        if len(score_items):
            score = int(round(sum(score_items) / len(score_items)))

        json_item = {}
        if region_id != 'all':
            json_item['region'] = region.name
        else:
            json_item['region'] = 'All regions'
        json_item['indicator'] = indicator_item
        json_item['sources'] = sources
        json_item['score'] = score
        json.append(json_item)

    json = simplejson.dumps(json)
    return HttpResponse(json, content_type='application/json')