Exemplo n.º 1
0
    def search(self, address, date_range, guests, distance=200000, **kwargs):
        """Search spaces basing on the address, date_range, guests, etc
        :param address the raw address
        :date_range the sequence of 2 dates: start and end date
        :guests the number of guests that a space could accommodate
        :distance the meters from the raw address to look for, default: 20 km
        """
        start_date, end_date = date_range
        query = self.filter(guests__gte=guests)

        if start_date:
            query = query.filter(daterange__start_date__lte=start_date)

        if end_date:
            query = query.filter(daterange__end_date__gte=end_date)

        # TODO(hoatle): this takes lot of time, need to improve this
        if address.raw:
            location = address_to_location(address.raw)
        else:
            location = address_to_location(address)

        address_lat, address_lon = location_to_latlon(location)

        city = location_to_city(location)
        if city is not None:
            city = normalize_name(city)
            query = query.filter(city=city)
        else:
            country = location_to_country(location)
            if country is not None:
                country = normalize_name(country)
                query = query.filter(country=country)
            distance = 2000000
        current_point = geos.fromstr('POINT(%s %s)' % (address_lon, address_lat))
        distance_from_point = {'m': distance}
        query = query.filter(location__distance_lte=(current_point,
                                                     measure.D(**distance_from_point)))
        return query.distance(current_point).order_by('distance')
Exemplo n.º 2
0
    def save(self, **kwargs):
        # TODO(hoatle): update location only if address is changed
        # https://github.com/smn/django-dirtyfields
        location = address_to_location(self.address.raw)

        self.public_address = location_to_public_address(location)

        lat, lon = location_to_latlon(location)
        point = 'POINT(%s %s)' % (lon, lat)
        self.location = geos.fromstr(point)

        city = location_to_city(location)
        if city is not None:
            # save ascii lower case no white space only
            self.city = normalize_name(city)

        country = location_to_country(location)
        if country is not None:
            # save ascii lower case no white space only
            self.country = normalize_name(country)

        super(CitizenSpace, self).save()