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) lat, lon = location_to_latlon(location) point = "POINT(%s %s)" % (lon, lat) self.location = geos.fromstr(point) super(NGO, self).save()
def find_nearby(self, space, distance=5000, **kwargs): """Find nearby NGOs from a provided space :param space the provide space with address """ location = address_to_location(space.address.raw) address_lat, address_lon = location_to_latlon(location) current_point = geos.fromstr('POINT(%s %s)' % (address_lon, address_lat)) distance_from_point = {'m': distance} query = self.filter(location__distance_lte=(current_point, measure.D(**distance_from_point))) return query.distance(current_point).order_by('distance')
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')
def email_ngos(ngos, space): """send emails to nearby ngos to a new created space""" data_list = [] email_from = settings.DEFAULT_FROM_EMAIL custom_headers = attachments = None for ngo in ngos: context = { "ngo": ngo, "space": space, "site": Site.objects.get_current(), "area": location_to_administrative_area(address_to_location(space.address.raw)), } subject = render_to_string("citizen_refuge/emails/ngo_email_subject.txt", context) subject = "".join(subject.splitlines()) msg_plain = render_to_string("citizen_refuge/emails/ngo_email_message.txt", context) msg_html = render_to_string("citizen_refuge/emails/ngo_email_message.html", context) data_list.append((subject, msg_plain, msg_html, email_from, [ngo.email], custom_headers, attachments)) send_mass_html_mail(tuple(data_list))
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()