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)
def form_submitted(request): pc = request.POST.get('pc', '') if hasattr(countries, 'canonical_postcode'): pc = countries.canonical_postcode(pc) if not request.method == 'POST' or not pc: return redirect('/') return redirect('mapit-postcode', postcode=pc, format='html')
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)