def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.name) if not self.latlng: if self.zip and self.country: location = self.geo_address self.latlng = get_lat_lng(location) else: location = "+".join(filter(None, (self.address1, self.address2, self.city, self.state, self.country))) self.latlng = get_lat_lng(location) super(Winery, self).save(*args, **kwargs)
def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(self.name) if not self.latlng: if self.zip and self.country: location = self.geo_address self.latlng = get_lat_lng(location) else: location = '+'.join(filter(None, (self.address1, self.address2, self.city, self.state, self.country))) self.latlng = get_lat_lng(location) super(Winery, self).save(*args, **kwargs)
def save(self, *args, **kwargs): if not self.latlng: if self.zipcode and self.country: location = self.geo_address self.latlng = get_lat_lng(location) else: location = '+'.join( filter( None, (self.address, self.city, self.state, self.country))) self.latlng = get_lat_lng(location) super(Location, self).save(*args, **kwargs)
def infoWindow(): lat_lng = helpers.get_lat_lng() # Returns a tuple containing lat and lng as two 6 decimal floats lat = lat_lng[0] lng = lat_lng[1] # FInding the specific row in DbStaticInfo relating to the lat and lng provided static_row = DbStaticInfo.query.filter(DbStaticInfo.lat == lat).filter( DbStaticInfo.lng == lng).all() static_info = helpers.get_static_data(static_row) # Ordering the Dynamic Info to get the past weeks occupancy limited_rows = DbDynamicInfo.query.filter( DbDynamicInfo.number == static_info['number'][0]).order_by( desc(DbDynamicInfo.id)).limit(2016).all() address = static_info['address'] bikestands = static_info['bikestands'] available_bikes = limited_rows[0].available_bikes # Selecting all available bikes and times for past week. station_history = list(map(lambda x: x.available_bikes, limited_rows)) # Times are returned as hrs and minutes. time = list(map(lambda x: x.last_update.strftime("%H:%M"), limited_rows)) results = json.dumps({ "address": address, "bikestands": bikestands, "available_bikes": available_bikes, "station_history": station_history, "time": time }) return results
def prediction_day(): # Getting a days result from a bike station # Get date and time result = helpers.get_date_time() # aDDING 2 to make python .weekday method match with mysql's weekdays # Monday in mysql is 2 and 0 in python weekday = (result[1] + 1) % 7 + 1 lat_lng = helpers.get_lat_lng() # Returns a tuple containing lat and lng as two 6 decimal floats lat = lat_lng[0] lng = lat_lng[1] # Finding the specific row in DbStaticInfo relating to the lat and lng provided static_row = DbStaticInfo.query.filter(DbStaticInfo.lat == lat).filter( DbStaticInfo.lng == lng).all() static_info = helpers.get_static_data(static_row) address = static_info['address'] # Getting the previous weeks result limited_rows = DbDynamicInfo.query.filter( DbDynamicInfo.number == static_info['number'][0]).filter( DbDynamicInfo.weekday == weekday).order_by(desc( DbDynamicInfo.id)).limit(288).all() day_results = list(map(lambda x: x.available_bikes, limited_rows)) time = list(map(lambda x: x.last_update.strftime("%H:%M"), limited_rows)) results = json.dumps({ "address": address, "day_results": day_results, "time": time, 'day': weekday }) return results
def homepage(): """This view function takes you to the dateMeet homepage - anon users: no messages -logged in: 50 most recent posts by users if a city has not been entered. """ if g.user: recent_location = Location.query.filter( Location.user_id == g.user.id).order_by( Location.id.desc()).first() if recent_location: session[CURR_LOCATION] = recent_location.id g.location = recent_location if g.location: return redirect('/users/datelocations') form = UserLocationForm() if form.validate_on_submit(): name = form.name.data address = form.address.data lat_lng_addy = get_lat_lng(GEOCODE_API_KEY, address) if lat_lng_addy["latitude"] == 0 and lat_lng_addy["longitude"] == 0: flash("The address you've entered is not a valid address", "danger") return render_template('home.html', form=form) location = Location(name=name, address=lat_lng_addy["full_address"], long=lat_lng_addy["longitude"], lat=lat_lng_addy["latitude"], city=lat_lng_addy["city"], state=lat_lng_addy["state"], user_id=g.user.id) db.session.add(location) db.session.commit() session[CURR_LOCATION] = location.id return redirect('/users/datelocations') return render_template('home.html', form=form) else: return render_template('home-anon.html')
def save(self, *args, **kwargs): # Parse address into components if necessary: if not all([ self.address_number, self.street_name, self.street_name_post_type, self.place_name, self.state_name, self.zip_code ]): # Remove some characters we don't want included: address_string = self.address_string for char in '.,': address_string = address_string.replace(char, '') # Parse address: parsed_address = usaddress.parse(address_string) # Turn this into a dictionary for convenience: parsed_address = {value: key for key, value in parsed_address} # TODO: there's a more pythonic way to do the following... if 'AddressNumber' in parsed_address: self.address_number = parsed_address['AddressNumber'] if 'StreetName' in parsed_address: self.street_name = parsed_address['StreetName'] if 'StreetNamePostType' in parsed_address: self.street_name_post_type = parsed_address[ 'StreetNamePostType'] if 'PlaceName' in parsed_address: self.place_name = parsed_address['PlaceName'] if 'StateName' in parsed_address: self.state_name = parsed_address['StateName'] if 'ZipCode' in parsed_address: self.zip_code = parsed_address['ZipCode'] # Geocode if necessary: if not self.latitude or not self.longitude: lat_long = get_lat_lng(self.address_string) self.latitude = lat_long.split(',')[0] self.longitude = lat_long.split(',')[1] # Get legislators if necessary: # I have the "pyopenstates" package installed, but it doesn't appear to work correctly: # pyopenstates.locate_legislators('36.191402','86.73449959999999') # [] # Instead, make a request like so: # https://openstates.org/api/v1/legislators/geo/?lat=36.191402&long=-86.73449959999999 # Header: X-API-KEY=settings.OPENSTATES_API_KEY # # Next up: figure out how to store legislator info super(CachedAddress, self).save(*args, **kwargs)
def edit_location(): """This view function is used to edit the user's location.""" if not g.user: flash("Access Unauthorized", "danger") return redirect("/") if g.user and not g.location: flash("Please enter location", "danger") return redirect("/") location = g.location form = EditUserLocationForm(obj=location) if form.validate_on_submit(): name = form.name.data address = form.address.data lat_lng_addy = get_lat_lng(GEOCODE_API_KEY, address) if lat_lng_addy["latitude"] == 0 and lat_lng_addy["longitude"] == 0: flash("The address you've entered is not a valid address", "danger") return render_template('users/edit_location.html', form=form) location.name = name, location.address = lat_lng_addy["full_address"] location.long = lat_lng_addy["longitude"] location.lat = lat_lng_addy["latitude"] location.city = lat_lng_addy["city"] location.state = lat_lng_addy["state"] db.session.commit() return redirect('/users/datelocations') return render_template('users/edit_location.html', form=form)
def save(self, *args, **kwargs): if not self.latitude and not self.longitude: location = '+'.join(filter(None, (self.address, self.city, self.state))) self.latitude, self.longitude, self.formatted_address = get_lat_lng(location) super(Incident, self).save(*args, **kwargs)