def clean(self): super(Event, self).clean() if self.parent is None or not isinstance( self.parent.get_content_model(), Calendar): raise ValidationError("This event must belong to a Calendar.") if self.lat and not self.lon: raise ValidationError("Longitude required if specifying latitude.") if self.lon and not self.lat: raise ValidationError("Latitude required if specifying longitude.") if not (self.lat and self.lon) and not self.mappable_location: self.mappable_location = self.location.replace("\n",", ") if self.mappable_location: #location should always override lat/long if set g = GoogleMaps(domain=settings.MZEVENTS_GOOGLE_MAPS_DOMAIN) try: location, (lat, lon) = g.geocode(self.mappable_location.encode('utf-8')) except GQueryError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) except ValueError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) self.mappable_location = location self.lat = lat self.lon = lon
def clean(self): """ Validate set/validate mappable_location, longitude and latitude. """ super(EventLocation, self).clean() if self.lat and not self.lon: raise ValidationError("Longitude required if specifying latitude.") if self.lon and not self.lat: raise ValidationError("Latitude required if specifying longitude.") if not (self.lat and self.lon) and not self.mappable_location: self.mappable_location = self.address.replace("\n",", ") if self.mappable_location and not (self.lat and self.lon): #location should always override lat/long if set g = GoogleMaps(domain=settings.EVENT_GOOGLE_MAPS_DOMAIN) try: mappable_location, (lat, lon) = g.geocode(self.mappable_location.encode('utf-8')) except GQueryError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) except ValueError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) self.mappable_location = mappable_location self.lat = lat self.lon = lon
def clean(self): super(Event, self).clean() if self.lat and not self.lon: raise ValidationError(_("Longitude required if specifying latitude.")) if self.lon and not self.lat: raise ValidationError(_("Latitude required if specifying longitude.")) if not (self.lat and self.lon) and not self.mappable_location: self.mappable_location = self.location.replace("\n", ", ") if self.mappable_location: # location should always override lat/long if set g = GoogleMaps(domain=settings.MZEVENTS_GOOGLE_MAPS_DOMAIN) try: location, (lat, lon) = g.geocode(self.mappable_location.encode("utf-8")) except GQueryError as e: raise ValidationError( _( 'The mappable location you specified could not be found on {service}: "{error}" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.' ).format(service="Google Maps", error=e.message) ) except ValueError as e: raise ValidationError( _( 'The mappable location you specified could not be found on {service}: "{error}" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.' ).format(service="Google Maps", error=e.message) ) self.mappable_location = location self.lat = Decimal(str(lat)) self.lon = Decimal(str(lon))
def clean(self): super(Event, self).clean() if not self.end_date: self.end_date = self.date if self.date > self.end_date: raise ValidationError("Start date must be sooner than end date.") if self.date == self.end_date and self.start_time > self.end_time: raise ValidationError("Start time must be sooner than end time.") if self.lat and not self.lon: raise ValidationError("Longitude required if specifying latitude.") if self.lon and not self.lat: raise ValidationError("Latitude required if specifying longitude.") if not (self.lat and self.lon) and not self.mappable_location: self.mappable_location = self.location.replace("\n",", ") if self.mappable_location: #location should always override lat/long if set g = GoogleMaps(domain=settings.MZEVENTS_GOOGLE_MAPS_DOMAIN) try: location, (lat, lon) = g.geocode(self.mappable_location.encode('utf-8')) except GQueryError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) except ValueError as e: raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e.message)) self.mappable_location = location self.lat = lat self.lon = lon
class GoogleGeoCoder: def __init__(self): self.geocoder = Google(settings.GOOGLE_API_KEY) def search(self, address, search_exactly): return self.geocoder.geocode( query=address, exactly_one=search_exactly, ) def coordinates_from(self, address): _, coordinates = self.search(address, True) return coordinates
def clean(self): super(Event, self).clean() if not self.end_date: self.end_date = self.date if self.date > self.end_date: raise ValidationError("Start date must be sooner than end date.") if self.date == self.end_date and self.start_time > self.end_time: raise ValidationError("Start time must be sooner than end time.") if self.lat and not self.lon: raise ValidationError("Longitude required if specifying latitude.") if self.lon and not self.lat: raise ValidationError("Latitude required if specifying longitude.") location_to_geocode = self.mappable_location if not (self.lat and self.lon) and not self.mappable_location: location_to_geocode = self.location.replace("\n",", ") if self.mappable_location: #location should always override lat/long if set g = GoogleMaps(domain=settings.MZEVENTS_GOOGLE_MAPS_DOMAIN) try: location, (lat, lon) = g.geocode(location_to_geocode.encode('utf-8')) self.mappable_location = location_to_geocode self.lat = lat self.lon = lon except GQueryError as e: if(not not self.mappable_location): raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e)) except ValueError as e: if(not not self.mappable_location): raise ValidationError("The mappable location you specified could not be found on {service}: \"{error}\" Try changing the mappable location, removing any business names, or leaving mappable location blank and using coordinates from getlatlon.com.".format(service="Google Maps", error=e))
def geocode( sender, instance, **kwargs ): if instance.location: g = Geo() location = g.geocode( instance.location ) instance.latitude = location.latitude instance.longitude = location.longitude