Ejemplo n.º 1
0
def org_location(request, format=None):
    org_type = request.GET.get('org_type', '')
    org_codes = utils.param_to_list(request.GET.get('q', ''))
    if org_type == 'practice':
        org_codes = utils.get_practice_ids_from_org(org_codes)
    if org_type == 'ccg':
        results = PCT.objects.filter(close_date__isnull=True) \
                             .filter(org_type='CCG')
        if org_codes:
            results = results.filter(code__in=org_codes)
        geo_field = 'boundary'
        fields = (
            'name',
            'code',
            'ons_code',
            'org_type',
            'boundary',
        )
    else:
        results = Practice.objects.filter(code__in=org_codes)
        geo_field = 'location'
        fields = (
            'name',
            'code',
            'setting',
            'is_dispensing',
            'location',
        )
    return HttpResponse(serialize('geojson',
                                  results,
                                  geometry_field=geo_field,
                                  fields=fields),
                        content_type='application/json')
def org_details(request, format=None):
    '''
    Get list size and ASTRO-PU by month, for CCGs or practices.
    '''
    org_type = request.GET.get('org_type', None)
    orgs = utils.param_to_list(request.query_params.get('org', []))

    if org_type == 'practice':
        orgs = utils.get_practice_ids_from_org(orgs)
        query = "SELECT pr.date as date, pr.practice_id as row_id, "
        query += "pc.name as row_name, "
        query += "pr.total_list_size, pr.astro_pu_cost, "
        query += "pr.astro_pu_items, pr.star_pu_oral_antibac_items "
        query += "FROM frontend_practicelist pr "
        query += "JOIN frontend_practice pc ON pr.practice_id=pc.code "
        if orgs:
            query += "WHERE "
            for i, c in enumerate(orgs):
                query += "pr.practice_id=%s "
                if (i != len(orgs)-1):
                    query += ' OR '
            query += "ORDER BY date, row_id"
        else:
            query += "ORDER BY date, row_id"
    elif org_type == 'ccg':
        query = "SELECT pr.date, pr.pct_id as row_id, "
        query += "pc.name as row_name, "
        query += "SUM(pr.total_list_size) AS total_list_size, "
        query += "SUM(pr.astro_pu_cost) AS astro_pu_cost, "
        query += "SUM(pr.astro_pu_items) AS astro_pu_items, "
        query += "SUM(pr.star_pu_oral_antibac_items) "
        query += "AS star_pu_oral_antibac_items "
        query += "FROM frontend_practicelist pr "
        query += "JOIN frontend_pct pc ON pr.pct_id=pc.code "
        query += "WHERE pc.org_type='CCG' "
        if orgs:
            query += "AND ("
            for i, c in enumerate(orgs):
                query += "pct_id=%s "
                if (i != len(orgs)-1):
                    query += ' OR '
            query += ') '
            query += "GROUP BY pr.pct_id, pc.name, date ORDER BY date, row_id"
        else:
            query += "GROUP BY pr.pct_id, pc.name, date ORDER BY date, row_id"
    else:
        # Total across NHS England.
        query = "SELECT date, SUM(total_list_size) as total_list_size, "
        query += "SUM(astro_pu_cost) AS astro_pu_cost, "
        query += "SUM(astro_pu_items) AS astro_pu_items, "
        query += "SUM(star_pu_oral_antibac_items) "
        query += "AS star_pu_oral_antibac_items "
        query += "FROM frontend_practicelist "
        query += "GROUP BY date ORDER BY date"

    data = utils.execute_query(query, [orgs])
    return Response(data)
Ejemplo n.º 3
0
def spending_by_org(request, format=None, org_type=None):
    codes = utils.param_to_list(request.query_params.get('code', []))
    codes = utils.get_bnf_codes_from_number_str(codes)
    org_ids = utils.param_to_list(request.query_params.get('org', []))
    org_type = request.query_params.get('org_type', org_type)
    date = request.query_params.get('date', None)

    # Accept both cases of CCG (better to fix this specific string rather than
    # make the whole API case-insensitive)
    if org_type == 'CCG':
        org_type = 'ccg'

    # Some special case handling for practices
    if org_type == 'practice':
        # Translate any CCG codes into the codes of all practices in that CCG
        org_ids = utils.get_practice_ids_from_org(org_ids)
        # Due to the number of practices we only return data for all practices
        # if a single date is specified
        if not date and not org_ids:
            return Response(
                'Error: You must supply either a list of practice IDs or a date '
                'parameter, e.g. date=2015-04-01',
                status=400)

    # We don't (yet?) have a "proper" code field for STPs so we're using their
    # ONS code
    code_field = 'code' if org_type != 'stp' else 'ons_code'

    if org_type == 'practice':
        orgs = Practice.objects.all()
    elif org_type == 'ccg':
        orgs = PCT.objects.filter(org_type='CCG')
    elif org_type == 'stp':
        orgs = STP.objects.all()
    elif org_type == 'regional_team':
        orgs = RegionalTeam.objects.all()
    else:
        return Response('Error: unrecognised org_type parameter', status=400)

    # Filter and sort
    if org_ids:
        orgs = orgs.filter(**{code_field + '__in': org_ids})
    orgs = orgs.order_by(code_field)

    # For most orgs we just want the code and name but for practices we want
    # the entire object because, for compatibility with the existing API, we
    # return extra data for practices
    if org_type != 'practice':
        orgs = orgs.only(code_field, 'name')

    data = _get_prescribing_entries(codes, orgs, org_type, date=date)
    return Response(list(data))
