<commit_before>from rest_framework.decorators import api_view
from frontend.models import PCT, Practice
import view_utils as utils
from django.http import HttpResponse
from django.core.serializers import serialize


@api_view(['GET'])
def org_location(request, format=None):
    org_type = request.GET.get('org_type', '')
    centroids = request.GET.get('centroids', '')
    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)
        if centroids:
            fields = ('name', 'code', 'ons_code', 'org_type', 'centroid', )
            geo_field = 'centroid'
        else:
            fields = ('name', 'code', 'ons_code', 'org_type', 'boundary', )
            geo_field = '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')
<commit_msg>Refactor org_location endpoint<commit_after>from django.http import HttpResponse
from django.core.serializers import serialize

from rest_framework.decorators import api_view

from frontend.models import PCT, Practice
import api.view_utils as utils


@api_view(['GET'])
def org_location(request, format=None):
    org_type = request.GET.get('org_type', '')
    centroids = request.GET.get('centroids', '')
    org_codes = utils.param_to_list(request.GET.get('q', ''))
    if org_type == 'practice':
        result_spec = _get_practices(org_codes, centroids)
    elif org_type == 'ccg':
        result_spec = _get_ccgs(org_codes, centroids)
    else:
        raise ValueError('Unknown org_type: {}'.format(org_type))
    results, geo_field, other_fields = result_spec
    fields = other_fields + (geo_field,)
    return HttpResponse(
        serialize('geojson', results, geometry_field=geo_field, fields=fields),
        content_type='application/json'
    )


def _get_practices(org_codes, centroids):
    org_codes = utils.get_practice_ids_from_org(org_codes)
    results = Practice.objects.filter(code__in=org_codes)
    geo_field = 'location'
    other_fields = ('name', 'code', 'setting', 'is_dispensing')
    return results, geo_field, other_fields


def _get_ccgs(org_codes, centroids):
    results = PCT.objects.filter(close_date__isnull=True, org_type='CCG')
    if org_codes:
        results = results.filter(code__in=org_codes)
    geo_field = 'centroid' if centroids else 'boundary'
    other_fields = ('name', 'code', 'ons_code', 'org_type')
    return results, geo_field, other_fields

{'fraction_non_alphanumeric': 0.11959798994974874, 'fraction_numerical': 0.0, 'mean_word_length': 3.565749235474006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3212923', 'n_tokens_mistral': 944, 'n_tokens_neox': 926, 'n_words': 189}