Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    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
Esempio n. 4
0
    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
Esempio n. 5
0
 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)
Esempio n. 6
0
 def test_get_location_with_coords(self):
     """Test get_location with coordinates."""
     response = location.get_coordinates((81, -81))
     self.assertEqual(response, (81, -81))