def org_location(request, format=None):
    org_type = request.GET.get('org_type', '')
    org_codes = utils.param_to_list(request.GET.get('q', ''))
    if org_type == 'practice':
        org_codes = utils.get_practice_ids_from_org(org_codes)

    if org_type == 'ccg':
        results = PCT.objects.filter(org_type='CCG')
        if org_codes:
            results = results.filter(code__in=org_codes)
        geo_field = 'boundary'
        fields = ('name', 'code', 'ons_code', 'org_type', 'boundary', )
    else:
        results = Practice.objects.filter(code__in=org_codes)
        geo_field = 'location'
        fields = ('name', 'code', 'setting', 'is_dispensing', 'location', )
    return HttpResponse(serialize('geojson', results,
                        geometry_field=geo_field, fields=fields),
                        content_type='application/json')
Ejemplo n.º 5
0
def spending_by_practice(request, format=None):
    codes = utils.param_to_list(request.query_params.get('code', []))
    codes = utils.get_bnf_codes_from_number_str(codes)
    orgs = utils.param_to_list(request.query_params.get('org', []))
    date = request.query_params.get('date', None)

    spending_type = utils.get_spending_type(codes)
    if spending_type is False:
        err = 'Error: Codes must all be the same length'
        return Response(err, status=400)
    if spending_type == 'bnf-section' or spending_type == 'product':
        codes = [c + '%' for c in codes]

    if not date and not orgs:
        err = 'Error: You must supply either '
        err += 'a list of practice IDs or a date parameter, e.g. '
        err += 'date=2015-04-01'
        return Response(err, status=400)

    org_for_param = None
    if not spending_type or spending_type == 'bnf-section' \
       or spending_type == 'chemical':
        # We can do presentation queries indexed by PCT ID, which is faster.
        # We have yet to update the *_by_practice matviews with PCT ID.
        # So for these queries, expand the CCG ID to a list of practice IDs.
        expanded_orgs = utils.get_practice_ids_from_org(orgs)
        if codes:
            query = _get_chemicals_or_sections_by_practice(codes,
                                                           expanded_orgs,
                                                           spending_type,
                                                           date)
            org_for_param = expanded_orgs
        else:
            query = _get_total_spending_by_practice(expanded_orgs, date)
            org_for_param = expanded_orgs
    else:
        query = _get_presentations_by_practice(codes, orgs, date)
        org_for_param = orgs
    data = utils.execute_query(
        query, [codes, org_for_param, [date] if date else []])
    return Response(data)
Ejemplo n.º 6
0
def spending_by_practice(request, format=None):
    codes = utils.param_to_list(request.query_params.get('code', []))
    codes = utils.get_bnf_codes_from_number_str(codes)
    orgs = utils.param_to_list(request.query_params.get('org', []))
    date = request.query_params.get('date', None)

    spending_type = utils.get_spending_type(codes)
    if spending_type is False:
        err = 'Error: Codes must all be the same length'
        return Response(err, status=400)
    if spending_type == 'bnf-section' or spending_type == 'product':
        codes = [c + '%' for c in codes]

    if not date and not orgs:
        err = 'Error: You must supply either '
        err += 'a list of practice IDs or a date parameter, e.g. '
        err += 'date=2015-04-01'
        return Response(err, status=400)

    org_for_param = None
    if not spending_type or spending_type == 'bnf-section' \
       or spending_type == 'chemical':
        # We can do presentation queries indexed by PCT ID, which is faster.
        # We have yet to update the *_by_practice matviews with PCT ID.
        # So for these queries, expand the CCG ID to a list of practice IDs.
        expanded_orgs = utils.get_practice_ids_from_org(orgs)
        if codes:
            query = _get_chemicals_or_sections_by_practice(codes,
                                                           expanded_orgs,
                                                           spending_type,
                                                           date)
            org_for_param = expanded_orgs
        else:
            query = _get_total_spending_by_practice(expanded_orgs, date)
            org_for_param = expanded_orgs
    else:
        query = _get_presentations_by_practice(codes, orgs, date)
        org_for_param = orgs
    data = utils.execute_query(
        query, [codes, org_for_param, [date] if date else []])
    return Response(data)