Exemple #1
0
 def search_in_venues(self, **kwargs):
     params = parameters.PlaceSearchParametersFactory.create(kwargs, self.city_service).get_db_params()
     latitude = params.get(params_names.LATITUDE)
     longitude = params.get(params_names.LONGITUDE)
     city_id = params.get(params_names.CITY_ID)
     query = params.get(params_names.QUERY)
     radius = params.get(params_names.RADIUS)
     result = venue.search({"latitude": latitude, "longitude": longitude}, query, radius)
     place_and_distance_list = map(
         lambda l: {
             "place": models.Place(
                 id=l[u"id"],
                 name=l[u"name"],
                 contact=l.get(u"contact"),
                 address=l[u"location"].get(u"address") and (u"%s" % l[u"location"].get(u"address")) or None,
                 crossStreet=l[u"location"].get(u"crossStreet")
                 and (u"%s" % l[u"location"].get(u"crossStreet"))
                 or None,
                 position=gis_core.ll_to_point(l[u"location"].get(u"lng"), l[u"location"].get(u"lat")),
                 city_id=None,
                 foursquare_icon_suffix=utils.safe_get(l, lambda el: el[u"categories"][0][u"icon"][u"suffix"]),
                 foursquare_icon_prefix=utils.safe_get(l, lambda el: el[u"categories"][0][u"icon"][u"prefix"]),
             ),
             "distance": l[u"location"].get(u"distance"),
             "azimuth": gis_core.azimuth(
                 longitude, latitude, l[u"location"].get(u"lng"), l[u"location"].get(u"lat")
             ),
         },
         result["venues"],
     )
     if len(place_and_distance_list) > 0:
         caching_manager = ModelCachingManager(
             [item["place"] for item in place_and_distance_list],
             lambda e: e.date,
             datetime.timedelta(seconds=60 * 60 * 24 * 3),
         )
         if len(caching_manager.for_insert) > 0:
             for e in caching_manager.for_insert:
                 e.city_id = city_id
         if len(caching_manager.cached) > 0:
             for e in caching_manager.cached:
                 stored_city = lists.first_match(lambda x: x.id == e.id, caching_manager.stored)
                 e.city_id = stored_city.city_id
         caching_manager.save()
     return place_and_distance_list
Exemple #2
0
 def test_first_match(self):
     self.assertEquals(lists.first_match(lambda x: x < 4, [4, 2, 3, 5]), 2)
     self.assertEquals(lists.first_match(lambda x: x < 1, [4, 2, 3, 5]), None)