예제 #1
0
def postcode(request, postcode, format=None):
    if hasattr(countries, 'canonical_postcode'):
        canon_postcode = countries.canonical_postcode(postcode)
        postcode = canon_postcode
        # if (postcode != canon_postcode and format is None) or format == 'json':
        #     return redirect('mapit.views.postcodes.postcode', postcode=canon_postcode)
    if format is None:
        format = 'json'
    if not is_valid_postcode(postcode):
        raise ViewException(format, "Postcode '%s' is not valid." % postcode,
                            400)
    postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
    try:
        generation = int(request.GET['generation'])
    except:
        generation = Generation.objects.current()
    if not hasattr(countries,
                   'is_special_postcode') or not countries.is_special_postcode(
                       postcode.postcode):
        areas = list(add_codes(Area.objects.by_postcode(postcode, generation)))
    else:
        areas = []

    # Shortcuts
    shortcuts = {}
    for area in areas:
        if area.type.code in ('COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW'):
            shortcuts['ward'] = area.id
            shortcuts['council'] = area.parent_area_id
        elif area.type.code == 'CED':
            shortcuts.setdefault('ward', {})['county'] = area.id
            shortcuts.setdefault('council', {})['county'] = area.parent_area_id
        elif area.type.code == 'DIW':
            shortcuts.setdefault('ward', {})['district'] = area.id
            shortcuts.setdefault('council',
                                 {})['district'] = area.parent_area_id
        elif area.type.code in ('WMC', ):
            # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG'):
            shortcuts[area.type.code] = area.id

    # Add manual enclosing areas.
    extra = []
    for area in areas:
        if area.type.code in enclosing_areas.keys():
            extra.extend(enclosing_areas[area.type.code])
    areas = itertools.chain(areas, Area.objects.filter(id__in=extra))

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

    out = postcode.as_dict()
    out['areas'] = dict((area.id, area.as_dict()) for area in areas)
    if shortcuts:
        out['shortcuts'] = shortcuts
    return output_json(out)
예제 #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 postcode(request, postcode, format=None):
    if hasattr(countries, 'canonical_postcode'):
        canon_postcode = countries.canonical_postcode(postcode)
        postcode = canon_postcode
        # if (postcode != canon_postcode and format is None) or format == 'json':
        #     return redirect('mapit.views.postcodes.postcode', postcode=canon_postcode)
    if format is None:
        format = 'json'
    if not is_valid_postcode(postcode):
        raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400)
    postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
    try:
        generation = int(request.GET['generation'])
    except:
        generation = Generation.objects.current()
    if not hasattr(countries, 'is_special_postcode') or not countries.is_special_postcode(postcode.postcode):
        areas = list(add_codes(Area.objects.by_postcode(postcode, generation)))
    else:
        areas = []

    # Shortcuts
    shortcuts = {}
    for area in areas:
        if area.type.code in ('COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW'):
            shortcuts['ward'] = area.id
            shortcuts['council'] = area.parent_area_id
        elif area.type.code == 'CED':
            shortcuts.setdefault('ward', {})['county'] = area.id
            shortcuts.setdefault('council', {})['county'] = area.parent_area_id
        elif area.type.code == 'DIW':
            shortcuts.setdefault('ward', {})['district'] = area.id
            shortcuts.setdefault('council', {})['district'] = area.parent_area_id
        elif area.type.code in ('WMC',):
            # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG'):
            shortcuts[area.type.code] = area.id

    # Add manual enclosing areas.
    extra = []
    for area in areas:
        if area.type.code in enclosing_areas.keys():
            extra.extend(enclosing_areas[area.type.code])
    areas = itertools.chain(areas, Area.objects.filter(id__in=extra))

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

    out = postcode.as_dict()
    out['areas'] = dict((area.id, area.as_dict()) for area in areas)
    if shortcuts:
        out['shortcuts'] = shortcuts
    return output_json(out)
예제 #4
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())
예제 #5
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())
예제 #6
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())
예제 #7
0
def check_postcode(format, postcode):
    postcode = re.sub('[^A-Z0-9]', '', postcode.upper())
    if not is_valid_postcode(postcode):
        return bad_request(format, "Postcode '%s' is not valid." % postcode)
    postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
    return postcode
예제 #8
0
def check_postcode(format, postcode):
    postcode = re.sub('[^A-Z0-9]', '', postcode.upper())
    if not is_valid_postcode(postcode):
        return bad_request(format, "Postcode '%s' is not valid." % postcode)
    postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
    return postcode
예제 #9
0
파일: postcodes.py 프로젝트: mberg/mapit
def check_postcode(format, postcode):
    postcode = re.sub('[^A-Z0-9]', '', postcode.upper())
    if not is_valid_postcode(postcode):
        raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400)
    postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
    return postcode