def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super().get_search_results(
            request, queryset, search_term)

        if len(queryset) < 3 and len(search_term) > 3:
            Place.create_or_update_from_geonames(search_term)
            queryset, use_distinct = super().get_search_results(
                request, queryset, search_term)

        return queryset, use_distinct
Esempio n. 2
0
 def test_autocomplete_view(self):
     self.assertEqual(Place.objects.count(), 0)
     url = reverse('place_autocomplete')
     c = Client()
     response = c.get(url, {'term': 'Lo'})
     self.assertEqual(response.json()['results'], [])
     p = Place(geonames_id=self.geonames_id)
     p.save()
     self.assertEqual(Place.objects.count(), 1)
     response = c.get(url, {'term': 'London'})
     self.assertNotEqual(len(response.json()['results']), 0)
     # There are many places with London in the name, and they
     # should now be stored in the Place model, having been fetched
     # from Geonames.
     self.assertTrue(Place.objects.count() > 1)
Esempio n. 3
0
    def test_update_from_geonames(self):
        p = Place()
        p.hydrate_from_geonames()
        self.assertIsNone(p.address)

        p.geonames_id = self.geonames_id
        p.hydrate_from_geonames()
        self.assertEquals(self.geonames_address, p.address)
def get_geonames_place_from_gsx_place(name: str) -> Optional[Place]:
    """Returns a Geonames `Place` from a Google Spreadsheet place `name`. The GSX place
    name is in the format `ID: Address [country_code]`."""
    if not name:
        return None

    matches = GSX_PLACE.match(name)
    if not matches:
        return None

    address = matches.group("address").strip()
    country_code = matches.group("country_code")
    return Place.get_or_create_from_geonames(address,
                                             country_code=country_code)
Esempio n. 5
0
    def test__hydrate(self):
        p = Place(geonames_id=self.geonames_id)
        p._hydrate(None)
        self.assertIsNone(p.address)

        g = geocoder.geonames(p.geonames_id,
                              key=settings.GEONAMES_KEY,
                              method='details')

        p._hydrate(g)
        self.assertEquals(self.geonames_address, p.address)
Esempio n. 6
0
def get_place(address, country_code):
    return Place.get_or_create_from_geonames(
        address=address, country_code=country_code)
Esempio n. 7
0
 def test_create_or_update_from_geonames(self):
     self.assertEquals(0, Place.create_or_update_from_geonames(None))
     self.assertEquals(0, Place.create_or_update_from_geonames('lo'))
     self.assertEquals(settings.GEONAMES_MAX_RESULTS,
                       Place.create_or_update_from_geonames(self.address))
Esempio n. 8
0
    def test_save(self):
        p = Place(geonames_id=self.geonames_id)
        p.save()
        self.assertEquals(self.geonames_address, p.address)

        with self.assertRaises(IntegrityError):
            p = Place()
            p.save()

            p.geonames_id = self.geonames_id
            p.update_from_geonames = False
            p.save()