def location_search(self, location, queryset=None): """Groups search by location.""" coords = get_coordinates(location) # If no coordinates are provided, return an empty queryset if not coords: return Group.objects.none() if queryset is None: queryset = Group.objects.published() # Pass the job of finding distance to the database using this query sql = ('SELECT ' '(degrees(acos( ' 'sin(radians(latitude)) ' '* sin(radians(%s)) ' '+ cos(radians(latitude)) ' '* cos(radians(%s)) ' '* cos(radians(longitude - %s) ) ' ') ) * 69.09)') result = queryset.extra( select={ 'distance': sql }, select_params=(coords[0], coords[0], coords[1]), # We use the same SQL again to do filtering by distance and # radius. We cannot use the param in the `SELECT` because # of a postgres limitation where=['(' + sql + ') <= "groups_group"."radius"'], params=(coords[0], coords[0], coords[1]), order_by=['-featured', 'distance', 'group__name']).distinct() return result
def get_queryset(self): """Modify queryset if there are search params in the request.""" location = self.request.GET.get('location') query = self.request.GET.get('q') coords = None if location: coords = get_coordinates(location) if coords: self.located = True queryset = Group.objects.search(search=query, location=coords) return queryset
def get_queryset(self): """Modify queryset if there are search params in the request.""" location = self.request.GET.get('location') query = self.request.GET.get('q') coords = None if location: coords = get_coordinates(location) if coords: self.located = True queryset = Group.objects.search( search=query, location=coords) return queryset
def location_search(self, location, queryset=None): """Groups search by location.""" coords = get_coordinates(location) # If no coordinates are provided, return an empty queryset if not coords: return Group.objects.none() if queryset is None: queryset = Group.objects.published() # Pass the job of finding distance to the database using this query sql = ( 'SELECT ' '(degrees(acos( ' 'sin(radians(latitude)) ' '* sin(radians(%s)) ' '+ cos(radians(latitude)) ' '* cos(radians(%s)) ' '* cos(radians(longitude - %s) ) ' ') ) * 69.09)' ) result = queryset.extra( select={'distance': sql}, select_params=(coords[0], coords[0], coords[1]), # We use the same SQL again to do filtering by distance and # radius. We cannot use the param in the `SELECT` because # of a postgres limitation where=['(' + sql + ') <= "groups_group"."radius"'], params=(coords[0], coords[0], coords[1]), order_by=['-featured', 'distance', 'group__name'] ).distinct() return result
def test_get_location_without_coords(self, mock): """Test get_location without coordinates.""" self.assertEqual(mock.call_count, 0) location.get_coordinates('awooga!') self.assertEqual(mock.call_count, 1)
def test_get_location_with_coords(self): """Test get_location with coordinates.""" response = location.get_coordinates((81, -81)) self.assertEqual(response, (81, -81))