Пример #1
0
    def clean_r(self):
        location = self.cleaned_data.get('l', None)
        radius = self.cleaned_data.get('r', None)
        if location not in EMPTY_VALUES and radius in EMPTY_VALUES:
            geocoder = GoogleGeocoder()
            departement = geocoder.get_departement(location)
            if departement:
                name, coordinates, radius = geocoder.geocode(
                    departement['long_name'])
            else:
                name, coordinates, radius = geocoder.geocode(location)

        if radius in EMPTY_VALUES:
            radius = DEFAULT_RADIUS
        return radius
Пример #2
0
 def _get_location(self, location):
     if not all((self._point, self._max_range)):
         _, coords, radius = GoogleGeocoder().geocode(location)
         self._max_range = radius or DEFAULT_RADIUS
         if all(coords):
             self._point = Point(coords)
     return (self._point, self._max_range)
Пример #3
0
    def build_filters(self, filters=None):
        if filters is None:
            filters = {}

        orm_filters = super(ProductResource, self).build_filters(filters)
        if "q" in filters or "l" in filters:
            sqs = product_search

            if "q" in filters:
                sqs = sqs.auto_query(filters['q'])

            if "l" in filters:
                name, coords, radius = GoogleGeocoder().geocode(filters['l'])
                if all(coords):
                    radius = filters.get('r',
                                         radius if radius else DEFAULT_RADIUS)
                    point = Point(coords)
                    sqs = sqs.dwithin(
                        'location', point,
                        Distance(km=radius))  #.distance('location', point)

            pk = []
            for p in sqs:
                try:
                    pk.append(int(p.pk))
                except ValueError:
                    pass

            orm_filters.update({"pk__in": pk})

        return orm_filters
Пример #4
0
 def test_google_geocoder(self, mock_func):
     mock_func.return_value = JSON_OUTPUT
     name, coordinates, radius = GoogleGeocoder(
         use_cache=False).geocode('11 rue debelleyme, 75003 Paris')
     self.assertTrue(mock_func.called)
     self.assertTrue(isinstance(coordinates, tuple))
     self.assertTrue(isinstance(name, basestring))
     self.assertTrue(isinstance(radius, int))
Пример #5
0
 def get_object_list(self, request):
     object_list = super(ProductResource, self).get_object_list(request)
     if request and "l" in request.GET:
         name, coords, radius = GoogleGeocoder().geocode(request.GET['l'])
         if all(coords):
             object_list = object_list.distance(
                 Point(coords), field_name='address__position')
     return object_list
Пример #6
0
def get_last_added_sqs(search_index,
                       location,
                       sort_by_date='-created_at_date'):
    # only objects that are 'good' to be shown
    sqs = search_index.filter(is_good=True)

    # try to find products in the same region
    region_point, region_radius = get_point_and_radius(
        location['region_coords'] or location['coordinates'],
        location['region_radius'] or location['radius'])
    last_added = sqs.dwithin('location', region_point,
                             Distance(km=region_radius)).distance(
                                 'location', region_point).order_by(
                                     sort_by_date, SORT.NEAR)

    # if there are no products found in the same region
    if not last_added.count():
        # try to find products in the same country
        try:
            country_point, country_radius = get_point_and_radius(
                GoogleGeocoder().geocode(location['country'])[1:3])
        except:
            # silently ignore exceptions like country name is missing or incorrect
            pass
        else:
            last_added = sqs.dwithin('location', country_point,
                                     Distance(km=country_radius)).distance(
                                         'location', country_point).order_by(
                                             sort_by_date, SORT.NEAR)

    # if there are no products found in the same country
    if not last_added.count():
        # do not filter on location, and return full list sorted by the provided date field only
        last_added = sqs.order_by(sort_by_date)

    return last_added
Пример #7
0
def get_position(address):
    """Identify geo position by address through Google service"""
    return GoogleGeocoder().geocode(address)[1]
Пример #8
0
 def get_coordinates(self, location):
     return GoogleGeocoder().geocode(location)[1]