예제 #1
0
def partial_postcode(request, postcode, format='json'):
    postcode = re.sub(r'\s+', '', postcode.upper())
    if is_valid_postcode(postcode):
        postcode = re.sub(r'\d[A-Z]{2}$', '', postcode)
    if not is_valid_partial_postcode(postcode):
        raise ViewException(format,
                            "Partial postcode '%s' is not valid." % postcode,
                            400)

    location = Postcode.objects.filter(postcode__startswith=postcode).extra(
        where=['length(postcode) = %d' % (len(postcode) + 3)]).aggregate(
            Collect('location'))['location__collect']
    if not location:
        raise ViewException(format, 'Postcode not found', 404)

    postcode = Postcode(postcode=postcode, location=location.centroid)

    if format == 'html':
        return render(
            request, 'mapit/postcode.html', {
                'postcode': postcode.as_dict(),
                'json_view': 'mapit-postcode-partial',
            })

    return output_json(postcode.as_dict())
예제 #2
0
def partial_postcode(request, postcode, format='json'):
    postcode = re.sub(r'\s+', '', postcode.upper())
    if is_valid_postcode(postcode):
        postcode = re.sub(r'\d[A-Z]{2}$', '', postcode)
    if not is_valid_partial_postcode(postcode):
        raise ViewException(format, "Partial postcode '%s' is not valid." % postcode, 400)

    location = Postcode.objects.filter(postcode__startswith=postcode).extra(
        where=['length(postcode) = %d' % (len(postcode) + 3)]
        ).aggregate(Collect('location'))['location__collect']
    if not location:
        raise ViewException(format, 'Postcode not found', 404)

    postcode = Postcode(postcode=postcode, location=location.centroid)

    if format == 'html':
        return render(request, 'mapit/postcode.html', {
            'postcode': postcode.as_dict(),
            'json_view': 'mapit-postcode-partial',
        })

    return output_json(postcode.as_dict())
예제 #3
0
def partial_postcode(request, postcode, format='json'):
    postcode = re.sub('\s+', '', postcode.upper())
    if is_valid_postcode(postcode):
        postcode = re.sub('\d[A-Z]{2}$', '', postcode)
    if not is_valid_partial_postcode(postcode):
        return bad_request(format, "Partial postcode '%s' is not valid." % postcode)
    try:
        postcode = Postcode(
            postcode = postcode,
            location = Postcode.objects.filter(postcode__startswith=postcode).extra(
                where = [ 'length(postcode) = %d' % (len(postcode)+3) ]
            ).collect().centroid
        )
    except:
        return output_error(format, 'Postcode not found', 404)

    if format == 'html':
        return render_to_response('mapit/postcode.html', {
            'postcode': postcode.as_dict(),
            'json': '/postcode/partial/',
        })

    return output_json(postcode.as_dict())
예제 #4
0
def partial_postcode(request, postcode, format='json'):
    postcode = re.sub('\s+', '', postcode.upper())
    if is_valid_postcode(postcode):
        postcode = re.sub('\d[A-Z]{2}$', '', postcode)
    if not is_valid_partial_postcode(postcode):
        return bad_request(format,
                           "Partial postcode '%s' is not valid." % postcode)
    try:
        postcode = Postcode(
            postcode=postcode,
            location=Postcode.objects.filter(
                postcode__startswith=postcode).extra(
                    where=['length(postcode) = %d' %
                           (len(postcode) + 3)]).collect().centroid)
    except:
        return output_error(format, 'Postcode not found', 404)

    if format == 'html':
        return render_to_response('mapit/postcode.html', {
            'postcode': postcode.as_dict(),
            'json': '/postcode/partial/',
        })

    return output_json(postcode.as_dict())