def locations(request): def _location_context(loc): return { 'id': str(loc['_id']), 'place_name': loc['place_name'], 'postcode': loc.get('postcode', ''), 'district': loc.get('district', '')} errors = [] data = [] response_code = 200 match = request.REQUEST.get('match') callback = request.REQUEST.get('callback') # postcodes True unless param exists and is 0 postcodes = _check_api_bool(request.REQUEST.get('postcodes'), True) if match and len(match) > 2: data = [_location_context(l) for l in get_location(match, just_one=False, starts_with=True, postcodes=postcodes)] else: response_code = 10 errors.append('Param \'match\' must be greater than 2 characters. You sent \'%s\'' % match) return JsonResponse( errors=errors and {'code': response_code, 'message': '. '.join(errors)} or {}, data=data, callback=callback, )
def test_get_location(self): from resources.search import get_location, get_or_create_location loc = get_location('muirhouse') self.assertEqual(loc['district'], 'North Lanarkshire') loc = get_location('muirhouse, City of Edinburgh') self.assertFalse(loc) loc = get_location('muirhouse: City of Edinburgh') self.assertEqual(loc['district'], 'City of Edinburgh') loc = get_location('G4 0qr') self.assertEqual(loc['place_name'], 'Anderston/City, Glasgow City') loc = get_location('g5') self.assertEqual(loc['place_name'], 'Glasgow') self.assertEqual(loc['postcode'], 'G5') loc = get_location('hounslow') self.assertEqual(loc, []) loc = get_or_create_location('hounslow') self.assertEqual(loc['_id'], 'Hounslow_Greater London') loc = get_location('hounslow') self.assertEqual(loc['_id'], 'Hounslow_Greater London') self.assertEqual(get_or_create_location('zzzzzzz'), []) self.assertEqual(get_or_create_location(' zzzzzzz '), []) self.assertEqual(get_or_create_location(''), []) self.assertEqual(get_or_create_location(' '), [])
def test_get_location(self): from resources.search import get_location, get_or_create_location loc = get_location("muirhouse") self.assertEqual(loc["district"], "North Lanarkshire") loc = get_location("muirhouse, City of Edinburgh") self.assertFalse(loc) loc = get_location("muirhouse: City of Edinburgh") self.assertEqual(loc["district"], "City of Edinburgh") loc = get_location("G4 0qr") self.assertEqual(loc["place_name"], "Anderston/City, Glasgow City") loc = get_location("g5") self.assertEqual(loc["place_name"], "Glasgow") self.assertEqual(loc["postcode"], "G5") loc = get_location("hounslow") self.assertEqual(loc, []) loc = get_or_create_location("hounslow") self.assertEqual(loc["_id"], "Hounslow_Greater London") loc = get_location("hounslow") self.assertEqual(loc["_id"], "Hounslow_Greater London") self.assertEqual(get_or_create_location("zzzzzzz"), []) self.assertEqual(get_or_create_location(" zzzzzzz "), []) self.assertEqual(get_or_create_location(""), []) self.assertEqual(get_or_create_location(" "), [])
def clean(self): # if errors in data, cleaned_data may be wiped, and/or fields not available cleaned_data = self.cleaned_data data = cleaned_data.get('location', '').strip() self.loc_found = get_location(data) if not self.loc_found: increment_failed_locations(data) raise forms.ValidationError("Location not found.") return cleaned_data
def clean(self): # if errors in data, cleaned_data may be wiped, and/or fields not available cleaned_data = self.cleaned_data data = cleaned_data.get("location", "").strip() self.loc_found = get_location(data) if not self.loc_found: increment_failed_locations(data) raise forms.ValidationError("Location not found.") return cleaned_data
def test_location(self): from resources.search import get_location loc = get_location('muirhouse') self.assertEqual(loc['district'], 'North Lanarkshire') loc = get_location('muirhouse, City of Edinburgh') self.assertFalse(loc) loc = get_location('muirhouse: City of Edinburgh') self.assertEqual(loc['district'], 'City of Edinburgh') loc = get_location('G4 0qr') self.assertEqual(loc['place_name'], 'Anderston/City, Glasgow City') loc = get_location('g5') self.assertEqual(loc['place_name'], 'Glasgow') self.assertEqual(loc['postcode'], 'G5')
def get_postcode_data(self, postcode): """docstring for get_postcode_data""" # from firebox.ordnancesurvey import get_os_postcode, OS_LABEL, OS_TYPE, OS_LAT, OS_LON, OS_WARD, OS_DISTRICT, OS_COUNTRY result = None if postcode == '': return result try: location = get_location(postcode, create_location=True) # print location if location: result = Location.objects.get(id=location['_id']) # [Location.objects.get(id=loc['_id']) for loc in locations] # print 'already got:', postcode except Location.DoesNotExist: print 'not found- ', postcode # print Location.create_from(postcode) return result